|
| 1 | +import { EnvironmentId, type ProjectReadFileResult } from "@t3tools/contracts"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import { Atom, AtomRegistry } from "effect/unstable/reactivity"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +const projectMocks = vi.hoisted(() => ({ |
| 7 | + listEntries: vi.fn(), |
| 8 | + optimisticFile: vi.fn(), |
| 9 | + readFile: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +const atomHooks = vi.hoisted(() => ({ |
| 13 | + registry: null as { |
| 14 | + get(atom: object): unknown; |
| 15 | + refresh(atom: object): void; |
| 16 | + } | null, |
| 17 | +})); |
| 18 | + |
| 19 | +const reactHooks = vi.hoisted(() => { |
| 20 | + let cursor = 0; |
| 21 | + let refs: Array<{ current: unknown }> = []; |
| 22 | + const nextIndex = () => cursor++; |
| 23 | + |
| 24 | + return { |
| 25 | + beginRender() { |
| 26 | + cursor = 0; |
| 27 | + }, |
| 28 | + reset() { |
| 29 | + cursor = 0; |
| 30 | + refs = []; |
| 31 | + }, |
| 32 | + useCallback<A>(callback: A): A { |
| 33 | + nextIndex(); |
| 34 | + return callback; |
| 35 | + }, |
| 36 | + useEffect(effect: () => void): void { |
| 37 | + nextIndex(); |
| 38 | + effect(); |
| 39 | + }, |
| 40 | + useRef<A>(initialValue: A): { current: A } { |
| 41 | + const index = nextIndex(); |
| 42 | + refs[index] ??= { current: initialValue }; |
| 43 | + return refs[index] as { current: A }; |
| 44 | + }, |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +vi.mock("@effect/atom-react", () => ({ |
| 49 | + useAtomRefresh: (atom: object) => () => { |
| 50 | + atomHooks.registry?.refresh(atom); |
| 51 | + }, |
| 52 | + useAtomValue: (atom: object) => atomHooks.registry?.get(atom), |
| 53 | +})); |
| 54 | + |
| 55 | +vi.mock("react", async (importOriginal) => { |
| 56 | + const actual = await importOriginal<typeof import("react")>(); |
| 57 | + return { |
| 58 | + ...actual, |
| 59 | + useCallback: reactHooks.useCallback, |
| 60 | + useEffect: reactHooks.useEffect, |
| 61 | + useRef: reactHooks.useRef, |
| 62 | + }; |
| 63 | +}); |
| 64 | + |
| 65 | +vi.mock("~/state/projects", () => ({ |
| 66 | + projectEnvironment: projectMocks, |
| 67 | +})); |
| 68 | + |
| 69 | +vi.mock("~/state/queries", () => ({ |
| 70 | + useProjectPathSearch: vi.fn(), |
| 71 | +})); |
| 72 | + |
| 73 | +import { useWorkspaceMutationRefresh } from "~/hooks/useWorkspaceMutationRefresh"; |
| 74 | +import { useProjectFileQuery } from "./projectFilesQueryState"; |
| 75 | + |
| 76 | +const environmentId = EnvironmentId.make("environment-1"); |
| 77 | + |
| 78 | +function deferred<A>() { |
| 79 | + let resolve!: (value: A) => void; |
| 80 | + const promise = new Promise<A>((complete) => { |
| 81 | + resolve = complete; |
| 82 | + }); |
| 83 | + return { promise, resolve }; |
| 84 | +} |
| 85 | + |
| 86 | +function file(contents: string): ProjectReadFileResult { |
| 87 | + return { |
| 88 | + relativePath: "src/preview.ts", |
| 89 | + contents, |
| 90 | + byteLength: contents.length, |
| 91 | + truncated: false, |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +async function flushEffects(): Promise<void> { |
| 96 | + await Promise.resolve(); |
| 97 | + await Promise.resolve(); |
| 98 | + await Promise.resolve(); |
| 99 | +} |
| 100 | + |
| 101 | +describe("project file query refresh", () => { |
| 102 | + beforeEach(() => { |
| 103 | + projectMocks.listEntries.mockReset(); |
| 104 | + projectMocks.optimisticFile.mockReset(); |
| 105 | + projectMocks.readFile.mockReset(); |
| 106 | + reactHooks.reset(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("replaces an in-flight initial read when a workspace mutation arrives", async () => { |
| 110 | + const requests: Array<ReturnType<typeof deferred<ProjectReadFileResult>>> = []; |
| 111 | + const readAtom = Atom.make( |
| 112 | + Effect.promise(() => { |
| 113 | + const request = deferred<ProjectReadFileResult>(); |
| 114 | + requests.push(request); |
| 115 | + return request.promise; |
| 116 | + }), |
| 117 | + ).pipe(Atom.swr({ staleTime: 30_000, revalidateOnMount: true })); |
| 118 | + const registry = AtomRegistry.make(); |
| 119 | + const unmount = registry.mount(readAtom); |
| 120 | + projectMocks.readFile.mockReturnValue(readAtom); |
| 121 | + projectMocks.optimisticFile.mockReturnValue(Atom.make(null)); |
| 122 | + atomHooks.registry = registry; |
| 123 | + let renderedContents: string | null = null; |
| 124 | + |
| 125 | + const render = (mutationId: string | null) => { |
| 126 | + reactHooks.beginRender(); |
| 127 | + const query = useProjectFileQuery(environmentId, "/repo", "src/preview.ts"); |
| 128 | + renderedContents = query.data?.contents ?? null; |
| 129 | + useWorkspaceMutationRefresh({ |
| 130 | + mutationId, |
| 131 | + refresh: query.refresh, |
| 132 | + resourceKey: "file:environment-1:/repo:src/preview.ts", |
| 133 | + }); |
| 134 | + }; |
| 135 | + |
| 136 | + try { |
| 137 | + render(null); |
| 138 | + await flushEffects(); |
| 139 | + expect(requests).toHaveLength(1); |
| 140 | + |
| 141 | + render("mutation-1"); |
| 142 | + await flushEffects(); |
| 143 | + expect(requests).toHaveLength(2); |
| 144 | + |
| 145 | + requests[1]!.resolve(file("fresh")); |
| 146 | + await flushEffects(); |
| 147 | + render("mutation-1"); |
| 148 | + expect(renderedContents).toBe("fresh"); |
| 149 | + |
| 150 | + requests[0]!.resolve(file("stale")); |
| 151 | + await flushEffects(); |
| 152 | + render("mutation-1"); |
| 153 | + expect(renderedContents).toBe("fresh"); |
| 154 | + } finally { |
| 155 | + unmount(); |
| 156 | + registry.dispose(); |
| 157 | + atomHooks.registry = null; |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + it("does not issue a file read for a disabled image preview", async () => { |
| 162 | + const requests: Array<ReturnType<typeof deferred<ProjectReadFileResult>>> = []; |
| 163 | + const readAtom = Atom.make( |
| 164 | + Effect.promise(() => { |
| 165 | + const request = deferred<ProjectReadFileResult>(); |
| 166 | + requests.push(request); |
| 167 | + return request.promise; |
| 168 | + }), |
| 169 | + ); |
| 170 | + const registry = AtomRegistry.make(); |
| 171 | + projectMocks.readFile.mockReturnValue(readAtom); |
| 172 | + projectMocks.optimisticFile.mockReturnValue(Atom.make(null)); |
| 173 | + atomHooks.registry = registry; |
| 174 | + |
| 175 | + try { |
| 176 | + reactHooks.beginRender(); |
| 177 | + const query = useProjectFileQuery(environmentId, "/repo", "preview.png", false); |
| 178 | + useWorkspaceMutationRefresh({ |
| 179 | + enabled: false, |
| 180 | + mutationId: "mutation-1", |
| 181 | + refresh: query.refresh, |
| 182 | + resourceKey: "file:environment-1:/repo:preview.png", |
| 183 | + }); |
| 184 | + await flushEffects(); |
| 185 | + |
| 186 | + expect(projectMocks.readFile).not.toHaveBeenCalled(); |
| 187 | + expect(requests).toHaveLength(0); |
| 188 | + } finally { |
| 189 | + registry.dispose(); |
| 190 | + atomHooks.registry = null; |
| 191 | + } |
| 192 | + }); |
| 193 | +}); |
0 commit comments