1package modes
2
3import (
4 "testing"
5 "time"
6
7 "github.com/bnema/zut/packages/core"
8 "github.com/bnema/zut/packages/provider"
9)
10
11func TestAgentActivityDescribesLifecycle(t *testing.T) {
12 a := newAgentActivity("anthropic", "claude-test")
13 check := func(want string) {
14 t.Helper()
15 if got := a.label(); got != want {
16 t.Fatalf("label = %q, want %q", got, want)
17 }
18 }
19
20 check("Preparing request")
21 a.apply(core.EvRequestStarted{Provider: "anthropic", Model: "claude-test", Scope: core.RetryScopeProvider, Attempt: 1, MaxAttempts: 3})
22 check("Sending request to anthropic")
23 a.apply(core.EvAssistantStart{})
24 check("Waiting for claude-test to respond")
25 a.apply(core.EvTextDelta{Delta: "hello"})
26 check("Receiving response from claude-test")
27 a.apply(core.EvToolUseStart{ID: "one", Name: "read"})
28 check("Preparing tool: read")
29 a.apply(core.EvToolCall{ID: "one", Name: "read"})
30 a.apply(core.EvToolCall{ID: "two", Name: "bash"})
31 a.apply(core.EvToolExecutionStarted{ID: "one", Name: "read"})
32 check("Running tool: read")
33 a.apply(core.EvToolResult{ID: "one", Result: core.ToolResult{}})
34 check("Preparing tool: bash")
35 a.apply(core.EvToolExecutionStarted{ID: "two", Name: "bash"})
36 a.apply(core.EvToolResult{ID: "two", Result: core.ToolResult{}})
37 check("Sending tool results to anthropic")
38 a.apply(core.EvRetryScheduled{Scope: core.RetryScopeAgent, Attempt: 2, MaxAttempts: 4, Delay: 2 * time.Second})
39 check("Retrying request in 2s (attempt 2/4)")
40}
41
42func TestActivityLabelsSanitizeAndFallback(t *testing.T) {
43 a := activity{kind: activitySendingRequest, provider: " \x1b[31mremote\n"}
44 if got, want := a.label(), "Sending request to [31mremote"; got != want {
45 t.Fatalf("sanitized label = %q, want %q", got, want)
46 }
47 a = activity{kind: activityRunningTool}
48 if got, want := a.label(), "Running tool: tool"; got != want {
49 t.Fatalf("fallback label = %q, want %q", got, want)
50 }
51}
52
53func TestAgentActivityIgnoresUnknownToolResult(t *testing.T) {
54 a := newAgentActivity("provider", "model")
55 a.apply(core.EvToolResult{ID: "unknown", Result: core.ToolResult{Content: []provider.Content{provider.TextBlock{Text: "ignored"}}}})
56 if got, want := a.label(), "Preparing request"; got != want {
57 t.Fatalf("label = %q, want %q", got, want)
58 }
59}
60
61func TestAgentActivityClearsDiscardedToolCallsOnAgentRetry(t *testing.T) {
62 a := newAgentActivity("provider", "model")
63 a.apply(core.EvToolCall{ID: "discarded", Name: "stale"})
64 a.apply(core.EvRetryScheduled{Scope: core.RetryScopeAgent, Attempt: 2, MaxAttempts: 2, Delay: time.Millisecond})
65 a.apply(core.EvToolCall{ID: "actual", Name: "read"})
66 a.apply(core.EvToolResult{ID: "actual", Result: core.ToolResult{}})
67
68 if got, want := a.label(), "Sending tool results to provider"; got != want {
69 t.Fatalf("label = %q, want %q", got, want)
70 }
71}