The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 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 { act, 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 | const Effect = await import("effect/Effect"); |
| 30 | type Reply = |
| 31 | | { readonly _tag: "Success"; readonly value: unknown } |
| 32 | | { readonly _tag: "Failure"; readonly cause: Cause.Cause<Error> }; |
| 33 | return { |
| 34 | ...(await original()), |
| 35 | threadTitlesRead: { |
| 36 | label: "thread-titles-read", |
| 37 | run: (_registry: unknown, input: unknown) => |
| 38 | Effect.promise(() => read(input) as Promise<Reply>).pipe( |
| 39 | Effect.flatMap((reply) => |
| 40 | reply._tag === "Success" ? Effect.succeed(reply.value) : Effect.failCause(reply.cause), |
| 41 | ), |
| 42 | ), |
| 43 | } as never, |
| 44 | }; |
| 45 | }); |
| 46 | vi.mock(import("../../state/entities"), async (original) => ({ |
| 47 | ...(await original()), |
| 48 | useThreadProjection: () => null, |
| 49 | useThreadRelationshipShells: () => [child], |
| 50 | // The parent is absent from this client's shell, as for a parent never loaded here. |
| 51 | useQuietThreadShell: () => null, |
| 52 | })); |
| 53 | vi.mock(import("../../state/environments"), async (original) => ({ |
| 54 | ...(await original()), |
| 55 | useEnvironment: (() => ({ label: "Linked" })) as never, |
| 56 | })); |
| 57 | vi.mock(import("../ui/toast"), async (original) => { |
| 58 | const actual = await original(); |
| 59 | return { ...actual, toastManager: { ...actual.toastManager, add: toast } }; |
| 60 | }); |
| 61 | |
| 62 | import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl"; |
| 63 | |
| 64 | const childPath = `/${childEnvironmentId}/${childThreadId}`; |
| 65 | const parentPath = `/${parentEnvironmentId}/parent-thread`; |
| 66 | |
| 67 | const live = { _tag: "Success", value: { "parent-thread": "Parent" } }; |
| 68 | const deleted = { _tag: "Success", value: {} }; |
| 69 | |
| 70 | // Renders the child's panel; each read of its parent answers with the next of `replies`. |
| 71 | function renderChild(...replies: ReadonlyArray<unknown>) { |
| 72 | read.mockReset(); |
| 73 | for (const reply of replies) read.mockImplementationOnce(async () => reply); |
| 74 | toast.mockReset(); |
| 75 | return render( |
| 76 | <ThreadRelationshipsPanel |
| 77 | environmentId={childEnvironmentId} |
| 78 | threadId={childThreadId} |
| 79 | showLineage |
| 80 | showSideChats={false} |
| 81 | />, |
| 82 | { router: childPath }, |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | const parentRow = (screen: Screen) => screen.getByRole("button", { name: /Parent/ }); |
| 87 | |
| 88 | it.each([ |
| 89 | { |
| 90 | parent: "deleted there", |
| 91 | reply: deleted, |
| 92 | feedback: { type: "info", title: "The parent thread was deleted" }, |
| 93 | }, |
| 94 | { |
| 95 | parent: "unreadable", |
| 96 | reply: { |
| 97 | _tag: "Failure", |
| 98 | cause: Cause.fail(new Error("Failed to load orchestration V2 thread")), |
| 99 | }, |
| 100 | feedback: { type: "error", title: "Could not open the parent thread" }, |
| 101 | }, |
| 102 | ])("keeps the child open with feedback when its remote parent is $parent", async (row) => { |
| 103 | await using screen = await renderChild(row.reply); |
| 104 | await parentRow(screen).click(); |
| 105 | await vi.waitFor(() => expect(toast).toHaveBeenCalledWith(row.feedback)); |
| 106 | expect(read).toHaveBeenCalledWith({ |
| 107 | environmentId: parentEnvironmentId, |
| 108 | input: { threadIds: ["parent-thread"] }, |
| 109 | }); |
| 110 | expect(screen.router?.state.location.pathname).toBe(childPath); |
| 111 | }); |
| 112 | |
| 113 | it("opens a remote parent this client has not loaded", async () => { |
| 114 | await using screen = await renderChild(live); |
| 115 | await parentRow(screen).click(); |
| 116 | await vi.waitFor(() => expect(screen.router?.state.location.pathname).toBe(parentPath)); |
| 117 | expect(toast).not.toHaveBeenCalled(); |
| 118 | }); |
| 119 | |
| 120 | it("leaves the user where they went when the parent read answers late", async () => { |
| 121 | let answerLate!: (reply: unknown) => void; |
| 122 | const late = new Promise((resolve) => { |
| 123 | answerLate = resolve; |
| 124 | }); |
| 125 | await using screen = await renderChild(late, deleted); |
| 126 | await parentRow(screen).click(); |
| 127 | await screen.router?.navigate({ to: "/new" }); |
| 128 | await act(async () => { |
| 129 | answerLate(live); |
| 130 | await late; |
| 131 | }); |
| 132 | expect(toast).not.toHaveBeenCalled(); |
| 133 | expect(screen.router?.latestLocation.pathname).toBe("/new"); |
| 134 | await parentRow(screen).click(); |
| 135 | await vi.waitFor(() => expect(read).toHaveBeenCalledTimes(2)); |
| 136 | await vi.waitFor(() => expect(toast).toHaveBeenCalledOnce()); |
| 137 | expect(screen.router?.latestLocation.pathname).toBe("/new"); |
| 138 | }); |