v0.22.0 fae6c20

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 TestEventToJSONActivityLifecycle(t *testing.T) {
87	request := EventToJSON(core.EvRequestStarted{
88		Provider:    "anthropic",
89		Model:       "claude-test",
90		Scope:       core.RetryScopeProvider,
91		Attempt:     2,
92		MaxAttempts: 3,
93	})
94	if got, want := request["type"], "request_started"; got != want {
95		t.Fatalf("type = %q, want %q", got, want)
96	}
97	for key, want := range map[string]any{
98		"provider":     "anthropic",
99		"model":        "claude-test",
100		"scope":        "provider",
101		"attempt":      2,
102		"max_attempts": 3,
103	} {
104		if got := request[key]; got != want {
105			t.Errorf("request[%q] = %#v, want %#v", key, got, want)
106		}
107	}
108
109	retry := EventToJSON(core.EvRetryScheduled{
110		Scope: core.RetryScopeAgent, Attempt: 2, MaxAttempts: 4, Delay: 1500 * time.Millisecond,
111	})
112	if got, want := retry["delay_ms"], int64(1500); got != want {
113		t.Fatalf("delay_ms = %#v, want %#v", got, want)
114	}
115	if _, exists := retry["delay"]; exists {
116		t.Fatal("retry event must not serialize a duration")
117	}
118
119	tool := EventToJSON(core.EvToolExecutionStarted{ID: "call-1", Name: "read"})
120	if got, want := tool["type"], "tool_execution_started"; got != want {
121		t.Fatalf("type = %q, want %q", got, want)
122	}
123	if tool["id"] != "call-1" || tool["name"] != "read" {
124		t.Fatalf("tool event = %#v", tool)
125	}
126}

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