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