|
| 1 | +import { RegistryContext } from "@effect/atom-react"; |
| 2 | +import { act } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { expect, it, vi } from "vite-plus/test"; |
| 5 | +import { Atom, AtomRegistry } from "effect/reactivity"; |
| 6 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 7 | + |
| 8 | +vi.mock("./atom-registry", () => ({ appAtomRegistry: AtomRegistry.make() })); |
| 9 | +vi.mock("./projects", () => ({ environmentProjects: {} })); |
| 10 | +vi.mock("./server", () => ({ |
| 11 | + environmentServerConfigsAtom: Atom.make(new Map()), |
| 12 | + serverEnvironment: {}, |
| 13 | +})); |
| 14 | +vi.mock("./assets", () => ({ projectFaviconUrlAtom: vi.fn() })); |
| 15 | +vi.mock("../native/ProjectChipIconImages", () => ({ |
| 16 | + projectGlyphImageAtom: vi.fn(), |
| 17 | + projectGlyphKey: vi.fn(), |
| 18 | +})); |
| 19 | +vi.mock("./threads", () => { |
| 20 | + const shell = Atom.make<{ title: string } | null>(null); |
| 21 | + return { |
| 22 | + environmentThreadDetails: {}, |
| 23 | + environmentThreadShells: { threadShellAtom: () => shell }, |
| 24 | + threadFamilies: { |
| 25 | + mount: (registry: AtomRegistry.AtomRegistry) => { |
| 26 | + registry.set(shell, { title: "Child result" }); |
| 27 | + return () => registry.set(shell, null); |
| 28 | + }, |
| 29 | + }, |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +import { appAtomRegistry } from "./atom-registry"; |
| 34 | +import { useLiveThreadLinkLabels } from "./entities"; |
| 35 | +import { environmentThreadShells } from "./threads"; |
| 36 | + |
| 37 | +it("keeps a linked child title mounted until the message stops citing it", async () => { |
| 38 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 39 | + const container = document.createElement("div"); |
| 40 | + const root = createRoot(container); |
| 41 | + let renders = 0; |
| 42 | + function Probe({ text }: { text: string }) { |
| 43 | + if (++renders > 20) throw new Error("Thread title subscription loop"); |
| 44 | + return useLiveThreadLinkLabels(text, EnvironmentId.make("local")); |
| 45 | + } |
| 46 | + try { |
| 47 | + await act(async () => |
| 48 | + root.render( |
| 49 | + <RegistryContext.Provider value={appAtomRegistry}> |
| 50 | + <Probe text="[Child](t3-thread://v2/local/child)" /> |
| 51 | + </RegistryContext.Provider>, |
| 52 | + ), |
| 53 | + ); |
| 54 | + expect(container.textContent).toBe("[Child result](t3-thread://v2/local/child)"); |
| 55 | + expect(renders).toBeLessThan(20); |
| 56 | + await act(async () => |
| 57 | + root.render( |
| 58 | + <RegistryContext.Provider value={appAtomRegistry}> |
| 59 | + <Probe text="No link" /> |
| 60 | + </RegistryContext.Provider>, |
| 61 | + ), |
| 62 | + ); |
| 63 | + expect(container.textContent).toBe("No link"); |
| 64 | + expect( |
| 65 | + appAtomRegistry.get( |
| 66 | + environmentThreadShells.threadShellAtom({ |
| 67 | + environmentId: EnvironmentId.make("local"), |
| 68 | + threadId: ThreadId.make("child"), |
| 69 | + }), |
| 70 | + ), |
| 71 | + ).toBeNull(); |
| 72 | + } finally { |
| 73 | + await act(async () => root.unmount()); |
| 74 | + } |
| 75 | +}); |
0 commit comments