cruise 0.1.34

YAML-driven coding agent workflow orchestrator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { render, screen, waitFor, cleanup, act } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App from "../App";
import type { Session } from "../types";
import * as commands from "../lib/commands";

// ─── Module mocks ──────────────────────────────────────────────────────────────

vi.mock("@tauri-apps/api/app", () => ({
  getVersion: vi.fn().mockResolvedValue("0.0.0"),
}));

vi.mock("@tauri-apps/api/core", () => ({
  Channel: class {
    onmessage: ((event: unknown) => void) | null = null;
  },
}));

vi.mock("@tauri-apps/plugin-opener", () => ({
  openUrl: vi.fn(),
}));

vi.mock("@tauri-apps/plugin-dialog", () => ({
  open: vi.fn(),
}));

vi.mock("../lib/commands", () => ({
  listSessions: vi.fn(),
  listConfigs: vi.fn(),
  createSession: vi.fn(),
  approveSession: vi.fn(),
  discardSession: vi.fn(),
  getSession: vi.fn(),
  getSessionLog: vi.fn(),
  getSessionPlan: vi.fn(),
  getConfigSteps: vi.fn().mockResolvedValue([]),
  listDirectory: vi.fn(),
  getUpdateReadiness: vi.fn(),
  cleanSessions: vi.fn(),
  deleteSession: vi.fn(),
  runSession: vi.fn(),
  cancelSession: vi.fn(),
  resetSession: vi.fn(),
  respondToOption: vi.fn(),
  runAllSessions: vi.fn(),
  fixSession: vi.fn(),
  askSession: vi.fn(),
  getAppConfig: vi.fn(),
  updateAppConfig: vi.fn(),
}));

vi.mock("../lib/updater", () => ({
  checkForUpdate: vi.fn().mockResolvedValue(null),
  downloadAndInstall: vi.fn(),
}));

vi.mock("../lib/desktopNotifications", () => ({
  notifyDesktop: vi.fn(),
}));

// ─── Helpers ──────────────────────────────────────────────────────────────────

function makeSession(overrides: Partial<Session> = {}): Session {
  return {
    id: "session-1",
    phase: "Planned",
    configSource: "default.yaml",
    baseDir: "/home/user/project",
    input: "test task",
    createdAt: "2026-01-01T00:00:00Z",
    workspaceMode: "Worktree",
    ...overrides,
  };
}

/**
 * Set up runAllSessions mock to capture the channel so tests can emit events.
 * Returns control handles for firing each event at an explicit moment.
 */
function setupRunAllMock() {
  let capturedChannel: { onmessage: ((event: unknown) => void) | null } | null = null;
  let resolveRunAll!: () => void;
  let capturedTotal = 0;

  vi.mocked(commands.runAllSessions).mockImplementationOnce(
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (channel: any) => {
      capturedChannel = channel;
      return new Promise<void>((resolve) => {
        resolveRunAll = resolve;
      });
    },
  );

  return {
    /** Emit runAllStarted event. */
    emitStarted(total: number): void {
      capturedTotal = total;
      capturedChannel!.onmessage?.({
        event: "runAllStarted",
        data: { total },
      });
    },
    /** Emit runAllSessionStarted event. */
    emitSessionStarted(sessionId: string, input: string, total?: number): void {
      capturedChannel!.onmessage?.({
        event: "runAllSessionStarted",
        data: { sessionId, input, total: total ?? capturedTotal },
      });
    },
    /** Emit runAllSessionFinished event. */
    emitSessionFinished(
      sessionId: string,
      input: string,
      phase: Session["phase"],
      error?: string,
    ): void {
      capturedChannel!.onmessage?.({
        event: "runAllSessionFinished",
        data: { sessionId, input, phase, error },
      });
    },
    /** Emit runAllCompleted event. */
    emitCompleted(cancelled = 0): void {
      capturedChannel!.onmessage?.({
        event: "runAllCompleted",
        data: { cancelled },
      });
      resolveRunAll();
    },
  };
}

// ─── Tests ────────────────────────────────────────────────────────────────────

