| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "slices" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/agent" |
| 11 | "reasonix/internal/control" |
| 12 | "reasonix/internal/event" |
| 13 | "reasonix/internal/provider" |
| 14 | "reasonix/internal/sessioncontext" |
| 15 | "reasonix/internal/tool" |
| 16 | ) |
| 17 | |
| 18 | type workspaceContextProvider struct{} |
| 19 | |
| 20 | func (workspaceContextProvider) Name() string { return "workspace-context" } |
| 21 | |
| 22 | func (workspaceContextProvider) Stream(context.Context, provider.Request) (<-chan provider.Chunk, error) { |
| 23 | ch := make(chan provider.Chunk, 2) |
| 24 | ch <- provider.Chunk{Type: provider.ChunkText, Text: "ok"} |
| 25 | ch <- provider.Chunk{Type: provider.ChunkDone} |
| 26 | close(ch) |
| 27 | return ch, nil |
| 28 | } |
| 29 | |
| 30 | func installStubControllerWithCurrentPrompt(t *testing.T, app *App, tab *WorkspaceTab) *control.Controller { |
| 31 | t.Helper() |
| 32 | if tab == nil || tab.Ctrl == nil { |
| 33 | t.Fatal("tab controller is required") |
| 34 | } |
| 35 | sys := systemPromptFrom(tab.Ctrl.History()) |
| 36 | if strings.TrimSpace(sys) == "" { |
| 37 | t.Fatal("tab controller did not expose a system prompt") |
| 38 | } |
| 39 | sessionDir := tab.Ctrl.SessionDir() |
| 40 | sessionPath := tab.Ctrl.SessionPath() |
| 41 | workspaceRoot := tab.Ctrl.WorkspaceRoot() |
| 42 | label := tab.Ctrl.Label() |
| 43 | tab.Ctrl.Close() |
| 44 | |
| 45 | sess := agent.NewSession(sys) |
| 46 | ag := agent.New(workspaceContextProvider{}, tool.NewRegistry(), sess, agent.Options{}, event.Discard) |
| 47 | ctrl := control.New(control.Options{ |
| 48 | Runner: ag, |
| 49 | Executor: ag, |
| 50 | SessionDir: sessionDir, |
| 51 | SessionPath: sessionPath, |
| 52 | WorkspaceRoot: workspaceRoot, |
| 53 | Label: label, |
| 54 | SystemPrompt: sys, |
| 55 | SessionContextStatic: sessioncontext.Sections{Workspace: "Current workspace: " + strconv.Quote(workspaceRoot)}, |
| 56 | Sink: event.Discard, |
| 57 | }) |
| 58 | app.mu.Lock() |
| 59 | tab.Ctrl = ctrl |
| 60 | app.mu.Unlock() |
| 61 | app.bindControllerDisplayRecorder(ctrl) |
| 62 | return ctrl |
| 63 | } |
| 64 | |
| 65 | func assertWorkspaceSessionContext(t *testing.T, messages []provider.Message, want, unwanted string) { |
| 66 | t.Helper() |
| 67 | for i := range slices.Backward(messages) { |
| 68 | snapshot, ok := sessioncontext.Parse(messages[i].Content) |
| 69 | if !ok || messages[i].Origin != provider.MessageOriginHost { |
| 70 | continue |
| 71 | } |
| 72 | if !strings.Contains(snapshot.Sections.Workspace, "Current workspace: "+strconv.Quote(want)) { |
| 73 | t.Fatalf("session-context missing workspace %q:\n%s", want, snapshot.Content) |
| 74 | } |
| 75 | if strings.Contains(snapshot.Sections.Workspace, "Current workspace: "+strconv.Quote(unwanted)) { |
| 76 | t.Fatalf("session-context retained workspace %q:\n%s", unwanted, snapshot.Content) |
| 77 | } |
| 78 | return |
| 79 | } |
| 80 | t.Fatalf("history contains no valid host session-context: %+v", messages) |
| 81 | } |
| 82 |