Open History to see the last commit
1import { AuthFilesystemReadScope, EnvironmentId, type AuthSessionState } from "@t3tools/contracts";
2import { beforeEach, expect, it, vi } from "vite-plus/test";
3
4const 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 contentAtom: {},
12 contentRequests: vi.fn(),
13 contentError: null as string | null,
14 contentData: {
15 matches: [{ path: "src/index.ts", lineNumber: 3, lineContent: "a match", matchRanges: [] }],
16 truncated: false,
17 },
18}));
19
20vi.mock("react", async (importOriginal) => ({
21 ...(await importOriginal<typeof import("react")>()),
22 useCallback: <A>(callback: A) => callback,
23 useEffect: () => {},
24 useMemo: <A>(factory: () => A) => factory(),
25 useState: <A>(value: A) => [value, vi.fn()],
26}));
27vi.mock("./session", () => ({
28 environmentSession: { sessionStateAtom: () => state.sessionAtom },
29}));
30vi.mock("./presentation", () => ({
31 useEnvironmentPresentation: () => ({
32 isReady: true,
33 presentation: { connection: { phase: state.phase, error: null } },
34 }),
35}));
36vi.mock("./projects", () => ({
37 projectEnvironment: { searchEntries: () => state.searchAtom },
38 projectContentSearch: (target: unknown) => {
39 state.contentRequests(target);
40 return state.contentAtom;
41 },
42}));
43vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: {} }));
44vi.mock("./orchestration", () => ({ orchestrationEnvironment: {} }));
45vi.mock("./threads", () => ({ useEnvironmentThread: vi.fn() }));
46vi.mock("./vcs", () => ({ vcsEnvironment: {} }));
47vi.mock("./query", () => ({
48 useEnvironmentQuery: (atom: unknown) => ({
49 data:
50 atom === state.sessionAtom
51 ? state.session
52 : atom === state.searchAtom
53 ? { entries: [{ path: "src/index.ts", kind: "file" }] }
54 : atom === state.contentAtom
55 ? state.contentData
56 : null,
57 error:
58 atom === state.sessionAtom
59 ? state.sessionError
60 : atom === state.contentAtom
61 ? state.contentError
62 : null,
63 isPending:
64 atom === state.sessionAtom &&
65 (state.sessionWaiting || (state.session === null && state.sessionError === null)),
66 refresh: vi.fn(),
67 }),
68}));
69
70import { useProjectContentSearch } from "./queries";
71
72const target = {
73 environmentId: EnvironmentId.make("test-environment"),
74 cwd: "/repo",
75 query: "src",
76};
77
78beforeEach(() => {
79 state.session = null;
80 state.sessionError = null;
81 state.sessionWaiting = false;
82 state.phase = "connected";
83 state.contentRequests.mockClear();
84 state.contentError = null;
85});
86
87const contentTarget = {
88 ...target,
89 query: " a match ",
90 caseSensitive: true,
91 wholeWord: true,
92 useRegex: false,
93};
94
95it("shows content-search denial before typing and never issues an unauthorized request", () => {
96 state.session = { authenticated: true, scopes: [] };
97 for (const query of ["", contentTarget.query]) {
98 const result = useProjectContentSearch({ ...contentTarget, query });
99 expect(state.contentRequests).not.toHaveBeenCalled();
100 expect(result).toMatchObject({
101 canReadFiles: false,
102 isCheckingAccess: false,
103 matches: [],
104 error: "This connection cannot search host files.",
105 isPending: false,
106 });
107 }
108 expect(state.contentRequests).not.toHaveBeenCalled();
109});
110
111it("keeps confirmed content access during revalidation and drops results when it is revoked", () => {
112 state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] };
113 state.sessionWaiting = true;
114 expect(useProjectContentSearch(contentTarget)).toMatchObject({
115 matches: state.contentData.matches,
116 isPending: false,
117 });
118
119 state.contentRequests.mockClear();
120 state.sessionWaiting = false;
121 state.session = { authenticated: true, scopes: [] };
122 const result = useProjectContentSearch(contentTarget);
123 expect(state.contentRequests).not.toHaveBeenCalled();
124 expect(result).toMatchObject({
125 canReadFiles: false,
126 matches: [],
127 error: "This connection cannot search host files.",
128 isPending: false,
129 });
130 expect(state.contentRequests).not.toHaveBeenCalled();
131});
132
133it("fails closed after a session check fails, even with a cached grant and matches", () => {
134 state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] };
135 state.sessionError = "The session has expired.";
136 const result = useProjectContentSearch(contentTarget);
137 expect(state.contentRequests).not.toHaveBeenCalled();
138 expect(result).toMatchObject({
139 canReadFiles: false,
140 matches: [],
141 error: state.sessionError,
142 isPending: false,
143 });
144 expect(state.contentRequests).not.toHaveBeenCalled();
145});