cruise 0.1.30

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
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { render, screen, waitFor, cleanup } 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(),
  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(),
}));

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: "Awaiting Approval",
    configSource: "default.yaml",
    baseDir: "/home/user/project",
    input: "pending task",
    createdAt: "2026-01-01T00:00:00Z",
    workspaceMode: "Worktree",
    planAvailable: true,
    ...overrides,
  };
}

// ─── Awaiting Approval: Fix and Ask button visibility ────────────────────────

describe("App: Awaiting Approval — Fix and Ask button visibility", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(commands.listConfigs).mockResolvedValue([]);
    vi.mocked(commands.getSessionLog).mockResolvedValue("");
    vi.mocked(commands.getSessionPlan).mockResolvedValue("# The plan");
    vi.mocked(commands.listDirectory).mockResolvedValue([]);
    vi.mocked(commands.getUpdateReadiness).mockResolvedValue({ canAutoUpdate: true });
    vi.mocked(commands.cleanSessions).mockResolvedValue({ deleted: 0, skipped: 0 });
  });

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

  it("shows Fix and Ask buttons when planAvailable is true", async () => {
    // Given: an Awaiting Approval session with a plan
    const session = makeSession({ planAvailable: true });
    vi.mocked(commands.listSessions).mockResolvedValue([session]);

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

    // When: select the session
    await userEvent.click(screen.getByRole("button", { name: /pending task/ }));

    // Then: Fix and Ask are visible
    await waitFor(() => {
      expect(screen.getByRole("button", { name: "Fix" })).toBeInTheDocument();
      expect(screen.getByRole("button", { name: "Ask" })).toBeInTheDocument();
    });
  });

  it("hides Fix and Ask when planAvailable is false", async () => {
    // Given: Awaiting Approval session without a plan yet
    const session = makeSession({ planAvailable: false });
    vi.mocked(commands.listSessions).mockResolvedValue([session]);

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

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

    // Then: Fix and Ask are absent (nothing to review yet)
    await waitFor(() => {
      expect(screen.queryByRole("button", { name: "Fix" })).toBeNull();
      expect(screen.queryByRole("button", { name: "Ask" })).toBeNull();
    });
  });
});

// ─── Awaiting Approval: Ask flow ─────────────────────────────────────────────

