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 * as Cause from "effect/Cause"; |
| 2 | import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 3 | import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 4 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 5 | import { expect, it, vi } from "vite-plus/test"; |
| 6 | import { page } from "vite-plus/test/browser"; |
| 7 | |
| 8 | const state = vi.hoisted(() => ({ |
| 9 | childThreadInputs: [] as EnvironmentThreadShell[], |
| 10 | switchRef: (async () => undefined) as (input: unknown) => Promise<unknown>, |
| 11 | })); |
| 12 | |
| 13 | vi.mock(import("../../state/session"), async (importOriginal) => ({ |
| 14 | ...(await importOriginal()), |
| 15 | useEnvironmentScope: (() => true) as never, |
| 16 | readEnvironmentScope: (() => true) as never, |
| 17 | })); |
| 18 | |
| 19 | vi.mock(import("../../state/use-atom-command"), async (importOriginal) => ({ |
| 20 | ...(await importOriginal()), |
| 21 | useAtomCommand: (() => (input: unknown) => state.switchRef(input)) as never, |
| 22 | })); |
| 23 | |
| 24 | vi.mock(import("../../state/use-orchestration-command"), async (importOriginal) => ({ |
| 25 | ...(await importOriginal()), |
| 26 | useOrchestrationCommand: (() => (input: unknown) => state.switchRef(input)) as never, |
| 27 | })); |
| 28 | |
| 29 | vi.mock(import("../../hooks/useThreadActions"), async (importOriginal) => ({ |
| 30 | ...(await importOriginal()), |
| 31 | useThreadActions: (() => ({})) as never, |
| 32 | useAcknowledgeThreadWoke: (() => () => undefined) as never, |
| 33 | })); |
| 34 | |
| 35 | vi.mock(import("../../state/entities"), async (importOriginal) => ({ |
| 36 | ...(await importOriginal()), |
| 37 | useChildThreadInputs: (() => state.childThreadInputs) as never, |
| 38 | })); |
| 39 | |
| 40 | import { render } from "../../../test/render"; |
| 41 | import { useThreadLifecycle } from "./useThreadLifecycle"; |
| 42 | |
| 43 | const environmentId = EnvironmentId.make("environment:test"); |
| 44 | |
| 45 | function setup() { |
| 46 | state.childThreadInputs = []; |
| 47 | state.switchRef = async () => undefined; |
| 48 | } |
| 49 | |
| 50 | function threadInput(threadId: string, branch: string) { |
| 51 | const ref = scopeThreadRef(environmentId, ThreadId.make(threadId)); |
| 52 | const thread = { id: ref.threadId, environmentId, branch, worktreePath: null }; |
| 53 | return { |
| 54 | routeThreadKey: scopedThreadKey(ref), |
| 55 | activeThread: thread as never, |
| 56 | activeThreadId: ref.threadId, |
| 57 | activeThreadRef: ref, |
| 58 | threadDetailRef: ref, |
| 59 | activeThreadKey: scopedThreadKey(ref), |
| 60 | localCheckoutBranchMismatch: { threadBranch: branch, currentBranch: "main" }, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | type ThreadInput = ReturnType<typeof threadInput>; |
| 65 | |
| 66 | it.each([ |
| 67 | { remote: false, approval: false, link: "Open question" }, |
| 68 | { remote: true, approval: false, link: "Open question" }, |
| 69 | { remote: true, approval: false, link: "Child" }, |
| 70 | { remote: true, approval: true, link: "Open approval" }, |
| 71 | ])("opens the child in its environment ($remote, $link)", async ({ remote, approval, link }) => { |
| 72 | setup(); |
| 73 | const parent = threadInput("parent", "main"); |
| 74 | const childEnvironmentId = remote ? EnvironmentId.make("remote") : environmentId; |
| 75 | state.childThreadInputs = [ |
| 76 | { |
| 77 | id: ThreadId.make("child"), |
| 78 | environmentId: childEnvironmentId, |
| 79 | title: "Child", |
| 80 | hasPendingApprovals: approval, |
| 81 | lineage: { sideChat: false }, |
| 82 | } as EnvironmentThreadShell, |
| 83 | ]; |
| 84 | await using screen = await render( |
| 85 | <RestoreBranchHarness |
| 86 | thread={parent} |
| 87 | currentRouteThreadKeyRef={{ current: parent.routeThreadKey }} |
| 88 | refreshGitStatus={() => undefined} |
| 89 | focusComposer={() => undefined} |
| 90 | />, |
| 91 | { router: "/environment:test/parent" }, |
| 92 | ); |
| 93 | await screen.getByRole("button", { name: link, exact: true }).click(); |
| 94 | await vi.waitFor(() => |
| 95 | expect(screen.router?.state.location.pathname).toBe( |
| 96 | `/${encodeURIComponent(childEnvironmentId)}/child`, |
| 97 | ), |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | function RestoreBranchHarness(props: { |
| 102 | readonly thread: ThreadInput; |
| 103 | readonly currentRouteThreadKeyRef: { readonly current: string | null }; |
| 104 | readonly refreshGitStatus: () => void; |
| 105 | readonly focusComposer: () => void; |
| 106 | }) { |
| 107 | const lifecycle = useThreadLifecycle({ |
| 108 | ...props.thread, |
| 109 | environmentId, |
| 110 | currentRouteThreadKeyRef: props.currentRouteThreadKeyRef, |
| 111 | canOperateThread: true, |
| 112 | clientSettingsHydrated: true, |
| 113 | activeThreadShell: null, |
| 114 | limitRecoveryHoldsSnooze: false, |
| 115 | activeLatestRun: null as never, |
| 116 | activeProjectCwd: "/repo", |
| 117 | serverConfig: null, |
| 118 | runtimeMode: "full-access" as never, |
| 119 | isWorking: false, |
| 120 | runIsLive: false, |
| 121 | pendingBackgroundTasks: [{ taskId: "background-command", kind: "command" }] as never, |
| 122 | turnItems: [], |
| 123 | activePendingApproval: null as never, |
| 124 | activePendingUserInput: null as never, |
| 125 | gitStatusQuery: { refresh: props.refreshGitStatus }, |
| 126 | nowMinute: "", |
| 127 | composerRef: { current: { focusAtEnd: props.focusComposer } } as never, |
| 128 | composerDraftTarget: { kind: "thread", ref: props.thread.activeThreadRef } as never, |
| 129 | sendInFlightRef: { current: false }, |
| 130 | setOptimisticUserMessages: () => undefined, |
| 131 | setThreadError: () => undefined, |
| 132 | clearUsageLimitsFor: () => undefined, |
| 133 | beginLocalDispatch: () => undefined, |
| 134 | resetLocalDispatch: () => undefined, |
| 135 | scrollToEnd: () => undefined, |
| 136 | persistThreadSettingsForNextTurn: async () => ({ _tag: "Success", value: undefined }) as never, |
| 137 | }); |
| 138 | return ( |
| 139 | <> |
| 140 | {lifecycle.childInputNow?.description} |
| 141 | {lifecycle.childInputNow?.actions} |
| 142 | {lifecycle.workNow?.actions} |
| 143 | <button |
| 144 | type="button" |
| 145 | disabled={lifecycle.isRestoringThreadBranch} |
| 146 | onClick={lifecycle.handleSwitchCheckoutToThreadStable} |
| 147 | > |
| 148 | {lifecycle.isRestoringThreadBranch ? "Restoring..." : "Restore branch"} |
| 149 | </button> |
| 150 | </> |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | it.each(["a", "b", "navigation"] as const)( |
| 155 | "isolates restore completion and queued focus for %s", |
| 156 | async (scenario) => { |
| 157 | const first = scenario === "b" ? "b" : "a"; |
| 158 | setup(); |
| 159 | const completions: Array<(result: unknown) => void> = []; |
| 160 | const switchRef = vi.fn(() => new Promise<unknown>((resolve) => completions.push(resolve))); |
| 161 | state.switchRef = switchRef; |
| 162 | const threadA = threadInput("thread:a", "feature-a"); |
| 163 | const threadB = threadInput("thread:b", "feature-b"); |
| 164 | const currentRouteThreadKeyRef = { current: threadA.routeThreadKey }; |
| 165 | const refreshGitStatus = vi.fn(); |
| 166 | const focusComposer = vi.fn(); |
| 167 | const harness = (thread: ThreadInput) => ( |
| 168 | <RestoreBranchHarness |
| 169 | thread={thread} |
| 170 | currentRouteThreadKeyRef={currentRouteThreadKeyRef} |
| 171 | refreshGitStatus={refreshGitStatus} |
| 172 | focusComposer={focusComposer} |
| 173 | /> |
| 174 | ); |
| 175 | await using screen = await render(harness(threadA)); |
| 176 | const focusFrames: FrameRequestCallback[] = []; |
| 177 | using _frame = vi.spyOn(window, "requestAnimationFrame").mockImplementation((callback) => { |
| 178 | focusFrames.push(callback); |
| 179 | return 0; |
| 180 | }); |
| 181 | |
| 182 | await page.getByRole("button", { name: "Restore branch" }).click(); |
| 183 | await expect.element(page.getByRole("button", { name: "Restoring..." })).toBeDisabled(); |
| 184 | |
| 185 | // ChatView stays mounted across server threads, so the hook sees thread B in place. |
| 186 | currentRouteThreadKeyRef.current = threadB.routeThreadKey; |
| 187 | await screen.rerender(harness(threadB)); |
| 188 | await expect.element(page.getByRole("button", { name: "Restore branch" })).toBeEnabled(); |
| 189 | await page.getByRole("button", { name: "Restore branch" }).click(); |
| 190 | expect(switchRef).toHaveBeenLastCalledWith( |
| 191 | expect.objectContaining({ input: { cwd: "/repo", refName: "feature-b" } }), |
| 192 | ); |
| 193 | |
| 194 | // Back on thread A, its own restore is still pending, so it can't start a second one. |
| 195 | currentRouteThreadKeyRef.current = threadA.routeThreadKey; |
| 196 | await screen.rerender(harness(threadA)); |
| 197 | await expect.element(page.getByRole("button", { name: "Restoring..." })).toBeDisabled(); |
| 198 | currentRouteThreadKeyRef.current = threadB.routeThreadKey; |
| 199 | await screen.rerender(harness(threadB)); |
| 200 | |
| 201 | const completed = first === "a" ? threadA : threadB; |
| 202 | const pending = first === "a" ? threadB : threadA; |
| 203 | currentRouteThreadKeyRef.current = pending.routeThreadKey; |
| 204 | await screen.rerender(harness(pending)); |
| 205 | completions[first === "a" ? 0 : 1]!({ |
| 206 | _tag: "Success", |
| 207 | value: { refName: completed.localCheckoutBranchMismatch.threadBranch }, |
| 208 | }); |
| 209 | await vi.waitFor(() => expect(refreshGitStatus).toHaveBeenCalledOnce()); |
| 210 | expect(focusComposer).not.toHaveBeenCalled(); |
| 211 | await expect.element(page.getByRole("button", { name: "Restoring..." })).toBeDisabled(); |
| 212 | expect(switchRef).toHaveBeenCalledTimes(2); |
| 213 | |
| 214 | completions[first === "a" ? 1 : 0]!({ |
| 215 | _tag: "Success", |
| 216 | value: { refName: pending.localCheckoutBranchMismatch.threadBranch }, |
| 217 | }); |
| 218 | await expect.element(page.getByRole("button", { name: "Restore branch" })).toBeEnabled(); |
| 219 | expect(refreshGitStatus).toHaveBeenCalledTimes(2); |
| 220 | expect(focusComposer).not.toHaveBeenCalled(); |
| 221 | expect(focusFrames).toHaveLength(1); |
| 222 | if (scenario === "navigation") { |
| 223 | currentRouteThreadKeyRef.current = completed.routeThreadKey; |
| 224 | await screen.rerender(harness(completed)); |
| 225 | } |
| 226 | focusFrames[0]!(0); |
| 227 | expect(focusComposer).toHaveBeenCalledTimes(scenario === "navigation" ? 0 : 1); |
| 228 | }, |
| 229 | ); |
| 230 | |
| 231 | it.each(["Success", "Failure"] as const)( |
| 232 | "keeps background stop requests per thread after %s", |
| 233 | async (outcome) => { |
| 234 | setup(); |
| 235 | const completions: Array<(result: unknown) => void> = []; |
| 236 | state.switchRef = vi.fn(() => new Promise((resolve) => completions.push(resolve))); |
| 237 | const threadA = threadInput("thread:a", "feature-a"); |
| 238 | const threadB = threadInput("thread:b", "feature-b"); |
| 239 | const currentRouteThreadKeyRef = { current: threadA.routeThreadKey }; |
| 240 | const harness = (thread: ThreadInput) => ( |
| 241 | <RestoreBranchHarness |
| 242 | thread={thread} |
| 243 | currentRouteThreadKeyRef={currentRouteThreadKeyRef} |
| 244 | refreshGitStatus={() => undefined} |
| 245 | focusComposer={() => undefined} |
| 246 | /> |
| 247 | ); |
| 248 | await using screen = await render(harness(threadA)); |
| 249 | await page.getByRole("button", { name: "Stop", exact: true }).click(); |
| 250 | await expect.element(page.getByRole("button", { name: "Stopping..." })).toBeDisabled(); |
| 251 | await screen.rerender(harness(threadB)); |
| 252 | await page.getByRole("button", { name: "Stop", exact: true }).click(); |
| 253 | await screen.rerender(harness(threadA)); |
| 254 | await expect.element(page.getByRole("button", { name: "Stopping..." })).toBeDisabled(); |
| 255 | completions[0]!( |
| 256 | outcome === "Success" |
| 257 | ? { _tag: "Success", value: undefined } |
| 258 | : { _tag: "Failure", cause: Cause.fail(new Error("stop failed")) }, |
| 259 | ); |
| 260 | if (outcome === "Success") { |
| 261 | await expect.element(page.getByRole("button", { name: "Stopping..." })).toBeDisabled(); |
| 262 | } else { |
| 263 | await expect.element(page.getByRole("button", { name: "Stop", exact: true })).toBeEnabled(); |
| 264 | } |
| 265 | await screen.rerender(harness(threadB)); |
| 266 | await expect.element(page.getByRole("button", { name: "Stopping..." })).toBeDisabled(); |
| 267 | expect(state.switchRef).toHaveBeenCalledTimes(2); |
| 268 | completions[1]!({ _tag: "Success", value: undefined }); |
| 269 | }, |
| 270 | ); |