1import { describe, expect, it } from "vite-plus/test";
2import { createDeferredNavigation } from "./deferred-navigation";
3
4describe("workspace mutation navigation", () => {
5 it("keeps only the latest selection until all checkouts settle", async () => {
6 const navigation = createDeferredNavigation();
7 const first = Promise.withResolvers<void>();
8 const second = Promise.withResolvers<void>();
9 const selected: string[] = [];
10 navigation.waitFor(first.promise);
11 navigation.waitFor(second.promise);
12 navigation.run(() => selected.push("old draft"));
13 navigation.run(() => selected.push("latest draft"));
14 first.resolve();
15 await first.promise;
16 expect(selected).toEqual([]);
17 second.resolve();
18 await second.promise;
19 expect(selected).toEqual(["latest draft"]);
20 navigation.run(() => selected.push("thread"));
21 expect(selected).toEqual(["latest draft", "thread"]);
22 });
23
24 it("waits for the removal guard commit after checkout completes", async () => {
25 const navigation = createDeferredNavigation();
26 const checkout = Promise.withResolvers<void>();
27 const guardCommit = Promise.withResolvers<void>();
28 let selected = false;
29 const ready = Promise.allSettled([checkout.promise, guardCommit.promise]);
30 navigation.waitFor(ready);
31 navigation.run(() => {
32 selected = true;
33 });
34 checkout.resolve();
35 await checkout.promise;
36 expect(selected).toBe(false);
37 guardCommit.resolve();
38 await ready;
39 expect(selected).toBe(true);
40 });
41
42 it("releases selection after a failed checkout", async () => {
43 const navigation = createDeferredNavigation();
44 const checkout = Promise.withResolvers<void>();
45 let selected = false;
46 navigation.waitFor(checkout.promise);
47 navigation.run(() => {
48 selected = true;
49 });
50 checkout.reject(new Error("Checkout failed"));
51 await checkout.promise.catch(() => undefined);
52 expect(selected).toBe(true);
53 });
54});