What changed: delItem read the "is the element alone on its line" check
from the comment-blanked copy, so a JSONC comment written before the
element on the same line (VS Code's chatLanguageModels.json carries the
user's notes beside their groups) counted as blank space, the delete took
the line from its start, and the comment went with the element. It reads
raw now, as delJSONBytes does for the same check on an object member, so
only the element (and its comma, or its line) goes.
Test: TestDelJSONItemKeepsACommentBeforeTheElement (edit.DelJSONItem,
three shapes) fails on the old code with "the comment before the element
went with it" and passes now; the edit package's other tests, among them
the CRLF and among-others deletes, pass unchanged.
Run: go test -tags nogui -count=1 ./internal/edit ./internal/agent; the
full go test -tags nogui ./... in a sandbox HOME (TestOffline red as on
the baseline; gateway's TestSearchFollowsTheSearchersProxy red once under
the full run, passes alone, no path from this diff); gofmt -l; go vet and
go build on darwin, GOOS=linux -tags nogui and GOOS=windows.
Review: the test fails on main (3f5702421) with "the comment before the
element went with it". Added a fourth shape, a block comment that ends on
the element's line: on the old code DelJSONItem cut from the line start
into the comment and refused the result as "invalid JSON/JSONC", so the
group could not be taken out at all; now it goes and the comment stays.
Re-run: go test ./internal/edit, green with the fix and red with
stripped[ls:a] put back by hand.
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
48 lines
1.7 KiB
Go
48 lines
1.7 KiB
Go
package edit
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/jsonc"
|
|
)
|
|
|
|
// A comment at the start of the element's line stays when the element is
|
|
// taken out, as a member's delete keeps the comment before it: VS Code's
|
|
// chatLanguageModels.json carries the user's notes beside their groups.
|
|
|
|
func TestDelJSONItemKeepsACommentBeforeTheElement(t *testing.T) {
|
|
where := map[string]string{"vendor": "customendpoint", "name": "magpie"}
|
|
magpie := `{"name": "magpie", "vendor": "customendpoint"}`
|
|
for _, in := range []string{
|
|
// last element, its comment on the line with it
|
|
"[\n {\"name\": \"a\", \"vendor\": \"openai\"},\n /* magpie's group */ " + magpie + "\n]\n",
|
|
// not the last
|
|
"[\n /* magpie's group */ " + magpie + ",\n {\"name\": \"b\", \"vendor\": \"ollama\"}\n]\n",
|
|
// on one line, the [ on it too
|
|
"[ {\"name\": \"a\", \"vendor\": \"openai\"}, /* magpie's group */ " + magpie + " ]\n",
|
|
// a block comment that ends on the element's line
|
|
"[\n {\"name\": \"a\", \"vendor\": \"openai\"},\n /* magpie's group\n */ " + magpie + "\n]\n",
|
|
} {
|
|
p := tmpFile(t, "chatLanguageModels.json", in)
|
|
if err := DelJSONItem(p, where); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b := read(t, p)
|
|
var got []map[string]any
|
|
if err := json.Unmarshal(jsonc.ToJSON([]byte(b)), &got); err != nil {
|
|
t.Fatalf("%q: not valid JSONC after: %v\n%s", in, err, b)
|
|
}
|
|
if len(got) != strings.Count(in, "\"vendor\"")-1 {
|
|
t.Fatalf("%q: %d elements left:\n%s", in, len(got), b)
|
|
}
|
|
if _, ok := GetJSONItem(p, where); ok {
|
|
t.Fatalf("%q: the element is still there:\n%s", in, b)
|
|
}
|
|
if !strings.Contains(b, "/* magpie's group") || !strings.Contains(b, "*/") {
|
|
t.Fatalf("%q: the comment before the element went with it:\n%s", in, b)
|
|
}
|
|
}
|
|
}
|