File size: 11,321 Bytes
0997c23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
"""
Cache Verification Test Script for MediaDescribe Node

This script verifies that the caching mechanism is working correctly after
recent changes to the media_describe nodes.

Tests:
1. Cache module imports correctly
2. File identifier generation works
3. Tensor identifier generation works
4. Cache set/get operations work correctly
5. Different options create different cache keys
6. All MediaDescribe options are included in cache keys
"""

from nodes.cache import get_cache, get_file_media_identifier, get_tensor_media_identifier

def test_cache_imports():
    """Test that cache module imports correctly"""
    print("\n=== Test 1: Cache Module Import ===")
    try:
        cache = get_cache()
        print("βœ… Cache instance created successfully")
        print(f"   Cache directory: {cache.cache_dir}")
        return True
    except Exception as e:
        print(f"❌ Failed to create cache instance: {e}")
        return False

def test_file_identifier():
    """Test file identifier generation"""
    print("\n=== Test 2: File Identifier Generation ===")
    try:
        # Test with non-existent file
        test_path = 'nonexistent_file.txt'
        file_id = get_file_media_identifier(test_path)
        print("βœ… File identifier generated for non-existent file")
        print(f"   Identifier: {file_id}")

        # Test with existing file
        existing_path = __file__  # This script itself
        file_id2 = get_file_media_identifier(existing_path)
        print("βœ… File identifier generated for existing file")
        print(f"   Identifier: {file_id2[:60]}...")

        # Verify identifiers are different
        if file_id != file_id2:
            print("βœ… Different files generate different identifiers")
        else:
            print("❌ Different files generated same identifier")
            return False

        return True
    except Exception as e:
        print(f"❌ File identifier generation failed: {e}")
        return False

def test_tensor_identifier():
    """Test tensor identifier generation"""
    print("\n=== Test 3: Tensor Identifier Generation ===")
    try:
        # Test with different tensor data
        tensor1 = [[[1, 2, 3], [4, 5, 6]]]
        tensor2 = [[[7, 8, 9], [10, 11, 12]]]

        id1 = get_tensor_media_identifier(tensor1)
        id2 = get_tensor_media_identifier(tensor2)

        print("βœ… Tensor identifiers generated")
        print(f"   Tensor 1: {id1}")
        print(f"   Tensor 2: {id2}")

        if id1 != id2:
            print("βœ… Different tensors generate different identifiers")
        else:
            print("❌ Different tensors generated same identifier")
            return False

        # Verify same tensor generates same identifier
        id1_duplicate = get_tensor_media_identifier(tensor1)
        if id1 == id1_duplicate:
            print("βœ… Same tensor generates same identifier")
        else:
            print("❌ Same tensor generated different identifier")
            return False

        return True
    except Exception as e:
        print(f"❌ Tensor identifier generation failed: {e}")
        return False

def test_cache_set_get():
    """Test cache set/get operations"""
    print("\n=== Test 4: Cache Set/Get Operations ===")
    try:
        cache = get_cache()

        # Test basic set/get
        media_id = 'test:media:basic'
        options = {
            'describe_clothing': True,
            'describe_hair_style': False,
            'describe_bokeh': True
        }

        cache.set(
            media_identifier=media_id,
            gemini_model='models/gemini-2.5-flash',
            description='Test description',
            model_type='Text2Image',
            options=options
        )
        print("βœ… Cache entry stored successfully")

        result = cache.get(
            media_identifier=media_id,
            gemini_model='models/gemini-2.5-flash',
            model_type='Text2Image',
            options=options
        )

        if result and result['description'] == 'Test description':
            print("βœ… Cache retrieval successful")
            print(f"   Description: {result['description']}")
            print(f"   Timestamp: {result['human_timestamp']}")
            print(f"   Cache key: {result['cache_key'][:16]}...")
        else:
            print("❌ Cache retrieval failed")
            return False

        return True
    except Exception as e:
        print(f"❌ Cache set/get failed: {e}")
        return False

def test_options_affect_cache_key():
    """Test that different options create different cache keys"""
    print("\n=== Test 5: Options Affect Cache Key ===")
    try:
        cache = get_cache()
        media_id = 'test:media:options'

        # Store with first set of options
        options1 = {
            'describe_clothing': True,
            'describe_hair_style': False,
            'describe_bokeh': True
        }

        cache.set(
            media_identifier=media_id,
            gemini_model='models/gemini-2.5-flash',
            description='Description with options1',
            model_type='Text2Image',
            options=options1
        )

        # Try to get with different options
        options2 = {
            'describe_clothing': False,  # Changed
            'describe_hair_style': False,
            'describe_bokeh': True
        }

        result = cache.get(
            media_identifier=media_id,
            gemini_model='models/gemini-2.5-flash',
            model_type='Text2Image',
            options=options2
        )

        if result is None:
            print("βœ… Different options correctly create different cache keys")
        else:
            print("❌ Different options did not create different cache keys")
            return False

        # Verify we can retrieve with original options
        result2 = cache.get(
            media_identifier=media_id,
            gemini_model='models/gemini-2.5-flash',
            model_type='Text2Image',
            options=options1
        )

        if result2 and result2['description'] == 'Description with options1':
            print("βœ… Original options still retrieve correct cached result")
        else:
            print("❌ Could not retrieve with original options")
            return False

        return True
    except Exception as e:
        print(f"❌ Options cache key test failed: {e}")
        return False

