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 | sessionWaiting: false, |
| 8 | phase: "connected" as "connected" | "offline", |
| 9 | sessionAtom: {}, |
| 10 | searchAtom: {}, |
| 11 | searchRequests: vi.fn(), |
| 12 | contentAtom: {}, |
| 13 | contentRequests: vi.fn(), |
| 14 | contentError: null as string | null, |
| 15 | contentData: { |
| 16 | matches: [{ path: "src/index.ts", lineNumber: 3, lineContent: "a match", matchRanges: [] }], |
| 17 | truncated: false, |
| 18 | }, |
| 19 | })); |
| 20 | |
| 21 | vi.mock("react", async (importOriginal) => ({ |
| 22 | ...(await importOriginal<typeof import("react")>()), |
| 23 | useCallback: <A>(callback: A) => callback, |
| 24 | useEffect: () => {}, |
| 25 | useMemo: <A>(factory: () => A) => factory(), |
| 26 | useState: <A>(value: A) => [value, vi.fn()], |
| 27 | })); |
| 28 | vi.mock("./session", () => ({ |
| 29 | environmentSession: { sessionStateAtom: () => state.sessionAtom }, |
| 30 | })); |
| 31 | vi.mock("./presentation", () => ({ |
| 32 | useEnvironmentPresentation: () => ({ |
| 33 | isReady: true, |
| 34 | presentation: { connection: { phase: state.phase, error: null } }, |
| 35 | }), |
| 36 | })); |
| 37 | vi.mock("./projects", () => ({ |
| 38 | projectEnvironment: { |
| 39 | searchEntries: (target: unknown) => { |
| 40 | state.searchRequests(target); |
| 41 | return state.searchAtom; |
| 42 | }, |
| 43 | }, |
| 44 | projectContentSearch: (target: unknown) => { |
| 45 | state.contentRequests(target); |
| 46 | return state.contentAtom; |
| 47 | }, |
| 48 | })); |
| 49 | vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: {} })); |
| 50 | vi.mock("./orchestration", () => ({ orchestrationEnvironment: {} })); |
| 51 | vi.mock("./threads", () => ({ useEnvironmentThread: vi.fn() })); |
| 52 | vi.mock("./vcs", () => ({ vcsEnvironment: {} })); |
| 53 | vi.mock("./query", () => ({ |
| 54 | useEnvironmentQuery: (atom: unknown) => ({ |
| 55 | data: |
| 56 | atom === state.sessionAtom |
| 57 | ? state.session |
| 58 | : atom === state.searchAtom |
| 59 | ? { entries: [{ path: "src/index.ts", kind: "file" }] } |
| 60 | : atom === state.contentAtom |
| 61 | ? state.contentData |
| 62 | : null, |
| 63 | error: |
| 64 | atom === state.sessionAtom |
| 65 | ? state.sessionError |
| 66 | : atom === state.contentAtom |
| 67 | ? state.contentError |
| 68 | : null, |
| 69 | isPending: |
| 70 | atom === state.sessionAtom && |
| 71 | (state.sessionWaiting || (state.session === null && state.sessionError === null)), |
| 72 | refresh: vi.fn(), |
| 73 | }), |
| 74 | })); |
| 75 | |
| 76 | import { useComposerPathSearch, useProjectContentSearch } from "./queries"; |
| 77 | |
| 78 | const target = { |
| 79 | environmentId: EnvironmentId.make("test-environment"), |
| 80 | cwd: "/repo", |
| 81 | query: "src", |
| 82 | }; |
| 83 | |
| 84 | beforeEach(() => { |
| 85 | state.session = null; |
| 86 | state.sessionError = null; |
| 87 | state.sessionWaiting = false; |
| 88 | state.phase = "connected"; |
| 89 | state.searchRequests.mockClear(); |
| 90 | state.contentRequests.mockClear(); |
| 91 | state.contentError = null; |
| 92 | }); |
| 93 | |
| 94 | const contentTarget = { |
| 95 | ...target, |
| 96 | query: " a match ", |
| 97 | caseSensitive: true, |
| 98 | wholeWord: true, |
| 99 | useRegex: false, |
| 100 | }; |
| 101 | |
| 102 | it.each([ |
| 103 | { query: "", expected: "" }, |
| 104 | { query: "src", expected: "src" }, |
| 105 | { query: "a".repeat(256), expected: "a".repeat(256) }, |
| 106 | { |
| 107 | query: ["a".repeat(86), "b".repeat(86), "c".repeat(86), "unique.ts"].join("/"), |
| 108 | expected: ["a".repeat(86), "b".repeat(86), "c".repeat(82)].join("/"), |
| 109 | }, |
| 110 | { query: `${"a".repeat(255)}😀/boundary.ts`, expected: "a".repeat(255) }, |
| 111 | { query: `${"a".repeat(254)}😀/boundary.ts`, expected: `${"a".repeat(254)}😀` }, |
| 112 | ])( |
| 113 | "bounds the path search request while preserving query identity: $query", |
| 114 | ({ query, expected }) => { |
| 115 | state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] }; |
| 116 | const result = useComposerPathSearch({ ...target, query: ` ${query} ` }); |
| 117 | |
| 118 | expect(state.searchRequests).toHaveBeenCalledExactlyOnceWith({ |
| 119 | environmentId: target.environmentId, |
| 120 | input: { cwd: target.cwd, query: expected, limit: 80 }, |
| 121 | }); |
| 122 | expect(result.searchedTarget).toMatchObject({ ...target, query }); |
| 123 | expect(result.searchedQuery).toBe(query); |
| 124 | expect(result.isPending).toBe(false); |
| 125 | }, |
| 126 | ); |
| 127 | |
| 128 | it("explains an overlong content query instead of sending it", () => { |
| 129 | state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] }; |
| 130 | const result = useProjectContentSearch({ ...contentTarget, query: "a".repeat(257) }); |
| 131 | |
| 132 | expect(state.contentRequests).not.toHaveBeenCalled(); |
| 133 | expect(result.matches).toEqual([]); |
| 134 | expect(result.isPending).toBe(false); |
| 135 | expect(result.error).toMatch(/256/); |
| 136 | }); |
| 137 | |
| 138 | it("shows content-search denial before typing and never issues an unauthorized request", () => { |
| 139 | state.session = { authenticated: true, scopes: [] }; |
| 140 | for (const query of ["", contentTarget.query]) { |
| 141 | const result = useProjectContentSearch({ ...contentTarget, query }); |
| 142 | expect(state.contentRequests).not.toHaveBeenCalled(); |
| 143 | expect(result).toMatchObject({ |
| 144 | canReadFiles: false, |
| 145 | isCheckingAccess: false, |
| 146 | matches: [], |
| 147 | error: "This connection cannot search host files.", |
| 148 | isPending: false, |
| 149 | }); |
| 150 | } |
| 151 | expect(state.contentRequests).not.toHaveBeenCalled(); |
| 152 | }); |
| 153 | |
| 154 | it("keeps confirmed content access during revalidation and drops results when it is revoked", () => { |
| 155 | state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] }; |
| 156 | state.sessionWaiting = true; |
| 157 | expect(useProjectContentSearch(contentTarget)).toMatchObject({ |
| 158 | matches: state.contentData.matches, |
| 159 | isPending: false, |
| 160 | }); |
| 161 | |
| 162 | state.contentRequests.mockClear(); |
| 163 | state.sessionWaiting = false; |
| 164 | state.session = { authenticated: true, scopes: [] }; |
| 165 | const result = useProjectContentSearch(contentTarget); |
| 166 | expect(state.contentRequests).not.toHaveBeenCalled(); |
| 167 | expect(result).toMatchObject({ |
| 168 | canReadFiles: false, |
| 169 | matches: [], |
| 170 | error: "This connection cannot search host files.", |
| 171 | isPending: false, |
| 172 | }); |
| 173 | expect(state.contentRequests).not.toHaveBeenCalled(); |
| 174 | }); |
| 175 | |
| 176 | it("fails closed after a session check fails, even with a cached grant and matches", () => { |
| 177 | state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] }; |
| 178 | state.sessionError = "The session has expired."; |
| 179 | const result = useProjectContentSearch(contentTarget); |
| 180 | expect(state.contentRequests).not.toHaveBeenCalled(); |
| 181 | expect(result).toMatchObject({ |
| 182 | canReadFiles: false, |
| 183 | matches: [], |
| 184 | error: state.sessionError, |
| 185 | isPending: false, |
| 186 | }); |
| 187 | expect(state.contentRequests).not.toHaveBeenCalled(); |
| 188 | }); |