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