|
| 1 | +import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | +import { scopedThreadKey } from "@t3tools/client-runtime/environment"; |
| 3 | +import { expect, it, vi } from "vite-plus/test"; |
| 4 | +import { act, render } from "../../test/render"; |
| 5 | +import { selectThreadRightPanelState, useRightPanelStore } from "../rightPanelStore"; |
| 6 | +import { useTerminalUiStateStore } from "../terminalUiStateStore"; |
| 7 | +import { useRightPanelSurfaceClose } from "./useRightPanelSurfaceClose"; |
| 8 | + |
| 9 | +const fixture = vi.hoisted(() => ({ |
| 10 | + confirm: (_labels: readonly string[]): Promise<boolean> => Promise.resolve(false), |
| 11 | + allowed: new Set<string>(), |
| 12 | + sessions: new Map<string, string>(), |
| 13 | + failClose: false, |
| 14 | +})); |
| 15 | +vi.mock("../lib/terminalCloseConfirm", () => ({ |
| 16 | + confirmTerminalClose: (labels: readonly string[]) => fixture.confirm(labels), |
| 17 | +})); |
| 18 | +vi.mock("../components/ChatView.logic", () => ({ |
| 19 | + agentControlledBrowserCloseConfirmation: () => null, |
| 20 | +})); |
| 21 | +vi.mock("../previewStateStore", () => ({ |
| 22 | + useThreadPreviewState: () => ({ sessions: {}, desktopByTabId: {} }), |
| 23 | + setActivePreviewTab: () => {}, |
| 24 | + beginPreviewSessionClose: () => {}, |
| 25 | + cancelPreviewSessionClose: () => {}, |
| 26 | +})); |
| 27 | +vi.mock("../state/session", () => ({ |
| 28 | + useEnvironmentScope: (id: string) => fixture.allowed.has(id), |
| 29 | + readEnvironmentScope: (id: string) => fixture.allowed.has(id), |
| 30 | +})); |
| 31 | +vi.mock("../state/entities", () => ({ |
| 32 | + useQuietThreadShell: () => null, |
| 33 | + useProject: () => null, |
| 34 | +})); |
| 35 | +vi.mock("../state/terminalSessions", () => ({ useKnownTerminalSessions: () => null })); |
| 36 | +vi.mock("../composerDraftStore", async () => { |
| 37 | + const Schema = await import("effect/Schema"); |
| 38 | + return { |
| 39 | + useComposerDraftStore: () => null, |
| 40 | + DraftId: Schema.String.pipe(Schema.brand("DraftId")), |
| 41 | + }; |
| 42 | +}); |
| 43 | +vi.mock("../state/use-atom-command", () => ({ |
| 44 | + useAtomCommand: |
| 45 | + (_command: unknown, label: string) => |
| 46 | + async (request: { |
| 47 | + environmentId: string; |
| 48 | + input: { threadId: string; terminalId: string; data?: string }; |
| 49 | + }) => { |
| 50 | + const key = `${request.environmentId}/${request.input.threadId}/${request.input.terminalId}`; |
| 51 | + if (label === "terminal close" && fixture.failClose) return { _tag: "Failure" }; |
| 52 | + if (label === "terminal close" || label === "terminal write") fixture.sessions.delete(key); |
| 53 | + return { _tag: "Success" }; |
| 54 | + }, |
| 55 | +})); |
| 56 | +// The commands are replaced above; importing their runtime would connect a client. |
| 57 | +vi.mock("../state/terminal", () => ({ terminalEnvironment: { open: {}, close: {}, write: {} } })); |
| 58 | +vi.mock("../state/preview", () => ({ previewEnvironment: { close: {} } })); |
| 59 | +vi.mock("@t3tools/client-runtime/state/runtime", () => ({ |
| 60 | + isAtomCommandInterrupted: () => false, |
| 61 | +})); |
| 62 | + |
| 63 | +function setup() { |
| 64 | + fixture.allowed = new Set(["environment-a", "environment-b"]); |
| 65 | + fixture.sessions = new Map(); |
| 66 | + fixture.failClose = false; |
| 67 | + useRightPanelStore.setState({ byThreadKey: {} }); |
| 68 | + useTerminalUiStateStore.setState({ terminalUiStateByThreadKey: {} }); |
| 69 | + const original: ScopedThreadRef = { |
| 70 | + environmentId: EnvironmentId.make("environment-a"), |
| 71 | + threadId: ThreadId.make("thread-a"), |
| 72 | + }; |
| 73 | + const other: ScopedThreadRef = { |
| 74 | + environmentId: EnvironmentId.make("environment-b"), |
| 75 | + threadId: ThreadId.make("thread-b"), |
| 76 | + }; |
| 77 | + for (const ref of [original, other]) { |
| 78 | + fixture.sessions.set(`${ref.environmentId}/${ref.threadId}/term-1`, "history to preserve"); |
| 79 | + useRightPanelStore.getState().openTerminal(ref, "term-1"); |
| 80 | + } |
| 81 | + const surface = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, original) |
| 82 | + .surfaces[0]!; |
| 83 | + let resolve!: (confirmed: boolean) => void; |
| 84 | + const confirmation = new Promise<boolean>((done) => { |
| 85 | + resolve = done; |
| 86 | + }); |
| 87 | + fixture.confirm = () => confirmation; |
| 88 | + function Harness({ threadRef }: { threadRef: ScopedThreadRef }) { |
| 89 | + const { closeSurface } = useRightPanelSurfaceClose({ |
| 90 | + threadRef, |
| 91 | + terminalLabelsById: new Map(), |
| 92 | + }); |
| 93 | + return <button onClick={() => closeSurface(surface)}>Close original terminal</button>; |
| 94 | + } |
| 95 | + return { original, other, Harness, resolve }; |
| 96 | +} |
| 97 | + |
| 98 | +it.each([false, true])( |
| 99 | + "keeps a pending close on its original environment and thread, fallback=%s", |
| 100 | + async (failClose) => { |
| 101 | + const { original, other, Harness, resolve } = setup(); |
| 102 | + fixture.failClose = failClose; |
| 103 | + await using screen = await render(<Harness threadRef={original} />); |
| 104 | + await screen.getByRole("button").click(); |
| 105 | + await screen.rerender(<Harness threadRef={other} />); |
| 106 | + await act(async () => { |
| 107 | + resolve(true); |
| 108 | + }); |
| 109 | + expect([...fixture.sessions]).toEqual([ |
| 110 | + ["environment-b/thread-b/term-1", "history to preserve"], |
| 111 | + ]); |
| 112 | + const panels = useRightPanelStore.getState().byThreadKey; |
| 113 | + expect(selectThreadRightPanelState(panels, original).surfaces).toEqual([]); |
| 114 | + expect(selectThreadRightPanelState(panels, other).surfaces).toHaveLength(1); |
| 115 | + }, |
| 116 | +); |
| 117 | + |
| 118 | +it.each(["cancel", "revoked"])( |
| 119 | + "leaves both sessions intact when confirmation is %s", |
| 120 | + async (outcome) => { |
| 121 | + const { original, other, Harness, resolve } = setup(); |
| 122 | + await using screen = await render(<Harness threadRef={original} />); |
| 123 | + await screen.getByRole("button").click(); |
| 124 | + await screen.rerender(<Harness threadRef={other} />); |
| 125 | + if (outcome === "revoked") fixture.allowed.delete(original.environmentId); |
| 126 | + await act(async () => { |
| 127 | + resolve(outcome !== "cancel"); |
| 128 | + }); |
| 129 | + expect(fixture.sessions.size).toBe(2); |
| 130 | + expect( |
| 131 | + useRightPanelStore.getState().byThreadKey[scopedThreadKey(other)]?.surfaces, |
| 132 | + ).toHaveLength(1); |
| 133 | + }, |
| 134 | +); |
0 commit comments