describe("App: Awaiting Approval — Ask flow", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(commands.listConfigs).mockResolvedValue([]);
    vi.mocked(commands.getSessionLog).mockResolvedValue("");
    vi.mocked(commands.getSessionPlan).mockResolvedValue("# The plan");
    vi.mocked(commands.listDirectory).mockResolvedValue([]);
    vi.mocked(commands.getUpdateReadiness).mockResolvedValue({ canAutoUpdate: true });
    vi.mocked(commands.cleanSessions).mockResolvedValue({ deleted: 0, skipped: 0 });
  });

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

  async function selectAwaitingApprovalSession(): Promise<void> {
    const session = makeSession({ planAvailable: true });
    vi.mocked(commands.listSessions).mockResolvedValue([session]);
    render(<App />);
    await waitFor(() => screen.getByText("pending task"));
    await userEvent.click(screen.getByRole("button", { name: /pending task/ }));
    await waitFor(() => screen.getByRole("button", { name: "Ask" }));
  }

  it("shows question input when Ask is clicked", async () => {
    // Given: an Awaiting Approval session is selected
    await selectAwaitingApprovalSession();

    // When: click Ask
    await userEvent.click(screen.getByRole("button", { name: "Ask" }));

    // Then: question textarea is visible
    expect(
      screen.getByPlaceholderText("Ask a question about the plan…")
    ).toBeInTheDocument();
  });

  it("calls askSession with session ID and question, then displays the answer", async () => {
    // Given: askSession is ready to return an answer
    vi.mocked(commands.askSession).mockResolvedValue("The plan uses approach X because of Y.");
    await selectAwaitingApprovalSession();

    // When: ask, type question, and submit
    await userEvent.click(screen.getByRole("button", { name: "Ask" }));
    await userEvent.type(
      screen.getByPlaceholderText("Ask a question about the plan…"),
      "Why approach X?"
    );
    await userEvent.click(screen.getByRole("button", { name: "Submit" }));

    // Then: askSession is called with correct args
    await waitFor(() => {
      expect(commands.askSession).toHaveBeenCalledWith("session-1", "Why approach X?");
    });

    // And: the answer is displayed on the Plan tab
    await waitFor(() => {
      expect(
        screen.getByText("The plan uses approach X because of Y.")
      ).toBeInTheDocument();
    });
  });

  it("shows action buttons again after receiving an Ask answer", async () => {
    // Given: an Ask has been answered
    vi.mocked(commands.askSession).mockResolvedValue("Some answer.");
    await selectAwaitingApprovalSession();

    await userEvent.click(screen.getByRole("button", { name: "Ask" }));
    await userEvent.type(
      screen.getByPlaceholderText("Ask a question about the plan…"),
      "Question?"
    );
    await userEvent.click(screen.getByRole("button", { name: "Submit" }));
    await waitFor(() => screen.getByText("Some answer."));

    // Then: Approve, Fix, Ask, and Delete are all still accessible
    expect(screen.getByRole("button", { name: "Approve" })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Fix" })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Ask" })).toBeInTheDocument();
  });

  it("clears the Ask answer when a different session is selected", async () => {
    // Given: two sessions; A is Awaiting Approval with a plan
    const sessA = makeSession({ id: "sess-a", input: "task A", planAvailable: true });
    const sessB = makeSession({
      id: "sess-b",
      input: "task B",
      phase: "Planned",
      planAvailable: false,
    });
    vi.mocked(commands.listSessions).mockResolvedValue([sessA, sessB]);
    vi.mocked(commands.askSession).mockResolvedValue("The answer.");

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

    // Select A, get an Ask answer
    await userEvent.click(screen.getByRole("button", { name: /task A/ }));
    await waitFor(() => screen.getByRole("button", { name: "Ask" }));
    await userEvent.click(screen.getByRole("button", { name: "Ask" }));
    await userEvent.type(
      screen.getByPlaceholderText("Ask a question about the plan…"),
      "Question?"
    );
    await userEvent.click(screen.getByRole("button", { name: "Submit" }));
    await waitFor(() => screen.getByText("The answer."));

    // When: navigate to session B, then back to A
    await userEvent.click(screen.getByRole("button", { name: /task B/ }));
    await userEvent.click(screen.getByRole("button", { name: /task A/ }));

    // Then: the stale answer is gone
    expect(screen.queryByText("The answer.")).toBeNull();
  });

  it("shows error and keeps question editor open when Ask fails", async () => {
    // Given: askSession rejects
    vi.mocked(commands.askSession).mockRejectedValue(new Error("Ask failed"));
    await selectAwaitingApprovalSession();

    await userEvent.click(screen.getByRole("button", { name: "Ask" }));
    await userEvent.type(
      screen.getByPlaceholderText("Ask a question about the plan…"),
      "A question"
    );
    await userEvent.click(screen.getByRole("button", { name: "Submit" }));

    // Then: an error is visible
    await waitFor(() => {
      expect(screen.getByText(/Ask failed/)).toBeInTheDocument();
    });

    // And: the question editor remains open so the user can retry
    expect(
      screen.getByPlaceholderText("Ask a question about the plan…")
    ).toBeInTheDocument();
  });

  it("collapses the question editor without clearing ask answer when Cancel is clicked", async () => {
    // Given: Ask editor is open (no pending ask yet)
    await selectAwaitingApprovalSession();
    await userEvent.click(screen.getByRole("button", { name: "Ask" }));
    expect(
      screen.getByPlaceholderText("Ask a question about the plan…")
    ).toBeInTheDocument();

    // When: cancel
    await userEvent.click(screen.getByRole("button", { name: "Cancel" }));

    // Then: editor is hidden and primary actions are visible again
    expect(
      screen.queryByPlaceholderText("Ask a question about the plan…")
    ).toBeNull();
    expect(screen.getByRole("button", { name: "Approve" })).toBeInTheDocument();
  });
});

// ─── Awaiting Approval: Fix flow ─────────────────────────────────────────────

