v0.35.0 21873e8

json_activity_test.go RawHistoryFolder

1package modes
2
3import (
4	"bytes"
5	"context"
6	"encoding/json"
7	"errors"
8	"strings"
9	"testing"
10	"time"
11
12	"github.com/bnema/zut/packages/core"
13	"github.com/bnema/zut/packages/provider"
14)
15
16func TestRunJSONCompletionUserMessageIsValidJSONL(t *testing.T) {
17	message := core.EvUserMessage{Message: provider.Message{
18		Role:    provider.RoleUser,
19		Content: []provider.Content{provider.TextBlock{Text: "[auto-subagents update] worker finished"}},
20	}}
21	var line bytes.Buffer
22	if err := json.NewEncoder(&line).Encode(EventToJSON(message)); err != nil {
23		t.Fatal(err)
24	}
25	var decoded map[string]any
26	if err := json.Unmarshal(bytes.TrimSpace(line.Bytes()), &decoded); err != nil {
27		t.Fatalf("completion event is not JSON: %v", err)
28	}
29	if decoded["type"] != "user_message" {
30		t.Fatalf("event type = %#v, want user_message", decoded["type"])
31	}
32}
33
34type failingJSONWriter struct{ err error }
35
36func (w failingJSONWriter) Write([]byte) (int, error) { return 0, w.err }
37
38func TestRunJSONPropagatesOutputErrors(t *testing.T) {
39	providerErr := errors.New("provider unavailable")
40	writerErr := errors.New("stdout unavailable")
41	ag := core.NewAgent(&streamTestClient{err: providerErr}, "test-model", "", nil)
42	_, err := RunJSONWithContextRecovery(context.Background(), ag, "hello", nil, failingJSONWriter{err: writerErr}, nil)
43	if !errors.Is(err, providerErr) || !errors.Is(err, writerErr) {
44		t.Fatalf("RunJSONWithContextRecovery error = %v, want provider and output errors", err)
45	}
46}
47
48func TestRunJSONFailureEmitsOneErrorObject(t *testing.T) {
49	client := &streamTestClient{err: errors.New("provider unavailable")}
50	ag := core.NewAgent(client, "test-model", "", nil)
51	var out bytes.Buffer
52	_, err := RunJSONWithContextRecovery(context.Background(), ag, "hello", nil, &out, nil)
53	if err == nil {
54		t.Fatal("RunJSONWithContextRecovery unexpectedly succeeded")
55	}
56	lines := strings.Split(strings.TrimSpace(out.String()), "\n")
57	errorObjects := 0
58	for _, line := range lines {
59		var decoded map[string]any
60		if err := json.Unmarshal([]byte(line), &decoded); err != nil {
61			t.Fatalf("invalid JSONL line %q: %v", line, err)
62		}
63		if decoded["type"] == "error" {
64			errorObjects++
65		}
66	}
67	if errorObjects != 1 {
68		t.Fatalf("error objects = %d, want one; output=%q", errorObjects, out.String())
69	}
70}
71
72func TestEventToJSONCacheDiagnosticsAreSanitized(t *testing.T) {
73	event := EventToJSON(core.EvCacheDiagnostics{
74		Eligible: true, Mode: "implicit", Transport: "websocket", Continuation: "incremental",
75	})
76	if event["type"] != "cache_diagnostics" || event["eligible"] != true || event["mode"] != "implicit" || event["transport"] != "websocket" || event["continuation"] != "incremental" {
77		t.Fatalf("cache diagnostics event = %#v", event)
78	}
79	for _, forbidden := range []string{"prompt", "session_id", "thread_id", "request_body", "credential"} {
80		if _, ok := event[forbidden]; ok {
81			t.Fatalf("cache diagnostics leaked %q: %#v", forbidden, event)
82		}
83	}
84}
85
86func TestEventToJSONPlanUpdate(t *testing.T) {
87	explanation := "Discovery complete"
88	event := EventToJSON(core.EvPlanUpdate{Update: core.PlanUpdate{
89		Explanation: &explanation,
90		Plan:        []core.PlanStep{{Step: "Implement", Status: core.PlanInProgress}},
91	}})
92	if event["type"] != "plan_update" || event["explanation"] != "Discovery complete" {
93		t.Fatalf("plan event = %#v", event)
94	}
95	plan, ok := event["plan"].([]core.PlanStep)
96	if !ok || len(plan) != 1 || plan[0].Status != core.PlanInProgress {
97		t.Fatalf("plan payload = %#v", event["plan"])
98	}
99	withoutExplanation := EventToJSON(core.EvPlanUpdate{Update: core.PlanUpdate{Plan: []core.PlanStep{}}})
100	if withoutExplanation["explanation"] != nil {
101		t.Fatalf("missing explanation = %#v, want null", withoutExplanation["explanation"])
102	}
103}
104
105func TestEventToJSONActivityLifecycle(t *testing.T) {
106	request := EventToJSON(core.EvRequestStarted{
107		Provider:    "anthropic",
108		Model:       "claude-test",
109		Scope:       core.RetryScopeProvider,
110		Attempt:     2,
111		MaxAttempts: 3,
112	})
113	if got, want := request["type"], "request_started"; got != want {
114		t.Fatalf("type = %q, want %q", got, want)
115	}
116	for key, want := range map[string]any{
117		"provider":     "anthropic",
118		"model":        "claude-test",
119		"scope":        "provider",
120		"attempt":      2,
121		"max_attempts": 3,
122	} {
123		if got := request[key]; got != want {
124			t.Errorf("request[%q] = %#v, want %#v", key, got, want)
125		}
126	}
127
128	retry := EventToJSON(core.EvRetryScheduled{
129		Scope: core.RetryScopeAgent, Attempt: 2, MaxAttempts: 4, Delay: 1500 * time.Millisecond,
130	})
131	if got, want := retry["delay_ms"], int64(1500); got != want {
132		t.Fatalf("delay_ms = %#v, want %#v", got, want)
133	}
134	if _, exists := retry["delay"]; exists {
135		t.Fatal("retry event must not serialize a duration")
136	}
137
138	tool := EventToJSON(core.EvToolExecutionStarted{ID: "call-1", Name: "read"})
139	if got, want := tool["type"], "tool_execution_started"; got != want {
140		t.Fatalf("type = %q, want %q", got, want)
141	}
142	if tool["id"] != "call-1" || tool["name"] != "read" {
143		t.Fatalf("tool event = %#v", tool)
144	}
145}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close