Pin Agent-Native as a submodule so the module builds without the multi-repo sibling path. Co-authored-by: Cursor <cursoragent@cursor.com>
78 lines
2.3 KiB
Go
78 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"agent-squad/squad"
|
|
)
|
|
|
|
func TestFileEventSinkPersistAndRedact(t *testing.T) {
|
|
dir := t.TempDir()
|
|
var got []squad.Event
|
|
next := squad.ProgressFunc(func(ev squad.Event) { got = append(got, ev) })
|
|
sink := NewFileEventSink(dir, next)
|
|
defer sink.Close()
|
|
|
|
sink.Emit(squad.Event{Type: squad.EvRunStarted, RunID: "run_x", Text: "开始 sk-api-8abcdefgh1234567890"})
|
|
sink.Emit(squad.Event{Type: squad.EvDetail, RunID: "run_x", StepID: "s1",
|
|
Text: "Bearer abcdefghijklmnopqrst",
|
|
Data: json.RawMessage(`{"note":"api_key=abcdefghijklmnopqrstuvwxyz123456"}`)})
|
|
sink.Emit(squad.Event{Type: "no_run"}) // 无 RunID:不落盘但转发
|
|
|
|
// 转发不丢。
|
|
if len(got) != 3 {
|
|
t.Fatalf("forwarded events want 3, got %d", len(got))
|
|
}
|
|
// 落盘 2 行,带 seq。
|
|
f, err := os.Open(filepath.Join(dir, "runs", "run_x", "events.jsonl"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
sc := bufio.NewScanner(f)
|
|
var lines []persistedEvent
|
|
for sc.Scan() {
|
|
var pe persistedEvent
|
|
if err := json.Unmarshal(sc.Bytes(), &pe); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lines = append(lines, pe)
|
|
}
|
|
if len(lines) != 2 || lines[0].Seq != 1 || lines[1].Seq != 2 {
|
|
t.Fatalf("seq/lines wrong: %+v", lines)
|
|
}
|
|
// 脱敏生效。
|
|
if strings.Contains(lines[0].Text, "sk-api-8") || !strings.Contains(lines[0].Text, "***") {
|
|
t.Fatalf("sk key not redacted: %q", lines[0].Text)
|
|
}
|
|
if strings.Contains(lines[1].Text, "abcdefghijklmnopqrst") {
|
|
t.Fatalf("bearer not redacted: %q", lines[1].Text)
|
|
}
|
|
if strings.Contains(string(lines[1].Data), "abcdefghijklmnopqrstuvwxyz123456") {
|
|
t.Fatalf("api key in data not redacted: %s", lines[1].Data)
|
|
}
|
|
}
|
|
|
|
func TestRedactShapes(t *testing.T) {
|
|
cases := map[string]string{
|
|
"key sk-proj-1234567890abcdef": "key ***",
|
|
"Authorization: Bearer abcdef1234567890abc": "Authorization: Bearer ***",
|
|
`{"api_key":"abcdefghijklmnop12"}`: `{"api_key":"***"}`,
|
|
"eyJhbGciOiJIU.eyJzdWIiOiJI.SflKxwRJSMeK": "***",
|
|
`{"password":"hunter2secret"}`: `{"password":"***"}`,
|
|
"普通文本不受影响 sk-short": "普通文本不受影响 sk-short",
|
|
}
|
|
for in, want := range cases {
|
|
if got := Redact(in); got != want {
|
|
t.Errorf("Redact(%q)=%q, want %q", in, got, want)
|
|
}
|
|
}
|
|
_ = time.Now
|
|
}
|