Files
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

160 lines
6.1 KiB
Go

package tool
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Autumn-27/norma/permission"
)
func run(t *testing.T, tl CoreTool, dir string, in any) Result {
t.Helper()
raw, _ := json.Marshal(in)
if err := ValidateInput(tl.InputSchema(), raw); err != nil {
t.Fatalf("%s schema: %v", tl.Name(), err)
}
res, err := tl.Call(context.Background(), raw, &ToolContext{WorkingDir: dir})
if err != nil {
t.Fatalf("%s: %v", tl.Name(), err)
}
return res
}
func TestFileTools(t *testing.T) {
dir := t.TempDir()
run(t, NewWrite(), dir, map[string]any{"file_path": "a.txt", "content": "hello\nworld\n"})
if b, _ := os.ReadFile(filepath.Join(dir, "a.txt")); string(b) != "hello\nworld\n" {
t.Fatalf("write: %q", b)
}
res := run(t, NewRead(), dir, map[string]any{"file_path": "a.txt"})
if !strings.Contains(res.Flatten(), "1\thello") {
t.Fatalf("read: %q", res.Flatten())
}
run(t, NewEdit(), dir, map[string]any{"file_path": "a.txt", "old_string": "world", "new_string": "gophers"})
if b, _ := os.ReadFile(filepath.Join(dir, "a.txt")); string(b) != "hello\ngophers\n" {
t.Fatalf("edit: %q", b)
}
run(t, NewWrite(), dir, map[string]any{"file_path": "b.txt", "content": "x x"})
if r := run(t, NewEdit(), dir, map[string]any{"file_path": "b.txt", "old_string": "x", "new_string": "y"}); !r.IsError {
t.Fatalf("expected non-unique edit error")
}
}
func TestSearchTools(t *testing.T) {
t.Setenv("NORMA_DISABLE_RIPGREP", "1")
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "one.go"), []byte("package main\nfunc Foo(){}\n"), 0o644)
os.WriteFile(filepath.Join(dir, "two.txt"), []byte("Foo lives\n"), 0o644)
res := run(t, NewGrep(), dir, map[string]any{"pattern": "Foo", "glob": "*.go", "output_mode": "files_with_matches"})
if !strings.Contains(res.Flatten(), "one.go") || strings.Contains(res.Flatten(), "two.txt") {
t.Fatalf("grep glob: %q", res.Flatten())
}
res = run(t, NewGlob(), dir, map[string]any{"pattern": "**/*.go"})
if !strings.Contains(res.Flatten(), "one.go") {
t.Fatalf("glob: %q", res.Flatten())
}
}
func TestGrepContextLines(t *testing.T) {
t.Setenv("NORMA_DISABLE_RIPGREP", "1")
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "f.txt"), []byte("a\nb\nMATCH\nd\ne\n"), 0o644)
// -C 1 should include the line before and after the match, with the match
// line using ':' and context lines using '-'.
res := run(t, NewGrep(), dir, map[string]any{"pattern": "MATCH", "output_mode": "content", "-C": 1})
out := res.Flatten()
for _, want := range []string{"f.txt-2-b", "f.txt:3:MATCH", "f.txt-4-d"} {
if !strings.Contains(out, want) {
t.Fatalf("context output missing %q:\n%s", want, out)
}
}
if strings.Contains(out, "f.txt-1-a") {
t.Fatalf("-C 1 should not reach line 1:\n%s", out)
}
}
func TestGrepTypeFilter(t *testing.T) {
t.Setenv("NORMA_DISABLE_RIPGREP", "1")
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "a.go"), []byte("needle here\n"), 0o644)
os.WriteFile(filepath.Join(dir, "b.py"), []byte("needle here\n"), 0o644)
os.WriteFile(filepath.Join(dir, "c.txt"), []byte("needle here\n"), 0o644)
// type: go → only the .go file.
out := run(t, NewGrep(), dir, map[string]any{"pattern": "needle", "type": "go"}).Flatten()
if !strings.Contains(out, "a.go") || strings.Contains(out, "b.py") || strings.Contains(out, "c.txt") {
t.Fatalf("type=go filter: %q", out)
}
// Unknown type falls back to *.<type>.
out = run(t, NewGrep(), dir, map[string]any{"pattern": "needle", "type": "txt"}).Flatten()
if !strings.Contains(out, "c.txt") || strings.Contains(out, "a.go") {
t.Fatalf("type=txt fallback: %q", out)
}
}
func TestGrepMultiline(t *testing.T) {
t.Setenv("NORMA_DISABLE_RIPGREP", "1")
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "f.txt"), []byte("start\nalpha\nbeta\nend\n"), 0o644)
// Without multiline, a cross-line pattern doesn't match.
out := run(t, NewGrep(), dir, map[string]any{"pattern": "alpha.*beta", "output_mode": "files_with_matches"}).Flatten()
if strings.Contains(out, "f.txt") {
t.Fatalf("cross-line pattern should not match without multiline: %q", out)
}
// With multiline, . spans the newline.
out = run(t, NewGrep(), dir, map[string]any{"pattern": "alpha.*beta", "multiline": true, "output_mode": "files_with_matches"}).Flatten()
if !strings.Contains(out, "f.txt") {
t.Fatalf("multiline should match across lines: %q", out)
}
}
func TestGrepHeadLimitAndOffset(t *testing.T) {
t.Setenv("NORMA_DISABLE_RIPGREP", "1")
dir := t.TempDir()
for _, n := range []string{"a", "b", "c", "d", "e"} {
os.WriteFile(filepath.Join(dir, n+".txt"), []byte("hit\n"), 0o644)
}
// head_limit caps the number of files and notes the remainder.
out := run(t, NewGrep(), dir, map[string]any{"pattern": "hit", "head_limit": 2}).Flatten()
if strings.Count(out, ".txt") != 2 || !strings.Contains(out, "more result") {
t.Fatalf("head_limit=2 should keep 2 and note the rest: %q", out)
}
// offset pages past the first results.
out = run(t, NewGrep(), dir, map[string]any{"pattern": "hit", "head_limit": 2, "offset": 2}).Flatten()
if !strings.Contains(out, "c.txt") || strings.Contains(out, "a.txt") {
t.Fatalf("offset=2 should skip a/b and show c: %q", out)
}
}
func TestBashAndSafetyFloor(t *testing.T) {
dir := t.TempDir()
res := run(t, NewBash(), dir, map[string]any{"command": "echo hi"})
if !strings.Contains(res.Flatten(), "hi") {
t.Fatalf("bash: %q", res.Flatten())
}
// Safety floor: rm -rf / must be denied by CheckPermissions.
bash := NewBash()
raw, _ := json.Marshal(map[string]any{"command": "rm -rf /"})
if d := bash.CheckPermissions(context.Background(), raw, permission.Context{}); d.Behavior != permission.Deny {
t.Fatalf("expected deny for rm -rf /, got %v", d.Behavior)
}
}
func TestSchemaValidation(t *testing.T) {
// Missing required field.
raw, _ := json.Marshal(map[string]any{"offset": 1})
if err := ValidateInput(NewRead().InputSchema(), raw); err == nil {
t.Fatal("expected missing file_path error")
}
// Wrong type.
raw, _ = json.Marshal(map[string]any{"file_path": 123})
if err := ValidateInput(NewRead().InputSchema(), raw); err == nil {
t.Fatal("expected type error")
}
}