-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_test.go
More file actions
406 lines (373 loc) · 10.7 KB
/
Copy pathmodel_test.go
File metadata and controls
406 lines (373 loc) · 10.7 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package classifier
import (
"fmt"
"math"
"strings"
"testing"
)
func TestLoadEmbedded(t *testing.T) {
model, err := LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded returned error: %v", err)
}
if model.ModelID() == "" {
t.Fatal("expected non-empty model id")
}
if model.Version() == "" {
t.Fatal("expected non-empty version")
}
if model.Threshold() <= 0 || model.Threshold() >= 1 {
t.Fatalf("expected threshold in (0,1), got %v", model.Threshold())
}
if EmbeddedArtifactBytes() <= 0 {
t.Fatalf("expected embedded artifact bytes > 0")
}
}
func TestClassifyDeterministic(t *testing.T) {
model, err := LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded returned error: %v", err)
}
text := "It seems this approach may potentially help in many cases."
base := model.Classify(text)
for i := 0; i < 20; i++ {
got := model.Classify(text)
if got.Label != base.Label {
t.Fatalf("run %d label mismatch: got %q want %q", i, got.Label, base.Label)
}
if math.Abs(got.RiskScore-base.RiskScore) > 1e-12 {
t.Fatalf(
"run %d risk mismatch: got %.12f want %.12f",
i,
got.RiskScore,
base.RiskScore,
)
}
}
}
func TestClassifySeparatesVerboseAndDirect(t *testing.T) {
model, err := LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded returned error: %v", err)
}
verbose := model.Classify(
"Basically, it seems we might want to consider this approach " +
"in order to improve outcomes in many situations.",
)
direct := model.Classify(
"Run go test ./... and publish checksums for release artifacts.",
)
if verbose.RiskScore <= direct.RiskScore {
t.Fatalf(
"expected verbose risk > direct risk, got %.4f <= %.4f",
verbose.RiskScore,
direct.RiskScore,
)
}
if verbose.Label != "verbose-actionable" {
t.Fatalf("expected verbose label, got %q", verbose.Label)
}
if direct.Label != "acceptable" {
t.Fatalf("expected acceptable label, got %q", direct.Label)
}
}
func TestLoadEmbedded_LexiconCoverage(t *testing.T) {
model, err := LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded returned error: %v", err)
}
counts := model.LexiconCounts()
if counts.FillerWords < minFillerWords {
t.Fatalf(
"expected filler_words >= %d, got %d",
minFillerWords,
counts.FillerWords,
)
}
if counts.ModalWords < minModalWords {
t.Fatalf(
"expected modal_words >= %d, got %d",
minModalWords,
counts.ModalWords,
)
}
if counts.VagueWords < minVagueWords {
t.Fatalf(
"expected vague_words >= %d, got %d",
minVagueWords,
counts.VagueWords,
)
}
if counts.ActionWords < minActionWords {
t.Fatalf(
"expected action_words >= %d, got %d",
minActionWords,
counts.ActionWords,
)
}
if counts.StopWords < minStopWords {
t.Fatalf(
"expected stop_words >= %d, got %d",
minStopWords,
counts.StopWords,
)
}
if counts.HedgePhrases < minHedgePhrases {
t.Fatalf(
"expected hedge_phrases >= %d, got %d",
minHedgePhrases,
counts.HedgePhrases,
)
}
if counts.VerbosePhrases < minVerbosePhrases {
t.Fatalf(
"expected verbose_phrases >= %d, got %d",
minVerbosePhrases,
counts.VerbosePhrases,
)
}
}
func TestNormalizeCueList_RejectsDuplicatesAfterNormalization(t *testing.T) {
_, err := normalizeCueList("filler_words", []string{"Maybe", "maybe"}, 1, true)
if err == nil {
t.Fatal("expected duplicate normalization error")
}
if !strings.Contains(err.Error(), "duplicate entry") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestNormalizeCueList_RejectsInvalidToken(t *testing.T) {
_, err := normalizeCueList("filler_words", []string{"two words"}, 1, true)
if err == nil {
t.Fatal("expected invalid token error")
}
if !strings.Contains(err.Error(), "invalid token") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestValidateWeights_RejectsUnknownKey(t *testing.T) {
err := validateWeights(map[string]float64{
"filler_rate": 1,
"hedge_rate": 1,
"verbose_phrase_rate": 1,
"modal_rate": 1,
"vague_rate": 1,
"action_rate": 1,
"content_ratio": 1,
"log_word_count": 1,
"compression_ratio": 1,
"type_token_ratio": 1,
"nominal_density": 1,
"sent_len_variance": 1,
"func_word_ratio": 1,
"avg_word_length": 1,
"ly_adverb_density": 1,
"unexpected": 1,
})
if err == nil {
t.Fatal("expected unknown key error")
}
if !strings.Contains(err.Error(), "unknown key") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestExtractFeatures_NewFeatures(t *testing.T) {
model, err := LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded returned error: %v", err)
}
text := "Basically, it seems that we are just trying to explain the same idea in order to make it very clear."
result := model.Classify(text)
// All 15 features should be present
expectedFeatures := []string{
"filler_rate", "hedge_rate", "verbose_phrase_rate",
"modal_rate", "vague_rate", "action_rate",
"content_ratio", "log_word_count",
"compression_ratio", "type_token_ratio", "nominal_density",
"sent_len_variance", "func_word_ratio", "avg_word_length",
"ly_adverb_density",
}
for _, name := range expectedFeatures {
if _, ok := result.FeatureSummary[name]; !ok {
t.Errorf("missing feature %q in FeatureSummary", name)
}
}
if len(result.FeatureSummary) != 15 {
t.Errorf("expected 15 features, got %d", len(result.FeatureSummary))
}
}
func normalizeText(text string) string {
tokens := wordPattern.FindAllString(strings.ToLower(text), -1)
if len(tokens) == 0 {
return " "
}
return " " + strings.Join(tokens, " ") + " "
}
func TestCountPhraseMatches_UsesBoundaries(t *testing.T) {
norm := normalizeText("This statement is in order too noisy to match the cue.")
count, cues := countPhraseMatches(norm, []string{"in order to"})
if count != 0 {
t.Fatalf("expected 0 phrase matches, got %d (cues=%v)", count, cues)
}
norm = normalizeText("This statement is in order to reduce noise.")
count, cues = countPhraseMatches(norm, []string{"in order to"})
if count != 1 {
t.Fatalf("expected 1 phrase match, got %d (cues=%v)", count, cues)
}
}
func TestClassify_WordCount(t *testing.T) {
model, err := LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded returned error: %v", err)
}
result := model.Classify("hello world foo bar")
if result.WordCount != 4 {
t.Fatalf("expected WordCount=4, got %d", result.WordCount)
}
}
func TestClassify_ActionWordsNotInCues(t *testing.T) {
model, err := LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded returned error: %v", err)
}
result := model.Classify("Run and build the test then deploy the update.")
for _, cue := range result.TriggeredCues {
for _, action := range []string{"run", "build", "test", "deploy", "update"} {
if cue == action {
t.Errorf("action word %q should not appear in TriggeredCues", cue)
}
}
}
}
func TestClassify_EmptyInputKeepsCueSliceNonNil(t *testing.T) {
model, err := LoadEmbedded()
if err != nil {
t.Fatalf("LoadEmbedded returned error: %v", err)
}
result := model.Classify("")
if result.TriggeredCues == nil {
t.Fatal("expected non-nil TriggeredCues slice for empty input")
}
if len(result.TriggeredCues) != 0 {
t.Fatalf(
"expected zero triggered cues for empty input, got %d",
len(result.TriggeredCues),
)
}
}
// =====================================================================
// Phase 4 coverage: validateArtifact field validation
// =====================================================================
func TestValidateArtifact_EmptyModelID(t *testing.T) {
a := artifact{ModelID: "", Version: "1.0", Threshold: 0.5}
err := validateArtifact(a)
if err == nil {
t.Fatal("expected error for empty model_id")
}
if !strings.Contains(err.Error(), "model_id") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestValidateArtifact_EmptyVersion(t *testing.T) {
a := artifact{ModelID: "test", Version: "", Threshold: 0.5}
err := validateArtifact(a)
if err == nil {
t.Fatal("expected error for empty version")
}
if !strings.Contains(err.Error(), "version") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestValidateArtifact_InvalidThreshold(t *testing.T) {
for _, th := range []float64{0, 1, -0.1, 1.5} {
a := artifact{ModelID: "test", Version: "1.0", Threshold: th}
err := validateArtifact(a)
if err == nil {
t.Fatalf("expected error for threshold %v", th)
}
if !strings.Contains(err.Error(), "threshold") {
t.Fatalf("threshold %v: unexpected error: %v", th, err)
}
}
}
func TestValidateArtifact_EmptyWeights(t *testing.T) {
a := artifact{
ModelID: "test",
Version: "1.0",
Threshold: 0.5,
Weights: map[string]float64{},
}
err := validateArtifact(a)
if err == nil {
t.Fatal("expected error for empty weights")
}
if !strings.Contains(err.Error(), "weights") {
t.Fatalf("unexpected error: %v", err)
}
}
// =====================================================================
// Phase 4 coverage: compileLexicon per-list errors
// =====================================================================
func TestCompileLexicon_InsufficientFillerWords(t *testing.T) {
raw := lexiconArtifact{FillerWords: []string{}}
_, err := compileLexicon(raw)
if err == nil {
t.Fatal("expected error for insufficient filler_words")
}
if !strings.Contains(err.Error(), "filler_words") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestCompileLexicon_InsufficientModalWords(t *testing.T) {
fillers := make([]string, minFillerWords)
for i := range fillers {
fillers[i] = fmt.Sprintf("filler%d", i)
}
raw := lexiconArtifact{
FillerWords: fillers,
ModalWords: []string{},
}
_, err := compileLexicon(raw)
if err == nil {
t.Fatal("expected error for insufficient modal_words")
}
if !strings.Contains(err.Error(), "modal_words") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestCompileLexicon_InsufficientVagueWords(t *testing.T) {
fillers := make([]string, minFillerWords)
for i := range fillers {
fillers[i] = fmt.Sprintf("filler%d", i)
}
modals := make([]string, minModalWords)
for i := range modals {
modals[i] = fmt.Sprintf("modal%d", i)
}
raw := lexiconArtifact{
FillerWords: fillers,
ModalWords: modals,
VagueWords: []string{},
}
_, err := compileLexicon(raw)
if err == nil {
t.Fatal("expected error for insufficient vague_words")
}
if !strings.Contains(err.Error(), "vague_words") {
t.Fatalf("unexpected error: %v", err)
}
}
func BenchmarkClassify(b *testing.B) {
model, err := LoadEmbedded()
if err != nil {
b.Fatalf("LoadEmbedded returned error: %v", err)
}
text := "Basically, it seems that we are just trying to explain " +
"the same idea in order to make it very clear, and it " +
"appears that we are really saying very little new " +
"information overall."
b.ResetTimer()
for i := 0; i < b.N; i++ {
model.Classify(text)
}
}