def test_all_mediadescribe_options():
    """Test that all MediaDescribe options are included in cache keys"""
    print("\n=== Test 6: All MediaDescribe Options in Cache ===")

    # Define all options that should affect caching
    image_options = [
        'describe_clothing',
        'change_clothing_color',
        'describe_hair_style',
        'describe_bokeh',
        'describe_subject'
    ]

    video_options = [
        'describe_clothing',
        'change_clothing_color',
        'describe_hair_style',
        'describe_bokeh',
        'describe_subject',
        'replace_action_with_twerking',  # Video-specific
        'max_duration'  # Video-specific
    ]

    try:
        cache = get_cache()

        # Test image options
        print("\n--- Testing Image Options ---")
        base_image_options = {opt: False for opt in image_options}
        media_id_img = 'test:media:image_options'

        cache.set(
            media_identifier=media_id_img,
            gemini_model='models/gemini-2.5-flash',
            description='Base image description',
            model_type='Text2Image',
            options=base_image_options
        )

        # Test each option individually
        for option in image_options:
            modified_options = base_image_options.copy()
            modified_options[option] = True  # Change one option

            result = cache.get(
                media_identifier=media_id_img,
                gemini_model='models/gemini-2.5-flash',
                model_type='Text2Image',
                options=modified_options
            )

            if result is None:
                print(f"βœ… '{option}' affects image cache key")
            else:
                print(f"❌ '{option}' does NOT affect image cache key")
                return False

        # Test video options
        print("\n--- Testing Video Options ---")
        base_video_options = {opt: False if opt != 'max_duration' else 5.0 for opt in video_options}
        media_id_vid = 'test:media:video_options'

        cache.set(
            media_identifier=media_id_vid,
            gemini_model='models/gemini-2.5-flash',
            description='Base video description',
            model_type='',  # Videos don't use model_type
            options=base_video_options
        )

        # Test each option individually
        for option in video_options:
            modified_options = base_video_options.copy()
            if option == 'max_duration':
                modified_options[option] = 10.0  # Change duration
            else:
                modified_options[option] = True  # Change boolean option

            result = cache.get(
                media_identifier=media_id_vid,
                gemini_model='models/gemini-2.5-flash',
                model_type='',
                options=modified_options
            )

            if result is None:
                print(f"βœ… '{option}' affects video cache key")
            else:
                print(f"❌ '{option}' does NOT affect video cache key")
                return False

        print("\nβœ… All MediaDescribe options correctly affect cache keys")
        return True

    except Exception as e:
        print(f"❌ MediaDescribe options test failed: {e}")
        return False

def test_cache_info():
    """Test cache info retrieval"""
    print("\n=== Test 7: Cache Info ===")
    try:
        cache = get_cache()
        info = cache.get_cache_info()

        print("βœ… Cache info retrieved:")
        print(f"   Directory: {info['cache_dir']}")
        print(f"   Entries: {info['entries']}")
        print(f"   Total size: {info['total_size_mb']} MB")

        return True
    except Exception as e:
        print(f"❌ Cache info test failed: {e}")
        return False

def main():
    """Run all tests"""
    print("=" * 70)
    print("CACHE VERIFICATION TEST SUITE FOR MEDIADESCRIBE")
    print("=" * 70)

    tests = [
        test_cache_imports,
        test_file_identifier,
        test_tensor_identifier,
        test_cache_set_get,
        test_options_affect_cache_key,
        test_all_mediadescribe_options,
        test_cache_info
    ]

    results = []
    for test in tests:
        try:
            result = test()
            results.append(result)
        except Exception as e:
            print(f"\n❌ Test failed with exception: {e}")
            results.append(False)

    # Summary
    print("\n" + "=" * 70)
    print("TEST SUMMARY")
    print("=" * 70)
    passed = sum(results)
    total = len(results)
    print(f"Tests passed: {passed}/{total}")

    if passed == total:
        print("\nβœ… ALL TESTS PASSED - CACHING MECHANISM IS WORKING CORRECTLY")
        return 0
    else:
        print(f"\n❌ {total - passed} TEST(S) FAILED")
        return 1

if __name__ == "__main__":
    exit(main())