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 { AuthFilesystemReadScope, EnvironmentId, type AuthSessionState } from "@t3tools/contracts"; |
| 2 | import { beforeEach, expect, it, vi } from "vite-plus/test"; |
| 3 | |
| 4 | const state = vi.hoisted(() => ({ |
| 5 | session: null as Pick<AuthSessionState, "authenticated" | "scopes"> | null, |
| 6 | sessionError: null as string | null, |
| 7 | phase: "connected" as "connected" | "offline", |
| 8 | sessionAtom: {}, |
| 9 | searchAtom: {}, |
| 10 | searchRequests: vi.fn(), |
| 11 | })); |
| 12 | |
| 13 | vi.mock("react", async (importOriginal) => ({ |
| 14 | ...(await importOriginal<typeof import("react")>()), |
| 15 | useCallback: <A>(callback: A) => callback, |
| 16 | useEffect: () => {}, |
| 17 | useMemo: <A>(factory: () => A) => factory(), |
| 18 | useState: <A>(value: A) => [value, vi.fn()], |
| 19 | })); |
| 20 | vi.mock("gt-react-native", async (importOriginal) => { |
| 21 | const gt = await importOriginal<typeof import("gt-react-native")>(); |
| 22 | return { ...gt, useGT: () => gt.mFallback, useMessages: () => gt.mFallback }; |
| 23 | }); |
| 24 | vi.mock("./session", () => ({ |
| 25 | environmentSession: { sessionStateAtom: () => state.sessionAtom }, |
| 26 | })); |
| 27 | vi.mock("./presentation", () => ({ |
| 28 | useEnvironmentPresentation: () => ({ |
| 29 | isReady: true, |
| 30 | presentation: { connection: { phase: state.phase, error: null } }, |
| 31 | }), |
| 32 | })); |
| 33 | vi.mock("./pull-requests", () => ({ composerPullRequests: {} })); |
| 34 | vi.mock("./projects", () => ({ |
| 35 | projectEnvironment: { |
| 36 | searchEntries: (target: unknown) => { |
| 37 | state.searchRequests(target); |
| 38 | return state.searchAtom; |
| 39 | }, |
| 40 | }, |
| 41 | })); |
| 42 | vi.mock("./atom-registry", () => ({ appAtomRegistry: {} })); |
| 43 | vi.mock("./orchestration", () => ({ orchestrationEnvironment: {} })); |
| 44 | vi.mock("./threads", () => ({ useEnvironmentThread: vi.fn() })); |
| 45 | vi.mock("./vcs", () => ({ vcsEnvironment: {} })); |
| 46 | vi.mock("./query", () => ({ |
| 47 | useEnvironmentQuery: (atom: unknown) => ({ |
| 48 | data: |
| 49 | atom === state.sessionAtom |
| 50 | ? state.session |
| 51 | : atom === state.searchAtom |
| 52 | ? { entries: [{ path: "src/index.ts", kind: "file" }] } |
| 53 | : null, |
| 54 | error: atom === state.sessionAtom ? state.sessionError : null, |
| 55 | isPending: atom === state.sessionAtom && state.session === null && state.sessionError === null, |
| 56 | refresh: vi.fn(), |
| 57 | }), |
| 58 | })); |
| 59 | |
| 60 | import { useComposerPathSearch } from "./queries"; |
| 61 | |
| 62 | const target = { |
| 63 | environmentId: EnvironmentId.make("test-environment"), |
| 64 | cwd: "/repo", |
| 65 | query: "src", |
| 66 | }; |
| 67 | |
| 68 | beforeEach(() => { |
| 69 | state.session = null; |
| 70 | state.sessionError = null; |
| 71 | state.phase = "connected"; |
| 72 | state.searchRequests.mockClear(); |
| 73 | }); |
| 74 | |
| 75 | it("keeps a file search pending until its grant loads, then shows matches", () => { |
| 76 | expect(useComposerPathSearch(target)).toMatchObject({ |
| 77 | entries: [], |
| 78 | error: null, |
| 79 | isPending: true, |
| 80 | }); |
| 81 | state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] }; |
| 82 | expect(useComposerPathSearch(target)).toMatchObject({ |
| 83 | entries: [{ path: "src/index.ts", kind: "file" }], |
| 84 | error: null, |
| 85 | isPending: false, |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | it("searches workspace entries for a bare @", () => { |
| 90 | state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] }; |
| 91 | useComposerPathSearch({ ...target, query: "" }); |
| 92 | expect(state.searchRequests).toHaveBeenCalledExactlyOnceWith({ |
| 93 | environmentId: target.environmentId, |
| 94 | input: { cwd: target.cwd, query: "", limit: 20 }, |
| 95 | }); |
| 96 | }); |