|
| 1 | +import { EnvironmentId, ThreadId, type OrchestrationV2ThreadShell } from "@t3tools/contracts"; |
| 2 | +import * as Cause from "effect/Cause"; |
| 3 | +import { expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +import { render, type Screen } from "../../../test/render"; |
| 6 | + |
| 7 | +const { read, toast } = vi.hoisted(() => ({ |
| 8 | + read: vi.fn<(input: unknown) => Promise<unknown>>(), |
| 9 | + toast: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +const childEnvironmentId = EnvironmentId.make("child-environment"); |
| 13 | +const childThreadId = ThreadId.make("child-thread"); |
| 14 | +const parentEnvironmentId = EnvironmentId.make("parent-environment"); |
| 15 | +// The panel reads only identity, lineage and delegation from the current thread. |
| 16 | +const child = { |
| 17 | + id: childThreadId, |
| 18 | + title: "Child", |
| 19 | + lineage: { rootThreadId: childThreadId, parentThreadId: null, relationshipToParent: null }, |
| 20 | + forkedFrom: null, |
| 21 | + delegatedFrom: { |
| 22 | + environmentId: parentEnvironmentId, |
| 23 | + threadId: ThreadId.make("parent-thread"), |
| 24 | + title: "Parent", |
| 25 | + }, |
| 26 | +} as Partial<OrchestrationV2ThreadShell> as OrchestrationV2ThreadShell; |
| 27 | + |
| 28 | +vi.mock(import("../../state/orchestration"), async (original) => ({ |
| 29 | + ...(await original()), |
| 30 | + threadProjectionRead: { |
| 31 | + label: "thread-projection-read", |
| 32 | + run: (_registry: unknown, input: unknown) => read(input), |
| 33 | + } as never, |
| 34 | +})); |
| 35 | +vi.mock(import("../../state/entities"), async (original) => ({ |
| 36 | + ...(await original()), |
| 37 | + useThreadProjection: () => null, |
| 38 | + useThreadRelationshipShells: () => [child], |
| 39 | + // The parent is absent from this client's shell, as for a parent never loaded here. |
| 40 | + useQuietThreadShell: () => null, |
| 41 | +})); |
| 42 | +vi.mock(import("../../state/environments"), async (original) => ({ |
| 43 | + ...(await original()), |
| 44 | + useEnvironment: (() => ({ label: "Linked" })) as never, |
| 45 | +})); |
| 46 | +vi.mock(import("../ui/toast"), async (original) => { |
| 47 | + const actual = await original(); |
| 48 | + return { ...actual, toastManager: { ...actual.toastManager, add: toast } }; |
| 49 | +}); |
| 50 | + |
| 51 | +import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl"; |
| 52 | + |
| 53 | +const childPath = `/${childEnvironmentId}/${childThreadId}`; |
| 54 | +const parentPath = `/${parentEnvironmentId}/parent-thread`; |
| 55 | + |
| 56 | +const live = { _tag: "Success", value: { thread: { deletedAt: null } } }; |
| 57 | +const deleted = { _tag: "Success", value: { thread: { deletedAt: "2026-10-30T06:20:20.897Z" } } }; |
| 58 | + |
| 59 | +// Renders the child's panel; each read of its parent answers with the next of `replies`. |
| 60 | +function renderChild(...replies: ReadonlyArray<unknown>) { |
| 61 | + read.mockReset(); |
| 62 | + for (const reply of replies) read.mockImplementationOnce(async () => reply); |
| 63 | + toast.mockReset(); |
| 64 | + return render( |
| 65 | + <ThreadRelationshipsPanel environmentId={childEnvironmentId} threadId={childThreadId} />, |
| 66 | + { router: childPath }, |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +const parentRow = (screen: Screen) => screen.getByRole("button", { name: /Parent/ }); |
| 71 | + |
| 72 | +it.each([ |
| 73 | + { |
| 74 | + parent: "deleted there", |
| 75 | + reply: deleted, |
| 76 | + feedback: { type: "info", title: "The parent thread was deleted" }, |
| 77 | + }, |
| 78 | + { |
| 79 | + parent: "unreadable", |
| 80 | + reply: { |
| 81 | + _tag: "Failure", |
| 82 | + cause: Cause.fail(new Error("Failed to load orchestration V2 thread")), |
| 83 | + }, |
| 84 | + feedback: { type: "error", title: "Could not open the parent thread" }, |
| 85 | + }, |
| 86 | +])("keeps the child open with feedback when its remote parent is $parent", async (row) => { |
| 87 | + await using screen = await renderChild(row.reply); |
| 88 | + await parentRow(screen).click(); |
| 89 | + await vi.waitFor(() => expect(toast).toHaveBeenCalledWith(row.feedback)); |
| 90 | + expect(read).toHaveBeenCalledWith({ |
| 91 | + environmentId: parentEnvironmentId, |
| 92 | + input: { threadId: "parent-thread" }, |
| 93 | + }); |
| 94 | + expect(screen.router?.state.location.pathname).toBe(childPath); |
| 95 | +}); |
| 96 | + |
| 97 | +it("opens a remote parent this client has not loaded", async () => { |
| 98 | + await using screen = await renderChild(live); |
| 99 | + await parentRow(screen).click(); |
| 100 | + await vi.waitFor(() => expect(screen.router?.state.location.pathname).toBe(parentPath)); |
| 101 | + expect(toast).not.toHaveBeenCalled(); |
| 102 | +}); |
| 103 | + |
| 104 | +it("leaves the user where they went when the parent read answers late", async () => { |
| 105 | + let answerLate!: (reply: unknown) => void; |
| 106 | + const late = new Promise((resolve) => { |
| 107 | + answerLate = resolve; |
| 108 | + }); |
| 109 | + await using screen = await renderChild(late, deleted); |
| 110 | + await parentRow(screen).click(); |
| 111 | + await screen.router?.navigate({ to: "/new" }); |
| 112 | + answerLate(live); |
| 113 | + // The row takes a second click only after the first one has finished. |
| 114 | + await vi.waitFor(async () => { |
| 115 | + await parentRow(screen).click(); |
| 116 | + expect(read).toHaveBeenCalledTimes(2); |
| 117 | + }); |
| 118 | + await vi.waitFor(() => expect(toast).toHaveBeenCalledOnce()); |
| 119 | + expect(screen.router?.latestLocation.pathname).toBe("/new"); |
| 120 | +}); |
0 commit comments