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

thread-order.test.ts

117 lines4.0 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});
27​
28// The mocked shell source is writable so tests can deliver canonical upserts.
29const shellsAtom = environmentThreadShells.threadShellsAtom as Atom.Writable<
30 readonly EnvironmentThreadShell[],
31 readonly EnvironmentThreadShell[]
32>;
33​
34function fixture() {
35 // Only section membership and order fields are read by this coordinator.
36 const rows = ["a", "b"].map(
37 (id, index) =>
38 ({
39 id: ThreadId.make(id),
40 environmentId: EnvironmentId.make("env"),
41 createdAt: `2026-06-01T0${2 - index}:00:00.000Z`,
42 archivedAt: null,
43 pinnedAt: null,
44 activeOrderKey: null,
45 }) as EnvironmentThreadShell,
46 );
47 appAtomRegistry.set(shellsAtom, rows);
48 const pending = createPendingThreadOrder({
49 section: "active",
50 ordered: rows,
51 movedId: "env:b",
52 direction: "up",
53 assignments: [
54 { id: "env:b", orderKey: "aa" },
55 { id: "env:a", orderKey: "bb" },
56 ],
57 });
58 const start = () => beginPendingThreadOrder(pending);
59 const upsert = (id: string, key: string) => {
60 const current = appAtomRegistry.get(shellsAtom);
61 appAtomRegistry.set(
62 shellsAtom,
63 current.map((row) => (row.id === id ? { ...row, activeOrderKey: key } : row)),
64 );
65 };
66 return { rows, start, upsert };
68​
69afterEach(() => appAtomRegistry.reset());
70​
71describe("shared mobile pending move", () => {
72 it("blocks another pickup after receipts and clears on final canonical upsert", () => {
73 const { start, upsert } = fixture();
74 const move = start();
75 move.complete();
76 expect(getPendingThreadOrder()).not.toBeNull();
77 upsert("b", "aa");
78 expect(getPendingThreadOrder()).not.toBeNull();
79 upsert("a", "bb");
80 expect(getPendingThreadOrder()).toBeNull();
81 expect(move.isPending()).toBe(false);
82 });
83​
84 it("waits for receipts when shells arrive first", () => {
85 const { start, upsert } = fixture();
86 const move = start();
87 upsert("b", "aa");
88 upsert("a", "bb");
89 expect(getPendingThreadOrder()).not.toBeNull();
90 move.complete();
91 expect(getPendingThreadOrder()).toBeNull();
92 });
93​
94 it.each(["failure", "interruption"])("releases a %s without restoring old canonical keys", () => {
95 const { start, upsert } = fixture();
96 const move = start();
97 upsert("b", "aa");
98 move.cancel();
99 expect(getPendingThreadOrder()).toBeNull();
100 expect(appAtomRegistry.get(shellsAtom)[1]?.activeOrderKey).toBe("aa");
101 const next = start();
102 move.cancel();
103 expect(next.isPending()).toBe(true);
104 next.cancel();
105 });
106​
107 it("stops remaining writes when a canonical membership change invalidates the move", () => {
108 const { rows, start } = fixture();
109 const move = start();
110 appAtomRegistry.set(shellsAtom, rows.slice(1));
111 expect(move.isPending()).toBe(false);
112 expect(appAtomRegistry.get(pendingThreadOrderAtom)).toBeNull();
113 move.complete();
114 appAtomRegistry.set(shellsAtom, rows);
115 expect(getPendingThreadOrder()).toBeNull();
116 });
117});