import importlib.util, json, tempfile, unittest from pathlib import Path HERE=Path(__file__).resolve().parents[1] spec=importlib.util.spec_from_file_location("import_run",HERE/"scripts/import_run.py") m=importlib.util.module_from_spec(spec); spec.loader.exec_module(m) class ImportRunTests(unittest.TestCase): def fixture(self): td=tempfile.TemporaryDirectory(); root=Path(td.name); (root/"raw").mkdir(); (root/"canonical-prompt.txt").write_text("prompt\n") prompt=root/"input-prompt.txt"; response=root/"input-response.md"; prompt.write_text("prompt\n"); response.write_text("real answer\n") return td,root,prompt,response def call(self,root,prompt,response,**kw): args=dict(family="family-a",run_id="run-001",prompt_path=prompt,response_path=response,provider="provider",model_identifier="model-v1",collected_at="2026-10-20T12:00:00Z",interface="API",sampling="{\"temperature\":0}",adaptation=None); args.update(kw) return m.import_run(root,**args) def test_imports_canonical_run(self): td,root,prompt,response=self.fixture() try: dest=self.call(root,prompt,response); meta=json.loads((dest/"metadata.json").read_text()); self.assertIsNone(meta["adaptation"]); self.assertIn("response.md",(dest/"sha256.txt").read_text()) finally: td.cleanup() def test_rejects_changed_prompt_without_note(self): td,root,prompt,response=self.fixture(); prompt.write_text("adapted\n") try: with self.assertRaises(ValueError): self.call(root,prompt,response) finally: td.cleanup() def test_rejects_duplicate_without_overwrite(self): td,root,prompt,response=self.fixture() try: self.call(root,prompt,response) with self.assertRaises(FileExistsError): self.call(root,prompt,response) self.assertEqual((root/"raw/family-a/run-001/response.md").read_text(),"real answer\n") finally: td.cleanup() if __name__=="__main__": unittest.main()