from __future__ import annotations import hashlib import json import sys import tempfile import unittest from pathlib import Path WORKFLOWS = Path(__file__).resolve().parents[1] / "workflows" sys.path.insert(0, str(WORKFLOWS)) from preprocess_text import run_preprocessing # noqa: E402 from validate_manifest import validate_manifest # noqa: E402 def approved_manifest(content: bytes) -> dict[str, object]: return { "schema_version": "1.0", "dataset_id": "synthetic-test@1", "decision": { "status": "approved", "reviewed_by": "test-reviewer", "reviewed_at": "2026-10-11T00:00:00Z", "notes": ["Synthetic unit-test fixture only."], }, "sources": [ { "source_id": "synthetic-one", "title": "Synthetic unit-test text", "publisher": "DATA LAB tests", "canonical_url": "https://example.invalid/synthetic-one", "version": "1", "retrieved_at": "2026-10-11T00:00:00Z", "content_path": "input.txt", "sha256": hashlib.sha256(content).hexdigest(), "license": { "identifier": "CC0-1.0", "url": "https://creativecommons.org/publicdomain/zero/1.0/", "permission_status": "approved", "redistribution": "allowed", "attribution": "", }, "privacy": { "review_status": "passed", "contains_personal_data": False, "consent_or_lawful_basis": "Synthetic text created by the test.", "notes": [], }, "use": { "purpose": "Workflow unit test", "allowed": True, "restrictions": [], }, } ], } class ManifestTests(unittest.TestCase): def test_approved_manifest_is_valid(self) -> None: self.assertEqual(validate_manifest(approved_manifest(b"hello\n")), []) def test_traversal_path_is_rejected(self) -> None: manifest = approved_manifest(b"hello\n") manifest["sources"][0]["content_path"] = "../outside.txt" # type: ignore[index] self.assertTrue(any("safe" in error for error in validate_manifest(manifest))) def test_approval_requires_privacy_gate(self) -> None: manifest = approved_manifest(b"hello\n") manifest["sources"][0]["privacy"]["review_status"] = "pending" # type: ignore[index] self.assertTrue(any("privacy" in error for error in validate_manifest(manifest))) class PreprocessingTests(unittest.TestCase): def test_output_is_deterministic_and_contains_no_absolute_path(self) -> None: content = "Synthetic text. \r\n\r\n\r\nSecond line.\r\n".encode() with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) input_root = root / "input" input_root.mkdir() (input_root / "input.txt").write_bytes(content) manifest_path = root / "manifest.json" manifest_path.write_text(json.dumps(approved_manifest(content)), encoding="utf-8") report_a = run_preprocessing(manifest_path, input_root, root / "out-a") report_b = run_preprocessing(manifest_path, input_root, root / "out-b") records_a = (root / "out-a" / "records.jsonl").read_bytes() records_b = (root / "out-b" / "records.jsonl").read_bytes() self.assertEqual(records_a, records_b) self.assertEqual(report_a, report_b) self.assertNotIn(str(root).encode(), records_a) self.assertEqual(report_a["counts"]["accepted_records"], 1) def test_proposed_manifest_cannot_run(self) -> None: content = b"synthetic\n" with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) input_root = root / "input" input_root.mkdir() (input_root / "input.txt").write_bytes(content) manifest = approved_manifest(content) manifest["decision"]["status"] = "proposed" # type: ignore[index] manifest_path = root / "manifest.json" manifest_path.write_text(json.dumps(manifest), encoding="utf-8") with self.assertRaisesRegex(ValueError, "approved"): run_preprocessing(manifest_path, input_root, root / "out") if __name__ == "__main__": unittest.main()