1package modes
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7
8 "github.com/bnema/zut/packages/agent/internal/orchestration"
9 "github.com/bnema/zut/packages/agent/subagents"
10 "github.com/bnema/zut/packages/core"
11 "github.com/bnema/zut/packages/provider"
12)
13
14func TestApplySessionAgentWithCWDRebindsInteractiveWorkspace(t *testing.T) {
15 oldCWD := t.TempDir()
16 newCWD := filepath.Join(t.TempDir(), "selected-workspace")
17 if err := os.MkdirAll(newCWD, 0o755); err != nil {
18 t.Fatal(err)
19 }
20 current := core.NewAgent(nil, "model", "", nil)
21 selected := core.NewAgent(nil, "model", "", nil)
22 selected.SetMessages([]provider.Message{{Role: provider.RoleUser, Content: []provider.Content{provider.TextBlock{Text: "selected"}}}})
23 i := NewInteractive(InteractiveConfig{Agent: current, CWD: oldCWD, StartupContextPaths: []string{filepath.Join(oldCWD, "AGENTS.md")}})
24 i.view.StartupContextPaths = []string{filepath.Join(oldCWD, "AGENTS.md")}
25 i.ApplySessionAgentWithCWD(selected, "provider", "model", newCWD, nil)
26 if i.cfg.CWD != newCWD || i.agent != selected {
27 t.Fatalf("interactive workspace = %q agent=%p, want cwd=%q agent=%p", i.cfg.CWD, i.agent, newCWD, selected)
28 }
29 if len(i.cfg.StartupContextPaths) != 0 || len(i.view.StartupContextPaths) != 0 {
30 t.Fatalf("old workspace paths remained: cfg=%q view=%q", i.cfg.StartupContextPaths, i.view.StartupContextPaths)
31 }
32 text, _ := i.view.Messages[0].Content[0].(provider.TextBlock)
33 if text.Text != "selected" {
34 t.Fatalf("resumed view text = %q", text.Text)
35 }
36}
37
38func TestCanResumeSessionSelectionRejectsBusyTurn(t *testing.T) {
39 i := NewInteractive(InteractiveConfig{Agent: core.NewAgent(nil, "model", "", nil), LoadSession: func(string) error { return nil }})
40 i.busy = true
41 if i.canResumeSessionSelection() {
42 t.Fatal("busy turn allowed session selection")
43 }
44}
45
46func TestSubmitOrQueuePreservesImagesInHostQueue(t *testing.T) {
47 i := &Interactive{agent: &core.Agent{}, busy: true, compacting: true}
48 image := provider.ImageBlock{MimeType: "image/png", Data: []byte("png-1")}
49
50 i.submitOrQueue("inspect", []provider.ImageBlock{image}, false)
51
52 if len(i.queued) != 1 || i.queued[0].Text != "inspect" {
53 t.Fatalf("queued messages = %#v, want one inspect prompt", i.queued)
54 }
55 if len(i.queued[0].Images) != 1 || string(i.queued[0].Images[0].Data) != "png-1" {
56 t.Fatalf("queued images = %#v, want png-1", i.queued[0].Images)
57 }
58}
59
60func TestSubmitOrQueuePreservesSealedWorkerWaveWithPendingWorker(t *testing.T) {
61 i := &Interactive{agent: &core.Agent{}}
62 i.applyCoordinator(orchestration.Event{Kind: orchestration.EventManagerStarted})
63 i.applyCoordinator(orchestration.Event{Kind: orchestration.EventWorkerRegistered, WorkerID: "worker#1"})
64 i.applyCoordinator(orchestration.Event{Kind: orchestration.EventManagerFinished})
65
66 image := provider.ImageBlock{MimeType: "image/png", Data: []byte("png-1")}
67 i.submitOrQueue("review the result", []provider.ImageBlock{image}, true)
68
69 actions := i.applyCoordinator(orchestration.Event{
70 Kind: orchestration.EventWorkerFinished,
71 WorkerID: "worker#1",
72 Completion: subagents.Completion{
73 AgentID: "worker",
74 Status: "completed",
75 },
76 })
77 if len(actions) != 1 {
78 t.Fatalf("completion actions = %d, want 1", len(actions))
79 }
80 if got := actions[0].Text; got != "review the result" {
81 t.Fatalf("completion action text = %q, want queued user input", got)
82 }
83 if len(actions[0].Images) != 1 || string(actions[0].Images[0].Data) != "png-1" {
84 t.Fatalf("completion action images = %#v, want queued image", actions[0].Images)
85 }
86}