| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestProvisionalResumeClearsPreviousSessionJobsAndRollbackRestoresThem(t *testing.T) { |
| 10 | const oldPath = "/sessions/old.jsonl" |
| 11 | const targetPath = "/sessions/target.jsonl" |
| 12 | client := &http.Client{} |
| 13 | tab := &remoteTab{ |
| 14 | id: "remote-1", state: "ready", client: client, gen: 7, |
| 15 | routing: remoteTabSessionRouting{currentPath: oldPath, running: map[string]bool{}}, |
| 16 | pendingEvents: map[string]json.RawMessage{ |
| 17 | "approval_request:old": json.RawMessage(`{"kind":"approval_request"}`), |
| 18 | }, |
| 19 | runtime: remoteTabRuntimeState{ |
| 20 | running: true, turnStartedAt: 99, backgroundJobs: 3, |
| 21 | pendingPrompt: true, cancelRequested: true, cancellable: true, |
| 22 | }, |
| 23 | } |
| 24 | a := &App{remoteTabs: map[string]*remoteTab{tab.id: tab}} |
| 25 | route := a.beginRemoteTabProvisionalResume(tab.id, tab, client, tab.gen, targetPath) |
| 26 | if !route.active || route.previousRuntime.backgroundJobs != 3 { |
| 27 | t.Fatalf("provisional snapshot = %+v", route) |
| 28 | } |
| 29 | if tab.runtime.backgroundJobs != 0 || tab.runtime.running || tab.runtime.cancellable || tab.runtime.pendingPrompt || tab.runtime.cancelRequested || len(tab.pendingEvents) != 0 { |
| 30 | t.Fatalf("provisional target retained previous foreground state: runtime=%+v pending=%d", tab.runtime, len(tab.pendingEvents)) |
| 31 | } |
| 32 | if !a.rollbackRemoteTabProvisionalResume(tab.id, tab, client, tab.gen, route) { |
| 33 | t.Fatal("provisional route did not roll back") |
| 34 | } |
| 35 | if tab.runtime.backgroundJobs != 3 || !tab.runtime.running || !tab.runtime.cancellable || len(tab.pendingEvents) != 1 { |
| 36 | t.Fatalf("rollback did not restore previous foreground state: runtime=%+v pending=%d", tab.runtime, len(tab.pendingEvents)) |
| 37 | } |
| 38 | } |
| 39 |