cruise 0.1.49

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
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { render, screen, act, cleanup, fireEvent } from "@testing-library/react";
import { useState } from "react";
import { DirectoryPicker } from "../components/DirectoryPicker";
import * as commands from "../lib/commands";
import type { DirEntry } from "../types";

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

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

// Controlled wrapper so that fireEvent.change drives value through the real
// onChange path (not just a direct prop update from the parent).
function Controlled({ initialValue }: { initialValue: string }) {
  const [value, setValue] = useState(initialValue);
  return <DirectoryPicker value={value} onChange={setValue} />;
}

function makeEntries(names: string[], parentDir: string): DirEntry[] {
  return names.map((name) => ({ name, path: `${parentDir}${name}` }));
}

// Flush all fake timers AND pending microtasks (Promise callbacks) in one go.
async function flushAll() {
  await act(async () => {
    await vi.runAllTimersAsync();
  });
}

describe("DirectoryPicker", () => {
  beforeEach(() => {
    vi.useFakeTimers();
    vi.clearAllMocks();
  });

  afterEach(() => {
    cleanup();
    vi.useRealTimers();
  });

  // ---------------------------------------------------------------------------
  // IPC call / cache behaviour
  // ---------------------------------------------------------------------------

  describe("IPC call behaviour", () => {
    it("calls listDirectory after the debounce when a value is set", async () => {
      // Given
      vi.mocked(commands.listDirectory).mockResolvedValue([]);
      render(<DirectoryPicker value="/Users/" onChange={vi.fn()} />);

      // When: debounce fires
      await flushAll();

      // Then
      expect(commands.listDirectory).toHaveBeenCalledWith("/Users/");
    });

    it("does NOT call listDirectory when value is empty", async () => {
      // Given / When
      render(<DirectoryPicker value="" onChange={vi.fn()} />);
      await flushAll();

      // Then
      expect(commands.listDirectory).not.toHaveBeenCalled();
    });

    it("makes NO additional IPC call when the user types multiple characters within the same parent directory", async () => {
      // Given: initial fetch for /Users/takumi/ fills the cache
      const entries = makeEntries(["apps", "projects"], "/Users/takumi/");
      vi.mocked(commands.listDirectory).mockResolvedValue(entries);
      render(<Controlled initialValue="/Users/takumi/" />);
      await flushAll();
      expect(commands.listDirectory).toHaveBeenCalledTimes(1);

      const input = screen.getByRole("combobox");

      // When: two more keystrokes, all within /Users/takumi/
      fireEvent.change(input, { target: { value: "/Users/takumi/a" } });
      await flushAll();

      fireEvent.change(input, { target: { value: "/Users/takumi/ap" } });
      await flushAll();

      // Then: still only one IPC call total — cache was preserved across all keystrokes
      expect(commands.listDirectory).toHaveBeenCalledTimes(1);
    });

    it("makes a NEW IPC call when the user navigates to a different parent directory", async () => {
      // Given: cache populated for /Users/takumi/
      const first = makeEntries(["apps", "projects"], "/Users/takumi/");
      const second = makeEntries(["cruise"], "/Users/takumi/apps/");
      vi.mocked(commands.listDirectory)
        .mockResolvedValueOnce(first)
        .mockResolvedValueOnce(second);

      const { rerender } = render(
        <DirectoryPicker value="/Users/takumi/" onChange={vi.fn()} />
      );
      await flushAll();
      expect(commands.listDirectory).toHaveBeenCalledTimes(1);

      // When: value moves to a new parent directory
      rerender(<DirectoryPicker value="/Users/takumi/apps/" onChange={vi.fn()} />);
      await flushAll();

      // Then: a second IPC call for the new directory
      expect(commands.listDirectory).toHaveBeenCalledTimes(2);
      expect(commands.listDirectory).toHaveBeenNthCalledWith(2, "/Users/takumi/apps/");
    });
  });

  // ---------------------------------------------------------------------------
  // Dropdown visibility and entry filtering
  // ---------------------------------------------------------------------------

  describe("dropdown visibility", () => {
    it("opens the listbox when IPC returns entries", async () => {
      // Given
      const entries = makeEntries(["takumi", "shared"], "/Users/");
      vi.mocked(commands.listDirectory).mockResolvedValue(entries);
      render(<DirectoryPicker value="/Users/" onChange={vi.fn()} />);

      // When
      await flushAll();

      // Then
      expect(screen.getByRole("listbox")).toBeInTheDocument();
      expect(screen.getByText("takumi/")).toBeInTheDocument();
      expect(screen.getByText("shared/")).toBeInTheDocument();
    });

    it("filters displayed entries by the typed prefix using the cache", async () => {
      // Given: cache populated with apps, projects, Documents
      const entries = makeEntries(["apps", "projects", "Documents"], "/Users/takumi/");
      vi.mocked(commands.listDirectory).mockResolvedValue(entries);
      const { rerender } = render(
        <DirectoryPicker value="/Users/takumi/" onChange={vi.fn()} />
      );
      await flushAll();
      expect(screen.getByRole("listbox")).toBeInTheDocument();

      // When: value changes to "/Users/takumi/a" — same parent dir, prefix "a"
      rerender(<DirectoryPicker value="/Users/takumi/a" onChange={vi.fn()} />);
      await flushAll();

      // Then: only "apps/" is visible (prefix filter applied from cache)
      expect(screen.getByText("apps/")).toBeInTheDocument();
      expect(screen.queryByText("projects/")).not.toBeInTheDocument();
      expect(screen.queryByText("Documents/")).not.toBeInTheDocument();
    });

    it("closes the listbox when IPC returns an empty array", async () => {
      // Given
      vi.mocked(commands.listDirectory).mockResolvedValue([]);
      render(<DirectoryPicker value="/nonexistent/" onChange={vi.fn()} />);

      // When
      await flushAll();

      // Then
      expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
    });

    it("closes the listbox when IPC throws an error", async () => {
      // Given
      vi.mocked(commands.listDirectory).mockRejectedValue(new Error("permission denied"));
      render(<DirectoryPicker value="/root/" onChange={vi.fn()} />);

      // When
      await flushAll();

      // Then
      expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
    });
  });

  // ---------------------------------------------------------------------------
  // Drill-in behaviour (exact match on typed prefix)
  // ---------------------------------------------------------------------------

  describe("drill-in behaviour", () => {
    it("drills into subdirectories when the typed prefix exactly matches an entry name", async () => {
      // Given: parent directory /Users/takumi/ has apps and projects
      const parentEntries = makeEntries(["apps", "projects"], "/Users/takumi/");
      const childEntries = makeEntries(["cruise", "worktrees"], "/Users/takumi/apps/");
      vi.mocked(commands.listDirectory)
        .mockResolvedValueOnce(parentEntries)
        .mockResolvedValueOnce(childEntries);

      render(<Controlled initialValue="/Users/takumi/" />);
      await flushAll();
      expect(commands.listDirectory).toHaveBeenCalledTimes(1);

      // When: user types "/Users/takumi/apps" (exact match, no trailing slash)
      const input = screen.getByRole("combobox");
      fireEvent.change(input, { target: { value: "/Users/takumi/apps" } });
      await flushAll();

      // Then: second IPC call was made for the drill-in directory
      expect(commands.listDirectory).toHaveBeenCalledTimes(2);
      expect(commands.listDirectory).toHaveBeenNthCalledWith(2, "/Users/takumi/apps");

      // Then: dropdown shows children of apps/ (not the parent entries)
      expect(screen.getByText("cruise/")).toBeInTheDocument();
      expect(screen.getByText("worktrees/")).toBeInTheDocument();
      expect(screen.queryByText("apps/")).not.toBeInTheDocument();
      expect(screen.queryByText("projects/")).not.toBeInTheDocument();
    });

    it("does NOT drill into subdirectories when the typed prefix is only a partial match", async () => {
      // Given: parent directory /Users/takumi/ has apps and projects
      const parentEntries = makeEntries(["apps", "projects"], "/Users/takumi/");
      vi.mocked(commands.listDirectory).mockResolvedValue(parentEntries);

      render(<Controlled initialValue="/Users/takumi/" />);
      await flushAll();
      expect(commands.listDirectory).toHaveBeenCalledTimes(1);

      // When: user types "/Users/takumi/app" (partial match, not exact)
      const input = screen.getByRole("combobox");
      fireEvent.change(input, { target: { value: "/Users/takumi/app" } });
      await flushAll();

      // Then: no additional IPC call — drill-in is not triggered
      expect(commands.listDirectory).toHaveBeenCalledTimes(1);

      // Then: dropdown shows the prefix-filtered result (only apps/)
      expect(screen.getByText("apps/")).toBeInTheDocument();
      expect(screen.queryByText("projects/")).not.toBeInTheDocument();
    });

    it("shows the matched entry itself when drill-in target has no subdirectories", async () => {
      // Given: parent has apps, but apps/ is empty
      const parentEntries = makeEntries(["apps", "projects"], "/Users/takumi/");
      vi.mocked(commands.listDirectory)
        .mockResolvedValueOnce(parentEntries)
        .mockResolvedValueOnce([]);

      render(<Controlled initialValue="/Users/takumi/" />);
      await flushAll();

      // When: user types exact match that leads to empty directory
      const input = screen.getByRole("combobox");
      fireEvent.change(input, { target: { value: "/Users/takumi/apps" } });
      await flushAll();

      // Then: second IPC call was made for the drill-in
      expect(commands.listDirectory).toHaveBeenCalledTimes(2);
      expect(commands.listDirectory).toHaveBeenNthCalledWith(2, "/Users/takumi/apps");

      // Then: fallback shows the matched entry itself (empty dir, dropdown stays open)
      expect(screen.getByText("apps/")).toBeInTheDocument();
      expect(screen.queryByText("projects/")).not.toBeInTheDocument();
    });

    it("caches the drill-in result so subsequent typing within that directory uses the cache", async () => {
      // Given: drill-in happened, cache is set to /Users/takumi/apps/
      const parentEntries = makeEntries(["apps"], "/Users/takumi/");
      const childEntries = makeEntries(["cruise"], "/Users/takumi/apps/");
      vi.mocked(commands.listDirectory)
        .mockResolvedValueOnce(parentEntries)
        .mockResolvedValueOnce(childEntries);

      render(<Controlled initialValue="/Users/takumi/" />);
      await flushAll();

      const input = screen.getByRole("combobox");
      fireEvent.change(input, { target: { value: "/Users/takumi/apps" } });
      await flushAll();
      expect(commands.listDirectory).toHaveBeenCalledTimes(2);
      expect(screen.getByText("cruise/")).toBeInTheDocument();

      // When: user types more characters within the drilled directory
      fireEvent.change(input, { target: { value: "/Users/takumi/apps/x" } });
      await flushAll();

      // Then: no additional IPC call — cache was preserved with normalized key
      expect(commands.listDirectory).toHaveBeenCalledTimes(2);
    });
  });

  // ---------------------------------------------------------------------------
  // Keyboard navigation
  // ---------------------------------------------------------------------------

  describe("keyboard navigation", () => {
    async function renderOpenDropdown() {
      const entries = makeEntries(["apps", "projects", "Documents"], "/Users/takumi/");
      vi.mocked(commands.listDirectory).mockResolvedValue(entries);
      const onChange = vi.fn();
      render(<DirectoryPicker value="/Users/takumi/" onChange={onChange} />);
      await flushAll();
      expect(screen.getByRole("listbox")).toBeInTheDocument();
      return { onChange };
    }

    it("closes the listbox on Escape", async () => {
      // Given: listbox is open
      await renderOpenDropdown();

      // When
      fireEvent.keyDown(screen.getByRole("combobox"), { key: "Escape" });

      // Then
      expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
    });

    it("highlights the first option on ArrowDown", async () => {
      // Given: listbox is open, nothing highlighted (highlighted = -1)
      await renderOpenDropdown();

      // When
      fireEvent.keyDown(screen.getByRole("combobox"), { key: "ArrowDown" });

      // Then: first option is aria-selected
      const options = screen.getAllByRole("option");
      expect(options[0]).toHaveAttribute("aria-selected", "true");
      expect(options[1]).toHaveAttribute("aria-selected", "false");
    });

    it("moves the highlight back up on ArrowUp", async () => {
      // Given: second item is highlighted
      await renderOpenDropdown();
      const input = screen.getByRole("combobox");
      fireEvent.keyDown(input, { key: "ArrowDown" });
      fireEvent.keyDown(input, { key: "ArrowDown" });

      // When
      fireEvent.keyDown(input, { key: "ArrowUp" });

      // Then: back to first item
      const options = screen.getAllByRole("option");
      expect(options[0]).toHaveAttribute("aria-selected", "true");
      expect(options[1]).toHaveAttribute("aria-selected", "false");
    });

    it("calls onChange with the selected path and closes the listbox on Enter", async () => {
      // Given: first item is highlighted
      const { onChange } = await renderOpenDropdown();
      fireEvent.keyDown(screen.getByRole("combobox"), { key: "ArrowDown" });

      // When
      fireEvent.keyDown(screen.getByRole("combobox"), { key: "Enter" });

      // Then
      expect(onChange).toHaveBeenCalledWith("/Users/takumi/apps/");
      expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
    });

    it("does not call onChange or close the listbox when Enter is pressed with no highlight", async () => {
      // Given: listbox is open, nothing highlighted
      const { onChange } = await renderOpenDropdown();

      // When: Enter pressed without selecting anything
      fireEvent.keyDown(screen.getByRole("combobox"), { key: "Enter" });

      // Then: no selection
      expect(onChange).not.toHaveBeenCalled();
      expect(screen.getByRole("listbox")).toBeInTheDocument();
    });
  });

  // ---------------------------------------------------------------------------
  // Cache reset after entry selection (selectEntry)
  // ---------------------------------------------------------------------------

  describe("cache reset after selectEntry", () => {
    it("calls onChange with path + '/' and closes the listbox when an entry is clicked", async () => {
      // Given
      vi.mocked(commands.listDirectory).mockResolvedValue(
        makeEntries(["apps"], "/Users/takumi/")
      );
      const onChange = vi.fn();
      render(<DirectoryPicker value="/Users/takumi/" onChange={onChange} />);
      await flushAll();
      expect(screen.getByRole("listbox")).toBeInTheDocument();

      // When: user clicks the "apps/" option
      fireEvent.mouseDown(screen.getByText("apps/"));

      // Then
      expect(onChange).toHaveBeenCalledWith("/Users/takumi/apps/");
      expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
    });

    it("resets the cache after selection so the new directory is fetched on the next debounce", async () => {
      // Given: cache warm for /Users/takumi/ with the Controlled wrapper
      const firstEntries = makeEntries(["apps"], "/Users/takumi/");
      vi.mocked(commands.listDirectory).mockResolvedValue(firstEntries);

      render(<Controlled initialValue="/Users/takumi/" />);
      await flushAll();
      expect(commands.listDirectory).toHaveBeenCalledTimes(1);
      expect(screen.getByRole("listbox")).toBeInTheDocument();

      // When: user selects "apps/" (triggers selectEntry → cache reset → onChange("/Users/takumi/apps/"))
      vi.mocked(commands.listDirectory).mockResolvedValue(
        makeEntries(["cruise"], "/Users/takumi/apps/")
      );
      fireEvent.mouseDown(screen.getByText("apps/"));
      // The Controlled wrapper updates value to "/Users/takumi/apps/" via onChange
      await flushAll();

      // Then: listDirectory was called for the new directory
      // (cache was reset by selectEntry, so the new dir triggers a fresh IPC call)
      expect(commands.listDirectory).toHaveBeenCalledWith("/Users/takumi/apps/");
    });
  });

  // ---------------------------------------------------------------------------
  // Disabled state
  // ---------------------------------------------------------------------------

  describe("disabled state", () => {
    it("disables both the input and the Browse button when disabled=true", () => {
      // Given / When
      render(<DirectoryPicker value="" onChange={vi.fn()} disabled={true} />);

      // Then
      expect(screen.getByRole("combobox")).toBeDisabled();
      expect(screen.getByRole("button", { name: /browse/i })).toBeDisabled();
    });

    it("leaves input and Browse button enabled by default", () => {
      // Given / When
      render(<DirectoryPicker value="" onChange={vi.fn()} />);

      // Then
      expect(screen.getByRole("combobox")).not.toBeDisabled();
      expect(screen.getByRole("button", { name: /browse/i })).not.toBeDisabled();
    });
  });

  // ---------------------------------------------------------------------------
  // ARIA attributes
  // ---------------------------------------------------------------------------

  describe("ARIA attributes", () => {
    it("sets aria-expanded=false when the listbox is closed", () => {
      // Given / When
      render(<DirectoryPicker value="" onChange={vi.fn()} />);

      // Then
      expect(screen.getByRole("combobox")).toHaveAttribute("aria-expanded", "false");
    });

    it("sets aria-expanded=true when the listbox is open", async () => {
      // Given
      vi.mocked(commands.listDirectory).mockResolvedValue(
        makeEntries(["takumi"], "/Users/")
      );
      render(<DirectoryPicker value="/Users/" onChange={vi.fn()} />);

      // When
      await flushAll();

      // Then
      expect(screen.getByRole("combobox")).toHaveAttribute("aria-expanded", "true");
    });
  });
});