-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathanalysis_refiner_test.go
More file actions
538 lines (484 loc) · 13.3 KB
/
analysis_refiner_test.go
File metadata and controls
538 lines (484 loc) · 13.3 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package ai
import (
"context"
"strings"
"testing"
"github.com/steveyegge/vc/internal/iterative"
"github.com/steveyegge/vc/internal/types"
)
// TestNewAnalysisRefiner tests refiner creation
func TestNewAnalysisRefiner(t *testing.T) {
// Create test issue
issue := &types.Issue{
ID: "test-1",
Title: "Test Issue",
Description: "Test description",
AcceptanceCriteria: "1. Should work\n2. Should be tested",
}
tests := []struct {
name string
supervisor *Supervisor
issue *types.Issue
agentOutput string
success bool
wantErr bool
}{
{
name: "valid refiner creation",
supervisor: &Supervisor{}, // Mock supervisor (won't actually call API in this test)
issue: issue,
agentOutput: "test output",
success: true,
wantErr: false,
},
{
name: "nil supervisor",
supervisor: nil,
issue: issue,
agentOutput: "test output",
success: true,
wantErr: true,
},
{
name: "nil issue",
supervisor: &Supervisor{},
issue: nil,
agentOutput: "test output",
success: true,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
refiner, err := NewAnalysisRefiner(tt.supervisor, tt.issue, tt.agentOutput, tt.success)
if (err != nil) != tt.wantErr {
t.Errorf("NewAnalysisRefiner() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && refiner == nil {
t.Error("NewAnalysisRefiner() returned nil refiner without error")
}
if !tt.wantErr {
if refiner.supervisor != tt.supervisor {
t.Error("Refiner supervisor not set correctly")
}
if refiner.issue != tt.issue {
t.Error("Refiner issue not set correctly")
}
if refiner.minConfidence <= 0 {
t.Error("Refiner minConfidence not set")
}
}
})
}
}
// TestSerializeAnalysis tests analysis serialization
func TestSerializeAnalysis(t *testing.T) {
analysis := &Analysis{
Completed: true,
Confidence: 0.95,
Summary: "Test summary",
PuntedItems: []string{"Item 1", "Item 2"},
DiscoveredIssues: []DiscoveredIssue{
{
Title: "Bug found",
Description: "A bug was discovered",
Type: "bug",
Priority: "P1",
DiscoveryType: "blocker",
},
},
QualityIssues: []string{"Missing tests"},
ScopeValidation: &ScopeValidation{
OnTask: true,
Explanation: "Agent worked on correct task",
},
}
serialized := serializeAnalysis(analysis)
// Check that key elements are present in serialization
if serialized == "" {
t.Fatal("serializeAnalysis() returned empty string")
}
// Check for key markers
expectedStrings := []string{
"Completed: true",
"Confidence: 0.95",
"Test summary",
"Punted Items (2)",
"Item 1",
"Discovered Issues (1)",
"Bug found",
"Quality Issues (1)",
"Missing tests",
"Scope Validation",
"On Task: true",
}
for _, expected := range expectedStrings {
if !contains(serialized, expected) {
t.Errorf("serializeAnalysis() missing expected string: %q\nGot: %s", expected, serialized)
}
}
}
// TestAnalysisRefinerBuildRefinementPrompt tests prompt construction
func TestAnalysisRefinerBuildRefinementPrompt(t *testing.T) {
issue := &types.Issue{
ID: "test-1",
Title: "Test Issue",
Description: "Test description",
AcceptanceCriteria: "1. Should work",
}
refiner := &AnalysisRefiner{
supervisor: &Supervisor{},
issue: issue,
agentOutput: "Agent completed the work successfully",
success: true,
}
artifact := &iterative.Artifact{
Type: "analysis",
Content: "Previous analysis content",
Context: "Some context",
}
prompt := refiner.buildRefinementPrompt(artifact)
// Check that prompt contains key elements
expectedStrings := []string{
"ITERATIVE REFINEMENT",
"test-1",
"Test Issue",
"Test description",
"1. Should work",
"succeeded",
"Agent completed the work successfully",
"Previous analysis content",
"Some context",
"Discovered Issues",
"Punted Items",
"Quality Issues",
"Scope Validation",
}
for _, expected := range expectedStrings {
if !contains(prompt, expected) {
t.Errorf("buildRefinementPrompt() missing expected string: %q", expected)
}
}
}
// TestAnalysisRefinerBuildConvergencePrompt tests convergence prompt construction
func TestAnalysisRefinerBuildConvergencePrompt(t *testing.T) {
refiner := &AnalysisRefiner{
supervisor: &Supervisor{},
issue: &types.Issue{ID: "test-1"},
}
current := &iterative.Artifact{
Type: "analysis",
Content: "Current analysis with 3 discovered issues",
Context: "Found more issues in iteration 2",
}
previous := &iterative.Artifact{
Type: "analysis",
Content: "Previous analysis with 1 discovered issue",
Context: "Initial iteration",
}
prompt := refiner.buildConvergencePrompt(current, previous)
// Check that prompt contains key elements
expectedStrings := []string{
"converged",
"PREVIOUS VERSION",
"Previous analysis with 1 discovered issue",
"CURRENT VERSION",
"Current analysis with 3 discovered issues",
"CONTEXT",
"Found more issues in iteration 2",
"Diff size",
"Completeness",
"Gaps",
"Marginal value",
"confidence",
}
for _, expected := range expectedStrings {
if !contains(prompt, expected) {
t.Errorf("buildConvergencePrompt() missing expected string: %q", expected)
}
}
}
// TestAnalysisRefinerBuildIterationContext tests iteration context building
func TestAnalysisRefinerBuildIterationContext(t *testing.T) {
refiner := &AnalysisRefiner{
supervisor: &Supervisor{},
issue: &types.Issue{ID: "test-1"},
}
artifact := &iterative.Artifact{
Type: "analysis",
Content: "previous content",
Context: "initial context",
}
analysis := &Analysis{
Completed: true,
Confidence: 0.90,
DiscoveredIssues: []DiscoveredIssue{
{Title: "Bug 1", Type: "bug", Priority: "P1", DiscoveryType: "blocker"},
{Title: "Bug 2", Type: "bug", Priority: "P2", DiscoveryType: "related"},
},
PuntedItems: []string{"Item 1", "Item 2"},
QualityIssues: []string{"Issue 1"},
}
context := refiner.buildIterationContext(artifact, analysis)
// Check that context includes previous context
if !contains(context, "initial context") {
t.Error("expected context to include previous context")
}
// Check for key statistics
expectedStrings := []string{
"Previous iteration found:",
"Completed: true",
"Discovered issues: 2",
"Punted items: 2",
"Quality issues: 1",
"Discovered issues found:",
"1. Bug 1 (type=bug, priority=P1, discovery=blocker)",
"2. Bug 2 (type=bug, priority=P2, discovery=related)",
}
for _, expected := range expectedStrings {
if !contains(context, expected) {
t.Errorf("buildIterationContext() missing expected string: %q\nGot: %s", expected, context)
}
}
}
// TestAnalysisRefinerBuildIterationContextEmpty tests empty context
func TestAnalysisRefinerBuildIterationContextEmpty(t *testing.T) {
refiner := &AnalysisRefiner{
supervisor: &Supervisor{},
issue: &types.Issue{ID: "test-1"},
}
artifact := &iterative.Artifact{
Type: "analysis",
Content: "content",
Context: "", // Empty context
}
analysis := &Analysis{
Completed: false,
DiscoveredIssues: []DiscoveredIssue{},
PuntedItems: []string{},
QualityIssues: []string{},
}
context := refiner.buildIterationContext(artifact, analysis)
// Check that it still generates useful output
expectedStrings := []string{
"Previous iteration found:",
"Completed: false",
"Discovered issues: 0",
"Punted items: 0",
"Quality issues: 0",
}
for _, expected := range expectedStrings {
if !contains(context, expected) {
t.Errorf("buildIterationContext() missing expected string: %q", expected)
}
}
// Should NOT contain "Discovered issues found:" section
if contains(context, "Discovered issues found:") {
t.Error("buildIterationContext() should not contain discovered issues section when empty")
}
}
// TestDeserializeAnalysis tests deserialization error cases
func TestDeserializeAnalysis(t *testing.T) {
tests := []struct {
name string
artifact *iterative.Artifact
expectError string
}{
{
name: "nil artifact",
artifact: nil,
expectError: "artifact cannot be nil",
},
{
name: "wrong type",
artifact: &iterative.Artifact{
Type: "assessment",
Content: "some content",
},
expectError: "expected artifact type 'analysis'",
},
{
name: "analysis type - not supported",
artifact: &iterative.Artifact{
Type: "analysis",
Content: "some content",
},
expectError: "deserialization from text format is not supported",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
analysis, err := deserializeAnalysis(tt.artifact)
if err == nil {
t.Errorf("expected error containing '%s', got nil", tt.expectError)
} else if !strings.Contains(err.Error(), tt.expectError) {
t.Errorf("expected error containing '%s', got: %v", tt.expectError, err)
}
if analysis != nil {
t.Error("expected nil analysis from deserializeAnalysis")
}
})
}
}
// TestSerializeAnalysisEdgeCases tests serialization edge cases
func TestSerializeAnalysisEdgeCases(t *testing.T) {
tests := []struct {
name string
analysis *Analysis
expected []string
notExpected []string
}{
{
name: "empty analysis",
analysis: &Analysis{
Completed: false,
Confidence: 0.0,
},
expected: []string{
"Completed: false",
"Confidence: 0.00",
},
notExpected: []string{
"Scope Validation",
"Punted Items",
"Discovered Issues",
"Quality Issues",
},
},
{
name: "with acceptance criteria",
analysis: &Analysis{
Completed: true,
Confidence: 1.0,
AcceptanceCriteriaMet: map[string]*CriterionResult{
"criterion1": {Met: true, Evidence: "evidence", Reason: "reason"},
"criterion2": {Met: false, Evidence: "", Reason: "not met"},
},
},
expected: []string{
"Acceptance Criteria:",
"criterion1: met=true",
"criterion2: met=false",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
serialized := serializeAnalysis(tt.analysis)
for _, expected := range tt.expected {
if !contains(serialized, expected) {
t.Errorf("expected serialization to contain %q\nGot: %s", expected, serialized)
}
}
for _, notExpected := range tt.notExpected {
if contains(serialized, notExpected) {
t.Errorf("expected serialization NOT to contain %q", notExpected)
}
}
})
}
}
// TestAnalysisRefinerRefineNilArtifact tests error handling for nil artifact
func TestAnalysisRefinerRefineNilArtifact(t *testing.T) {
refiner := &AnalysisRefiner{
supervisor: &Supervisor{},
issue: &types.Issue{ID: "test-1"},
}
_, err := refiner.Refine(context.Background(), nil)
if err == nil {
t.Error("expected error for nil artifact")
}
if !contains(err.Error(), "artifact cannot be nil") {
t.Errorf("unexpected error message: %v", err)
}
}
// TestCheckConvergencePromptContent tests convergence prompt structure
func TestCheckConvergencePromptContent(t *testing.T) {
refiner := &AnalysisRefiner{
supervisor: &Supervisor{},
issue: &types.Issue{ID: "test-1"},
}
current := &iterative.Artifact{
Type: "analysis",
Content: "Current version with many details that should be visible in the prompt",
Context: "Iteration 3 context information",
}
previous := &iterative.Artifact{
Type: "analysis",
Content: "Previous version with fewer details",
Context: "Iteration 2 context",
}
prompt := refiner.buildConvergencePrompt(current, previous)
// Verify all key sections are present
requiredSections := []string{
"converged",
"PREVIOUS VERSION",
"CURRENT VERSION",
"CONTEXT",
"Diff size",
"Completeness",
"Gaps",
"Marginal value",
"confidence",
"Previous version with fewer details",
"Current version with many details",
"Iteration 3 context information",
}
for _, section := range requiredSections {
if !contains(prompt, section) {
t.Errorf("convergence prompt missing required section: %q", section)
}
}
}
// TestTruncateForPrompt tests prompt truncation
func TestTruncateForPrompt(t *testing.T) {
tests := []struct {
name string
input string
maxChars int
wantLen int
}{
{
name: "short text unchanged",
input: "short",
maxChars: 100,
wantLen: 5,
},
{
name: "long text truncated",
input: string(make([]byte, 5000)),
maxChars: 1000,
wantLen: 1015, // includes "...[truncated]" suffix (actual measured length)
},
{
name: "empty text",
input: "",
maxChars: 100,
wantLen: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := truncateForPrompt(tt.input, tt.maxChars)
if len(result) != tt.wantLen {
t.Errorf("truncateForPrompt() length = %d, want %d", len(result), tt.wantLen)
}
if tt.wantLen > tt.maxChars && !contains(result, "truncated") {
t.Error("expected truncated text to contain truncation marker")
}
})
}
}
// Helper function to check if string contains substring
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && containsHelper(s, substr))
}
func containsHelper(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}