| import json |
| import tempfile |
| import unittest |
| from pathlib import Path |
|
|
| from approach.vlm.gpt4v.gpt4v import get_steam_app_data, load_app_metadata_cache |
|
|
|
|
| class AppMetadataCacheTests(unittest.TestCase): |
| def test_json_cache_supplies_app_context(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| cache_path = Path(tmpdir) / "app_metadata.json" |
| cache_path.write_text( |
| json.dumps( |
| { |
| "123": { |
| "app_name": "Pinned App", |
| "app_description": "Pinned description", |
| } |
| } |
| ), |
| encoding="utf-8", |
| ) |
|
|
| cache = load_app_metadata_cache(cache_path) |
|
|
| self.assertEqual( |
| get_steam_app_data("123", "123_4.jpg", metadata_cache=cache), |
| ("Pinned App", "Pinned description"), |
| ) |
|
|
| def test_cache_missing_app_id_fails_closed(self): |
| with self.assertRaises(KeyError): |
| get_steam_app_data("999", "999_1.jpg", metadata_cache={}) |
|
|
| def test_jsonl_cache_is_keyed_by_app_id(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| cache_path = Path(tmpdir) / "app_metadata.jsonl" |
| cache_path.write_text( |
| json.dumps( |
| { |
| "app_id": 456, |
| "name": "JSONL App", |
| "description": "JSONL description", |
| } |
| ) |
| + "\n", |
| encoding="utf-8", |
| ) |
|
|
| cache = load_app_metadata_cache(cache_path) |
|
|
| self.assertEqual( |
| get_steam_app_data("456", "456_7.jpg", metadata_cache=cache), |
| ("JSONL App", "JSONL description"), |
| ) |
|
|
| def test_duplicate_jsonl_app_id_is_rejected(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| cache_path = Path(tmpdir) / "app_metadata.jsonl" |
| cache_path.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", |
| ) |
|
|
| with self.assertRaises(ValueError): |
| load_app_metadata_cache(cache_path) |
|
|
| def test_duplicate_json_object_key_is_rejected(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| cache_path = Path(tmpdir) / "app_metadata.json" |
| cache_path.write_text( |
| '{"123":{"app_name":"First","app_description":"One"},' |
| '"123":{"app_name":"Second","app_description":"Two"}}', |
| encoding="utf-8", |
| ) |
|
|
| with self.assertRaises(ValueError): |
| load_app_metadata_cache(cache_path) |
|
|
| def test_mapping_key_and_record_app_id_must_match(self): |
| with tempfile.TemporaryDirectory() as tmpdir: |
| cache_path = Path(tmpdir) / "app_metadata.json" |
| cache_path.write_text( |
| json.dumps( |
| { |
| "123": { |
| "app_id": "456", |
| "app_name": "Mismatch", |
| "app_description": "Description", |
| } |
| } |
| ), |
| encoding="utf-8", |
| ) |
|
|
| with self.assertRaises(ValueError): |
| load_app_metadata_cache(cache_path) |
|
|
| def test_missing_cache_fields_fail_closed(self): |
| with self.assertRaises(ValueError): |
| get_steam_app_data("123", "123_4.jpg", metadata_cache={"123": {"app_name": "App"}}) |
|
|
|
|
| if __name__ == "__main__": |
| unittest.main() |
|
|