File size: 4,293 Bytes
3f3265f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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()