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 type { ReactNode } from "react"; |
| 2 | import { act } from "react"; |
| 3 | import * as React from "react"; |
| 4 | import { createRoot } from "react-dom/client"; |
| 5 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 6 | import { EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 7 | import { expect, it, vi } from "vite-plus/test"; |
| 8 | import { retargetSavedNewTaskDraft } from "../../state/use-composer-drafts"; |
| 9 | |
| 10 | const environmentId = EnvironmentId.make("env-1"); |
| 11 | |
| 12 | function project(id: string, workspaceRoot: string): EnvironmentProject { |
| 13 | return { |
| 14 | environmentId, |
| 15 | id: ProjectId.make(id), |
| 16 | title: id, |
| 17 | workspaceRoot, |
| 18 | repositoryIdentity: null, |
| 19 | defaultModelSelection: null, |
| 20 | defaultThreadEnvMode: "worktree", |
| 21 | scripts: [], |
| 22 | createdAt: "2026-10-26T00:00:00Z", |
| 23 | updatedAt: "2026-10-26T00:00:00Z", |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | const regular = project("app", "/repo/app"); |
| 28 | const scratch = project("scratch", "/scratch"); |
| 29 | |
| 30 | const harness = vi.hoisted(() => ({ |
| 31 | setProject: vi.fn(), |
| 32 | openScratch: vi.fn(), |
| 33 | selectDetail: vi.fn(), |
| 34 | navigation: { |
| 35 | goBack: vi.fn(), |
| 36 | getState: () => ({ |
| 37 | routeNames: ["Thread", "NewTaskProject"], |
| 38 | index: 1, |
| 39 | routes: [{ name: "Thread", params: { draft: {} } }, { name: "NewTaskProject" }], |
| 40 | }), |
| 41 | getParent: () => undefined, |
| 42 | canGoBack: () => true, |
| 43 | dispatch: vi.fn(), |
| 44 | navigate: vi.fn(), |
| 45 | }, |
| 46 | })); |
| 47 | |
| 48 | vi.mock("react-native", () => ({ |
| 49 | Platform: { OS: "ios" }, |
| 50 | View: ({ children }: { children?: ReactNode }) => <div>{children}</div>, |
| 51 | Pressable: (props: { |
| 52 | accessibilityLabel: string; |
| 53 | disabled?: boolean; |
| 54 | onPress: () => void; |
| 55 | children?: ReactNode; |
| 56 | }) => ( |
| 57 | <button aria-label={props.accessibilityLabel} disabled={props.disabled} onClick={props.onPress}> |
| 58 | {props.children} |
| 59 | </button> |
| 60 | ), |
| 61 | })); |
| 62 | vi.mock("react-native-safe-area-context", () => ({ |
| 63 | useSafeAreaInsets: () => ({ top: 0, bottom: 0, left: 0, right: 0 }), |
| 64 | })); |
| 65 | vi.mock("gt-react-native", () => ({ |
| 66 | msg: (text: string) => text, |
| 67 | T: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 68 | useGT: () => (text: string) => text, |
| 69 | useMessages: () => (text: string) => text, |
| 70 | })); |
| 71 | vi.mock("@react-navigation/native", () => ({ |
| 72 | StackActions: { push: (name: string) => ({ type: "PUSH", name }) }, |
| 73 | useFocusEffect: (effect: () => () => void) => React.useEffect(effect, [effect]), |
| 74 | useIsFocused: () => true, |
| 75 | useNavigation: () => harness.navigation, |
| 76 | })); |
| 77 | vi.mock("../../components/ui/NavigationRow", () => ({ |
| 78 | NavigationRow: (props: { title: string; disabled?: boolean; onPress?: () => void }) => ( |
| 79 | <button aria-label={props.title} disabled={props.disabled === true} onClick={props.onPress}> |
| 80 | {props.title} |
| 81 | </button> |
| 82 | ), |
| 83 | })); |
| 84 | vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() })); |
| 85 | vi.mock("../../components/ui/AppActivityIndicator", () => ({ AppActivityIndicator: () => null })); |
| 86 | vi.mock("../../components/ui/AppBar", () => ({ AppBar: () => null })); |
| 87 | vi.mock("../../components/ui/AppSymbol", () => ({ SymbolView: () => null })); |
| 88 | vi.mock("../../components/ui/Button", () => ({ Button: () => null })); |
| 89 | vi.mock("../../components/ui/ScreenContent", () => ({ |
| 90 | ScreenContent: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 91 | })); |
| 92 | vi.mock("../../components/ui/ScreenScrollView", () => ({ |
| 93 | ScreenScrollView: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 94 | })); |
| 95 | vi.mock("../../components/ui/AppText", () => ({ |
| 96 | AppText: ({ children }: { children?: ReactNode }) => <span>{children}</span>, |
| 97 | })); |
| 98 | vi.mock("../../components/ProjectFavicon", () => ({ ProjectFavicon: () => null })); |
| 99 | vi.mock("../connection/environmentStatusText", () => ({ runtimeConnectionErrorText: () => null })); |
| 100 | vi.mock("../../state/entities", () => ({ |
| 101 | useProjects: () => [regular, scratch], |
| 102 | useServerConfigs: () => new Map([[environmentId, { scratchWorkspaceRoot: "/scratch" }]]), |
| 103 | })); |
| 104 | vi.mock("../../state/projects", () => ({ projectEnvironment: { openScratch: {} } })); |
| 105 | vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => harness.openScratch })); |
| 106 | vi.mock("../../state/use-composer-drafts", () => ({ retargetSavedNewTaskDraft: vi.fn() })); |
| 107 | vi.mock("../../state/use-remote-environment-registry", () => ({ |
| 108 | useRemoteConnectionStatus: () => ({ |
| 109 | connectedEnvironments: [{ environmentId, connectionState: "connected" }], |
| 110 | }), |
| 111 | })); |
| 112 | vi.mock("../../state/workspace", () => ({ |
| 113 | useWorkspaceState: () => ({ state: { hasReadyEnvironment: true } }), |
| 114 | })); |
| 115 | vi.mock("../sharing/IncomingShareProvider", () => ({ |
| 116 | useIncomingShare: () => ({ getShare: () => null, releaseShareReservation: vi.fn() }), |
| 117 | })); |
| 118 | vi.mock("../layout/workspace/useSelectWorkspaceDetail", () => ({ |
| 119 | useSelectWorkspaceDetail: () => harness.selectDetail, |
| 120 | })); |
| 121 | vi.mock("./new-task-flow-provider", () => ({ |
| 122 | useNewTaskFlow: () => ({ |
| 123 | projectScopes: [ |
| 124 | { |
| 125 | key: "app", |
| 126 | title: "app", |
| 127 | representative: regular, |
| 128 | projects: [regular], |
| 129 | projectRefs: [], |
| 130 | pinned: false, |
| 131 | archived: false, |
| 132 | }, |
| 133 | ], |
| 134 | selectedEnvironmentId: environmentId, |
| 135 | setProject: harness.setProject, |
| 136 | }), |
| 137 | })); |
| 138 | |
| 139 | import { NewTaskRouteScreen } from "./NewTaskRouteScreen"; |
| 140 | |
| 141 | it("keeps the project picked while No project was still opening", async () => { |
| 142 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 143 | const opening = Promise.withResolvers<unknown>(); |
| 144 | harness.openScratch.mockReturnValue(opening.promise); |
| 145 | |
| 146 | const container = document.createElement("div"); |
| 147 | const root = createRoot(container); |
| 148 | using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 149 | act(() => root.render(<NewTaskRouteScreen route={{ params: undefined } as never} />)); |
| 150 | const row = (label: string) => container.querySelector<HTMLElement>(`[aria-label="${label}"]`)!; |
| 151 | |
| 152 | act(() => row("No project").click()); |
| 153 | await act(async () => row("app").click()); |
| 154 | await act(async () => opening.resolve({ _tag: "Success", value: scratch })); |
| 155 | |
| 156 | expect(harness.setProject.mock.calls).toEqual([[regular]]); |
| 157 | expect(harness.navigation.goBack).toHaveBeenCalledTimes(1); |
| 158 | }); |
| 159 | |
| 160 | function renderWithSavedDraft() { |
| 161 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 162 | const opening = Promise.withResolvers<unknown>(); |
| 163 | const retargeting = Promise.withResolvers<boolean>(); |
| 164 | harness.openScratch.mockReturnValue(opening.promise); |
| 165 | vi.mocked(retargetSavedNewTaskDraft) |
| 166 | .mockReset() |
| 167 | .mockImplementation((_draftId, target) => |
| 168 | target.projectId === scratch.id ? retargeting.promise : Promise.resolve(true), |
| 169 | ); |
| 170 | harness.selectDetail.mockClear(); |
| 171 | |
| 172 | const container = document.createElement("div"); |
| 173 | const root = createRoot(container); |
| 174 | const route = { params: { draftId: "existing-draft" } } as never; |
| 175 | act(() => root.render(<NewTaskRouteScreen key="first" route={route} />)); |
| 176 | return { |
| 177 | opening, |
| 178 | retargeting, |
| 179 | root, |
| 180 | route, |
| 181 | row: (label: string) => container.querySelector<HTMLElement>(`[aria-label="${label}"]`)!, |
| 182 | [Symbol.dispose]: () => act(() => root.unmount()), |
| 183 | }; |
| 184 | } |
| 185 | |
| 186 | const appDraftSelection = [ |
| 187 | [ |
| 188 | { |
| 189 | name: "Thread", |
| 190 | params: { draft: { environmentId, projectId: regular.id, draftId: "existing-draft" } }, |
| 191 | }, |
| 192 | ], |
| 193 | ]; |
| 194 | |
| 195 | it("keeps the picked project after backing out and reopening while No project is pending", async () => { |
| 196 | using screen = renderWithSavedDraft(); |
| 197 | |
| 198 | act(() => screen.row("No project").click()); |
| 199 | act(() => screen.root.render(null)); |
| 200 | act(() => screen.root.render(<NewTaskRouteScreen key="second" route={screen.route} />)); |
| 201 | await act(async () => screen.row("app").click()); |
| 202 | await act(async () => { |
| 203 | screen.opening.resolve({ _tag: "Success", value: scratch }); |
| 204 | screen.retargeting.resolve(true); |
| 205 | }); |
| 206 | |
| 207 | expect(harness.selectDetail.mock.calls).toEqual(appDraftSelection); |
| 208 | }); |
| 209 | |
| 210 | it("cancels the No project draft retarget when another project is picked", async () => { |
| 211 | using screen = renderWithSavedDraft(); |
| 212 | |
| 213 | act(() => screen.row("No project").click()); |
| 214 | await act(async () => screen.opening.resolve({ _tag: "Success", value: scratch })); |
| 215 | const scratchSignal = vi |
| 216 | .mocked(retargetSavedNewTaskDraft) |
| 217 | .mock.calls.find(([, target]) => target.projectId === scratch.id)?.[2]; |
| 218 | expect(scratchSignal?.aborted).toBe(false); |
| 219 | await act(async () => screen.row("app").click()); |
| 220 | expect(scratchSignal?.aborted).toBe(true); |
| 221 | await act(async () => screen.retargeting.resolve(true)); |
| 222 | |
| 223 | expect(harness.selectDetail.mock.calls).toEqual(appDraftSelection); |
| 224 | }); |