Open History to see the last commit
1import { createElement } from "react";
2import { renderToStaticMarkup } from "react-dom/server";
3import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
6
7const mocks = vi.hoisted(() => ({
8 hasLocalDraft: false,
9 shell: null as object | null,
10 detailAtom: vi.fn((ref: ScopedThreadRef) => ({ kind: "detail", ref })),
11 statusAtom: vi.fn((ref: ScopedThreadRef) => ({ kind: "status", ref })),
12 threadShellAtom: vi.fn((ref: ScopedThreadRef) => ({ kind: "shell", ref })),
13}));
14
15vi.mock("@effect/atom-react", () => ({
16 useAtomValue: (atom: { readonly kind?: string }) => {
17 if (atom.kind === "shell") return mocks.shell;
18 if (atom.kind === "detail") return { id: "detail" };
19 if (atom.kind === "status") return "deleted";
20 return null;
21 },
22}));
23
24vi.mock("@t3tools/client-runtime/state/threads", async (importOriginal) => ({
25 ...(await importOriginal<typeof import("@t3tools/client-runtime/state/threads")>()),
26 mergeEnvironmentThread: (detail: unknown, shell: unknown) => ({ detail, shell }),
27}));
28
29vi.mock("../composerDraftStore", () => ({
30 useComposerDraftStore: (
31 selector: (store: { getDraftThreadByRef: () => object | null }) => unknown,
32 ) =>
33 selector({
34 getDraftThreadByRef: () => (mocks.hasLocalDraft ? {} : null),
35 }),
36}));
37
38vi.mock("./threads", () => ({
39 environmentThreadDetails: {
40 detailAtom: mocks.detailAtom,
41 statusAtom: mocks.statusAtom,
42 },
43 environmentThreadShells: {
44 threadShellAtom: mocks.threadShellAtom,
45 },
46}));
47
48import {
49 classifyThreadDetail,
50 resolveThreadDetailRef,
51 useThread,
52 useThreadDetailWhenReady,
53 useThreadStatusWhenReady,
54} from "./entities";
55
56const THREAD_REF: ScopedThreadRef = {
57 environmentId: EnvironmentId.make("environment-1"),
58 threadId: ThreadId.make("thread-1"),
59};
60
61beforeEach(() => {
62 mocks.hasLocalDraft = false;
63 mocks.shell = null;
64 mocks.detailAtom.mockClear();
65 mocks.statusAtom.mockClear();
66 mocks.threadShellAtom.mockClear();
67});
68
69describe("thread detail subscription", () => {
70 it("starts the actual detail hook immediately for an ordinary thread", () => {
71 function Probe() {
72 useThread(THREAD_REF);
73 return null;
74 }
75
76 renderToStaticMarkup(createElement(Probe));
77 expect(mocks.detailAtom).toHaveBeenCalledOnce();
78 expect(mocks.detailAtom).toHaveBeenCalledWith(THREAD_REF);
79 });
80
81 it("starts the actual detail hook only after a local draft receives its shell", () => {
82 mocks.hasLocalDraft = true;
83
84 function Probe() {
85 useThread(THREAD_REF);
86 return null;
87 }
88
89 renderToStaticMarkup(createElement(Probe));
90 expect(mocks.detailAtom).not.toHaveBeenCalled();
91
92 mocks.shell = { id: THREAD_REF.threadId };
93 renderToStaticMarkup(createElement(Probe));
94 expect(mocks.detailAtom).toHaveBeenCalledOnce();
95 expect(mocks.detailAtom).toHaveBeenCalledWith(THREAD_REF);
96 });
97
98 it("preserves explicit shell gating for callers without a local draft", () => {
99 function Probe() {
100 useThread(THREAD_REF, { waitForShell: true });
101 return null;
102 }
103
104 renderToStaticMarkup(createElement(Probe));
105 expect(mocks.detailAtom).not.toHaveBeenCalled();
106
107 mocks.shell = { id: THREAD_REF.threadId };
108 renderToStaticMarkup(createElement(Probe));
109 expect(mocks.detailAtom).toHaveBeenCalledOnce();
110 expect(mocks.detailAtom).toHaveBeenCalledWith(THREAD_REF);
111 });
112
113 it("gates direct detail state consumers until a local draft receives its shell", () => {
114 function Probe({ hasServerShell }: { readonly hasServerShell: boolean }) {
115 const classification = classifyThreadDetail({
116 hasLocalDraft: true,
117 hasServerShell,
118 });
119 useThreadDetailWhenReady(THREAD_REF, classification);
120 useThreadStatusWhenReady(THREAD_REF, classification);
121 return null;
122 }
123
124 renderToStaticMarkup(createElement(Probe, { hasServerShell: false }));
125 expect(mocks.detailAtom).not.toHaveBeenCalled();
126 expect(mocks.statusAtom).not.toHaveBeenCalled();
127
128 renderToStaticMarkup(createElement(Probe, { hasServerShell: true }));
129 expect(mocks.detailAtom).toHaveBeenCalledOnce();
130 expect(mocks.detailAtom).toHaveBeenCalledWith(THREAD_REF);
131 expect(mocks.statusAtom).toHaveBeenCalledOnce();
132 expect(mocks.statusAtom).toHaveBeenCalledWith(THREAD_REF);
133 });
134});
135
136describe("resolveThreadDetailRef", () => {
137 it("does not subscribe to a reserved draft thread before it enters the shell index", () => {
138 expect(
139 resolveThreadDetailRef(
140 THREAD_REF,
141 classifyThreadDetail({
142 hasLocalDraft: true,
143 hasServerShell: false,
144 }),
145 ),
146 ).toBeNull();
147 });
148
149 it("subscribes once the reserved draft thread enters the shell index", () => {
150 expect(
151 resolveThreadDetailRef(
152 THREAD_REF,
153 classifyThreadDetail({
154 hasLocalDraft: true,
155 hasServerShell: true,
156 }),
157 ),
158 ).toBe(THREAD_REF);
159 });
160
161 it("keeps direct server-thread lookups enabled when the shell has not loaded it", () => {
162 expect(
163 resolveThreadDetailRef(
164 THREAD_REF,
165 classifyThreadDetail({
166 hasLocalDraft: false,
167 hasServerShell: false,
168 }),
169 ),
170 ).toBe(THREAD_REF);
171 });
172
173 it("maps explicit shell-gating callers through the same draft classification", () => {
174 expect(
175 resolveThreadDetailRef(
176 THREAD_REF,
177 classifyThreadDetail({
178 hasLocalDraft: false,
179 hasServerShell: false,
180 waitForShell: true,
181 }),
182 ),
183 ).toBeNull();
184 });
185});