describe("App: Run All re-entry and state persistence", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(commands.listSessions).mockResolvedValue([]);
    vi.mocked(commands.listConfigs).mockResolvedValue([]);
    vi.mocked(commands.getSessionLog).mockResolvedValue("");
    vi.mocked(commands.getSessionPlan).mockResolvedValue("");
    vi.mocked(commands.listDirectory).mockResolvedValue([]);
    vi.mocked(commands.getUpdateReadiness).mockResolvedValue({ canAutoUpdate: true });
    vi.mocked(commands.cleanSessions).mockResolvedValue({ deleted: 0, skipped: 0 });
    vi.mocked(commands.getAppConfig).mockResolvedValue({ runAllParallelism: 1 });
    vi.mocked(commands.updateAppConfig).mockResolvedValue();
  });

  afterEach(() => {
    cleanup();
  });

  // --- Re-entry: navigation away and back ---

  it("returns to the same Run All progress view after navigating to a session", async () => {
    // Given: two planned sessions
    const sess1 = makeSession({ id: "s1", input: "task one" });
    const sess2 = makeSession({ id: "s2", input: "task two" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1, sess2]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    // Start Run All
    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(2);
    });
    await act(async () => {
      control.emitSessionStarted("s1", "task one");
    });

    // Verify Run All is showing progress
    await waitFor(() => {
      expect(screen.getByText(/1 \/ 2 sessions/)).toBeInTheDocument();
    });

    // When: navigate to session detail
    await userEvent.click(screen.getByRole("button", { name: /task one/ }));

    // Then: Run All view is hidden (session detail is shown)
    await waitFor(() => {
      expect(screen.queryByText(/1 \/ 2 sessions/)).toBeNull();
    });

    // When: click Run All again (re-entry)
    await userEvent.click(screen.getByText("Run All"));

    // Then: progress is restored without a new runAllSessions call
    await waitFor(() => {
      expect(screen.getByText(/1 \/ 2 sessions/)).toBeInTheDocument();
    });
    // runAllSessions should still only have been called once
    expect(commands.runAllSessions).toHaveBeenCalledOnce();
  });

  it("does not call runAllSessions again on re-entry", async () => {
    // Given
    const sess1 = makeSession({ id: "s1", input: "task one" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    // Start Run All
    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(1);
      control.emitSessionStarted("s1", "task one");
    });

    // Navigate away then come back
    await userEvent.click(screen.getByRole("button", { name: /task one/ }));
    await userEvent.click(screen.getByText("Run All"));

    // Then: still only one call
    expect(commands.runAllSessions).toHaveBeenCalledOnce();
  });

  // --- State persistence through navigation ---

  it("preserves results when navigating away and back during execution", async () => {
    // Given
    const sess1 = makeSession({ id: "s1", input: "task one" });
    const sess2 = makeSession({ id: "s2", input: "task two" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1, sess2]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(2);
      control.emitSessionStarted("s1", "task one");
      control.emitSessionFinished("s1", "task one", "Completed");
      control.emitSessionStarted("s2", "task two");
    });

    // Navigate away
    await userEvent.click(screen.getByRole("button", { name: /task one/ }));

    // Navigate back
    await userEvent.click(screen.getByText("Run All"));

    // Then: previously completed result is still visible
    await waitFor(() => {
      expect(screen.getByText(/2 \/ 2 sessions/)).toBeInTheDocument();
    });
    // The completed session result should be listed
    expect(screen.getAllByText("task one").length).toBeGreaterThan(0);
  });

  // --- Run All active button state ---

  it("shows Run All button as active (colored) during execution", async () => {
    // Given
    const sess1 = makeSession({ id: "s1", input: "task one" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    // When: Run All is started
    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(1);
    });

    // Then: Run All button has active styling
    const runAllButtons = screen.getAllByRole("button").filter(
      (btn) => btn.textContent === "Run All",
    );
    // The sidebar Run All button should have active styling
    const sidebarButton = runAllButtons[0];
    expect(sidebarButton.className).toContain("bg-blue-600");
  });

  it("enables Run All button during execution even when sessions transition to Running", async () => {
    // Given: sessions start as Planned, transition to Running after Run All starts
    const sess1 = makeSession({ id: "s1", input: "task one", phase: "Planned" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(1);
    });

    // When: sessions transition to Running (listSessions returns Running state)
    vi.mocked(commands.listSessions).mockResolvedValue([
      makeSession({ id: "s1", input: "task one", phase: "Running" }),
    ]);

    // Then: Run All button is still enabled (active state)
    // When runAllActive=true, aria-label becomes "View running sessions"
    const runAllButton = screen.getByRole("button", { name: "View running sessions" });
    expect(runAllButton).not.toBeDisabled();
  });

  // --- Completion and Done ---

  it("can return to completed results view after navigating away", async () => {
    // Given
    const sess1 = makeSession({ id: "s1", input: "task one" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(1);
      control.emitSessionStarted("s1", "task one");
      control.emitSessionFinished("s1", "task one", "Completed");
      control.emitCompleted();
    });

    // Navigate away
    await userEvent.click(screen.getByRole("button", { name: /task one/ }));
    await waitFor(() => {
      expect(screen.queryByText("Done")).toBeNull();
    });

    // When: click Run All to return to results
    await userEvent.click(screen.getByText("Run All"));

    // Then: completed results are shown and Done button is available
    await waitFor(() => {
      expect(screen.getByText("Done")).toBeInTheDocument();
    });
    expect(screen.getAllByText("Completed").length).toBeGreaterThan(0);
  });

  it("clears Run All state when Done is clicked, allowing fresh start", async () => {
    // Given: Run All has completed
    const sess1 = makeSession({ id: "s1", input: "task one" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(1);
      control.emitSessionStarted("s1", "task one");
      control.emitSessionFinished("s1", "task one", "Completed");
      control.emitCompleted();
    });

    // When: click Done
    await userEvent.click(screen.getByText("Done"));

    // Then: view returns to session list
    await waitFor(() => {
      expect(screen.queryByText("Done")).toBeNull();
    });

    // When: click Run All again — should start a new execution
    setupRunAllMock();
    await userEvent.click(screen.getByText("Run All"));

    // Then: runAllSessions is called again (second time)
    await waitFor(() => {
      expect(commands.runAllSessions).toHaveBeenCalledTimes(2);
    });
  });

  // --- Error handling ---

  it("shows error state and preserves it on re-entry", async () => {
    // Given
    const sess1 = makeSession({ id: "s1", input: "task one" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1]);

    vi.mocked(commands.runAllSessions).mockImplementationOnce(
      () => Promise.reject(new Error("backend crashed")),
    );

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    await userEvent.click(screen.getByText("Run All"));

    // Then: error is shown
    await waitFor(() => {
      expect(screen.getByText("Error")).toBeInTheDocument();
    });

    // Navigate away and come back
    await userEvent.click(screen.getByRole("button", { name: /task one/ }));
    await userEvent.click(screen.getByText("Run All"));

    // Then: error state is preserved on re-entry
    await waitFor(() => {
      expect(screen.getByText("Error")).toBeInTheDocument();
    });
  });

  // --- Cancelled state ---

  it("shows cancelled state and preserves it on re-entry", async () => {
    // Given
    const sess1 = makeSession({ id: "s1", input: "task one" });
    const sess2 = makeSession({ id: "s2", input: "task two" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1, sess2]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => screen.getByText("task one"));

    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(2);
      control.emitSessionStarted("s1", "task one");
      control.emitSessionFinished("s1", "task one", "Completed");
      // 1 cancelled
      control.emitCompleted(1);
    });

    // Then: cancelled state is shown
    await waitFor(() => {
      expect(screen.getByText("Cancelled")).toBeInTheDocument();
    });

    // Navigate away and back
    await userEvent.click(screen.getByRole("button", { name: /task one/ }));
    await userEvent.click(screen.getByText("Run All"));

    // Then: cancelled state persists
    await waitFor(() => {
      expect(screen.getByText("Cancelled")).toBeInTheDocument();
    });
  });

  // --- Sidebar refresh on Done ---

  it("triggers sidebar refresh when Done is clicked after completion", async () => {
    // Given
    const sess1 = makeSession({ id: "s1", input: "task one" });
    vi.mocked(commands.listSessions).mockResolvedValue([sess1]);
    const control = setupRunAllMock();

    render(<App />);
    await waitFor(() => {
      expect(commands.listSessions).toHaveBeenCalled();
    });

    const callsBefore = vi.mocked(commands.listSessions).mock.calls.length;

    await userEvent.click(screen.getByText("Run All"));
    await act(async () => {
      control.emitStarted(1);
      control.emitSessionStarted("s1", "task one");
      control.emitSessionFinished("s1", "task one", "Completed");
      control.emitCompleted();
    });

    // When: click Done
    await userEvent.click(screen.getByText("Done"));

    // Then: sidebar refreshes (listSessions called again)
    await waitFor(() => {
      expect(vi.mocked(commands.listSessions).mock.calls.length).toBeGreaterThan(callsBefore);
    });
  });
});