|
| 1 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import * as Cause from "effect/Cause"; |
| 3 | +import { AsyncResult } from "effect/reactivity"; |
| 4 | +import { expect, it, vi } from "vite-plus/test"; |
| 5 | +import { page } from "vite-plus/test/browser"; |
| 6 | + |
| 7 | +const state = vi.hoisted(() => ({ projection: null as unknown, commandSucceeds: false })); |
| 8 | + |
| 9 | +vi.mock(import("@effect/atom-react"), async (original) => ({ |
| 10 | + ...(await original()), |
| 11 | + useAtomValue: (() => false) as never, |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock(import("../../state/entities"), () => ({ |
| 15 | + useQuietThreadShell: (() => null) as never, |
| 16 | + useThreadProjection: ((ref: unknown) => |
| 17 | + ref === null ? null : { projection: state.projection }) as never, |
| 18 | + useThreadRelationshipShells: (() => |
| 19 | + state.projection === null ? [] : [(state.projection as { thread: unknown }).thread]) as never, |
| 20 | + useProjects: (() => []) as never, |
| 21 | +})); |
| 22 | +vi.mock(import("../../state/environments"), () => ({ |
| 23 | + useEnvironment: (() => null) as never, |
| 24 | + useEnvironmentProviders: (() => new Map()) as never, |
| 25 | +})); |
| 26 | +vi.mock(import("../../lib/archivedThreadsState"), () => ({ |
| 27 | + useArchivedThreadSnapshots: (() => ({ snapshots: [] })) as never, |
| 28 | +})); |
| 29 | +vi.mock(import("../../state/use-atom-command"), () => ({ |
| 30 | + useAtomCommand: (() => async () => |
| 31 | + state.commandSucceeds |
| 32 | + ? AsyncResult.success(undefined) |
| 33 | + : AsyncResult.failure(Cause.fail(new Error("Connection lost")))) as never, |
| 34 | +})); |
| 35 | + |
| 36 | +import { render } from "../../../test/render"; |
| 37 | +import { ToastProvider } from "../ui/toast"; |
| 38 | +import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl"; |
| 39 | + |
| 40 | +const environmentId = EnvironmentId.make("environment"); |
| 41 | +const threadId = ThreadId.make("fork"); |
| 42 | + |
| 43 | +function setup() { |
| 44 | + state.commandSucceeds = false; |
| 45 | + state.projection = { |
| 46 | + thread: { |
| 47 | + id: threadId, |
| 48 | + title: "Fork", |
| 49 | + activeProviderThreadId: null, |
| 50 | + lineage: { parentThreadId: ThreadId.make("source"), relationshipToParent: "fork" }, |
| 51 | + forkedFrom: null, |
| 52 | + }, |
| 53 | + runs: [{ id: "run", ordinal: 1, status: "completed" }], |
| 54 | + subagents: [], |
| 55 | + contextTransfers: [], |
| 56 | + providerThreads: [], |
| 57 | + providerSessions: [{ id: "session", status: "ready" }], |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +it.each([ |
| 62 | + ["merge", "Could not merge back"], |
| 63 | + ["disconnect", "Could not disconnect agent session"], |
| 64 | +])("reports a rejected %s command and clears the busy state", async (action, title) => { |
| 65 | + setup(); |
| 66 | + await using screen = await render( |
| 67 | + <ToastProvider timeout={0}> |
| 68 | + <ThreadRelationshipsPanel environmentId={environmentId} threadId={threadId} /> |
| 69 | + </ToastProvider>, |
| 70 | + ); |
| 71 | + |
| 72 | + if (action === "merge") { |
| 73 | + await screen.getByRole("button", { name: "Merge back to source conversation" }).click(); |
| 74 | + } else { |
| 75 | + await screen.getByRole("button", { name: "More thread actions" }).click(); |
| 76 | + await page.getByRole("menuitem", { name: "Disconnect agent session" }).click(); |
| 77 | + } |
| 78 | + |
| 79 | + await expect.element(page.getByText(title, { exact: true })).toBeVisible(); |
| 80 | + await expect |
| 81 | + .element( |
| 82 | + screen.getByRole("button", { |
| 83 | + name: action === "merge" ? "Merge back to source conversation" : "More thread actions", |
| 84 | + }), |
| 85 | + ) |
| 86 | + .toBeEnabled(); |
| 87 | +}); |
| 88 | + |
| 89 | +it.each([ |
| 90 | + ["merge", "Could not merge back"], |
| 91 | + ["disconnect", "Could not disconnect agent session"], |
| 92 | +])("clears the previous %s error after a successful retry", async (action, title) => { |
| 93 | + setup(); |
| 94 | + await using screen = await render( |
| 95 | + <ToastProvider timeout={0}> |
| 96 | + <ThreadRelationshipsPanel environmentId={environmentId} threadId={threadId} /> |
| 97 | + </ToastProvider>, |
| 98 | + { router: "/environment/fork" }, |
| 99 | + ); |
| 100 | + |
| 101 | + const runAction = async () => { |
| 102 | + if (action === "merge") { |
| 103 | + await screen.getByRole("button", { name: "Merge back to source conversation" }).click(); |
| 104 | + } else { |
| 105 | + await screen.getByRole("button", { name: "More thread actions" }).click(); |
| 106 | + await page.getByRole("menuitem", { name: "Disconnect agent session" }).click(); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + await runAction(); |
| 111 | + await expect.element(page.getByText(title, { exact: true })).toBeVisible(); |
| 112 | + expect(screen.router?.state.location.pathname).toBe("/environment/fork"); |
| 113 | + |
| 114 | + state.commandSucceeds = true; |
| 115 | + await runAction(); |
| 116 | + |
| 117 | + await expect.element(page.getByText(title, { exact: true })).not.toBeInTheDocument(); |
| 118 | + await vi.waitFor(() => { |
| 119 | + expect(screen.router?.state.location.pathname).toBe( |
| 120 | + action === "merge" ? "/environment/source" : "/environment/fork", |
| 121 | + ); |
| 122 | + }); |
| 123 | + if (action === "disconnect") { |
| 124 | + await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled(); |
| 125 | + } |
| 126 | +}); |
0 commit comments