| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "path/filepath" |
| 6 | "reasonix/internal/checkpoint" |
| 7 | "reasonix/internal/control" |
| 8 | "reasonix/internal/event" |
| 9 | "reasonix/internal/eventwire" |
| 10 | "reasonix/internal/provider" |
| 11 | "reasonix/internal/turnevent" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | type turnResultReader struct { |
| 16 | control.SessionAPI |
| 17 | path string |
| 18 | changes *checkpoint.TurnChanges |
| 19 | afterRead func() |
| 20 | } |
| 21 | |
| 22 | func (r *turnResultReader) SessionPath() string { return r.path } |
| 23 | func (r *turnResultReader) CheckpointTurnChanges(int) *checkpoint.TurnChanges { |
| 24 | if r.afterRead != nil { |
| 25 | r.afterRead() |
| 26 | } |
| 27 | return r.changes |
| 28 | } |
| 29 | func (r *turnResultReader) History() []provider.Message { |
| 30 | if r.afterRead != nil { |
| 31 | r.afterRead() |
| 32 | } |
| 33 | return []provider.Message{{Role: provider.RoleTool, ID: "entry-old", ToolCallID: "call", Content: "old log"}, {Role: provider.RoleTool, ID: "entry-new", ToolCallID: "call", Content: "new log"}} |
| 34 | } |
| 35 | |
| 36 | func TestWorkspaceTurnResultIsSessionAndResultScoped(t *testing.T) { |
| 37 | ctrl := &turnResultReader{path: "session-a", changes: &checkpoint.TurnChanges{ID: "result-a", Turn: 0, Coverage: "complete", Files: []checkpoint.TurnFile{{Path: "f", Patch: "frozen"}}, Reasons: []string{}}} |
| 38 | a := &App{tabs: map[string]*WorkspaceTab{"a": {ID: "a", Ctrl: ctrl}}} |
| 39 | if got := a.WorkspaceTurnChanges("a", "session-a", 0, "result-a"); got.Coverage != "complete" || got.Files[0].Patch != "" { |
| 40 | t.Fatalf("summary: %+v", got) |
| 41 | } |
| 42 | if got := a.WorkspaceTurnChangeDetail("a", "session-a", 0, "result-a", "f"); got == nil || got.Patch != "frozen" { |
| 43 | t.Fatalf("detail: %+v", got) |
| 44 | } |
| 45 | if a.WorkspaceTurnChangeDetail("a", "session-a", 0, "old-result", "f") != nil { |
| 46 | t.Fatal("reused turn leaked another result") |
| 47 | } |
| 48 | if a.WorkspaceTurnChangeDetail("a", "session-a", 0, "result-a", "../unrecorded") != nil { |
| 49 | t.Fatal("unrecorded path read") |
| 50 | } |
| 51 | if a.TurnCheckLog("a", "session-a", "call", "entry-old").Output != "old log" { |
| 52 | t.Fatal("reused provider ID selected a newer log") |
| 53 | } |
| 54 | if a.TurnCheckLog("a", "session-a", "call", "missing") != nil { |
| 55 | t.Fatal("missing source ID read succeeded") |
| 56 | } |
| 57 | if a.TurnCheckLog("a", "session-b", "call", "entry-old") != nil { |
| 58 | t.Fatal("wrong session log") |
| 59 | } |
| 60 | ctrl.afterRead = func() { ctrl.path = "session-b" } |
| 61 | if a.WorkspaceTurnChanges("a", "session-a", 0, "result-a").Coverage != "unknown" { |
| 62 | t.Fatal("session rotated during result read") |
| 63 | } |
| 64 | ctrl.path = "session-a" |
| 65 | if a.TurnCheckLog("a", "session-a", "call", "entry-old") != nil { |
| 66 | t.Fatal("session rotated during log read") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestWorkspaceTurnResultMissingControllerHasEmptyArrays(t *testing.T) { |
| 71 | a := &App{} |
| 72 | r := a.WorkspaceTurnChanges("missing", "session", 0, "result") |
| 73 | if r.Files == nil || r.Reasons == nil || r.Coverage != "unknown" { |
| 74 | t.Fatalf("result: %+v", r) |
| 75 | } |
| 76 | if a.WorkspaceTurnChangeDetail("missing", "session", 0, "result", "f") != nil || a.TurnCheckLog("missing", "session", "tool", "entry") != nil { |
| 77 | t.Fatal("missing controller read succeeded") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestTurnResultDisplayPersistsAndReplaysWithoutModelMessages(t *testing.T) { |
| 82 | turn := 0 |
| 83 | receipt := &event.CompletionReceipt{Verdict: "partial", Diff: &checkpoint.TurnChanges{ID: "frozen", Turn: 0, Coverage: "complete", Files: []checkpoint.TurnFile{}, Reasons: []string{}}, Verifications: []event.ReceiptVerification{{Command: "go test ./...", ToolCallID: "check-id", Passed: true}}} |
| 84 | done := event.Event{Kind: event.TurnDone, TurnID: "turn-id", CheckpointTurn: &turn, Receipt: receipt} |
| 85 | var tab WorkspaceTab |
| 86 | tab.recordDisplayEvent(event.Event{Kind: event.Message, Text: "normal answer"}) |
| 87 | tab.recordDisplayEvent(done) |
| 88 | rows := tab.takeDisplayTurn(false) |
| 89 | if len(rows) != 1 || rows[0].CompletionReceipt == nil || rows[0].CheckpointTurn == nil || *rows[0].CheckpointTurn != 0 { |
| 90 | t.Fatalf("display rows: %+v", rows) |
| 91 | } |
| 92 | dir := t.TempDir() |
| 93 | path := filepath.Join(dir, "session.json") |
| 94 | if err := recordSessionPlannerDisplayForTurn(dir, path, "turn-id", "prompt", rows); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | stored := sessionPlannerDisplayTurns(dir, path) |
| 98 | if len(stored) != 1 || stored[0].Messages[0].CompletionReceipt.Verifications[0].ToolCallID != "check-id" { |
| 99 | t.Fatalf("stored: %+v", stored) |
| 100 | } |
| 101 | b, err := json.Marshal(rows[0]) |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | var old struct { |
| 106 | Role string `json:"role"` |
| 107 | Content string `json:"content"` |
| 108 | } |
| 109 | if err := json.Unmarshal(b, &old); err != nil || old.Role != "notice" || old.Content != "" { |
| 110 | t.Fatalf("old reader: %+v %v", old, err) |
| 111 | } |
| 112 | projection := turnevent.PendingProjection{Status: event.TurnCompleted, Events: []turnevent.Envelope{{Kind: "turn_done", TurnID: "turn-id", Event: eventwire.ToWire(done)}}} |
| 113 | replayed := displayMessagesFromProjection(projection) |
| 114 | if len(replayed) != 1 || replayed[0].CompletionReceipt.Verifications[0].ToolCallID != "check-id" { |
| 115 | t.Fatalf("durable replay: %+v", replayed) |
| 116 | } |
| 117 | } |
| 118 |