Open History to see the last commit
1import { AtomRegistry } from "effect/unstable/reactivity";
2import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
3import { describe, expect, it, vi } from "vite-plus/test";
4
5import { createWorkspaceFileImageAtomFamily } from "./workspace-file-image-cache";
6
7describe("workspaceFileImageAtom", () => {
8 it("reuses a prefetched image across route remounts", async () => {
9 const prefetch = vi.fn(async () => true);
10 const imageAtom = createWorkspaceFileImageAtomFamily({ idleTtlMs: 1_000, prefetch });
11 const registry = AtomRegistry.make({ timeoutResolution: 1 });
12 const first = imageAtom("https://example.test/image.png");
13 const firstUnmount = registry.mount(first);
14
15 await vi.waitFor(() => {
16 expect(AsyncResult.isSuccess(registry.get(first))).toBe(true);
17 });
18 firstUnmount();
19
20 const remounted = imageAtom("https://example.test/image.png");
21 const secondUnmount = registry.mount(remounted);
22
23 expect(remounted).toBe(first);
24 expect(AsyncResult.isSuccess(registry.get(remounted))).toBe(true);
25 expect(prefetch).toHaveBeenCalledTimes(1);
26
27 secondUnmount();
28 registry.dispose();
29 });
30
31 it("prefetches different asset URLs independently", async () => {
32 const prefetch = vi.fn(async () => true);
33 const imageAtom = createWorkspaceFileImageAtomFamily({ prefetch });
34 const registry = AtomRegistry.make();
35 const first = imageAtom("https://example.test/first.png");
36 const second = imageAtom("https://example.test/second.png");
37 const firstUnmount = registry.mount(first);
38 const secondUnmount = registry.mount(second);
39
40 await vi.waitFor(() => {
41 expect(AsyncResult.isSuccess(registry.get(first))).toBe(true);
42 expect(AsyncResult.isSuccess(registry.get(second))).toBe(true);
43 });
44 expect(prefetch).toHaveBeenCalledTimes(2);
45
46 firstUnmount();
47 secondUnmount();
48 registry.dispose();
49 });
50
51 it("exposes prefetch failures", async () => {
52 const imageAtom = createWorkspaceFileImageAtomFamily({ prefetch: async () => false });
53 const registry = AtomRegistry.make();
54 const atom = imageAtom("https://example.test/missing.png");
55 const unmount = registry.mount(atom);
56
57 await vi.waitFor(() => {
58 expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
59 });
60
61 unmount();
62 registry.dispose();
63 });
64});