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