-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_test.go
More file actions
119 lines (104 loc) · 3.16 KB
/
text_test.go
File metadata and controls
119 lines (104 loc) · 3.16 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
package integration
import (
"path/filepath"
"testing"
"github.com/flexigpt/llmtools-go/fstool"
"github.com/flexigpt/llmtools-go/texttool"
)
// Read/modify loop + disambiguated replacements + insert/delete.
func TestE2E_Text_ReadModifyLoop(t *testing.T) {
base := t.TempDir()
h := newHarness(t, base)
docRel := "doc.md"
docAbs := filepath.Join(base, docRel)
// 1) Create a file via writefile (end-to-end).
initial := "" +
"# Title\n" +
"Intro line\n" +
"\n" +
"## Section A\n" +
"<!-- A START -->\n" +
"TODO: old\n" +
"<!-- A END -->\n" +
"\n" +
"## Section B\n" +
"<!-- B START -->\n" +
"TODO: old\n" +
"<!-- B END -->\n"
_ = callJSON[fstool.WriteFileOut](t, h.r, "writefile", fstool.WriteFileArgs{
Path: docRel,
Encoding: "text",
Content: initial,
Overwrite: false,
CreateParents: false,
})
// 2) Read a bounded range (marker-to-marker) to show how to constrain reads.
rng := callJSON[texttool.ReadTextRangeOut](t, h.r, "readtextrange", texttool.ReadTextRangeArgs{
Path: docRel,
StartMatchLines: []string{"<!-- B START -->"},
EndMatchLines: []string{"<!-- B END -->"},
})
if rng.LinesReturned == 0 {
t.Fatalf("expected some lines in range, got: %s", debugJSON(t, rng))
}
// 3) Find occurrences (substring) with context.
found := callJSON[texttool.FindTextOut](t, h.r, "findtext", texttool.FindTextArgs{
Path: docRel,
QueryType: "substring",
Query: "TODO:",
ContextLines: 1,
MaxMatches: 10,
})
if found.MatchesReturned < 2 {
t.Fatalf("expected 2 TODO matches, got: %s", debugJSON(t, found))
}
// 4) Replace only the TODO in Section B using beforeLines/afterLines disambiguation.
one := 1
_ = callJSON[texttool.ReplaceTextLinesOut](t, h.r, "replacetextlines", texttool.ReplaceTextLinesArgs{
Path: docRel,
BeforeLines: []string{"<!-- B START -->"},
MatchLines: []string{"TODO: old"},
AfterLines: []string{"<!-- B END -->"},
ReplaceWithLines: []string{"TODO: new"},
ExpectedReplacements: &one,
})
// 5) Insert after a uniquely-matched anchor.
_ = callJSON[texttool.InsertTextLinesOut](t, h.r, "inserttextlines", texttool.InsertTextLinesArgs{
Path: docRel,
Position: "afterAnchor",
AnchorMatchLines: []string{"<!-- A END -->"},
LinesToInsert: []string{
"",
"Inserted after A",
},
})
// 6) Delete a line block (exact match).
_ = callJSON[texttool.DeleteTextLinesOut](t, h.r, "deletetextlines", texttool.DeleteTextLinesArgs{
Path: docRel,
MatchLines: []string{"Intro line"},
ExpectedDeletions: 1,
})
// 7) Verify final content via readfile.
out := callRaw(t, h.r, "readfile", fstool.ReadFileArgs{
Path: docRel,
Encoding: "text",
})
got := requireSingleTextOutput(t, out)
want := "" +
"# Title\n" +
"\n" +
"## Section A\n" +
"<!-- A START -->\n" +
"TODO: old\n" +
"<!-- A END -->\n" +
"\n" +
"Inserted after A\n" +
"\n" +
"## Section B\n" +
"<!-- B START -->\n" +
"TODO: new\n" +
"<!-- B END -->\n"
if got != want {
t.Fatalf("final doc mismatch\npath=%s\n--- got ---\n%s\n--- want ---\n%s", docAbs, got, want)
}
}