Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

assetUrls.test.ts

107 lines3.1 KB

1import { EnvironmentId, ThreadId, type AssetResource } from "@t3tools/contracts";
2import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
3import { describe, expect, it } from "vite-plus/test";
4​
5import { makeRevisionedAssetUrlFamily } from "./assetUrls";
6​
7const environmentId = EnvironmentId.make("environment-1");
8const resource: AssetResource = {
9 _tag: "workspace-file",
10 threadId: ThreadId.make("thread-1"),
11 path: "docs/diagram.png",
12};
13​
14type MintResult = AsyncResult.AsyncResult<{ relativeUrl: string; expiresAt: number }>;
15​
16const minted = (n: number): MintResult =>
17 AsyncResult.success({ relativeUrl: `/a/${n}`, expiresAt: 0 });
18​
19// A stand-in for the asset URL query: a refresh starts a mint (counted, shown as
20// waiting) and `settle` completes the latest one.
21function setup(initial: MintResult) {
22 let mints = 0;
23 // Node removal runs on the registry's scheduler; `unmount` flushes it so the
24 // next mount of the same revision builds a fresh node.
25 const scheduled: Array<() => void> = [];
26 const registry = AtomRegistry.make({
27 scheduleTask: (task) => {
28 scheduled.push(task);
29 return () => {};
30 },
31 });
32 const server = Atom.make(initial).pipe(Atom.keepAlive);
33 const query = Atom.readable(
34 (get) => get(server),
35 () => {
36 mints += 1;
37 registry.set(server, AsyncResult.waiting(registry.get(server)));
38 },
39 ).pipe(Atom.keepAlive);
40 const revisioned = makeRevisionedAssetUrlFamily(() => query);
41 const mount = (revision: string | null) => {
42 const release = registry.mount(revisioned({ environmentId, input: { resource }, revision }));
43 return () => {
44 release();
45 for (const task of scheduled.splice(0)) task();
46 };
47 };
48 const settle = () => registry.set(server, minted(mints + 1));
49 return { mount, settle, mints: () => mints };
51​
52describe("makeRevisionedAssetUrlFamily", () => {
53 it("re-mints a settled URL on first read and on each new revision, not on remount", () => {
54 const { mount, settle, mints } = setup(minted(0));
55​
56 mount("mutation-1")();
57 expect(mints()).toBe(1);
58 settle();
59​
60 mount("mutation-1")();
61 expect(mints()).toBe(1);
62​
63 mount("mutation-2")();
64 expect(mints()).toBe(2);
65 });
66​
67 it("re-mints when a revert returns to an earlier revision", () => {
68 const { mount, settle, mints } = setup(minted(0));
69​
70 mount("mutation-a")();
71 settle();
72 mount("mutation-b")();
73 settle();
74 expect(mints()).toBe(2);
75​
76 const reverted = mount("mutation-a");
77 expect(mints()).toBe(3);
78 reverted();
79 });
80​
81 it("shares a mint in flight on the first read", () => {
82 const { mount, mints } = setup(AsyncResult.initial(true));
83​
84 mount("mutation-1")();
85 expect(mints()).toBe(0);
86 });
87​
88 it("re-mints after a mint that was in flight when the revision changed", () => {
89 const { mount, settle, mints } = setup(minted(0));
90​
91 const before = mount("mutation-1");
92 settle();
93 const edited = mount("mutation-2");
94 expect(mints()).toBe(2);
95 before();
96​
97 const stillMinting = mount("mutation-3");
98 edited();
99 expect(mints()).toBe(2);
100​
101 settle();
102 expect(mints()).toBe(3);
103 settle();
104 expect(mints()).toBe(3);
105 stillMinting();
106 });
107});