package modelloop import ( "strings" "testing" "github.com/samcharles93/ai-sdk/core" "github.com/samcharles93/archie-core/internal/domain/agentrun" ) func TestTranscriptToolEntryRedactsAndClips(t *testing.T) { const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhcmNoaWUifQ.c2lnbmF0dXJlc2lnbmF0dXJl" const pem = "-----BEGIN RSA PRIVATE KEY-----\nMIIEow\n-----END RSA PRIVATE KEY-----" //nolint:gosec // G101: a fake key the redaction must catch. long := strings.Repeat("x", transcriptFieldLimit+10) tests := []struct { name string input string output string wantArgs string wantOutput string wantBytes int }{ { name: "secret key in arguments, token inside output", input: `{"path":"a.txt","api_key":"plain-value"}`, output: "curl -H 'Authorization: Bearer " + jwt + "' done", wantArgs: `{"api_key":"[redacted]","path":"a.txt"}`, wantOutput: "curl -H 'Authorization: Bearer [redacted]' done", }, { name: "private key block inside output", input: `{}`, output: "before\n" + pem + "\nafter", wantArgs: `{}`, wantOutput: "before\n[redacted]\nafter", }, { name: "oversized output keeps its full size", input: `{}`, output: long, wantArgs: `{}`, wantOutput: long[:transcriptFieldLimit], wantBytes: len(long), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var got []agentrun.TranscriptEntry recorder := newTranscript(func(entry agentrun.TranscriptEntry) { got = append(got, entry) }) call := core.ToolCall{ToolCallID: "c1", ToolName: "shell", Input: tt.input} if _, err := recorder.BeforeToolExecute(t.Context(), &call); err != nil { t.Fatal(err) } recorder.AfterToolExecute(t.Context(), call, &core.ToolResult{Output: tt.output}) if len(got) != 1 { t.Fatalf("entries = %d, want 1", len(got)) } entry := got[0] if entry.Args != tt.wantArgs { t.Errorf("args = %q, want %q", entry.Args, tt.wantArgs) } if entry.Output != tt.wantOutput { t.Errorf("output = %q, want %q", entry.Output, tt.wantOutput) } if entry.OutputBytes != tt.wantBytes { t.Errorf("output bytes = %d, want %d", entry.OutputBytes, tt.wantBytes) } }) } }