|
| 1 | +import type { ReactNode } from "react"; |
| 2 | +import { act, useSyncExternalStore } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { AsyncResult } from "effect/reactivity"; |
| 5 | +import { expect, it, vi } from "vite-plus/test"; |
| 6 | + |
| 7 | +interface Snapshot { |
| 8 | + readonly projects: ReadonlyArray<{ |
| 9 | + readonly environmentId: string; |
| 10 | + readonly id: string; |
| 11 | + readonly workspaceRoot: string; |
| 12 | + }>; |
| 13 | + readonly catalog: { |
| 14 | + readonly isLoadingConnections: boolean; |
| 15 | + readonly hasLoadedShellSnapshot: boolean; |
| 16 | + readonly hasConnectingEnvironment: boolean; |
| 17 | + readonly connectionError: string | null; |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +const harness = vi.hoisted(() => { |
| 22 | + const listeners = new Set<() => void>(); |
| 23 | + const state: { snapshot: Snapshot | null } = { snapshot: null }; |
| 24 | + return { |
| 25 | + state, |
| 26 | + set(next: Snapshot) { |
| 27 | + state.snapshot = next; |
| 28 | + for (const listener of listeners) listener(); |
| 29 | + }, |
| 30 | + subscribe(listener: () => void) { |
| 31 | + listeners.add(listener); |
| 32 | + return () => listeners.delete(listener); |
| 33 | + }, |
| 34 | + route: { key: "thread-route", name: "Thread", params: {} as object }, |
| 35 | + reset: () => undefined, |
| 36 | + checkout: vi.fn(), |
| 37 | + // Hook results stay stable across renders, as the real hooks' are. |
| 38 | + layout: { deferThreadSelectionUntil: () => undefined }, |
| 39 | + navigation: { goBack: () => undefined }, |
| 40 | + preventContext: { preventedRoutes: {} }, |
| 41 | + }; |
| 42 | +}); |
| 43 | + |
| 44 | +function useSnapshot(): Snapshot { |
| 45 | + return useSyncExternalStore(harness.subscribe, () => harness.state.snapshot!); |
| 46 | +} |
| 47 | + |
| 48 | +vi.mock("react-native", () => ({ |
| 49 | + View: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 50 | +})); |
| 51 | +vi.mock("../../components/ui/AppText", () => ({ |
| 52 | + AppText: ({ children }: { children: ReactNode }) => <span>{children}</span>, |
| 53 | +})); |
| 54 | +vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() })); |
| 55 | +vi.mock("../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null })); |
| 56 | +vi.mock("./ThreadScreenHost", () => ({ |
| 57 | + ThreadScreenFallback: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 58 | +})); |
| 59 | +vi.mock("./NewTaskDraftScreen", () => ({ |
| 60 | + NewTaskDraftScreen: (props: { initialProjectRef: { branch?: string | null } }) => ( |
| 61 | + <p>draft on {props.initialProjectRef.branch}</p> |
| 62 | + ), |
| 63 | +})); |
| 64 | +vi.mock("./new-task-context-presentation", () => ({ |
| 65 | + resolveNewTaskBranchWorktreePath: () => null, |
| 66 | +})); |
| 67 | +vi.mock("./new-task-flow-provider", () => ({ |
| 68 | + useNewTaskFlow: () => harness, |
| 69 | +})); |
| 70 | +vi.mock("./checkout-new-task-branch", () => ({ checkoutNewTaskBranch: harness.checkout })); |
| 71 | +vi.mock("../layout/AdaptiveWorkspaceLayout", () => ({ |
| 72 | + useAdaptiveWorkspaceLayout: () => harness.layout, |
| 73 | +})); |
| 74 | +vi.mock("../../state/entities", () => ({ useProjects: () => useSnapshot().projects })); |
| 75 | +vi.mock("../../state/workspace", () => ({ |
| 76 | + useWorkspaceState: () => ({ state: useSnapshot().catalog }), |
| 77 | +})); |
| 78 | +vi.mock("../../state/vcs", () => ({ vcsEnvironment: { readRefs: {}, switchRef: {} } })); |
| 79 | +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => harness.checkout })); |
| 80 | +vi.mock("@react-navigation/native", () => ({ |
| 81 | + CommonActions: { goBack: () => ({ type: "GO_BACK" }) }, |
| 82 | + createNavigationContainerRef: () => ({ current: null }), |
| 83 | + useNavigation: () => harness.navigation, |
| 84 | + useRoute: () => harness.route, |
| 85 | + usePreventRemove: () => undefined, |
| 86 | + usePreventRemoveContext: () => harness.preventContext, |
| 87 | +})); |
| 88 | + |
| 89 | +import { NewTaskDraftRouteScreen } from "./NewTaskDraftRouteScreen"; |
| 90 | + |
| 91 | +const project = { environmentId: "env-1", id: "project-1", workspaceRoot: "/repo" }; |
| 92 | +const connected: Snapshot["catalog"] = { |
| 93 | + isLoadingConnections: false, |
| 94 | + hasLoadedShellSnapshot: true, |
| 95 | + hasConnectingEnvironment: false, |
| 96 | + connectionError: null, |
| 97 | +}; |
| 98 | + |
| 99 | +it("checks the branch out once when a reconnect drops and restores the project", async () => { |
| 100 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 101 | + const firstCheckout = Promise.withResolvers<unknown>(); |
| 102 | + harness.checkout.mockReturnValueOnce(firstCheckout.promise); |
| 103 | + harness.checkout.mockResolvedValue( |
| 104 | + AsyncResult.success({ name: "feature", current: true, isDefault: false, worktreePath: null }), |
| 105 | + ); |
| 106 | + harness.set({ projects: [project], catalog: connected }); |
| 107 | + const draft = { environmentId: "env-1", projectId: "project-1", branch: "feature" }; |
| 108 | + harness.route = { key: "thread-route", name: "Thread", params: { draft } }; |
| 109 | + |
| 110 | + const container = document.createElement("div"); |
| 111 | + const root = createRoot(container); |
| 112 | + using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 113 | + act(() => |
| 114 | + root.render( |
| 115 | + <NewTaskDraftRouteScreen |
| 116 | + route={{ key: "thread-route", name: "Thread", params: draft } as never} |
| 117 | + />, |
| 118 | + ), |
| 119 | + ); |
| 120 | + await vi.waitFor(() => expect(harness.checkout).toHaveBeenCalledTimes(1)); |
| 121 | + |
| 122 | + // The connection drops while the checkout runs, then the project comes back. |
| 123 | + act(() => harness.set({ projects: [], catalog: { ...connected, isLoadingConnections: true } })); |
| 124 | + act(() => harness.set({ projects: [project], catalog: connected })); |
| 125 | + await act(async () => { |
| 126 | + firstCheckout.resolve( |
| 127 | + AsyncResult.success({ name: "feature", current: true, isDefault: false, worktreePath: null }), |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + await vi.waitFor(() => |
| 132 | + expect( |
| 133 | + harness.checkout.mock.calls.length > 1 || container.textContent === "draft on feature", |
| 134 | + ).toBe(true), |
| 135 | + ); |
| 136 | + expect(harness.checkout).toHaveBeenCalledTimes(1); |
| 137 | + expect(container.textContent).toBe("draft on feature"); |
| 138 | +}); |
0 commit comments