Open History to see the last commit
1import { act, useLayoutEffect } from "react";
2import { createRoot } from "react-dom/client";
3import { RegistryContext } from "@effect/atom-react";
4import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
5import { EnvironmentId, ThreadId } from "@t3tools/contracts";
6import { type Atom, AtomRegistry } from "effect/reactivity";
7import { describe, expect, it, vi } from "vite-plus/test";
8
9import { scopedThreadKey } from "../../lib/scopedEntities";
10import { environmentThreadShells } from "../../state/threads";
11import { makeThreadShellFixture } from "../../test-fixtures";
12import { useSidebarRowThreadKey } from "./useSidebarRowThreadKey";
13
14vi.mock("../../state/threads", async () => {
15 const { Atom: ReactivityAtom } = await import("effect/reactivity");
16 return {
17 environmentThreadShells: {
18 quietThreadShellsAtom: ReactivityAtom.make<readonly EnvironmentThreadShell[]>([]).pipe(
19 ReactivityAtom.keepAlive,
20 ),
21 },
22 };
23});
24
25const quietThreadShellsAtom = environmentThreadShells.quietThreadShellsAtom as Atom.Writable<
26 readonly EnvironmentThreadShell[]
27>;
28
29describe("useSidebarRowThreadKey", () => {
30 it("re-renders only when the resolved row key changes", async () => {
31 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
32 using _globals = { [Symbol.dispose]: () => vi.unstubAllGlobals() };
33 const registry = AtomRegistry.make();
34 using _registry = { [Symbol.dispose]: () => registry.dispose() };
35
36 const envId = EnvironmentId.make("env-1");
37 const parentId = ThreadId.make("thread-parent");
38 const childId = ThreadId.make("thread-child");
39 const parentKey = scopedThreadKey(envId, parentId);
40 const childKey = scopedThreadKey(envId, childId);
41 const parentThread = makeThreadShellFixture({
42 environmentId: envId,
43 id: parentId,
44 title: "Parent",
45 });
46 const childThread = makeThreadShellFixture({
47 environmentId: envId,
48 id: childId,
49 title: "Delegated child",
50 delegatedFrom: { environmentId: envId, threadId: parentId, title: "Parent" },
51 });
52 const otherThread = (title: string) =>
53 makeThreadShellFixture({ environmentId: envId, id: ThreadId.make("thread-other"), title });
54
55 registry.set(quietThreadShellsAtom, [parentThread, childThread, otherThread("v1")]);
56
57 const seen: Array<string | null> = [];
58 function Probe(props: { openThreadKey: string | null }) {
59 const value = useSidebarRowThreadKey(props.openThreadKey);
60 useLayoutEffect(() => {
61 seen.push(value);
62 });
63 return null;
64 }
65
66 const root = createRoot(document.createElement("div"));
67 using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
68 const renderKey = (openThreadKey: string | null) =>
69 act(async () =>
70 root.render(
71 <RegistryContext value={registry}>
72 <Probe openThreadKey={openThreadKey} />
73 </RegistryContext>,
74 ),
75 );
76 const setShells = (shells: readonly EnvironmentThreadShell[]) =>
77 act(async () => registry.set(quietThreadShellsAtom, shells));
78
79 await renderKey(null);
80 await setShells([parentThread, childThread, otherThread("v2")]);
81 expect(seen).toEqual([null]);
82
83 await renderKey(childKey);
84 await setShells([parentThread, childThread, otherThread("v3")]);
85 expect(seen).toEqual([null, parentKey]);
86
87 await setShells([
88 { ...parentThread, archivedAt: "2026-01-02T00:00:00.000Z" },
89 childThread,
90 otherThread("v3"),
91 ]);
92 expect(seen).toEqual([null, parentKey, childKey]);
93 });
94});