Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import { type AssetCreateUrlResult, EnvironmentId, WS_METHODS } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Queue from "effect/Queue";
8import * as Stream from "effect/Stream";
9import * as SubscriptionRef from "effect/SubscriptionRef";
10import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
11
12import {
13 AVAILABLE_CONNECTION_STATE,
14 type PreparedConnection,
15 PrimaryConnectionTarget,
16 type SupervisorConnectionState,
17} from "../connection/model.ts";
18import * as EnvironmentRegistry from "../connection/registry.ts";
19import * as EnvironmentSupervisor from "../connection/supervisor.ts";
20import type * as RpcSession from "../rpc/session.ts";
21import {
22 ASSET_URL_MAX_IN_FLIGHT,
23 createAssetEnvironmentAtoms,
24 createProjectFaviconUrlAtomFamily,
25} from "./assets.ts";
26
27describe("project favicon URL cache", () => {
28 it("does not reuse another environment, workspace, or selected icon's cached URL", () => {
29 const registry = AtomRegistry.make();
30 const result = Atom.make<AsyncResult.AsyncResult<AssetCreateUrlResult, unknown>>(
31 AsyncResult.success({
32 expiresAt: 4_000_000_000_000,
33 relativeUrl: "/api/assets/token/icon.svg",
34 }),
35 );
36 const favicon = createProjectFaviconUrlAtomFamily({
37 createUrl: () => result,
38 preparedConnection: () => Atom.make(Option.some({ httpBaseUrl: "https://remote.test" })),
39 });
40 const target = { environmentId: EnvironmentId.make("remote"), cwd: "/workspace" };
41 const unmount = registry.mount(favicon(target));
42 try {
43 expect(registry.get(favicon(target))).toBe("https://remote.test/api/assets/token/icon.svg");
44 registry.set(result, AsyncResult.failure(Cause.die("disconnected")));
45 expect(
46 registry.get(favicon({ ...target, environmentId: EnvironmentId.make("other") })),
47 ).toBeNull();
48 expect(registry.get(favicon({ ...target, cwd: "/other" }))).toBeNull();
49 expect(registry.get(favicon({ ...target, faviconPath: "brand.svg" }))).toBeNull();
50 expect(registry.get(favicon({ ...target, faviconPath: null }))).toBe(
51 "https://remote.test/api/assets/token/icon.svg",
52 );
53 } finally {
54 unmount();
55 registry.dispose();
56 }
57 });
58});
59
60/**
61 * Environments whose `assets.createUrl` never answers until the test releases a call.
62 * `started` receives the environment of each call as it begins, and `inFlight` counts
63 * calls that have begun and not yet been released or interrupted.
64 */
65const makeAssetUrlHarness = Effect.fn("TestAssetUrl.makeHarness")(function* (
66 environmentIds: ReadonlyArray<EnvironmentId>,
67) {
68 const started = yield* Queue.unbounded<EnvironmentId>();
69 const inFlight = new Map<EnvironmentId, number>();
70 const maxInFlight = new Map<EnvironmentId, number>();
71 const releases = new Map<EnvironmentId, Queue.Queue<void>>();
72 const supervisors = new Map<
73 EnvironmentId,
74 EnvironmentSupervisor.EnvironmentSupervisor["Service"]
75 >();
76 const sessions = new Map<
77 EnvironmentId,
78 SubscriptionRef.SubscriptionRef<Option.Option<RpcSession.RpcSession>>
79 >();
80 const makeSession = (environmentId: EnvironmentId) =>
81 ({
82 client: {
83 [WS_METHODS.assetsCreateUrl]: () =>
84 Effect.gen(function* () {
85 const now = (inFlight.get(environmentId) ?? 0) + 1;
86 inFlight.set(environmentId, now);
87 maxInFlight.set(environmentId, Math.max(maxInFlight.get(environmentId) ?? 0, now));
88 yield* Queue.offer(started, environmentId);
89 yield* Queue.take(releases.get(environmentId)!);
90 return { relativeUrl: "/api/assets/token/image.png", expiresAt: 4_000_000_000_000 };
91 }).pipe(
92 Effect.ensuring(
93 Effect.sync(() =>
94 inFlight.set(environmentId, (inFlight.get(environmentId) ?? 1) - 1),
95 ),
96 ),
97 ),
98 },
99 }) as unknown as RpcSession.RpcSession;
100 for (const environmentId of environmentIds) {
101 releases.set(environmentId, yield* Queue.unbounded<void>());
102 const session = yield* SubscriptionRef.make(Option.some(makeSession(environmentId)));
103 sessions.set(environmentId, session);
104 supervisors.set(
105 environmentId,
106 EnvironmentSupervisor.EnvironmentSupervisor.of({
107 target: new PrimaryConnectionTarget({
108 environmentId,
109 label: environmentId,
110 httpBaseUrl: "https://assets.example.test",
111 wsBaseUrl: "wss://assets.example.test",
112 }),
113 state: yield* SubscriptionRef.make<SupervisorConnectionState>({
114 ...AVAILABLE_CONNECTION_STATE,
115 desired: true,
116 network: "online",
117 phase: "connected",
118 attempt: 1,
119 generation: 1,
120 }),
121 session,
122 prepared: yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(Option.none()),
123 connect: Effect.void,
124 disconnect: Effect.void,
125 retryNow: Effect.void,
126 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]),
127 );
128 }
129 const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = (environmentId, effect) =>
130 Effect.provideService(
131 effect,
132 EnvironmentSupervisor.EnvironmentSupervisor,
133 supervisors.get(environmentId)!,
134 );
135 const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = (
136 environmentId,
137 stream,
138 ) =>
139 Stream.provideService(
140 stream,
141 EnvironmentSupervisor.EnvironmentSupervisor,
142 supervisors.get(environmentId)!,
143 );
144 const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({
145 run,
146 followStream,
147 } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);
148 const registry = AtomRegistry.make();
149 yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
150 const atoms = createAssetEnvironmentAtoms(
151 Atom.runtime(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry)),
152 );
153 return {
154 started,
155 inFlight: (environmentId: EnvironmentId) => inFlight.get(environmentId) ?? 0,
156 maxInFlight: (environmentId: EnvironmentId) => maxInFlight.get(environmentId) ?? 0,
157 release: (environmentId: EnvironmentId) => Queue.offer(releases.get(environmentId)!, undefined),
158 replaceSession: (environmentId: EnvironmentId) =>
159 SubscriptionRef.set(sessions.get(environmentId)!, Option.some(makeSession(environmentId))),
160 mountImages: (environmentId: EnvironmentId, count: number) => {
161 for (let index = 0; index < count; index += 1) {
162 registry.mount(
163 atoms.createUrl({
164 environmentId,
165 input: { resource: { _tag: "attachment", attachmentId: `image-${index}` } },
166 }),
167 );
168 }
169 },
170 };
171});
172
173describe("asset URL requests", () => {
174 const remote = EnvironmentId.make("asset-remote");
175 const other = EnvironmentId.make("asset-other");
176
177 it.effect("keeps a thread of images from flooding one environment with requests", () =>
178 Effect.scoped(
179 Effect.gen(function* () {
180 const harness = yield* makeAssetUrlHarness([remote]);
181 const total = ASSET_URL_MAX_IN_FLIGHT * 3;
182 harness.mountImages(remote, total);
183
184 for (let call = 0; call < ASSET_URL_MAX_IN_FLIGHT; call += 1) {
185 yield* Queue.take(harness.started);
186 }
187 expect(harness.inFlight(remote)).toBe(ASSET_URL_MAX_IN_FLIGHT);
188
189 // Each answer admits the next waiting request.
190 for (let call = 0; call < total; call += 1) {
191 yield* harness.release(remote);
192 if (call < total - ASSET_URL_MAX_IN_FLIGHT) yield* Queue.take(harness.started);
193 }
194 expect(harness.maxInFlight(remote)).toBe(ASSET_URL_MAX_IN_FLIGHT);
195 }),
196 ),
197 );
198
199 it.effect("limits each environment on its own", () =>
200 Effect.scoped(
201 Effect.gen(function* () {
202 const harness = yield* makeAssetUrlHarness([remote, other]);
203 harness.mountImages(remote, ASSET_URL_MAX_IN_FLIGHT + 2);
204 for (let call = 0; call < ASSET_URL_MAX_IN_FLIGHT; call += 1) {
205 yield* Queue.take(harness.started);
206 }
207 harness.mountImages(other, 1);
208 expect(yield* Queue.take(harness.started)).toBe(other);
209 expect(harness.inFlight(remote)).toBe(ASSET_URL_MAX_IN_FLIGHT);
210 }),
211 ),
212 );
213
214 it.effect("frees the limit when the session behind the requests is replaced", () =>
215 Effect.scoped(
216 Effect.gen(function* () {
217 const harness = yield* makeAssetUrlHarness([remote]);
218 harness.mountImages(remote, ASSET_URL_MAX_IN_FLIGHT * 2);
219 for (let call = 0; call < ASSET_URL_MAX_IN_FLIGHT; call += 1) {
220 yield* Queue.take(harness.started);
221 }
222
223 yield* harness.replaceSession(remote);
224 for (let call = 0; call < ASSET_URL_MAX_IN_FLIGHT; call += 1) {
225 yield* Queue.take(harness.started);
226 }
227 expect(harness.maxInFlight(remote)).toBe(ASSET_URL_MAX_IN_FLIGHT);
228 }),
229 ),
230 );
231});