@snap/valdi 1.0.11 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.metadata/.bazelrc.template +7 -6
- package/.metadata/MODULE.bazel.template +154 -0
- package/bundled-skills/registry.json +13 -2
- package/bundled-skills/skills/valdi-animations/skill.md +197 -0
- package/bundled-skills/skills/valdi-async/tests/src/reference.tsx +1 -1
- package/bundled-skills/skills/valdi-compiler/skill.md +4 -4
- package/bundled-skills/skills/valdi-component-dedup/skill.md +263 -0
- package/bundled-skills/skills/valdi-component-tests/tests/test/ProfileCardTest.spec.tsx +1 -1
- package/bundled-skills/skills/valdi-custom-view/skill.md +43 -1
- package/bundled-skills/skills/valdi-error-handling/skill.md +238 -0
- package/bundled-skills/skills/valdi-gestures/skill.md +198 -0
- package/bundled-skills/skills/valdi-inspect/skill.md +19 -1
- package/bundled-skills/skills/valdi-localization/skill.md +142 -0
- package/bundled-skills/skills/valdi-mock-breakdown/skill.md +327 -0
- package/bundled-skills/skills/valdi-mock-iterate/skill.md +216 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/diff.py +392 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/extract_colors.py +277 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/find_icon.py +303 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/snapshot-and-diff.sh +53 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/test_diff.py +207 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/test_extract_colors.py +167 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/test_find_icon.py +188 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/watch-and-diff.sh +181 -0
- package/bundled-skills/skills/valdi-mock-to-module/skill.md +451 -0
- package/bundled-skills/skills/valdi-navigation/skill.md +187 -0
- package/bundled-skills/skills/valdi-overview/skill.md +7 -4
- package/bundled-skills/skills/valdi-perf/skill.md +223 -0
- package/bundled-skills/skills/valdi-polyglot-module/skill.md +80 -0
- package/bundled-skills/skills/valdi-preview/skill.md +303 -0
- package/bundled-skills/skills/valdi-setup/skill.md +55 -5
- package/bundled-skills/skills/valdi-testing/skill.md +23 -7
- package/bundled-skills/skills/valdi-tsx/skill.md +69 -5
- package/bundled-skills/skills/valdi-tsx/tests/src/reference.tsx +29 -5
- package/dist/commands/agentCheck.js +184 -0
- package/dist/commands/analyze.js +16 -0
- package/dist/commands/analyze_commands/duplicates.js +134 -0
- package/dist/commands/bootstrap.js +57 -73
- package/dist/commands/build.js +6 -0
- package/dist/commands/doctor.js +42 -266
- package/dist/commands/hotreload.js +15 -0
- package/dist/commands/inspect_commands/snapshot.js +71 -6
- package/dist/commands/inspect_commands/snapshot.spec.js +105 -0
- package/dist/commands/install.js +6 -0
- package/dist/commands/preview.js +81 -0
- package/dist/commands/projectsync.js +9 -7
- package/dist/commands/skills_commands/install.js +6 -1
- package/dist/commands/skills_commands/update.js +2 -1
- package/dist/core/analyze/clusterer.js +150 -0
- package/dist/core/analyze/fingerprint.js +2 -0
- package/dist/core/analyze/knownPatterns.js +76 -0
- package/dist/core/analyze/parser.js +177 -0
- package/dist/core/analyze/reporter.js +83 -0
- package/dist/core/analyze/scanner.js +172 -0
- package/dist/core/analyze/similarity.js +56 -0
- package/dist/core/constants.js +6 -3
- package/dist/core/packageFiles.spec.js +1 -1
- package/dist/preview/previewGenerator.js +240 -0
- package/dist/preview/previewGenerator.spec.js +203 -0
- package/dist/preview/viewModelParser.js +86 -0
- package/dist/preview/viewModelParser.spec.js +171 -0
- package/dist/setup/DevSetupHelper.js +0 -54
- package/dist/setup/linuxSetup.js +0 -16
- package/dist/setup/macOSSetup.js +1 -5
- package/dist/utils/BazelClient.js +15 -0
- package/dist/utils/applicationUtils.js +15 -0
- package/dist/utils/daemonClient.js +3 -1
- package/dist/utils/linuxDistro.js +0 -21
- package/dist/utils/linuxDistro.spec.js +0 -48
- package/dist/utils/skillsAdapters.js +126 -19
- package/dist/utils/skillsAdapters.spec.js +141 -0
- package/dist/utils/skillsRegistry.js +8 -0
- package/package.json +16 -8
- package/.metadata/WORKSPACE.template +0 -57
- package/dist/setup/versions.js +0 -8
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Tests for diff.py — pixel diff and shadow detection logic."""
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
import tempfile
|
|
7
|
+
import unittest
|
|
8
|
+
|
|
9
|
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
10
|
+
from diff import (
|
|
11
|
+
_crop_to_phone_frame,
|
|
12
|
+
_auto_detect_masks,
|
|
13
|
+
_apply_masks,
|
|
14
|
+
_segment_match,
|
|
15
|
+
_parse_mask_region,
|
|
16
|
+
detect_missing_shadows,
|
|
17
|
+
generate_diff,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
from PIL import Image, ImageChops
|
|
22
|
+
except ImportError:
|
|
23
|
+
print("Pillow required for tests: pip3 install Pillow", file=sys.stderr)
|
|
24
|
+
sys.exit(1)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class TestCropToPhoneFrame(unittest.TestCase):
|
|
28
|
+
def test_crops_centered_white_frame(self):
|
|
29
|
+
# Simulate a desktop window with dark bg and white phone frame
|
|
30
|
+
img = Image.new('RGB', (800, 600), (51, 51, 51)) # #333 background
|
|
31
|
+
# White phone frame 390px wide, centered
|
|
32
|
+
left = (800 - 390) // 2
|
|
33
|
+
for y in range(600):
|
|
34
|
+
for x in range(left, left + 390):
|
|
35
|
+
img.putpixel((x, y), (255, 255, 255))
|
|
36
|
+
cropped = _crop_to_phone_frame(img)
|
|
37
|
+
# Should be approximately 390px wide (with small padding)
|
|
38
|
+
self.assertGreater(cropped.width, 380)
|
|
39
|
+
self.assertLess(cropped.width, 410)
|
|
40
|
+
|
|
41
|
+
def test_returns_full_image_when_no_frame(self):
|
|
42
|
+
# All-dark image — no phone frame to detect
|
|
43
|
+
img = Image.new('RGB', (400, 600), (51, 51, 51))
|
|
44
|
+
cropped = _crop_to_phone_frame(img)
|
|
45
|
+
self.assertEqual(cropped.size, img.size)
|
|
46
|
+
|
|
47
|
+
def test_returns_full_image_when_all_white(self):
|
|
48
|
+
img = Image.new('RGB', (400, 600), (255, 255, 255))
|
|
49
|
+
cropped = _crop_to_phone_frame(img)
|
|
50
|
+
# Entire image is "bright", so it returns the whole thing
|
|
51
|
+
self.assertEqual(cropped.width, 400)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class TestAutoDetectMasks(unittest.TestCase):
|
|
55
|
+
def test_detects_dark_blocks(self):
|
|
56
|
+
img = Image.new('RGB', (200, 200), (255, 255, 255))
|
|
57
|
+
# Dark block in left column
|
|
58
|
+
for y in range(40, 80):
|
|
59
|
+
for x in range(15, 35):
|
|
60
|
+
img.putpixel((x, y), (50, 50, 50))
|
|
61
|
+
masks = _auto_detect_masks(img)
|
|
62
|
+
self.assertGreater(len(masks), 0)
|
|
63
|
+
|
|
64
|
+
def test_no_masks_on_white(self):
|
|
65
|
+
img = Image.new('RGB', (200, 200), (255, 255, 255))
|
|
66
|
+
masks = _auto_detect_masks(img)
|
|
67
|
+
self.assertEqual(len(masks), 0)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class TestApplyMasks(unittest.TestCase):
|
|
71
|
+
def test_fills_mask_regions(self):
|
|
72
|
+
img = Image.new('RGB', (100, 100), (255, 0, 0))
|
|
73
|
+
masked = _apply_masks(img, [(10, 10, 20, 20)], fill=(128, 128, 128))
|
|
74
|
+
# Center of masked region should be gray
|
|
75
|
+
self.assertEqual(masked.getpixel((20, 20)), (128, 128, 128))
|
|
76
|
+
# Outside mask should still be red
|
|
77
|
+
self.assertEqual(masked.getpixel((0, 0)), (255, 0, 0))
|
|
78
|
+
|
|
79
|
+
def test_clamps_to_image_bounds(self):
|
|
80
|
+
img = Image.new('RGB', (50, 50), (0, 255, 0))
|
|
81
|
+
# Mask extends beyond image — should not crash
|
|
82
|
+
masked = _apply_masks(img, [(40, 40, 20, 20)])
|
|
83
|
+
self.assertEqual(masked.size, (50, 50))
|
|
84
|
+
|
|
85
|
+
def test_empty_masks_returns_copy(self):
|
|
86
|
+
img = Image.new('RGB', (50, 50), (100, 100, 100))
|
|
87
|
+
masked = _apply_masks(img, [])
|
|
88
|
+
self.assertEqual(masked.getpixel((25, 25)), (100, 100, 100))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class TestSegmentMatch(unittest.TestCase):
|
|
92
|
+
def test_identical_images_100_percent(self):
|
|
93
|
+
# All-black diff = perfect match
|
|
94
|
+
diff = Image.new('RGB', (100, 100), (0, 0, 0))
|
|
95
|
+
segments = _segment_match(diff, num_segments=4, threshold=30)
|
|
96
|
+
self.assertEqual(len(segments), 4)
|
|
97
|
+
for label, pct in segments:
|
|
98
|
+
self.assertEqual(pct, 100.0)
|
|
99
|
+
|
|
100
|
+
def test_all_different_0_percent(self):
|
|
101
|
+
# All-white diff = no match
|
|
102
|
+
diff = Image.new('RGB', (100, 100), (255, 255, 255))
|
|
103
|
+
segments = _segment_match(diff, num_segments=2, threshold=30)
|
|
104
|
+
self.assertEqual(len(segments), 2)
|
|
105
|
+
for label, pct in segments:
|
|
106
|
+
self.assertEqual(pct, 0.0)
|
|
107
|
+
|
|
108
|
+
def test_mixed_segments(self):
|
|
109
|
+
# Top half black (match), bottom half white (no match)
|
|
110
|
+
diff = Image.new('RGB', (100, 100), (0, 0, 0))
|
|
111
|
+
for y in range(50, 100):
|
|
112
|
+
for x in range(100):
|
|
113
|
+
diff.putpixel((x, y), (255, 255, 255))
|
|
114
|
+
segments = _segment_match(diff, num_segments=2, threshold=30)
|
|
115
|
+
self.assertEqual(len(segments), 2)
|
|
116
|
+
self.assertEqual(segments[0][1], 100.0) # top = match
|
|
117
|
+
self.assertEqual(segments[1][1], 0.0) # bottom = no match
|
|
118
|
+
|
|
119
|
+
def test_label_format(self):
|
|
120
|
+
diff = Image.new('RGB', (100, 100), (0, 0, 0))
|
|
121
|
+
segments = _segment_match(diff, num_segments=4)
|
|
122
|
+
labels = [s[0] for s in segments]
|
|
123
|
+
self.assertIn('rows 0-25%', labels)
|
|
124
|
+
self.assertIn('rows 75-100%', labels)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class TestParseMaskRegion(unittest.TestCase):
|
|
128
|
+
def test_valid_region(self):
|
|
129
|
+
self.assertEqual(_parse_mask_region('10,20,30,40'), (10, 20, 30, 40))
|
|
130
|
+
|
|
131
|
+
def test_invalid_region(self):
|
|
132
|
+
with self.assertRaises(ValueError):
|
|
133
|
+
_parse_mask_region('10,20,30')
|
|
134
|
+
with self.assertRaises(ValueError):
|
|
135
|
+
_parse_mask_region('10,20,30,40,50')
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class TestDetectMissingShadows(unittest.TestCase):
|
|
139
|
+
def test_identical_images_no_shadows(self):
|
|
140
|
+
img = Image.new('RGB', (100, 100), (200, 200, 200))
|
|
141
|
+
overlay, count, pct = detect_missing_shadows(img, img)
|
|
142
|
+
self.assertEqual(count, 0)
|
|
143
|
+
self.assertEqual(pct, 0)
|
|
144
|
+
|
|
145
|
+
def test_returns_image_of_correct_size(self):
|
|
146
|
+
mock = Image.new('RGB', (100, 100), (180, 180, 180))
|
|
147
|
+
render = Image.new('RGB', (100, 100), (220, 220, 220))
|
|
148
|
+
overlay, count, pct = detect_missing_shadows(mock, render)
|
|
149
|
+
self.assertEqual(overlay.size, (100, 100))
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class TestGenerateDiff(unittest.TestCase):
|
|
153
|
+
def setUp(self):
|
|
154
|
+
self.tmpdir = tempfile.mkdtemp()
|
|
155
|
+
|
|
156
|
+
def tearDown(self):
|
|
157
|
+
import shutil
|
|
158
|
+
shutil.rmtree(self.tmpdir)
|
|
159
|
+
|
|
160
|
+
def test_identical_images_high_match(self):
|
|
161
|
+
img = Image.new('RGB', (400, 800), (255, 255, 255))
|
|
162
|
+
mock_path = os.path.join(self.tmpdir, 'mock.png')
|
|
163
|
+
render_path = os.path.join(self.tmpdir, 'render.png')
|
|
164
|
+
output_path = os.path.join(self.tmpdir, 'diff.png')
|
|
165
|
+
img.save(mock_path)
|
|
166
|
+
img.save(render_path)
|
|
167
|
+
match_pct = generate_diff(mock_path, render_path, output_path)
|
|
168
|
+
self.assertGreater(match_pct, 90.0)
|
|
169
|
+
self.assertTrue(os.path.exists(output_path))
|
|
170
|
+
|
|
171
|
+
def test_output_is_4_panels_wide(self):
|
|
172
|
+
img = Image.new('RGB', (400, 800), (200, 200, 200))
|
|
173
|
+
mock_path = os.path.join(self.tmpdir, 'mock.png')
|
|
174
|
+
render_path = os.path.join(self.tmpdir, 'render.png')
|
|
175
|
+
output_path = os.path.join(self.tmpdir, 'diff.png')
|
|
176
|
+
img.save(mock_path)
|
|
177
|
+
img.save(render_path)
|
|
178
|
+
generate_diff(mock_path, render_path, output_path, width=200)
|
|
179
|
+
result = Image.open(output_path)
|
|
180
|
+
# 4 panels of 200px + 3 gaps of 6px = 818
|
|
181
|
+
self.assertEqual(result.width, 200 * 4 + 6 * 3)
|
|
182
|
+
|
|
183
|
+
def test_with_segments(self):
|
|
184
|
+
img = Image.new('RGB', (400, 800), (255, 255, 255))
|
|
185
|
+
mock_path = os.path.join(self.tmpdir, 'mock.png')
|
|
186
|
+
render_path = os.path.join(self.tmpdir, 'render.png')
|
|
187
|
+
output_path = os.path.join(self.tmpdir, 'diff.png')
|
|
188
|
+
img.save(mock_path)
|
|
189
|
+
img.save(render_path)
|
|
190
|
+
# Should not crash with segments enabled
|
|
191
|
+
match_pct = generate_diff(mock_path, render_path, output_path, num_segments=4)
|
|
192
|
+
self.assertGreater(match_pct, 90.0)
|
|
193
|
+
|
|
194
|
+
def test_with_mask_regions(self):
|
|
195
|
+
img = Image.new('RGB', (400, 800), (255, 255, 255))
|
|
196
|
+
mock_path = os.path.join(self.tmpdir, 'mock.png')
|
|
197
|
+
render_path = os.path.join(self.tmpdir, 'render.png')
|
|
198
|
+
output_path = os.path.join(self.tmpdir, 'diff.png')
|
|
199
|
+
img.save(mock_path)
|
|
200
|
+
img.save(render_path)
|
|
201
|
+
match_pct = generate_diff(mock_path, render_path, output_path,
|
|
202
|
+
mask_regions=[(10, 10, 50, 50)])
|
|
203
|
+
self.assertGreater(match_pct, 90.0)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
if __name__ == '__main__':
|
|
207
|
+
unittest.main()
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Tests for extract_colors.py — color extraction logic."""
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
import tempfile
|
|
7
|
+
import unittest
|
|
8
|
+
|
|
9
|
+
# Add the script directory to path
|
|
10
|
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
11
|
+
from extract_colors import (
|
|
12
|
+
dominant_color_kmeans,
|
|
13
|
+
sample_point,
|
|
14
|
+
extract_region,
|
|
15
|
+
auto_detect_thumbnails,
|
|
16
|
+
auto_detect_colored_regions,
|
|
17
|
+
_resolve_macos_path,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
from PIL import Image
|
|
22
|
+
except ImportError:
|
|
23
|
+
print("Pillow required for tests: pip3 install Pillow", file=sys.stderr)
|
|
24
|
+
sys.exit(1)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class TestDominantColorKmeans(unittest.TestCase):
|
|
28
|
+
def test_empty_pixels(self):
|
|
29
|
+
self.assertEqual(dominant_color_kmeans([]), '#000000')
|
|
30
|
+
|
|
31
|
+
def test_single_pixel(self):
|
|
32
|
+
result = dominant_color_kmeans([(255, 0, 0)])
|
|
33
|
+
self.assertEqual(result, '#FF0000')
|
|
34
|
+
|
|
35
|
+
def test_two_pixels(self):
|
|
36
|
+
# With < k pixels, averages them
|
|
37
|
+
result = dominant_color_kmeans([(255, 0, 0), (255, 0, 0)])
|
|
38
|
+
self.assertEqual(result, '#FF0000')
|
|
39
|
+
|
|
40
|
+
def test_uniform_color(self):
|
|
41
|
+
pixels = [(100, 50, 200)] * 100
|
|
42
|
+
result = dominant_color_kmeans(pixels)
|
|
43
|
+
self.assertEqual(result, '#6432C8')
|
|
44
|
+
|
|
45
|
+
def test_prefers_saturated_over_white(self):
|
|
46
|
+
# Mix of white (background) and red (content)
|
|
47
|
+
pixels = [(255, 255, 255)] * 80 + [(200, 30, 30)] * 20
|
|
48
|
+
result = dominant_color_kmeans(pixels)
|
|
49
|
+
# Should pick the red cluster, not white (which gets penalized)
|
|
50
|
+
r = int(result[1:3], 16)
|
|
51
|
+
g = int(result[3:5], 16)
|
|
52
|
+
b = int(result[5:7], 16)
|
|
53
|
+
self.assertGreater(r, g) # Red-ish
|
|
54
|
+
self.assertGreater(r, b)
|
|
55
|
+
|
|
56
|
+
def test_prefers_larger_cluster(self):
|
|
57
|
+
# Large blue cluster vs small green cluster
|
|
58
|
+
pixels = [(30, 30, 200)] * 90 + [(30, 200, 30)] * 10
|
|
59
|
+
result = dominant_color_kmeans(pixels)
|
|
60
|
+
b = int(result[5:7], 16)
|
|
61
|
+
g = int(result[3:5], 16)
|
|
62
|
+
self.assertGreater(b, g) # Should be blue-ish
|
|
63
|
+
|
|
64
|
+
def test_penalizes_near_black(self):
|
|
65
|
+
pixels = [(5, 5, 5)] * 80 + [(100, 50, 200)] * 20
|
|
66
|
+
result = dominant_color_kmeans(pixels)
|
|
67
|
+
# The purple should win despite fewer pixels
|
|
68
|
+
r = int(result[1:3], 16)
|
|
69
|
+
self.assertGreater(r, 20) # Not near-black
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class TestSamplePoint(unittest.TestCase):
|
|
73
|
+
def test_center_pixel_of_solid_image(self):
|
|
74
|
+
img = Image.new('RGB', (20, 20), (128, 64, 32))
|
|
75
|
+
result = sample_point(img, 10, 10)
|
|
76
|
+
self.assertEqual(result, '#804020')
|
|
77
|
+
|
|
78
|
+
def test_edge_pixel_clamped(self):
|
|
79
|
+
img = Image.new('RGB', (10, 10), (255, 0, 0))
|
|
80
|
+
# Sampling at (0, 0) with radius=3 should clamp and not crash
|
|
81
|
+
result = sample_point(img, 0, 0, radius=3)
|
|
82
|
+
self.assertEqual(result, '#FF0000')
|
|
83
|
+
|
|
84
|
+
def test_sample_returns_median(self):
|
|
85
|
+
# Create an image with a gradient-like pattern
|
|
86
|
+
img = Image.new('RGB', (20, 20), (100, 100, 100))
|
|
87
|
+
# Put a bright pixel in the center
|
|
88
|
+
img.putpixel((10, 10), (255, 255, 255))
|
|
89
|
+
result = sample_point(img, 10, 10, radius=3)
|
|
90
|
+
# Median of mostly gray + one white should still be gray
|
|
91
|
+
r = int(result[1:3], 16)
|
|
92
|
+
self.assertLess(r, 200) # Not the outlier white
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class TestExtractRegion(unittest.TestCase):
|
|
96
|
+
def test_solid_region(self):
|
|
97
|
+
img = Image.new('RGB', (100, 100), (0, 128, 255))
|
|
98
|
+
result = extract_region(img, 10, 10, 20, 20)
|
|
99
|
+
self.assertEqual(result, '#0080FF')
|
|
100
|
+
|
|
101
|
+
def test_region_clamps_to_image_bounds(self):
|
|
102
|
+
img = Image.new('RGB', (50, 50), (255, 0, 0))
|
|
103
|
+
# Region extends beyond image — should not crash
|
|
104
|
+
result = extract_region(img, 40, 40, 20, 20)
|
|
105
|
+
self.assertEqual(result, '#FF0000')
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class TestAutoDetectThumbnails(unittest.TestCase):
|
|
109
|
+
def test_detects_dark_block_in_left_column(self):
|
|
110
|
+
# White image with a dark square in the left column
|
|
111
|
+
img = Image.new('RGB', (400, 400), (255, 255, 255))
|
|
112
|
+
# Draw a dark block at x=30-70, y=50-100 (in the left 11% scan zone)
|
|
113
|
+
for y in range(50, 100):
|
|
114
|
+
for x in range(30, 70):
|
|
115
|
+
img.putpixel((x, y), (50, 50, 50))
|
|
116
|
+
results = auto_detect_thumbnails(img)
|
|
117
|
+
self.assertGreater(len(results), 0)
|
|
118
|
+
name, x, y, w, h = results[0]
|
|
119
|
+
self.assertGreaterEqual(y, 40)
|
|
120
|
+
self.assertLessEqual(y + h, 110)
|
|
121
|
+
|
|
122
|
+
def test_no_thumbnails_on_white_image(self):
|
|
123
|
+
img = Image.new('RGB', (400, 400), (255, 255, 255))
|
|
124
|
+
results = auto_detect_thumbnails(img)
|
|
125
|
+
self.assertEqual(len(results), 0)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class TestAutoDetectColoredRegions(unittest.TestCase):
|
|
129
|
+
def test_detects_saturated_region(self):
|
|
130
|
+
# White image with a small bright blue rectangle
|
|
131
|
+
img = Image.new('RGB', (200, 200), (255, 255, 255))
|
|
132
|
+
for y in range(50, 70):
|
|
133
|
+
for x in range(100, 140):
|
|
134
|
+
img.putpixel((x, y), (0, 100, 255))
|
|
135
|
+
results = auto_detect_colored_regions(img)
|
|
136
|
+
self.assertGreater(len(results), 0)
|
|
137
|
+
|
|
138
|
+
def test_no_colored_regions_on_white_image(self):
|
|
139
|
+
img = Image.new('RGB', (200, 200), (255, 255, 255))
|
|
140
|
+
results = auto_detect_colored_regions(img)
|
|
141
|
+
self.assertEqual(len(results), 0)
|
|
142
|
+
|
|
143
|
+
def test_ignores_near_white_and_near_black(self):
|
|
144
|
+
# Image with only near-white and near-black — no saturated pixels
|
|
145
|
+
img = Image.new('RGB', (200, 200), (250, 250, 250))
|
|
146
|
+
for y in range(50, 100):
|
|
147
|
+
for x in range(50, 100):
|
|
148
|
+
img.putpixel((x, y), (10, 10, 10))
|
|
149
|
+
results = auto_detect_colored_regions(img)
|
|
150
|
+
self.assertEqual(len(results), 0)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class TestResolveMacosPath(unittest.TestCase):
|
|
154
|
+
def test_existing_path_unchanged(self):
|
|
155
|
+
with tempfile.NamedTemporaryFile(delete=False) as f:
|
|
156
|
+
result = _resolve_macos_path(f.name)
|
|
157
|
+
self.assertEqual(result, f.name)
|
|
158
|
+
os.unlink(f.name)
|
|
159
|
+
|
|
160
|
+
def test_nonexistent_path_returned_as_is(self):
|
|
161
|
+
path = '/tmp/nonexistent_test_file_12345.png'
|
|
162
|
+
result = _resolve_macos_path(path)
|
|
163
|
+
self.assertEqual(result, path)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
if __name__ == '__main__':
|
|
167
|
+
unittest.main()
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Tests for find_icon.py — fuzzy icon matching logic."""
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
import tempfile
|
|
7
|
+
import unittest
|
|
8
|
+
|
|
9
|
+
# Add the script directory to path
|
|
10
|
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
11
|
+
from find_icon import split_camel_case, expand_query, score_icon, parse_icons, find_icons
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class TestSplitCamelCase(unittest.TestCase):
|
|
15
|
+
def test_simple_camel_case(self):
|
|
16
|
+
self.assertEqual(split_camel_case('chatBubbleFill'), ['chat', 'bubble', 'fill'])
|
|
17
|
+
|
|
18
|
+
def test_multi_word(self):
|
|
19
|
+
self.assertEqual(split_camel_case('arrowRightShareFill'), ['arrow', 'right', 'share', 'fill'])
|
|
20
|
+
|
|
21
|
+
def test_single_word(self):
|
|
22
|
+
self.assertEqual(split_camel_case('camera'), ['camera'])
|
|
23
|
+
|
|
24
|
+
def test_consecutive_capitals(self):
|
|
25
|
+
# 'SC' treated as one segment
|
|
26
|
+
parts = split_camel_case('SCMyModule')
|
|
27
|
+
self.assertEqual(parts[0], 'sc')
|
|
28
|
+
|
|
29
|
+
def test_fill_suffix(self):
|
|
30
|
+
parts = split_camel_case('gearFill')
|
|
31
|
+
self.assertEqual(parts, ['gear', 'fill'])
|
|
32
|
+
|
|
33
|
+
def test_xmark(self):
|
|
34
|
+
parts = split_camel_case('xmarkFill')
|
|
35
|
+
self.assertEqual(parts, ['xmark', 'fill'])
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class TestExpandQuery(unittest.TestCase):
|
|
39
|
+
def test_known_synonym(self):
|
|
40
|
+
result = expand_query(['settings'])
|
|
41
|
+
self.assertIn('gear', result)
|
|
42
|
+
self.assertIn('settings', result)
|
|
43
|
+
|
|
44
|
+
def test_no_synonym(self):
|
|
45
|
+
result = expand_query(['chat'])
|
|
46
|
+
self.assertEqual(result, ['chat'])
|
|
47
|
+
|
|
48
|
+
def test_multiple_synonyms(self):
|
|
49
|
+
result = expand_query(['search'])
|
|
50
|
+
self.assertIn('magnifying', result)
|
|
51
|
+
self.assertIn('glass', result)
|
|
52
|
+
|
|
53
|
+
def test_mixed_known_unknown(self):
|
|
54
|
+
result = expand_query(['send', 'big'])
|
|
55
|
+
self.assertIn('send', result)
|
|
56
|
+
self.assertIn('big', result)
|
|
57
|
+
self.assertIn('arrow', result)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class TestScoreIcon(unittest.TestCase):
|
|
61
|
+
def test_exact_match_scores_high(self):
|
|
62
|
+
icon_words = ['chat', 'bubble', 'fill']
|
|
63
|
+
query_words = ['chat', 'bubble']
|
|
64
|
+
expanded = expand_query(query_words)
|
|
65
|
+
score = score_icon(icon_words, query_words, expanded)
|
|
66
|
+
self.assertGreater(score, 0)
|
|
67
|
+
|
|
68
|
+
def test_no_match_scores_zero(self):
|
|
69
|
+
icon_words = ['gear', 'fill']
|
|
70
|
+
query_words = ['chat', 'bubble']
|
|
71
|
+
expanded = expand_query(query_words)
|
|
72
|
+
score = score_icon(icon_words, query_words, expanded)
|
|
73
|
+
self.assertEqual(score, 0.0)
|
|
74
|
+
|
|
75
|
+
def test_synonym_match(self):
|
|
76
|
+
icon_words = ['gear', 'fill']
|
|
77
|
+
query_words = ['settings']
|
|
78
|
+
expanded = expand_query(query_words)
|
|
79
|
+
score = score_icon(icon_words, query_words, expanded)
|
|
80
|
+
self.assertGreater(score, 0)
|
|
81
|
+
|
|
82
|
+
def test_fill_variant_boosted(self):
|
|
83
|
+
fill_words = ['chat', 'bubble', 'fill']
|
|
84
|
+
stroke_words = ['chat', 'bubble', 'stroke']
|
|
85
|
+
query_words = ['chat', 'bubble']
|
|
86
|
+
expanded = expand_query(query_words)
|
|
87
|
+
fill_score = score_icon(fill_words, query_words, expanded)
|
|
88
|
+
stroke_score = score_icon(stroke_words, query_words, expanded)
|
|
89
|
+
self.assertGreater(fill_score, stroke_score)
|
|
90
|
+
|
|
91
|
+
def test_empty_query_scores_zero(self):
|
|
92
|
+
icon_words = ['chat', 'bubble']
|
|
93
|
+
score = score_icon(icon_words, [], [])
|
|
94
|
+
self.assertEqual(score, 0.0)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class TestParseIcons(unittest.TestCase):
|
|
98
|
+
def test_parses_static_getters(self):
|
|
99
|
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.tsx', delete=False) as f:
|
|
100
|
+
f.write("""
|
|
101
|
+
export class SIGIcon {
|
|
102
|
+
static get chatBubbleFill(): Asset { return Asset.fromModule('sig_chat_bubble_fill'); }
|
|
103
|
+
static get gearFill(): Asset { return Asset.fromModule('sig_gear_fill'); }
|
|
104
|
+
static get bellStroke(): Asset { return Asset.fromModule('sig_bell_stroke'); }
|
|
105
|
+
}
|
|
106
|
+
""")
|
|
107
|
+
f.flush()
|
|
108
|
+
icons = parse_icons(f.name)
|
|
109
|
+
os.unlink(f.name)
|
|
110
|
+
self.assertEqual(icons, ['chatBubbleFill', 'gearFill', 'bellStroke'])
|
|
111
|
+
|
|
112
|
+
def test_empty_file(self):
|
|
113
|
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.tsx', delete=False) as f:
|
|
114
|
+
f.write("// empty\n")
|
|
115
|
+
f.flush()
|
|
116
|
+
icons = parse_icons(f.name)
|
|
117
|
+
os.unlink(f.name)
|
|
118
|
+
self.assertEqual(icons, [])
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class TestFindIcons(unittest.TestCase):
|
|
122
|
+
def setUp(self):
|
|
123
|
+
self.icons_file = tempfile.NamedTemporaryFile(mode='w', suffix='.tsx', delete=False)
|
|
124
|
+
self.icons_file.write("""
|
|
125
|
+
export class SIGIcon {
|
|
126
|
+
static get chatBubbleFill(): Asset { return Asset.fromModule('a'); }
|
|
127
|
+
static get chatBubbleStroke(): Asset { return Asset.fromModule('b'); }
|
|
128
|
+
static get gearFill(): Asset { return Asset.fromModule('c'); }
|
|
129
|
+
static get gearStroke(): Asset { return Asset.fromModule('d'); }
|
|
130
|
+
static get bellFill(): Asset { return Asset.fromModule('e'); }
|
|
131
|
+
static get arrowRightFill(): Asset { return Asset.fromModule('f'); }
|
|
132
|
+
static get mapPinFill(): Asset { return Asset.fromModule('g'); }
|
|
133
|
+
static get personFill(): Asset { return Asset.fromModule('h'); }
|
|
134
|
+
static get magnifyingGlassFill(): Asset { return Asset.fromModule('i'); }
|
|
135
|
+
static get threeDotHorizontalFill(): Asset { return Asset.fromModule('j'); }
|
|
136
|
+
}
|
|
137
|
+
""")
|
|
138
|
+
self.icons_file.flush()
|
|
139
|
+
|
|
140
|
+
def tearDown(self):
|
|
141
|
+
os.unlink(self.icons_file.name)
|
|
142
|
+
|
|
143
|
+
def test_direct_match(self):
|
|
144
|
+
results = find_icons('chat bubble', self.icons_file.name)
|
|
145
|
+
self.assertTrue(len(results) > 0)
|
|
146
|
+
# chatBubbleFill should be the top result
|
|
147
|
+
self.assertEqual(results[0][0], 'chatBubbleFill')
|
|
148
|
+
|
|
149
|
+
def test_synonym_match(self):
|
|
150
|
+
results = find_icons('settings', self.icons_file.name)
|
|
151
|
+
self.assertTrue(len(results) > 0)
|
|
152
|
+
top_names = [r[0] for r in results]
|
|
153
|
+
self.assertTrue(any('gear' in name.lower() for name in top_names))
|
|
154
|
+
|
|
155
|
+
def test_notification_synonym(self):
|
|
156
|
+
results = find_icons('notification', self.icons_file.name)
|
|
157
|
+
self.assertTrue(len(results) > 0)
|
|
158
|
+
top_names = [r[0] for r in results]
|
|
159
|
+
self.assertIn('bellFill', top_names)
|
|
160
|
+
|
|
161
|
+
def test_search_synonym(self):
|
|
162
|
+
results = find_icons('search', self.icons_file.name)
|
|
163
|
+
self.assertTrue(len(results) > 0)
|
|
164
|
+
self.assertEqual(results[0][0], 'magnifyingGlassFill')
|
|
165
|
+
|
|
166
|
+
def test_no_match(self):
|
|
167
|
+
results = find_icons('xyznonexistent', self.icons_file.name)
|
|
168
|
+
self.assertEqual(len(results), 0)
|
|
169
|
+
|
|
170
|
+
def test_top_n_limiting(self):
|
|
171
|
+
results = find_icons('fill', self.icons_file.name, top_n=3)
|
|
172
|
+
self.assertTrue(len(results) <= 3)
|
|
173
|
+
|
|
174
|
+
def test_profile_synonym(self):
|
|
175
|
+
results = find_icons('profile', self.icons_file.name)
|
|
176
|
+
self.assertTrue(len(results) > 0)
|
|
177
|
+
top_names = [r[0] for r in results]
|
|
178
|
+
self.assertIn('personFill', top_names)
|
|
179
|
+
|
|
180
|
+
def test_menu_synonym(self):
|
|
181
|
+
results = find_icons('menu', self.icons_file.name)
|
|
182
|
+
self.assertTrue(len(results) > 0)
|
|
183
|
+
top_names = [r[0] for r in results]
|
|
184
|
+
self.assertIn('threeDotHorizontalFill', top_names)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
if __name__ == '__main__':
|
|
188
|
+
unittest.main()
|