|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - exercises the hook with Metro's release transform. |
| 2 | +import * as NodeModule from "node:module"; |
| 3 | +import * as NodeVM from "node:vm"; |
| 4 | +import { |
| 5 | + EnvironmentId, |
| 6 | + type ProjectEntry, |
| 7 | + type ProjectListEntriesResult, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import * as Cause from "effect/Cause"; |
| 10 | +import * as React from "react"; |
| 11 | +import { act, StrictMode, useEffect, useLayoutEffect } from "react"; |
| 12 | +import { createRoot } from "react-dom/client"; |
| 13 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 14 | + |
| 15 | +type Hook = typeof import("./useFileTreeEntries").useFileTreeEntries; |
| 16 | +type Input = Parameters<Hook>[0]; |
| 17 | +type Query = { |
| 18 | + readonly environmentId: EnvironmentId; |
| 19 | + readonly input: { readonly cwd: string; readonly directoryPath?: string }; |
| 20 | +}; |
| 21 | +type Result = |
| 22 | + | { readonly _tag: "Success"; readonly value: ProjectListEntriesResult } |
| 23 | + | { readonly _tag: "Failure"; readonly cause: Cause.Cause<Error> }; |
| 24 | + |
| 25 | +const require = NodeModule.createRequire(import.meta.url); |
| 26 | +const babel: { |
| 27 | + transformFileSync(file: string, options: Record<string, unknown>): { code: string }; |
| 28 | +} = require("@babel/core"); |
| 29 | +const sourcePath = require.resolve("./useFileTreeEntries.ts"); |
| 30 | +const rootEntries: ProjectEntry[] = [{ kind: "directory", path: "src" }]; |
| 31 | +const firstTarget = { cwd: "/first", environmentId: EnvironmentId.make("first") }; |
| 32 | + |
| 33 | +function compileHook(compiler: boolean) { |
| 34 | + const { code } = babel.transformFileSync(sourcePath, { |
| 35 | + babelrc: false, |
| 36 | + configFile: false, |
| 37 | + presets: [[require.resolve("babel-preset-expo"), { unstable_transformImportMeta: true }]], |
| 38 | + caller: { |
| 39 | + name: "metro", |
| 40 | + bundler: "metro", |
| 41 | + platform: "ios", |
| 42 | + isDev: false, |
| 43 | + isServer: false, |
| 44 | + isNodeModule: false, |
| 45 | + supportsStaticESM: false, |
| 46 | + supportsReactCompiler: compiler, |
| 47 | + }, |
| 48 | + }); |
| 49 | + if (compiler) expect(code).toContain("react/compiler-runtime"); |
| 50 | + return NodeVM.compileFunction(code, ["require", "exports"], { filename: sourcePath }); |
| 51 | +} |
| 52 | + |
| 53 | +function setup(moduleFactory: ReturnType<typeof compileHook>, autoLoad = false) { |
| 54 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 55 | + const requests: { |
| 56 | + readonly query: Query; |
| 57 | + readonly signal?: AbortSignal; |
| 58 | + readonly resolve: (result: Result) => void; |
| 59 | + }[] = []; |
| 60 | + const rootQuery = { |
| 61 | + data: { entries: rootEntries, truncated: false }, |
| 62 | + error: null, |
| 63 | + isPending: false, |
| 64 | + refresh: vi.fn(), |
| 65 | + }; |
| 66 | + const modules: Record<string, unknown> = { |
| 67 | + react: React, |
| 68 | + "react/compiler-runtime": require("react/compiler-runtime"), |
| 69 | + "effect/Cause": Cause, |
| 70 | + "@t3tools/client-runtime/state/runtime": { |
| 71 | + executeAtomQuery: (_registry: unknown, query: Query, options: { signal?: AbortSignal }) => |
| 72 | + new Promise<Result>((resolve) => requests.push({ query, signal: options.signal, resolve })), |
| 73 | + }, |
| 74 | + "../../state/atom-registry": { appAtomRegistry: { refresh: vi.fn() } }, |
| 75 | + "../../state/projects": { |
| 76 | + projectEnvironment: { |
| 77 | + listEntries: (query: Query) => query, |
| 78 | + searchEntries: (query: Query) => query, |
| 79 | + }, |
| 80 | + }, |
| 81 | + "../../state/queries": { useDebouncedValue: (value: string) => value }, |
| 82 | + "../../state/query": { useEnvironmentQuery: () => rootQuery }, |
| 83 | + }; |
| 84 | + const exports = {} as { useFileTreeEntries: Hook }; |
| 85 | + moduleFactory((name: string) => { |
| 86 | + if (name.startsWith("@babel/runtime/")) |
| 87 | + return NodeModule.createRequire(require.resolve("babel-preset-expo"))(name); |
| 88 | + if (!(name in modules)) throw new Error(`Unexpected hook dependency: ${name}`); |
| 89 | + return modules[name]; |
| 90 | + }, exports); |
| 91 | + const useFileTreeEntries = exports.useFileTreeEntries; |
| 92 | + const root = createRoot(document.createElement("div")); |
| 93 | + let current: ReturnType<Hook>; |
| 94 | + function Probe(input: Input) { |
| 95 | + const result = useFileTreeEntries(input); |
| 96 | + const { loadDirectory } = result; |
| 97 | + useEffect(() => { |
| 98 | + if (autoLoad) void loadDirectory("src"); |
| 99 | + }, [loadDirectory]); |
| 100 | + useLayoutEffect(() => { |
| 101 | + current = result; |
| 102 | + }); |
| 103 | + return null; |
| 104 | + } |
| 105 | + const unmount = () => act(() => root.unmount()); |
| 106 | + return { |
| 107 | + requests, |
| 108 | + unmount, |
| 109 | + get current() { |
| 110 | + return current; |
| 111 | + }, |
| 112 | + render(target: Partial<Input> = {}, strict = false) { |
| 113 | + const probe = <Probe {...firstTarget} searchQuery="" {...target} />; |
| 114 | + act(() => root.render(strict ? <StrictMode>{probe}</StrictMode> : probe)); |
| 115 | + }, |
| 116 | + load(path = "src") { |
| 117 | + act(() => { |
| 118 | + void current.loadDirectory(path); |
| 119 | + }); |
| 120 | + }, |
| 121 | + refresh() { |
| 122 | + let completion: ReturnType<ReturnType<Hook>["refresh"]>; |
| 123 | + act(() => { |
| 124 | + completion = current.refresh(); |
| 125 | + }); |
| 126 | + return completion!; |
| 127 | + }, |
| 128 | + async succeed(index: number, entries: ProjectEntry[] = []) { |
| 129 | + await act(async () => { |
| 130 | + requests[index]!.resolve({ _tag: "Success", value: { entries, truncated: false } }); |
| 131 | + }); |
| 132 | + }, |
| 133 | + async fail(index: number, message = "Directory unavailable") { |
| 134 | + await act(async () => { |
| 135 | + requests[index]!.resolve({ _tag: "Failure", cause: Cause.fail(new Error(message)) }); |
| 136 | + }); |
| 137 | + }, |
| 138 | + [Symbol.dispose]: unmount, |
| 139 | + }; |
| 140 | +} |
| 141 | + |
| 142 | +describe.each([false, true])("file tree (React Compiler: %s)", (compiler) => { |
| 143 | + const moduleFactory = compileHook(compiler); |
| 144 | + |
| 145 | + it("publishes loading and loaded snapshots, filters descendants, and deduplicates loads", async () => { |
| 146 | + using screen = setup(moduleFactory); |
| 147 | + screen.render(); |
| 148 | + const initial = screen.current; |
| 149 | + screen.load(); |
| 150 | + expect(screen.current.isPending).toBe(true); |
| 151 | + expect([...screen.current.loadingDirectories]).toEqual(["src"]); |
| 152 | + expect([...initial.loadingDirectories]).toEqual([]); |
| 153 | + screen.render(); |
| 154 | + screen.load(); |
| 155 | + expect(screen.requests).toHaveLength(1); |
| 156 | + await screen.succeed(0, [ |
| 157 | + { kind: "file", path: "src/new.ts" }, |
| 158 | + { kind: "file", path: "src/nested/hidden.ts" }, |
| 159 | + ]); |
| 160 | + expect(screen.current.entries.map((entry) => entry.path)).toEqual(["src", "src/new.ts"]); |
| 161 | + expect([...screen.current.loadedDirectories]).toEqual(["src"]); |
| 162 | + expect([...screen.current.loadingDirectories]).toEqual([]); |
| 163 | + expect(screen.current.isPending).toBe(false); |
| 164 | + screen.load(); |
| 165 | + expect(screen.requests).toHaveLength(1); |
| 166 | + }); |
| 167 | + |
| 168 | + it("publishes failures and clears them when retrying", async () => { |
| 169 | + using screen = setup(moduleFactory); |
| 170 | + screen.render(); |
| 171 | + screen.load(); |
| 172 | + await screen.fail(0); |
| 173 | + expect(screen.current.error).toBe("Directory unavailable"); |
| 174 | + expect(screen.current.isPending).toBe(false); |
| 175 | + expect([...screen.current.loadingDirectories]).toEqual([]); |
| 176 | + expect([...screen.current.loadedDirectories]).toEqual([]); |
| 177 | + screen.load(); |
| 178 | + expect(screen.current.error).toBeNull(); |
| 179 | + expect(screen.current.isPending).toBe(true); |
| 180 | + await screen.succeed(1, [{ kind: "file", path: "src/retried.ts" }]); |
| 181 | + expect(screen.current.entries.map((entry) => entry.path)).toContain("src/retried.ts"); |
| 182 | + expect(screen.current.isPending).toBe(false); |
| 183 | + }); |
| 184 | + |
| 185 | + it.each([{ cwd: "/second" }, { environmentId: EnvironmentId.make("second") }, { cwd: null }])( |
| 186 | + "resets directory data when switching target to %j", |
| 187 | + async (target) => { |
| 188 | + using screen = setup(moduleFactory); |
| 189 | + screen.render(); |
| 190 | + screen.load(); |
| 191 | + await screen.succeed(0, [{ kind: "file", path: "src/old.ts" }]); |
| 192 | + screen.render(target); |
| 193 | + expect(screen.current.entries.map((entry) => entry.path)).not.toContain("src/old.ts"); |
| 194 | + expect([...screen.current.loadedDirectories]).toEqual([]); |
| 195 | + expect([...screen.current.loadingDirectories]).toEqual([]); |
| 196 | + screen.load(); |
| 197 | + expect(screen.requests).toHaveLength(target.cwd === null ? 1 : 2); |
| 198 | + if (target.cwd !== null) { |
| 199 | + expect(screen.requests[1]!.query).toEqual({ |
| 200 | + environmentId: target.environmentId ?? firstTarget.environmentId, |
| 201 | + input: { cwd: target.cwd ?? firstTarget.cwd, directoryPath: "src" }, |
| 202 | + }); |
| 203 | + } |
| 204 | + }, |
| 205 | + ); |
| 206 | + |
| 207 | + it("aborts old target loads and ignores their late success and failure", async () => { |
| 208 | + using screen = setup(moduleFactory); |
| 209 | + screen.render(); |
| 210 | + screen.load(); |
| 211 | + screen.load("src/other"); |
| 212 | + screen.render({ cwd: "/second" }); |
| 213 | + expect(screen.requests[0]!.signal?.aborted).toBe(true); |
| 214 | + expect(screen.requests[1]!.signal?.aborted).toBe(true); |
| 215 | + screen.load(); |
| 216 | + await screen.succeed(0, [{ kind: "file", path: "src/stale.ts" }]); |
| 217 | + await screen.fail(1, "Stale error"); |
| 218 | + expect(screen.current.entries.map((entry) => entry.path)).toEqual(["src"]); |
| 219 | + expect(screen.current.error).toBeNull(); |
| 220 | + expect([...screen.current.loadingDirectories]).toEqual(["src"]); |
| 221 | + await screen.succeed(2, [{ kind: "file", path: "src/current.ts" }]); |
| 222 | + expect(screen.current.entries.map((entry) => entry.path)).toEqual(["src", "src/current.ts"]); |
| 223 | + }); |
| 224 | + |
| 225 | + it("refreshes loaded directories and ignores superseded loads", async () => { |
| 226 | + using screen = setup(moduleFactory); |
| 227 | + screen.render(); |
| 228 | + screen.load(); |
| 229 | + const firstRefresh = screen.refresh(); |
| 230 | + expect(screen.requests[0]!.signal?.aborted).toBe(true); |
| 231 | + expect(screen.current.isRefreshing).toBe(true); |
| 232 | + expect([...screen.current.loadingDirectories]).toEqual(["src"]); |
| 233 | + await screen.succeed(0, [{ kind: "file", path: "src/stale.ts" }]); |
| 234 | + expect(screen.current.entries.map((entry) => entry.path)).toEqual(["src"]); |
| 235 | + expect([...screen.current.loadingDirectories]).toEqual(["src"]); |
| 236 | + await screen.succeed(1); |
| 237 | + await screen.succeed(2, [{ kind: "file", path: "src/refreshed.ts" }]); |
| 238 | + await act(async () => { |
| 239 | + await firstRefresh; |
| 240 | + }); |
| 241 | + expect(screen.current.isRefreshing).toBe(false); |
| 242 | + expect(screen.current.isPending).toBe(false); |
| 243 | + expect(screen.current.entries.map((entry) => entry.path)).toEqual(["src", "src/refreshed.ts"]); |
| 244 | + const secondRefresh = screen.refresh(); |
| 245 | + expect(screen.requests[4]!.query.input.directoryPath).toBe("src"); |
| 246 | + await screen.succeed(3); |
| 247 | + await screen.fail(4); |
| 248 | + await act(async () => { |
| 249 | + await secondRefresh; |
| 250 | + }); |
| 251 | + expect(screen.current.isRefreshing).toBe(false); |
| 252 | + expect(screen.current.error).toBe("Directory unavailable"); |
| 253 | + }); |
| 254 | + |
| 255 | + it("does not let an old target refresh clear the new target refresh", async () => { |
| 256 | + using screen = setup(moduleFactory); |
| 257 | + screen.render(); |
| 258 | + const oldRefresh = screen.refresh(); |
| 259 | + screen.render({ cwd: "/second" }); |
| 260 | + expect(screen.current.isRefreshing).toBe(false); |
| 261 | + const newRefresh = screen.refresh(); |
| 262 | + await screen.succeed(0); |
| 263 | + await act(async () => { |
| 264 | + await oldRefresh; |
| 265 | + }); |
| 266 | + expect(screen.current.isRefreshing).toBe(true); |
| 267 | + await screen.succeed(1); |
| 268 | + await act(async () => { |
| 269 | + await newRefresh; |
| 270 | + }); |
| 271 | + expect(screen.current.isRefreshing).toBe(false); |
| 272 | + }); |
| 273 | + |
| 274 | + it("cancels loads on unmount and ignores late completions and callbacks", async () => { |
| 275 | + using screen = setup(moduleFactory); |
| 276 | + screen.render(); |
| 277 | + screen.load(); |
| 278 | + screen.unmount(); |
| 279 | + expect(screen.requests[0]!.signal?.aborted).toBe(true); |
| 280 | + await screen.succeed(0, [{ kind: "file", path: "src/stale.ts" }]); |
| 281 | + screen.load(); |
| 282 | + await screen.refresh(); |
| 283 | + expect(screen.requests).toHaveLength(1); |
| 284 | + }); |
| 285 | + |
| 286 | + it("allows consumer effects to reload after Strict Mode cancellation", async () => { |
| 287 | + using screen = setup(moduleFactory, true); |
| 288 | + screen.render({}, true); |
| 289 | + expect(screen.requests).toHaveLength(2); |
| 290 | + expect(screen.requests[0]!.signal?.aborted).toBe(true); |
| 291 | + expect(screen.requests[1]!.signal?.aborted).toBe(false); |
| 292 | + await screen.succeed(0, [{ kind: "file", path: "src/stale.ts" }]); |
| 293 | + expect([...screen.current.loadingDirectories]).toEqual(["src"]); |
| 294 | + await screen.succeed(1, [{ kind: "file", path: "src/current.ts" }]); |
| 295 | + expect(screen.current.entries.map((entry) => entry.path)).toEqual(["src", "src/current.ts"]); |
| 296 | + expect(screen.current.isPending).toBe(false); |
| 297 | + expect(screen.requests).toHaveLength(2); |
| 298 | + }); |
| 299 | +}); |
0 commit comments