把 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
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package hook
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestPreToolUseBlockAndRewrite(t *testing.T) {
|
|
r := NewRegistry()
|
|
r.On(PreToolUse, func(_ context.Context, ev Event) Result {
|
|
if ev.ToolName == "Bash" {
|
|
return Result{Decision: "block", Message: "no bash"}
|
|
}
|
|
return Result{UpdatedInput: []byte(`{"x":1}`)}
|
|
})
|
|
if block, msg, _ := r.PreToolUse(context.Background(), "Bash", nil); !block || msg != "no bash" {
|
|
t.Fatalf("expected block, got %v %q", block, msg)
|
|
}
|
|
if block, _, upd := r.PreToolUse(context.Background(), "Read", nil); block || string(upd) != `{"x":1}` {
|
|
t.Fatalf("expected rewrite, got block=%v upd=%s", block, upd)
|
|
}
|
|
}
|
|
|
|
func TestStopHookBlockingContinues(t *testing.T) {
|
|
r := NewRegistry()
|
|
calls := 0
|
|
r.On(Stop, func(_ context.Context, _ Event) Result {
|
|
calls++
|
|
if calls == 1 {
|
|
return Result{Decision: "block", AdditionalContext: "keep going"}
|
|
}
|
|
return Result{}
|
|
})
|
|
// First call: a "block" decision → continue with an injected message.
|
|
prevent, blocking, _ := r.Stop(context.Background(), nil)
|
|
if prevent || len(blocking) != 1 || blocking[0] != "keep going" {
|
|
t.Fatalf("blocking path wrong: prevent=%v blocking=%v", prevent, blocking)
|
|
}
|
|
// Second call: no decision → allow stop.
|
|
if prevent, blocking, _ := r.Stop(context.Background(), nil); prevent || len(blocking) != 0 {
|
|
t.Fatalf("second Stop should allow: prevent=%v blocking=%v", prevent, blocking)
|
|
}
|
|
}
|
|
|
|
func TestStopHookPreventTerminates(t *testing.T) {
|
|
r := NewRegistry()
|
|
r.On(Stop, func(_ context.Context, _ Event) Result {
|
|
return Result{PreventContinuation: true, Message: "halt"}
|
|
})
|
|
prevent, _, msg := r.Stop(context.Background(), nil)
|
|
if !prevent || msg != "halt" {
|
|
t.Fatalf("prevent path wrong: prevent=%v msg=%q", prevent, msg)
|
|
}
|
|
}
|