| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "reasonix/internal/agent" |
| 14 | "reasonix/internal/boot" |
| 15 | "reasonix/internal/config" |
| 16 | "reasonix/internal/control" |
| 17 | "reasonix/internal/event" |
| 18 | "reasonix/internal/evidence" |
| 19 | "reasonix/internal/store" |
| 20 | ) |
| 21 | |
| 22 | func newGoalDeliveryYoloTestApp(t *testing.T, goalStatus string) (*App, *WorkspaceTab, control.SessionAPI, string) { |
| 23 | t.Helper() |
| 24 | isolateDesktopUserDirs(t) |
| 25 | setDesktopTestCredential(t, "GOAL_DELIVERY_KEY", "sk-test") |
| 26 | setDesktopTestCredential(t, "GOAL_DELIVERY_ALT_KEY", "sk-test") |
| 27 | cfg := config.Default() |
| 28 | cfg.DefaultModel = "test/model" |
| 29 | cfg.Desktop.ProviderAccess = []string{"test", "alt"} |
| 30 | cfg.Providers = []config.ProviderEntry{ |
| 31 | { |
| 32 | Name: "test", Kind: "openai", BaseURL: "https://example.invalid/v1", Model: "model", APIKeyEnv: "GOAL_DELIVERY_KEY", |
| 33 | SupportedEfforts: []string{"low", "high"}, |
| 34 | }, |
| 35 | {Name: "alt", Kind: "openai", BaseURL: "https://example.invalid/v1", Model: "alt-model", APIKeyEnv: "GOAL_DELIVERY_ALT_KEY"}, |
| 36 | } |
| 37 | if err := cfg.SaveTo(config.UserConfigPath()); err != nil { |
| 38 | t.Fatalf("save config: %v", err) |
| 39 | } |
| 40 | |
| 41 | dir := config.SessionDir() |
| 42 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 43 | t.Fatalf("mkdir session dir: %v", err) |
| 44 | } |
| 45 | path := filepath.Join(dir, "goal-delivery-yolo.jsonl") |
| 46 | writeHistoryTestSession(t, path, "continue the delivery") |
| 47 | checkpoint := evidence.DeliveryCheckpoint{ |
| 48 | ScopeID: "goal-test-scope", |
| 49 | CriteriaEstablished: true, |
| 50 | WorkObserved: true, |
| 51 | MutationObserved: true, |
| 52 | PendingMutation: true, |
| 53 | } |
| 54 | state := map[string]any{ |
| 55 | "goal": "ship the combined mode", |
| 56 | "status": goalStatus, |
| 57 | "researchMode": control.GoalResearchOn, |
| 58 | "autoResearchTaskID": "research-task-1", |
| 59 | "scopeID": checkpoint.ScopeID, |
| 60 | "deliveryCheckpoint": checkpoint, |
| 61 | } |
| 62 | data, err := json.Marshal(state) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if err := os.WriteFile(store.SessionGoalState(path), data, 0o600); err != nil { |
| 67 | t.Fatalf("write Goal sidecar: %v", err) |
| 68 | } |
| 69 | |
| 70 | loaded, err := agent.LoadSession(path) |
| 71 | if err != nil { |
| 72 | t.Fatalf("load session: %v", err) |
| 73 | } |
| 74 | exec := agent.New(nil, nil, loaded, agent.Options{}, event.Discard) |
| 75 | oldCtrl := control.New(control.Options{Executor: exec, SessionDir: dir, SessionPath: path, Label: "test/model", Sink: event.Discard}) |
| 76 | oldCtrl.Resume(loaded, path) |
| 77 | oldCtrl.SetToolApprovalMode(control.ToolApprovalYolo) |
| 78 | |
| 79 | app := NewApp() |
| 80 | app.ctx = context.Background() |
| 81 | app.readyHook = func() {} |
| 82 | tab := &WorkspaceTab{ |
| 83 | ID: "tab_goal_delivery_yolo", Scope: "global", Ready: true, |
| 84 | SessionPath: path, model: "test/model", tokenMode: boot.TokenModeFull, |
| 85 | mode: "yolo", toolApprovalMode: control.ToolApprovalYolo, |
| 86 | goal: "stale tab goal", Ctrl: oldCtrl, |
| 87 | disabledMCP: map[string]ServerView{}, |
| 88 | } |
| 89 | tab.sink = &tabEventSink{tabID: tab.ID, app: app} |
| 90 | app.tabs = map[string]*WorkspaceTab{tab.ID: tab} |
| 91 | app.tabOrder = []string{tab.ID} |
| 92 | app.activeTabID = tab.ID |
| 93 | if err := tab.ensureSessionLease(path); err != nil { |
| 94 | t.Fatalf("ensure session lease: %v", err) |
| 95 | } |
| 96 | app.mu.Lock() |
| 97 | app.newSessionRuntimeLocked(tab, sessionRuntimeKey(path)) |
| 98 | app.advanceSessionRuntimeEpochLocked(tab) |
| 99 | app.mu.Unlock() |
| 100 | t.Cleanup(func() { |
| 101 | if ctrl := app.controllerForTab(tab); ctrl != nil { |
| 102 | ctrl.Close() |
| 103 | } |
| 104 | tab.releaseSessionLease() |
| 105 | }) |
| 106 | return app, tab, oldCtrl, path |
| 107 | } |
| 108 | |
| 109 | func TestGoalDeliveryYoloTokenSwitchPreservesBlockedGoalCheckpoint(t *testing.T) { |
| 110 | app, tab, oldCtrl, path := newGoalDeliveryYoloTestApp(t, control.GoalStatusBlocked) |
| 111 | if err := app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery); err != nil { |
| 112 | t.Fatalf("SetTokenModeForTab: %v", err) |
| 113 | } |
| 114 | ctrl := app.controllerForTab(tab) |
| 115 | if ctrl == nil || ctrl == oldCtrl { |
| 116 | t.Fatal("token-mode switch did not install a replacement controller") |
| 117 | } |
| 118 | if ctrl.GoalStatus() != control.GoalStatusBlocked || ctrl.Goal() != "ship the combined mode" { |
| 119 | t.Fatalf("rebuilt Goal = (%q, %q), want blocked Goal", ctrl.Goal(), ctrl.GoalStatus()) |
| 120 | } |
| 121 | if ctrl.ToolApprovalMode() != control.ToolApprovalYolo { |
| 122 | t.Fatalf("tool approval = %q, want yolo", ctrl.ToolApprovalMode()) |
| 123 | } |
| 124 | if currentTabTokenMode(tab) != boot.TokenModeDelivery { |
| 125 | t.Fatalf("token mode = %q, want delivery", currentTabTokenMode(tab)) |
| 126 | } |
| 127 | app.mu.RLock() |
| 128 | runtimeForPath := app.runtimeBySessionKey[sessionRuntimeKey(path)] |
| 129 | runtimeCount := len(app.runtimeBySessionKey) |
| 130 | app.mu.RUnlock() |
| 131 | if runtimeForPath == nil || runtimeForPath.Owner != tab || runtimeCount != 1 { |
| 132 | t.Fatalf("runtime registry after Goal+Delivery switch = owner %p count %d, want tab %p count 1", runtimeForPath, runtimeCount, tab) |
| 133 | } |
| 134 | var persisted struct { |
| 135 | DeliveryCheckpoint evidence.DeliveryCheckpoint `json:"deliveryCheckpoint"` |
| 136 | } |
| 137 | data, err := os.ReadFile(store.SessionGoalState(path)) |
| 138 | if err != nil { |
| 139 | t.Fatalf("read Goal sidecar: %v", err) |
| 140 | } |
| 141 | if err := json.Unmarshal(data, &persisted); err != nil { |
| 142 | t.Fatalf("decode Goal sidecar: %v", err) |
| 143 | } |
| 144 | if persisted.DeliveryCheckpoint.ScopeID != "goal-test-scope" || !persisted.DeliveryCheckpoint.PendingMutation { |
| 145 | t.Fatalf("persisted checkpoint = %+v", persisted.DeliveryCheckpoint) |
| 146 | } |
| 147 | if !app.ResumeGoalForTab(tab.ID) || ctrl.GoalStatus() != control.GoalStatusRunning { |
| 148 | t.Fatal("blocked Goal did not resume after controller rebuild") |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func TestGoalDeliveryYoloTokenSwitchDoesNotReviveCompletedGoal(t *testing.T) { |
| 153 | app, tab, _, _ := newGoalDeliveryYoloTestApp(t, control.GoalStatusComplete) |
| 154 | if err := app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery); err != nil { |
| 155 | t.Fatalf("SetTokenModeForTab: %v", err) |
| 156 | } |
| 157 | ctrl := app.controllerForTab(tab) |
| 158 | if ctrl.GoalStatus() == control.GoalStatusRunning { |
| 159 | t.Fatalf("completed Goal was revived: goal=%q status=%q", ctrl.Goal(), ctrl.GoalStatus()) |
| 160 | } |
| 161 | if app.ResumeGoalForTab(tab.ID) { |
| 162 | t.Fatal("completed Goal should not be resumable") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestPlanYoloDeliveryRebuildUsesLiveControllerAxes(t *testing.T) { |
| 167 | app, tab, oldCtrl, _ := newGoalDeliveryYoloTestApp(t, control.GoalStatusBlocked) |
| 168 | oldCtrl.SetPlanMode(true) |
| 169 | oldCtrl.SetToolApprovalMode(control.ToolApprovalYolo) |
| 170 | // Simulate stale tab metadata: the rebuild must snapshot the admitted |
| 171 | // Controller state, not restore these lagging persistence fields. |
| 172 | app.mu.Lock() |
| 173 | tab.mode = "normal" |
| 174 | tab.toolApprovalMode = "" |
| 175 | app.mu.Unlock() |
| 176 | |
| 177 | if err := app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery); err != nil { |
| 178 | t.Fatalf("SetTokenModeForTab: %v", err) |
| 179 | } |
| 180 | ctrl := app.controllerForTab(tab) |
| 181 | if ctrl == nil || ctrl == oldCtrl { |
| 182 | t.Fatal("token-mode switch did not install a replacement controller") |
| 183 | } |
| 184 | if !ctrl.PlanMode() || ctrl.ToolApprovalMode() != control.ToolApprovalYolo { |
| 185 | t.Fatalf("rebuilt axes plan=%v approval=%q, want true/yolo", ctrl.PlanMode(), ctrl.ToolApprovalMode()) |
| 186 | } |
| 187 | if ctrl.GoalStatus() != control.GoalStatusBlocked { |
| 188 | t.Fatalf("blocked Goal status = %q, want preserved while Plan is active", ctrl.GoalStatus()) |
| 189 | } |
| 190 | if currentTabTokenMode(tab) != boot.TokenModeDelivery { |
| 191 | t.Fatalf("token mode = %q, want delivery", currentTabTokenMode(tab)) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func TestPlanWinsRunningGoalConflictDuringDeliveryRebuild(t *testing.T) { |
| 196 | app, tab, oldCtrl, path := newGoalDeliveryYoloTestApp(t, control.GoalStatusRunning) |
| 197 | oldCtrl.SetPlanMode(true) |
| 198 | oldCtrl.SetToolApprovalMode(control.ToolApprovalYolo) |
| 199 | app.mu.Lock() |
| 200 | tab.mode = "plan-yolo" |
| 201 | tab.goal = "ship the combined mode" |
| 202 | app.mu.Unlock() |
| 203 | |
| 204 | if err := app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery); err != nil { |
| 205 | t.Fatalf("SetTokenModeForTab: %v", err) |
| 206 | } |
| 207 | ctrl := app.controllerForTab(tab) |
| 208 | if !ctrl.PlanMode() || ctrl.ToolApprovalMode() != control.ToolApprovalYolo { |
| 209 | t.Fatalf("rebuilt axes plan=%v approval=%q, want true/yolo", ctrl.PlanMode(), ctrl.ToolApprovalMode()) |
| 210 | } |
| 211 | if ctrl.GoalStatus() == control.GoalStatusRunning || strings.TrimSpace(ctrl.Goal()) != "" { |
| 212 | t.Fatalf("Plan/Goal conflict survived rebuild: goal=%q status=%q", ctrl.Goal(), ctrl.GoalStatus()) |
| 213 | } |
| 214 | var persisted struct { |
| 215 | Goal string `json:"goal"` |
| 216 | Status string `json:"status"` |
| 217 | } |
| 218 | data, err := os.ReadFile(store.SessionGoalState(path)) |
| 219 | if err != nil { |
| 220 | t.Fatal(err) |
| 221 | } |
| 222 | if err := json.Unmarshal(data, &persisted); err != nil { |
| 223 | t.Fatal(err) |
| 224 | } |
| 225 | if persisted.Status == control.GoalStatusRunning || strings.TrimSpace(persisted.Goal) != "" { |
| 226 | t.Fatalf("conflicting Goal sidecar = %+v, want cleared", persisted) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | func TestRunningGoalDeliveryYoloRebuildKeepsScopeAndAutoResearch(t *testing.T) { |
| 231 | app, tab, oldCtrl, path := newGoalDeliveryYoloTestApp(t, control.GoalStatusRunning) |
| 232 | if err := app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery); err != nil { |
| 233 | t.Fatalf("SetTokenModeForTab: %v", err) |
| 234 | } |
| 235 | ctrl := app.controllerForTab(tab) |
| 236 | if ctrl == nil || ctrl == oldCtrl || ctrl.GoalStatus() != control.GoalStatusRunning { |
| 237 | t.Fatalf("running Goal was not restored: ctrl=%T goal=%q status=%q", ctrl, ctrl.Goal(), ctrl.GoalStatus()) |
| 238 | } |
| 239 | if ctrl.ToolApprovalMode() != control.ToolApprovalYolo { |
| 240 | t.Fatalf("tool approval = %q, want yolo", ctrl.ToolApprovalMode()) |
| 241 | } |
| 242 | var persisted struct { |
| 243 | ScopeID string `json:"scopeID"` |
| 244 | AutoResearchTaskID string `json:"autoResearchTaskID"` |
| 245 | DeliveryCheckpoint evidence.DeliveryCheckpoint `json:"deliveryCheckpoint"` |
| 246 | } |
| 247 | data, err := os.ReadFile(store.SessionGoalState(path)) |
| 248 | if err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | if err := json.Unmarshal(data, &persisted); err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | if persisted.ScopeID != "goal-test-scope" || persisted.AutoResearchTaskID != "research-task-1" { |
| 255 | t.Fatalf("restored Goal identity = %+v", persisted) |
| 256 | } |
| 257 | if persisted.DeliveryCheckpoint.ScopeID != persisted.ScopeID || !persisted.DeliveryCheckpoint.PendingMutation { |
| 258 | t.Fatalf("restored Delivery checkpoint = %+v", persisted.DeliveryCheckpoint) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func TestGoalDeliveryYoloSurvivesEveryControllerRebuildPath(t *testing.T) { |
| 263 | for _, tc := range []struct { |
| 264 | name string |
| 265 | prepare func(*App, *WorkspaceTab) |
| 266 | rebuild func(*App, *WorkspaceTab) error |
| 267 | }{ |
| 268 | { |
| 269 | name: "settings", |
| 270 | prepare: func(app *App, tab *WorkspaceTab) { |
| 271 | app.mu.Lock() |
| 272 | tab.tokenMode = boot.TokenModeDelivery |
| 273 | app.mu.Unlock() |
| 274 | }, |
| 275 | rebuild: func(app *App, _ *WorkspaceTab) error { |
| 276 | return app.rebuildSetting("settings") |
| 277 | }, |
| 278 | }, |
| 279 | { |
| 280 | name: "model", |
| 281 | prepare: func(app *App, tab *WorkspaceTab) { |
| 282 | app.mu.Lock() |
| 283 | tab.tokenMode = boot.TokenModeDelivery |
| 284 | app.mu.Unlock() |
| 285 | }, |
| 286 | rebuild: func(app *App, tab *WorkspaceTab) error { |
| 287 | return app.SetModelForTab(tab.ID, "alt/alt-model") |
| 288 | }, |
| 289 | }, |
| 290 | { |
| 291 | name: "effort", |
| 292 | prepare: func(app *App, tab *WorkspaceTab) { |
| 293 | app.mu.Lock() |
| 294 | tab.tokenMode = boot.TokenModeDelivery |
| 295 | app.mu.Unlock() |
| 296 | }, |
| 297 | rebuild: func(app *App, tab *WorkspaceTab) error { |
| 298 | return app.SetEffortForTab(tab.ID, "high") |
| 299 | }, |
| 300 | }, |
| 301 | { |
| 302 | name: "token mode", |
| 303 | prepare: func(*App, *WorkspaceTab) {}, |
| 304 | rebuild: func(app *App, tab *WorkspaceTab) error { |
| 305 | return app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery) |
| 306 | }, |
| 307 | }, |
| 308 | } { |
| 309 | t.Run(tc.name, func(t *testing.T) { |
| 310 | app, tab, oldCtrl, path := newGoalDeliveryYoloTestApp(t, control.GoalStatusRunning) |
| 311 | tc.prepare(app, tab) |
| 312 | if err := tc.rebuild(app, tab); err != nil { |
| 313 | t.Fatalf("rebuild: %v", err) |
| 314 | } |
| 315 | |
| 316 | ctrl := app.controllerForTab(tab) |
| 317 | if ctrl == nil || ctrl == oldCtrl { |
| 318 | t.Fatal("rebuild did not install a replacement controller") |
| 319 | } |
| 320 | if ctrl.PlanMode() || ctrl.GoalStatus() != control.GoalStatusRunning || ctrl.Goal() != "ship the combined mode" { |
| 321 | t.Fatalf("collaboration state plan=%v goal=%q status=%q, want running Goal", ctrl.PlanMode(), ctrl.Goal(), ctrl.GoalStatus()) |
| 322 | } |
| 323 | if ctrl.ToolApprovalMode() != control.ToolApprovalYolo || currentTabTokenMode(tab) != boot.TokenModeDelivery { |
| 324 | t.Fatalf("runtime axes approval=%q token=%q, want yolo/delivery", ctrl.ToolApprovalMode(), currentTabTokenMode(tab)) |
| 325 | } |
| 326 | |
| 327 | var persisted struct { |
| 328 | ScopeID string `json:"scopeID"` |
| 329 | AutoResearchTaskID string `json:"autoResearchTaskID"` |
| 330 | DeliveryCheckpoint evidence.DeliveryCheckpoint `json:"deliveryCheckpoint"` |
| 331 | } |
| 332 | data, err := os.ReadFile(store.SessionGoalState(path)) |
| 333 | if err != nil { |
| 334 | t.Fatalf("read Goal sidecar: %v", err) |
| 335 | } |
| 336 | if err := json.Unmarshal(data, &persisted); err != nil { |
| 337 | t.Fatalf("decode Goal sidecar: %v", err) |
| 338 | } |
| 339 | if persisted.ScopeID != "goal-test-scope" || persisted.AutoResearchTaskID != "research-task-1" { |
| 340 | t.Fatalf("restored Goal identity = %+v", persisted) |
| 341 | } |
| 342 | if persisted.DeliveryCheckpoint.ScopeID != persisted.ScopeID || !persisted.DeliveryCheckpoint.PendingMutation { |
| 343 | t.Fatalf("restored Delivery checkpoint = %+v", persisted.DeliveryCheckpoint) |
| 344 | } |
| 345 | }) |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | func TestPlanYoloDeliveryOrderConverges(t *testing.T) { |
| 350 | for _, tc := range []struct { |
| 351 | name string |
| 352 | run func(*App, *WorkspaceTab) error |
| 353 | }{ |
| 354 | { |
| 355 | name: "plan then yolo then delivery", |
| 356 | run: func(app *App, tab *WorkspaceTab) error { |
| 357 | app.SetCollaborationModeForTab(tab.ID, "plan") |
| 358 | app.SetToolApprovalModeForTab(tab.ID, control.ToolApprovalYolo) |
| 359 | return app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery) |
| 360 | }, |
| 361 | }, |
| 362 | { |
| 363 | name: "delivery then plan then yolo", |
| 364 | run: func(app *App, tab *WorkspaceTab) error { |
| 365 | if err := app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery); err != nil { |
| 366 | return err |
| 367 | } |
| 368 | app.SetCollaborationModeForTab(tab.ID, "plan") |
| 369 | app.SetToolApprovalModeForTab(tab.ID, control.ToolApprovalYolo) |
| 370 | return nil |
| 371 | }, |
| 372 | }, |
| 373 | } { |
| 374 | t.Run(tc.name, func(t *testing.T) { |
| 375 | app, tab, _, _ := newGoalDeliveryYoloTestApp(t, control.GoalStatusComplete) |
| 376 | app.SetToolApprovalModeForTab(tab.ID, control.ToolApprovalAsk) |
| 377 | if err := tc.run(app, tab); err != nil { |
| 378 | t.Fatal(err) |
| 379 | } |
| 380 | ctrl := app.controllerForTab(tab) |
| 381 | if !ctrl.PlanMode() || ctrl.ToolApprovalMode() != control.ToolApprovalYolo || currentTabTokenMode(tab) != boot.TokenModeDelivery { |
| 382 | t.Fatalf("final axes plan=%v approval=%q token=%q", ctrl.PlanMode(), ctrl.ToolApprovalMode(), currentTabTokenMode(tab)) |
| 383 | } |
| 384 | }) |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | func TestEveryCollaborationAndTokenModeCombinationConvergesInBothOrders(t *testing.T) { |
| 389 | collaborationModes := []string{"normal", "plan", "goal"} |
| 390 | tokenModes := []string{boot.TokenModeEconomy, boot.TokenModeFull, boot.TokenModeDelivery} |
| 391 | orders := []string{"collaboration-first", "token-first"} |
| 392 | |
| 393 | for _, collaborationMode := range collaborationModes { |
| 394 | for _, tokenMode := range tokenModes { |
| 395 | for _, order := range orders { |
| 396 | t.Run(collaborationMode+"/"+tokenMode+"/"+order, func(t *testing.T) { |
| 397 | app, tab, _, path := newGoalDeliveryYoloTestApp(t, control.GoalStatusRunning) |
| 398 | app.SetToolApprovalModeForTab(tab.ID, control.ToolApprovalAsk) |
| 399 | |
| 400 | setCollaboration := func() { |
| 401 | switch collaborationMode { |
| 402 | case "goal": |
| 403 | app.SetGoalForTab(tab.ID, "exercise all runtime axes") |
| 404 | app.SetCollaborationModeForTab(tab.ID, "goal") |
| 405 | case "plan": |
| 406 | app.SetCollaborationModeForTab(tab.ID, "plan") |
| 407 | default: |
| 408 | app.SetGoalForTab(tab.ID, "") |
| 409 | app.SetCollaborationModeForTab(tab.ID, "normal") |
| 410 | } |
| 411 | } |
| 412 | setToken := func() { |
| 413 | if err := app.SetTokenModeForTab(tab.ID, tokenMode); err != nil { |
| 414 | t.Fatalf("SetTokenModeForTab(%q): %v", tokenMode, err) |
| 415 | } |
| 416 | } |
| 417 | if order == "collaboration-first" { |
| 418 | setCollaboration() |
| 419 | setToken() |
| 420 | } else { |
| 421 | setToken() |
| 422 | setCollaboration() |
| 423 | } |
| 424 | |
| 425 | ctrl := app.controllerForTab(tab) |
| 426 | if ctrl == nil { |
| 427 | t.Fatal("final controller is nil") |
| 428 | } |
| 429 | if got := currentTabTokenMode(tab); got != tokenMode { |
| 430 | t.Fatalf("token mode = %q, want %q", got, tokenMode) |
| 431 | } |
| 432 | switch collaborationMode { |
| 433 | case "goal": |
| 434 | if ctrl.PlanMode() || ctrl.GoalStatus() != control.GoalStatusRunning || |
| 435 | ctrl.Goal() != "exercise all runtime axes" { |
| 436 | t.Fatalf("Goal axes plan=%v goal=%q status=%q", ctrl.PlanMode(), ctrl.Goal(), ctrl.GoalStatus()) |
| 437 | } |
| 438 | case "plan": |
| 439 | if !ctrl.PlanMode() || ctrl.GoalStatus() == control.GoalStatusRunning { |
| 440 | t.Fatalf("Plan axes plan=%v goal=%q status=%q", ctrl.PlanMode(), ctrl.Goal(), ctrl.GoalStatus()) |
| 441 | } |
| 442 | default: |
| 443 | if ctrl.PlanMode() || ctrl.GoalStatus() == control.GoalStatusRunning { |
| 444 | t.Fatalf("Normal axes plan=%v goal=%q status=%q", ctrl.PlanMode(), ctrl.Goal(), ctrl.GoalStatus()) |
| 445 | } |
| 446 | } |
| 447 | app.mu.RLock() |
| 448 | runtimeForPath := app.runtimeBySessionKey[sessionRuntimeKey(path)] |
| 449 | runtimeCount := len(app.runtimeBySessionKey) |
| 450 | view := app.sessionRuntimeViewLocked(tab) |
| 451 | app.mu.RUnlock() |
| 452 | if runtimeForPath == nil || runtimeForPath.Owner != tab || runtimeCount != 1 { |
| 453 | t.Fatalf("runtime registry owner=%#v count=%d, want one runtime for tab", runtimeForPath, runtimeCount) |
| 454 | } |
| 455 | if view.Phase != sessionRuntimeReady || tab.sessionLeaseRuntimeKey() != sessionRuntimeKey(path) { |
| 456 | t.Fatalf("runtime phase=%q lease=%q, want ready/%q", view.Phase, tab.sessionLeaseRuntimeKey(), sessionRuntimeKey(path)) |
| 457 | } |
| 458 | }) |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | func TestGoalAndCollaborationResyncBeforeSendPreserveRunningDeliveryScope(t *testing.T) { |
| 465 | app, tab, ctrl, path := newGoalDeliveryYoloTestApp(t, control.GoalStatusRunning) |
| 466 | tab.goal = ctrl.Goal() |
| 467 | app.SetCollaborationModeForTab(tab.ID, "goal") |
| 468 | app.SetGoalForTab(tab.ID, ctrl.Goal()) |
| 469 | |
| 470 | var persisted struct { |
| 471 | ScopeID string `json:"scopeID"` |
| 472 | } |
| 473 | data, err := os.ReadFile(store.SessionGoalState(path)) |
| 474 | if err != nil { |
| 475 | t.Fatalf("read Goal sidecar: %v", err) |
| 476 | } |
| 477 | if err := json.Unmarshal(data, &persisted); err != nil { |
| 478 | t.Fatalf("decode Goal sidecar: %v", err) |
| 479 | } |
| 480 | if persisted.ScopeID != "goal-test-scope" { |
| 481 | t.Fatalf("Goal resync replaced delivery scope: got %q", persisted.ScopeID) |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | func TestTokenModeSwitchWaitsForForegroundTurnAdmission(t *testing.T) { |
| 486 | app, tab, _, _ := newGoalDeliveryYoloTestApp(t, control.GoalStatusBlocked) |
| 487 | tab.turnStartMu.Lock() |
| 488 | done := make(chan error, 1) |
| 489 | started := make(chan struct{}) |
| 490 | go func() { |
| 491 | close(started) |
| 492 | done <- app.SetTokenModeForTab(tab.ID, boot.TokenModeDelivery) |
| 493 | }() |
| 494 | <-started |
| 495 | for app.runtimeRebuildMu.TryLock() { |
| 496 | app.runtimeRebuildMu.Unlock() |
| 497 | select { |
| 498 | case err := <-done: |
| 499 | t.Fatalf("token-mode switch returned before reaching the admission gate: %v", err) |
| 500 | default: |
| 501 | runtime.Gosched() |
| 502 | } |
| 503 | } |
| 504 | select { |
| 505 | case err := <-done: |
| 506 | t.Fatalf("token-mode switch crossed the turn-admission gate: %v", err) |
| 507 | default: |
| 508 | } |
| 509 | tab.turnStartMu.Unlock() |
| 510 | select { |
| 511 | case err := <-done: |
| 512 | if err != nil { |
| 513 | t.Fatalf("SetTokenModeForTab after admission release: %v", err) |
| 514 | } |
| 515 | case <-time.After(10 * time.Second): |
| 516 | t.Fatal("token-mode switch did not finish after turn admission released") |
| 517 | } |
| 518 | } |
| 519 |