Skip to content

Commit d735d3b

Browse files
committed
test: add edge case unit tests for AST chunk merging
1 parent 0d4dd84 commit d735d3b

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

internal/service/tools/smart_search_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,82 @@ func TestGroupDocsByTree(t *testing.T) {
9292
}
9393
},
9494
},
95+
{
96+
name: "Different files with same signature should NOT merge",
97+
input: []mergedResult{
98+
{
99+
id: "1", filePath: "file1.md", symbolType: "documentation", signature: "### Intro",
100+
startLine: 1, endLine: 3, score: 0.8,
101+
},
102+
{
103+
id: "2", filePath: "file2.md", symbolType: "documentation", signature: "### Intro",
104+
startLine: 4, endLine: 6, score: 0.9,
105+
},
106+
},
107+
expected: 2, // Should stay separate because filePath differs
108+
check: func(t *testing.T, results []mergedResult) {
109+
if len(results) != 2 {
110+
t.Fatalf("Expected 2 results, got %d", len(results))
111+
}
112+
},
113+
},
114+
{
115+
name: "Gap retrieval works properly or falls back gracefully",
116+
input: []mergedResult{
117+
{
118+
id: "c1", filePath: tempFilePath, symbolType: "documentation", signature: "### Test Gap",
119+
startLine: 1, endLine: 2, score: 0.8, // Line A, B
120+
},
121+
{
122+
id: "c2", filePath: tempFilePath, symbolType: "documentation", signature: "### Test Gap",
123+
startLine: 19, endLine: 20, score: 0.9, // Line S, T
124+
},
125+
},
126+
expected: 1,
127+
check: func(t *testing.T, results []mergedResult) {
128+
if len(results) != 1 {
129+
t.Fatalf("Expected 1 result after merge, got %d", len(results))
130+
}
131+
merged := results[0]
132+
if merged.startLine != 1 || merged.endLine != 20 {
133+
t.Errorf("Expected lines 1-20, got %d-%d", merged.startLine, merged.endLine)
134+
}
135+
// By our current logic it will attempt to fetch lines 1-20. Let's verify.
136+
if !strings.Contains(merged.content, "Line F") {
137+
t.Errorf("Expected merged content to fetch gap lines, but didn't find 'Line F'")
138+
}
139+
},
140+
},
141+
{
142+
name: "Non-doc AST files (JSON, YAML) merge correctly if signature matches",
143+
input: []mergedResult{
144+
{
145+
id: "y1", filePath: "config.yaml", symbolType: "documentation", signature: "server:",
146+
startLine: 10, endLine: 12, score: 0.6, content: "port: 8080",
147+
},
148+
{
149+
id: "y2", filePath: "config.yaml", symbolType: "documentation", signature: "server:",
150+
startLine: 15, endLine: 18, score: 0.7, content: "host: localhost",
151+
},
152+
},
153+
expected: 1,
154+
check: func(t *testing.T, results []mergedResult) {
155+
if len(results) != 1 {
156+
t.Fatalf("Expected 1 result after merge, got %d", len(results))
157+
}
158+
// Because "config.yaml" doesn't actually exist on disk, we expect the fallback [...] logic!
159+
merged := results[0]
160+
if merged.startLine != 10 || merged.endLine != 18 {
161+
t.Errorf("Expected lines 10-18, got %d-%d", merged.startLine, merged.endLine)
162+
}
163+
if !strings.Contains(merged.content, "port: 8080") || !strings.Contains(merged.content, "host: localhost") {
164+
t.Errorf("Missing content in fake fallback merge")
165+
}
166+
if !strings.Contains(merged.content, "[...]") {
167+
t.Errorf("Expected fallback [...] marker for missing file, got:\n%s", merged.content)
168+
}
169+
},
170+
},
95171
}
96172

97173
for _, tc := range tests {

0 commit comments

Comments
 (0)