describe("App: Awaiting Approval — Fix flow", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(commands.listConfigs).mockResolvedValue([]);
    vi.mocked(commands.getSessionLog).mockResolvedValue("");
    vi.mocked(commands.getSessionPlan).mockResolvedValue("# The plan");
    vi.mocked(commands.listDirectory).mockResolvedValue([]);
    vi.mocked(commands.getUpdateReadiness).mockResolvedValue({ canAutoUpdate: true });
    vi.mocked(commands.cleanSessions).mockResolvedValue({ deleted: 0, skipped: 0 });
  });

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

  async function selectAwaitingApprovalSession(): Promise<void> {
    const session = makeSession({ planAvailable: true });
    vi.mocked(commands.listSessions).mockResolvedValue([session]);
    render(<App />);
    await waitFor(() => screen.getByText("pending task"));
    await userEvent.click(screen.getByRole("button", { name: /pending task/ }));
    await waitFor(() => screen.getByRole("button", { name: "Fix" }));
  }

  it("shows fix feedback editor when Fix is clicked", async () => {
    // Given: an Awaiting Approval session is selected
    await selectAwaitingApprovalSession();

    // When: click Fix
    await userEvent.click(screen.getByRole("button", { name: "Fix" }));

    // Then: fix feedback editor is visible
    expect(
      screen.getByPlaceholderText("Describe the changes needed…")
    ).toBeInTheDocument();
  });

  it("calls fixSession with feedback and updates plan content on success", async () => {
    // Given: fixSession streams planGenerated and returns updated content
    vi.mocked(commands.fixSession).mockImplementationOnce(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      async (_params: any, channel: any) => {
        channel.onmessage?.({
          event: "planGenerated",
          data: { content: "# Revised plan" },
        });
        return "# Revised plan";
      }
    );
    vi.mocked(commands.getSession).mockResolvedValue(
      makeSession({ planAvailable: true })
    );
    await selectAwaitingApprovalSession();

    // When: click Fix, type feedback, apply
    await userEvent.click(screen.getByRole("button", { name: "Fix" }));
    await userEvent.type(
      screen.getByPlaceholderText("Describe the changes needed…"),
      "Remove step 3"
    );
    await userEvent.click(screen.getByRole("button", { name: "Apply" }));

    // Then: fixSession is called with the session ID and feedback
    await waitFor(() => {
      expect(commands.fixSession).toHaveBeenCalledWith(
        expect.objectContaining({ sessionId: "session-1", feedback: "Remove step 3" }),
        expect.anything()
      );
    });
  });

  it("clears stale Ask answer when Fix succeeds", async () => {
    // Given: an Ask answer is displayed, then Fix is triggered
    vi.mocked(commands.askSession).mockResolvedValue("Old ask answer.");
    vi.mocked(commands.fixSession).mockImplementationOnce(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      async (_params: any, channel: any) => {
        channel.onmessage?.({
          event: "planGenerated",
          data: { content: "# Revised plan" },
        });
        return "# Revised plan";
      }
    );
    vi.mocked(commands.getSession).mockResolvedValue(
      makeSession({ planAvailable: true })
    );

    const session = makeSession({ planAvailable: true });
    vi.mocked(commands.listSessions).mockResolvedValue([session]);
    render(<App />);
    await waitFor(() => screen.getByText("pending task"));
    await userEvent.click(screen.getByRole("button", { name: /pending task/ }));
    await waitFor(() => screen.getByRole("button", { name: "Ask" }));

    // Get an Ask answer
    await userEvent.click(screen.getByRole("button", { name: "Ask" }));
    await userEvent.type(
      screen.getByPlaceholderText("Ask a question about the plan…"),
      "Question?"
    );
    await userEvent.click(screen.getByRole("button", { name: "Submit" }));
    await waitFor(() => screen.getByText("Old ask answer."));

    // When: Fix succeeds
    await userEvent.click(screen.getByRole("button", { name: "Fix" }));
    await userEvent.type(
      screen.getByPlaceholderText("Describe the changes needed…"),
      "Revise"
    );
    await userEvent.click(screen.getByRole("button", { name: "Apply" }));

    // Then: the stale Ask answer is cleared
    await waitFor(() => {
      expect(screen.queryByText("Old ask answer.")).toBeNull();
    });
  });

  it("shows error and keeps fix editor open when Fix fails", async () => {
    // Given: fixSession rejects
    vi.mocked(commands.fixSession).mockRejectedValue(new Error("Fix failed"));
    await selectAwaitingApprovalSession();

    await userEvent.click(screen.getByRole("button", { name: "Fix" }));
    await userEvent.type(
      screen.getByPlaceholderText("Describe the changes needed…"),
      "Do something"
    );
    await userEvent.click(screen.getByRole("button", { name: "Apply" }));

    // Then: an error is visible
    await waitFor(() => {
      expect(screen.getByText(/Fix failed/)).toBeInTheDocument();
    });

    // And: the fix editor remains open so the user can retry
    expect(
      screen.getByPlaceholderText("Describe the changes needed…")
    ).toBeInTheDocument();
  });

  it("refreshes session state after Fix so the sidebar reflects the updated plan", async () => {
    // Given: fixSession succeeds
    vi.mocked(commands.fixSession).mockImplementationOnce(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      async (_params: any, channel: any) => {
        channel.onmessage?.({
          event: "planGenerated",
          data: { content: "# Updated plan" },
        });
        return "# Updated plan";
      }
    );
    const refreshedSession = makeSession({ planAvailable: true, updatedAt: "2026-01-02T00:00:00Z" });
    vi.mocked(commands.getSession).mockResolvedValue(refreshedSession);

    await selectAwaitingApprovalSession();

    await userEvent.click(screen.getByRole("button", { name: "Fix" }));
    await userEvent.type(
      screen.getByPlaceholderText("Describe the changes needed…"),
      "Revise"
    );
    await userEvent.click(screen.getByRole("button", { name: "Apply" }));

    // Then: getSession is called to refresh the session DTO (updates title/updatedAt)
    await waitFor(() => {
      expect(commands.getSession).toHaveBeenCalledWith("session-1");
    });
  });
});