v0.13.0 befd698

session_repair_test.go RawHistoryFolder

1package core
2
3import (
4	"testing"
5
6	"github.com/bnema/zut/packages/provider"
7)
8
9// TestRepairToolUseResultPairsAppendsStub covers the most common
10// corruption shape: assistant tool_use row on disk, no
11// corresponding tool_result row after it. The repair must inject
12// a stub tool_result so the next request passes the provider's
13// validity check.
14func TestRepairToolUseResultPairsAppendsStub(t *testing.T) {
15	msgs := []provider.Message{
16		{Role: provider.RoleUser, Content: []provider.Content{provider.TextBlock{Text: "hi"}}},
17		{Role: provider.RoleAssistant, Content: []provider.Content{
18			provider.ToolCallBlock{ID: "t1", Name: "read", Arguments: []byte(`{"path":"/tmp/a"}`)},
19		}},
20	}
21	out := repairToolUseResultPairs(msgs)
22	if len(out) != 3 {
23		t.Fatalf("expected 3 messages after repair, got %d", len(out))
24	}
25	if out[2].Role != provider.RoleTool {
26		t.Fatalf("expected tool-role message at index 2, got %v", out[2].Role)
27	}
28	tr, ok := out[2].Content[0].(provider.ToolResultBlock)
29	if !ok {
30		t.Fatalf("expected ToolResultBlock, got %T", out[2].Content[0])
31	}
32	if tr.CallID != "t1" {
33		t.Errorf("CallID: want t1, got %q", tr.CallID)
34	}
35	if !tr.IsError {
36		t.Errorf("expected IsError=true on stub")
37	}
38}
39
40// TestRepairToolUseResultPairsMergesIntoExistingToolMessage covers
41// the case where a tool-role message exists but is missing one of
42// the call ids. The stub must be merged into it (no new message)
43// so the ordering stays tool_use, tool_result, tool_use, tool_result.
44func TestRepairToolUseResultPairsMergesIntoExistingToolMessage(t *testing.T) {
45	msgs := []provider.Message{
46		{Role: provider.RoleUser, Content: []provider.Content{provider.TextBlock{Text: "hi"}}},
47		{Role: provider.RoleAssistant, Content: []provider.Content{
48			provider.ToolCallBlock{ID: "t1", Name: "read"},
49			provider.ToolCallBlock{ID: "t2", Name: "bash"},
50		}},
51		{Role: provider.RoleTool, Content: []provider.Content{
52			provider.ToolResultBlock{CallID: "t1", Content: []provider.Content{provider.TextBlock{Text: "ok"}}},
53		}},
54	}
55	out := repairToolUseResultPairs(msgs)
56	if len(out) != 3 {
57		t.Fatalf("expected 3 messages (merged), got %d", len(out))
58	}
59	if out[2].Role != provider.RoleTool {
60		t.Fatalf("expected tool-role at index 2")
61	}
62	if len(out[2].Content) != 2 {
63		t.Fatalf("expected 2 tool_result blocks after merge, got %d", len(out[2].Content))
64	}
65	// second block must be the stub for t2
66	tr, ok := out[2].Content[1].(provider.ToolResultBlock)
67	if !ok {
68		t.Fatalf("expected ToolResultBlock at [1], got %T", out[2].Content[1])
69	}
70	if tr.CallID != "t2" {
71		t.Errorf("merged stub CallID: want t2, got %q", tr.CallID)
72	}
73}
74
75// TestRepairToolUseResultPairsLeavesValidTranscriptsAlone verifies
76// the repair is a no-op when everything is already paired.
77func TestRepairToolUseResultPairsLeavesValidTranscriptsAlone(t *testing.T) {
78	msgs := []provider.Message{
79		{Role: provider.RoleUser, Content: []provider.Content{provider.TextBlock{Text: "hi"}}},
80		{Role: provider.RoleAssistant, Content: []provider.Content{
81			provider.ToolCallBlock{ID: "t1", Name: "read"},
82		}},
83		{Role: provider.RoleTool, Content: []provider.Content{
84			provider.ToolResultBlock{CallID: "t1", Content: []provider.Content{provider.TextBlock{Text: "ok"}}},
85		}},
86		{Role: provider.RoleAssistant, Content: []provider.Content{provider.TextBlock{Text: "done"}}},
87	}
88	out := repairToolUseResultPairs(msgs)
89	if len(out) != len(msgs) {
90		t.Fatalf("no-op expected, len %d -> %d", len(msgs), len(out))
91	}
92	for i := range msgs {
93		if msgs[i].Role != out[i].Role {
94			t.Errorf("msg %d role changed", i)
95		}
96	}
97}
98
99// TestRepairToolUseResultPairsEmpty handles the zero-value case.
100func TestRepairToolUseResultPairsEmpty(t *testing.T) {
101	if out := repairToolUseResultPairs(nil); len(out) != 0 {
102		t.Fatalf("expected empty, got %d", len(out))
103	}
104}

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