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