Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-order.test.ts

121 lines4.2 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import type { Atom } from "effect/unstable/reactivity";
4import { afterEach, describe, expect, it, vi } from "vite-plus/test";
5​
6import { createPendingThreadOrder } from "../features/threads/threadOrder";
7import { appAtomRegistry } from "./atom-registry";
8import {
9 beginPendingThreadOrder,
10 getPendingThreadOrder,
11 pendingThreadOrderAtom,
12} from "./thread-order";
13import { environmentThreadShells } from "./threads";
14​
15vi.mock("./atom-registry", async () => {
16 const { AtomRegistry } = await import("effect/unstable/reactivity");
17 return { appAtomRegistry: AtomRegistry.make() };
18});
19vi.mock("./threads", async () => {
20 const { Atom } = await import("effect/unstable/reactivity");
21 return { environmentThreadShells: { threadShellsAtom: Atom.make([]).pipe(Atom.keepAlive) } };
22});
23vi.mock("./server", async () => {
24 const { Atom } = await import("effect/unstable/reactivity");
25 return { environmentServerConfigsAtom: Atom.make(new Map()).pipe(Atom.keepAlive) };
26});
27vi.mock("./use-thread-outbox", async () => {
28 const { Atom } = await import("effect/unstable/reactivity");
29 return { queuedThreadKeysAtom: Atom.make(new Set<string>()).pipe(Atom.keepAlive) };
30});
31​
32// The mocked shell source is writable so tests can deliver canonical upserts.
33const shellsAtom = environmentThreadShells.threadShellsAtom as Atom.Writable<
34 readonly EnvironmentThreadShell[],
35 readonly EnvironmentThreadShell[]
36>;
37​
38function fixture() {
39 // Only section membership and order fields are read by this coordinator.
40 const rows = ["a", "b"].map(
41 (id, index) =>
42 ({
43 id: ThreadId.make(id),
44 environmentId: EnvironmentId.make("env"),
45 createdAt: `2026-06-01T0${2 - index}:00:00.000Z`,
46 archivedAt: null,
47 pinnedAt: null,
48 activeOrderKey: null,
49 }) as EnvironmentThreadShell,
50 );
51 appAtomRegistry.set(shellsAtom, rows);
52 const pending = createPendingThreadOrder({
53 section: "active",
54 ordered: rows,
55 movedId: "env:b",
56 direction: "up",
57 assignments: [
58 { id: "env:b", orderKey: "aa" },
59 { id: "env:a", orderKey: "bb" },
60 ],
61 });
62 const start = () => beginPendingThreadOrder(pending);
63 const upsert = (id: string, key: string) => {
64 const current = appAtomRegistry.get(shellsAtom);
65 appAtomRegistry.set(
66 shellsAtom,
67 current.map((row) => (row.id === id ? { ...row, activeOrderKey: key } : row)),
68 );
69 };
70 return { rows, start, upsert };
72​
73afterEach(() => appAtomRegistry.reset());
74​
75describe("shared mobile pending move", () => {
76 it("blocks another pickup after receipts and clears on final canonical upsert", () => {
77 const { start, upsert } = fixture();
78 const move = start();
79 move.complete();
80 expect(getPendingThreadOrder()).not.toBeNull();
81 upsert("b", "aa");
82 expect(getPendingThreadOrder()).not.toBeNull();
83 upsert("a", "bb");
84 expect(getPendingThreadOrder()).toBeNull();
85 expect(move.isPending()).toBe(false);
86 });
87​
88 it("waits for receipts when shells arrive first", () => {
89 const { start, upsert } = fixture();
90 const move = start();
91 upsert("b", "aa");
92 upsert("a", "bb");
93 expect(getPendingThreadOrder()).not.toBeNull();
94 move.complete();
95 expect(getPendingThreadOrder()).toBeNull();
96 });
97​
98 it.each(["failure", "interruption"])("releases a %s without restoring old canonical keys", () => {
99 const { start, upsert } = fixture();
100 const move = start();
101 upsert("b", "aa");
102 move.cancel();
103 expect(getPendingThreadOrder()).toBeNull();
104 expect(appAtomRegistry.get(shellsAtom)[1]?.activeOrderKey).toBe("aa");
105 const next = start();
106 move.cancel();
107 expect(next.isPending()).toBe(true);
108 next.cancel();
109 });
110​
111 it("stops remaining writes when a canonical membership change invalidates the move", () => {
112 const { rows, start } = fixture();
113 const move = start();
114 appAtomRegistry.set(shellsAtom, rows.slice(1));
115 expect(move.isPending()).toBe(false);
116 expect(appAtomRegistry.get(pendingThreadOrderAtom)).toBeNull();
117 move.complete();
118 appAtomRegistry.set(shellsAtom, rows);
119 expect(getPendingThreadOrder()).toBeNull();
120 });
121});