| import json |
| import tempfile |
| import unittest |
| from pathlib import Path |
|
|
| from scripts.generate_questions import build_questions, write_metadata_template |
| from scripts.verify_assets import ( |
| check_embedding_cache, |
| check_metadata_cache, |
| check_questions, |
| sha256_file, |
| ) |
|
|
|
|
| class ReleaseScriptTests(unittest.TestCase): |
| def test_generate_questions_from_image_directory(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| root = Path(tmpdir) |
| images = root / "images" |
| images.mkdir() |
| (images / "123_4.jpg").write_bytes(b"fake") |
| (images / "123_5.png").write_bytes(b"fake") |
| (images / "123_10.png").write_bytes(b"fake") |
| (images / "notes.txt").write_text("ignored", encoding="utf-8") |
|
|
| questions = build_questions(images, "Prompt") |
| metadata_template = root / "metadata.json" |
| write_metadata_template(metadata_template, questions) |
|
|
| metadata = json.loads(metadata_template.read_text(encoding="utf-8")) |
|
|
| self.assertEqual([record["image_id"] for record in questions], [123004, 123005, 123010]) |
| self.assertEqual(questions[0]["text"], "Prompt") |
| self.assertEqual(sorted(metadata), ["123"]) |
|
|
| def test_recursive_question_generation_uses_basename_for_app_id(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| root = Path(tmpdir) |
| images = root / "images" |
| nested = images / "nested" |
| nested.mkdir(parents=True) |
| (nested / "123_4.jpg").write_bytes(b"fake") |
|
|
| questions = build_questions(images, "Prompt", recursive=True) |
| metadata_template = root / "metadata.json" |
| write_metadata_template(metadata_template, questions) |
| metadata = json.loads(metadata_template.read_text(encoding="utf-8")) |
|
|
| self.assertEqual(questions[0]["image"], "nested/123_4.jpg") |
| self.assertEqual(sorted(metadata), ["123"]) |
|
|
| def test_verify_questions_and_metadata_cache(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| root = Path(tmpdir) |
| images = root / "images" |
| images.mkdir() |
| (images / "123_4.jpg").write_bytes(b"fake") |
| questions = root / "questions.jsonl" |
| questions.write_text( |
| json.dumps( |
| { |
| "question_id": 0, |
| "image": "123_4.jpg", |
| "image_id": 123004, |
| "text": "Prompt", |
| } |
| ) |
| + "\n", |
| encoding="utf-8", |
| ) |
| metadata = root / "metadata.json" |
| metadata.write_text( |
| json.dumps( |
| { |
| "123": { |
| "app_name": "Test VR", |
| "app_description": "Description.", |
| } |
| } |
| ), |
| encoding="utf-8", |
| ) |
|
|
| report = check_questions(questions, images) |
| metadata_report = check_metadata_cache(metadata, report["app_ids"]) |
|
|
| self.assertEqual(report["questions"], 1) |
| self.assertEqual(metadata_report["covered_app_ids"], 1) |
|
|
| def test_verify_metadata_cache_rejects_duplicates_and_missing_fields(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| root = Path(tmpdir) |
| duplicate = root / "duplicate.jsonl" |
| duplicate.write_text( |
| "\n".join( |
| [ |
| json.dumps( |
| { |
| "app_id": 123, |
| "app_name": "First", |
| "app_description": "Description.", |
| } |
| ), |
| json.dumps( |
| { |
| "app_id": "123", |
| "app_name": "Second", |
| "app_description": "Description.", |
| } |
| ), |
| ] |
| ) |
| + "\n", |
| encoding="utf-8", |
| ) |
| missing_field = root / "missing_field.json" |
| missing_field.write_text( |
| json.dumps({"123": {"app_name": "Test VR"}}), |
| encoding="utf-8", |
| ) |
|
|
| with self.assertRaises(ValueError): |
| check_metadata_cache(duplicate, ["123"]) |
| with self.assertRaises(ValueError): |
| check_metadata_cache(missing_field, ["123"]) |
|
|
| def test_verify_external_embedding_cache_against_manifest(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| root = Path(tmpdir) |
| cache = root / "embedding_dict.json" |
| cache.write_bytes(b"frozen-cache") |
| manifest = root / "cache_manifest.json" |
| manifest.write_text( |
| json.dumps( |
| { |
| "artifact": "embedding_dict.json", |
| "size_bytes": cache.stat().st_size, |
| "sha256": sha256_file(cache), |
| "entry_count": 1, |
| "embedding_dimension": 2, |
| } |
| ), |
| encoding="utf-8", |
| ) |
|
|
| report = check_embedding_cache(cache, manifest) |
|
|
| self.assertEqual(report["entry_count"], 1) |
| self.assertEqual(report["embedding_dimension"], 2) |
|
|
|
|
| if __name__ == "__main__": |
| unittest.main() |
|
|