-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsemantic_test.go
More file actions
344 lines (320 loc) · 9.39 KB
/
Copy pathsemantic_test.go
File metadata and controls
344 lines (320 loc) · 9.39 KB
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
package semantic_test
import (
"context"
"testing"
"github.com/pinchtab/semantic"
)
func negativeMatchingFixture() []semantic.ElementDescriptor {
return []semantic.ElementDescriptor{
{Ref: "e0", Role: "button", Name: "Submit"},
{Ref: "e1", Role: "button", Name: "Cancel"},
{Ref: "e2", Role: "button", Name: "Sign In"},
{Ref: "e3", Role: "link", Name: "Logout"},
{Ref: "e4", Role: "textbox", Name: "Email"},
{Ref: "e5", Role: "textbox", Name: "Password"},
}
}
func findScore(matches []semantic.ElementMatch, ref string) (float64, bool) {
for _, m := range matches {
if m.Ref == ref {
return m.Score, true
}
}
return 0, false
}
func TestLexicalMatcher_NegativeMatching_Issue24Cases(t *testing.T) {
m := semantic.NewLexicalMatcher()
elements := negativeMatchingFixture()
tests := []struct {
name string
query string
check func(t *testing.T, result semantic.FindResult)
}{
{
name: "button not submit penalizes submit",
query: "button not submit",
check: func(t *testing.T, result semantic.FindResult) {
e0, ok := findScore(result.Matches, "e0")
if !ok {
t.Fatalf("expected e0 in matches")
}
e1, ok := findScore(result.Matches, "e1")
if !ok {
t.Fatalf("expected e1 in matches")
}
e2, ok := findScore(result.Matches, "e2")
if !ok {
t.Fatalf("expected e2 in matches")
}
if !(e1 > e0 || e2 > e0) {
t.Fatalf("expected e1 or e2 to rank above penalized e0, got e0=%.4f e1=%.4f e2=%.4f", e0, e1, e2)
}
},
},
{
name: "button not behaves as button",
query: "button not",
check: func(t *testing.T, result semantic.FindResult) {
if result.BestRef == "e4" || result.BestRef == "e5" {
t.Fatalf("expected a button-like result, got %s", result.BestRef)
}
},
},
{
name: "button not cancel penalizes cancel",
query: "button not cancel",
check: func(t *testing.T, result semantic.FindResult) {
e0, ok := findScore(result.Matches, "e0")
if !ok {
t.Fatalf("expected e0 in matches")
}
e1, ok := findScore(result.Matches, "e1")
if !ok {
t.Fatalf("expected e1 in matches")
}
if e0 <= e1 {
t.Fatalf("expected e0 above penalized e1, got e0=%.4f e1=%.4f", e0, e1)
}
},
},
{
name: "textbox not email prefers password",
query: "textbox not email",
check: func(t *testing.T, result semantic.FindResult) {
e4, ok := findScore(result.Matches, "e4")
if !ok {
t.Fatalf("expected e4 in matches")
}
e5, ok := findScore(result.Matches, "e5")
if !ok {
t.Fatalf("expected e5 in matches")
}
if e5 <= e4 {
t.Fatalf("expected e5 above penalized e4, got e4=%.4f e5=%.4f", e4, e5)
}
},
},
{
name: "button not login penalizes sign in by synonym",
query: "button not login",
check: func(t *testing.T, result semantic.FindResult) {
e0, ok := findScore(result.Matches, "e0")
if !ok {
t.Fatalf("expected e0 in matches")
}
e1, ok := findScore(result.Matches, "e1")
if !ok {
t.Fatalf("expected e1 in matches")
}
e2, ok := findScore(result.Matches, "e2")
if !ok {
t.Fatalf("expected e2 in matches")
}
if !(e0 > e2 && e1 > e2) {
t.Fatalf("expected e2 to be penalized by login/sign in synonym, got e0=%.4f e1=%.4f e2=%.4f", e0, e1, e2)
}
},
},
{
name: "button not sign in penalizes sign in",
query: "button not sign in",
check: func(t *testing.T, result semantic.FindResult) {
e0, ok := findScore(result.Matches, "e0")
if !ok {
t.Fatalf("expected e0 in matches")
}
e1, ok := findScore(result.Matches, "e1")
if !ok {
t.Fatalf("expected e1 in matches")
}
e2, ok := findScore(result.Matches, "e2")
if !ok {
t.Fatalf("expected e2 in matches")
}
if !(e0 > e2 && e1 > e2) {
t.Fatalf("expected e2 to be penalized by multi-token negative, got e0=%.4f e1=%.4f e2=%.4f", e0, e1, e2)
}
},
},
{
name: "button not submit not cancel penalizes both",
query: "button not submit not cancel",
check: func(t *testing.T, result semantic.FindResult) {
e0, ok := findScore(result.Matches, "e0")
if !ok {
t.Fatalf("expected e0 in matches")
}
e1, ok := findScore(result.Matches, "e1")
if !ok {
t.Fatalf("expected e1 in matches")
}
e2, ok := findScore(result.Matches, "e2")
if !ok {
t.Fatalf("expected e2 in matches")
}
if !(e2 > e0 && e2 > e1) {
t.Fatalf("expected both submit/cancel to be penalized, got e0=%.4f e1=%.4f e2=%.4f", e0, e1, e2)
}
},
},
{
name: "sign in button regression",
query: "sign in button",
check: func(t *testing.T, result semantic.FindResult) {
if result.BestRef != "e2" {
t.Fatalf("expected e2 as best result, got %s", result.BestRef)
}
},
},
{
name: "link without logout drives logout near zero",
query: "link without logout",
check: func(t *testing.T, result semantic.FindResult) {
e3, ok := findScore(result.Matches, "e3")
if !ok {
t.Fatalf("expected e3 in matches")
}
if e3 > 0.1 {
t.Fatalf("expected e3 near-zero score, got %.4f", e3)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := m.Find(context.Background(), tt.query, elements, semantic.FindOptions{
Threshold: 0,
TopK: len(elements),
})
if err != nil {
t.Fatalf("Find returned error: %v", err)
}
tt.check(t, result)
})
}
}
func TestLexicalMatcher_NegativeOnlyQuery(t *testing.T) {
m := semantic.NewLexicalMatcher()
elements := negativeMatchingFixture()
result, err := m.Find(context.Background(), "not submit", elements, semantic.FindOptions{
Threshold: 0,
TopK: len(elements),
})
if err != nil {
t.Fatalf("Find returned error: %v", err)
}
if len(result.Matches) == 0 {
t.Fatalf("expected non-empty matches for leading-not query")
}
if result.BestRef != "e0" {
t.Fatalf("expected leading-not query to behave as positive text, got best=%s", result.BestRef)
}
}
func TestNewCombinedMatcher_Find(t *testing.T) {
m := semantic.NewCombinedMatcher(semantic.NewHashingEmbedder(128))
elements := []semantic.ElementDescriptor{
{Ref: "e0", Role: "button", Name: "Sign In"},
{Ref: "e1", Role: "textbox", Name: "Email"},
{Ref: "e2", Role: "link", Name: "Forgot Password"},
}
result, err := m.Find(context.Background(), "login button", elements, semantic.FindOptions{
Threshold: 0.1,
TopK: 3,
})
if err != nil {
t.Fatalf("Find: %v", err)
}
if result.BestRef != "e0" {
t.Errorf("expected BestRef=e0, got %s", result.BestRef)
}
if result.ElementCount != 3 {
t.Errorf("expected ElementCount=3, got %d", result.ElementCount)
}
}
func TestNewLexicalMatcher(t *testing.T) {
m := semantic.NewLexicalMatcher()
if m.Strategy() != "lexical" {
t.Errorf("expected strategy=lexical, got %s", m.Strategy())
}
}
func TestNewEmbeddingMatcher(t *testing.T) {
e := semantic.NewHashingEmbedder(64)
m := semantic.NewEmbeddingMatcher(e)
if m.Strategy() != "embedding:hashing" {
t.Errorf("expected strategy=embedding:hashing, got %s", m.Strategy())
}
}
func TestNewEmbeddingMatcherWithNeighborWeight(t *testing.T) {
e := semantic.NewHashingEmbedder(64)
m := semantic.NewEmbeddingMatcherWithNeighborWeight(e, 0.2)
if m.Strategy() != "embedding:hashing" {
t.Errorf("expected strategy=embedding:hashing, got %s", m.Strategy())
}
}
func TestCalibrateConfidence(t *testing.T) {
if semantic.CalibrateConfidence(0.9) != "high" {
t.Error("0.9 should be high")
}
if semantic.CalibrateConfidence(0.7) != "medium" {
t.Error("0.7 should be medium")
}
if semantic.CalibrateConfidence(0.3) != "low" {
t.Error("0.3 should be low")
}
}
func TestLexicalScore(t *testing.T) {
score := semantic.LexicalScore("submit button", "button: Submit")
if score < 0.5 {
t.Errorf("expected high score for exact match, got %.2f", score)
}
}
func TestCosineSimilarity(t *testing.T) {
v := []float32{1, 0, 0}
if semantic.CosineSimilarity(v, v) < 0.99 {
t.Error("identical vectors should have similarity ~1.0")
}
}
func TestElementDescriptor_Composite(t *testing.T) {
d := semantic.ElementDescriptor{Role: "button", Name: "Submit"}
if d.Composite() != "button: Submit" {
t.Errorf("unexpected composite: %q", d.Composite())
}
}
func TestElementDescriptor_Composite_IncludesSection(t *testing.T) {
d := semantic.ElementDescriptor{Role: "button", Name: "Submit", Section: "Login form"}
if d.Composite() != "button: Submit {Login form}" {
t.Errorf("unexpected composite with section: %q", d.Composite())
}
}
func TestElementDescriptor_Composite_IncludesLocatorFields(t *testing.T) {
d := semantic.ElementDescriptor{
Role: "textbox",
Name: "Email",
Label: "Work Email",
Placeholder: "name@example.com",
Title: "Primary email address",
TestID: "email-input",
}
want := "textbox: Email label:Work Email placeholder:name@example.com title:Primary email address"
if d.Composite() != want {
t.Errorf("unexpected composite with locator fields: %q", d.Composite())
}
}
func TestElementDescriptor_PositionalHints(t *testing.T) {
d := semantic.ElementDescriptor{
Role: "textbox",
Name: "Email",
Positional: semantic.PositionalHints{
Depth: 3,
SiblingIndex: 1,
SiblingCount: 2,
LabelledBy: "Work Email",
},
}
if d.Positional.Depth != 3 {
t.Errorf("unexpected depth: %d", d.Positional.Depth)
}
if d.Positional.LabelledBy != "Work Email" {
t.Errorf("unexpected labelled_by: %q", d.Positional.LabelledBy)
}
}