|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { |
| 3 | + EnvironmentId, |
| 4 | + ThreadId, |
| 5 | + type OrchestrationV2ThreadFamily, |
| 6 | + type OrchestrationV2ThreadShell, |
| 7 | + type ScopedThreadRef, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import { AsyncResult } from "effect/reactivity"; |
| 10 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 11 | + |
| 12 | +import { act, render } from "../../../test/render"; |
| 13 | +import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry"; |
| 14 | +import { threadFamilies } from "../../state/threads"; |
| 15 | +import { ThreadContextChip } from "../ThreadContextChip"; |
| 16 | +import { MarkdownThreadLink } from "./MarkdownThreadLink"; |
| 17 | + |
| 18 | +const mocks = await vi.hoisted(async () => { |
| 19 | + const { Atom, AsyncResult } = await import("effect/reactivity"); |
| 20 | + return { |
| 21 | + shells: Atom.make<ReadonlyMap<string, OrchestrationV2ThreadShell>>(new Map()).pipe( |
| 22 | + Atom.keepAlive, |
| 23 | + ), |
| 24 | + reads: Atom.family((_key: string) => |
| 25 | + Atom.make<AsyncResult.AsyncResult<OrchestrationV2ThreadFamily, never>>( |
| 26 | + AsyncResult.initial(), |
| 27 | + ).pipe(Atom.keepAlive), |
| 28 | + ), |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +const key = (ref: ScopedThreadRef) => JSON.stringify([ref.environmentId, ref.threadId]); |
| 33 | + |
| 34 | +vi.mock("../../state/entities", () => ({ |
| 35 | + useProject: () => null, |
| 36 | + useThreadRefs: () => [], |
| 37 | + useThreadShell: (ref: ScopedThreadRef | null) => { |
| 38 | + const rows = useAtomValue(mocks.shells); |
| 39 | + if (ref === null) return null; |
| 40 | + const row = rows.get(key(ref)); |
| 41 | + return row ? { ...row, environmentId: ref.environmentId } : null; |
| 42 | + }, |
| 43 | +})); |
| 44 | + |
| 45 | +const ENV = EnvironmentId.make("thread-links-test"); |
| 46 | +const ref = (id: string, environmentId = ENV): ScopedThreadRef => ({ |
| 47 | + environmentId, |
| 48 | + threadId: ThreadId.make(id), |
| 49 | +}); |
| 50 | +const row = (id: string, title: string) => |
| 51 | + ({ id: ThreadId.make(id), title }) as OrchestrationV2ThreadShell; |
| 52 | + |
| 53 | +function setup(shells: ReadonlyMap<string, OrchestrationV2ThreadShell> = new Map()) { |
| 54 | + appAtomRegistry.set(mocks.shells, shells); |
| 55 | + const mounted = new Map<string, OrchestrationV2ThreadFamily>(); |
| 56 | + const counts = new Map<string, number>(); |
| 57 | + const requested = new Set<string>(); |
| 58 | + const queries = vi |
| 59 | + .spyOn(threadFamilies, "familyAtom") |
| 60 | + .mockImplementation((environmentId, threadId) => { |
| 61 | + const id = key({ environmentId, threadId }); |
| 62 | + if (!requested.has(id)) { |
| 63 | + requested.add(id); |
| 64 | + appAtomRegistry.set(mocks.reads(id), AsyncResult.initial()); |
| 65 | + } |
| 66 | + return mocks.reads(id); |
| 67 | + }); |
| 68 | + const mounts = vi |
| 69 | + .spyOn(threadFamilies, "mount") |
| 70 | + .mockImplementation((registry, environmentId, threadId) => { |
| 71 | + const id = key({ environmentId, threadId }); |
| 72 | + const query = threadFamilies.familyAtom(environmentId, threadId); |
| 73 | + counts.set(id, (counts.get(id) ?? 0) + 1); |
| 74 | + const publish = () => { |
| 75 | + const rows = new Map(shells); |
| 76 | + for (const [familyKey, family] of mounted) { |
| 77 | + const [environmentId] = JSON.parse(familyKey) as [EnvironmentId, ThreadId]; |
| 78 | + for (const thread of family.threads) { |
| 79 | + rows.set(key({ environmentId, threadId: thread.id }), thread); |
| 80 | + } |
| 81 | + } |
| 82 | + registry.set(mocks.shells, rows); |
| 83 | + }; |
| 84 | + const unsubscribe = registry.subscribe( |
| 85 | + query, |
| 86 | + (result) => { |
| 87 | + if (AsyncResult.isSuccess(result)) mounted.set(id, result.value); |
| 88 | + else mounted.delete(id); |
| 89 | + publish(); |
| 90 | + }, |
| 91 | + { immediate: true }, |
| 92 | + ); |
| 93 | + return () => { |
| 94 | + unsubscribe(); |
| 95 | + const count = (counts.get(id) ?? 1) - 1; |
| 96 | + counts.set(id, count); |
| 97 | + if (count === 0) mounted.delete(id); |
| 98 | + publish(); |
| 99 | + }; |
| 100 | + }); |
| 101 | + return { |
| 102 | + requested, |
| 103 | + resolve: (target: ScopedThreadRef, threads: ReadonlyArray<OrchestrationV2ThreadShell>) => |
| 104 | + act(() => |
| 105 | + appAtomRegistry.set( |
| 106 | + mocks.reads(key(target)), |
| 107 | + AsyncResult.success({ snapshotSequence: 1, threads }), |
| 108 | + ), |
| 109 | + ), |
| 110 | + [Symbol.dispose]() { |
| 111 | + mounts.mockRestore(); |
| 112 | + queries.mockRestore(); |
| 113 | + }, |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +function view(target: ScopedThreadRef, kind: "context" | "markdown" = "markdown") { |
| 118 | + return ( |
| 119 | + <AppAtomRegistryProvider> |
| 120 | + {kind === "context" ? ( |
| 121 | + <ThreadContextChip record={{ ...target, title: "Saved title" }} /> |
| 122 | + ) : ( |
| 123 | + <MarkdownThreadLink environmentId={ENV} link={target} label={null} /> |
| 124 | + )} |
| 125 | + </AppAtomRegistryProvider> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +describe("thread link family lookups", () => { |
| 130 | + it.each(["context", "markdown"] as const)( |
| 131 | + "retains a settled thread until its %s chip unmounts", |
| 132 | + async (kind) => { |
| 133 | + using fixture = setup(); |
| 134 | + const target = ref("older-settled"); |
| 135 | + await using screen = await render(view(target, kind)); |
| 136 | + await fixture.resolve(target, [row(target.threadId, "Older settled thread")]); |
| 137 | + await expect |
| 138 | + .element(screen.getByRole("link", { name: "Thread, Older settled thread" })) |
| 139 | + .toBeInTheDocument(); |
| 140 | + await screen.rerender(view(target, kind)); |
| 141 | + await expect |
| 142 | + .element(screen.getByRole("link", { name: "Thread, Older settled thread" })) |
| 143 | + .toBeInTheDocument(); |
| 144 | + expect(appAtomRegistry.get(mocks.shells).has(key(target))).toBe(true); |
| 145 | + await screen.unmount(); |
| 146 | + expect(appAtomRegistry.get(mocks.shells).size).toBe(0); |
| 147 | + }, |
| 148 | + ); |
| 149 | + |
| 150 | + it("prefers a literal percent escape to the decoded thread", async () => { |
| 151 | + const literal = ref("provider%3A1"); |
| 152 | + const decoded = ref("provider:1"); |
| 153 | + using fixture = setup(new Map([[key(decoded), row(decoded.threadId, "Decoded thread")]])); |
| 154 | + await using screen = await render(view(literal)); |
| 155 | + await expect |
| 156 | + .element(screen.getByRole("link", { name: "Thread, Decoded thread" })) |
| 157 | + .not.toBeInTheDocument(); |
| 158 | + expect(fixture.requested.has(key(literal))).toBe(true); |
| 159 | + expect(fixture.requested.has(key(decoded))).toBe(false); |
| 160 | + await fixture.resolve(literal, [row(literal.threadId, "Literal thread")]); |
| 161 | + await expect |
| 162 | + .element(screen.getByRole("link", { name: "Thread, Literal thread" })) |
| 163 | + .toBeInTheDocument(); |
| 164 | + expect(fixture.requested.has(key(decoded))).toBe(false); |
| 165 | + }); |
| 166 | + |
| 167 | + it("tries the decoded ID only after a successful empty literal lookup", async () => { |
| 168 | + using fixture = setup(); |
| 169 | + const literal = ref("provider%3A1"); |
| 170 | + const decoded = ref("provider:1"); |
| 171 | + await using screen = await render(view(literal)); |
| 172 | + expect(fixture.requested.has(key(decoded))).toBe(false); |
| 173 | + await fixture.resolve(literal, []); |
| 174 | + expect(fixture.requested.has(key(decoded))).toBe(true); |
| 175 | + await fixture.resolve(decoded, [row(decoded.threadId, "Decoded thread")]); |
| 176 | + await expect |
| 177 | + .element(screen.getByRole("link", { name: "Thread, Decoded thread" })) |
| 178 | + .toBeInTheDocument(); |
| 179 | + }); |
| 180 | + |
| 181 | + it("releases the previous family when the target environment changes", async () => { |
| 182 | + using fixture = setup(); |
| 183 | + const first = ref("same-id"); |
| 184 | + const second = ref("same-id", EnvironmentId.make("second-environment")); |
| 185 | + await using screen = await render(view(first)); |
| 186 | + await fixture.resolve(first, [row(first.threadId, "First environment")]); |
| 187 | + await screen.rerender(view(second)); |
| 188 | + await fixture.resolve(second, [row(second.threadId, "Second environment")]); |
| 189 | + await expect |
| 190 | + .element(screen.getByRole("link", { name: "Thread, Second environment" })) |
| 191 | + .toBeInTheDocument(); |
| 192 | + expect(appAtomRegistry.get(mocks.shells).has(key(first))).toBe(false); |
| 193 | + }); |
| 194 | +}); |
0 commit comments