|
1 | 1 | import { describe, expect, it } from "@effect/vitest"; |
2 | | -import { type AssetCreateUrlResult, EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { |
| 3 | + type AssetCreateUrlResult, |
| 4 | + AssetWorkspaceAssetNotFoundError, |
| 5 | + AssetWorkspaceAssetInspectionError, |
| 6 | + AssetWorkspaceContextNotFoundError, |
| 7 | + EnvironmentAuthorizationError, |
| 8 | + EnvironmentId, |
| 9 | + ThreadId, |
| 10 | + WS_METHODS, |
| 11 | +} from "@t3tools/contracts"; |
3 | 12 | import * as Cause from "effect/Cause"; |
| 13 | +import * as Effect from "effect/Effect"; |
| 14 | +import * as Stream from "effect/Stream"; |
| 15 | +import * as SubscriptionRef from "effect/SubscriptionRef"; |
4 | 16 | import * as Option from "effect/Option"; |
5 | 17 | import * as Layer from "effect/Layer"; |
6 | 18 | import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity"; |
7 | 19 |
|
8 | | -import type { EnvironmentRegistry } from "../connection/registry.ts"; |
| 20 | +import { EnvironmentRegistry } from "../connection/registry.ts"; |
| 21 | +import { |
| 22 | + AVAILABLE_CONNECTION_STATE, |
| 23 | + PrimaryConnectionTarget, |
| 24 | + type PreparedConnection, |
| 25 | + type SupervisorConnectionState, |
| 26 | +} from "../connection/model.ts"; |
| 27 | +import { EnvironmentSupervisor } from "../connection/supervisor.ts"; |
| 28 | +import type { RpcSession } from "../rpc/session.ts"; |
| 29 | +import type { WsRpcProtocolClient } from "../rpc/protocol.ts"; |
9 | 30 | import { createProjectFaviconCache } from "../projectFaviconCache.ts"; |
10 | 31 | import { |
11 | 32 | createAssetEnvironmentAtoms, |
@@ -37,6 +58,116 @@ describe("asset collection keys", () => { |
37 | 58 | }); |
38 | 59 |
|
39 | 60 | describe("createAssetEnvironmentAtoms", () => { |
| 61 | + for (const scenario of [ |
| 62 | + { name: "missing video", path: "/tmp/clip.mp4", fallback: true }, |
| 63 | + { name: "literal filename characters", path: "/tmp/frame#one?two.png", fallback: true }, |
| 64 | + { name: "windows path", path: "C:\\Users\\demo\\clip.mp4", fallback: true }, |
| 65 | + { name: "inspection failure", path: "/tmp/clip.mp4", error: "inspection", fallback: true }, |
| 66 | + { name: "foreign thread", path: "/tmp/clip.mp4", error: "context", fallback: true }, |
| 67 | + { name: "remote success", path: "/tmp/clip.mp4", success: true }, |
| 68 | + { name: "relative path", path: "clip.mp4" }, |
| 69 | + { name: "no primary", path: "/tmp/clip.mp4", primary: "none" }, |
| 70 | + { name: "primary reconnect", path: "/tmp/frame.png", primary: "reconnecting" }, |
| 71 | + { name: "same environment", path: "/tmp/clip.mp4", primary: "same" }, |
| 72 | + { name: "non-media", path: "/tmp/report.html" }, |
| 73 | + { name: "authorization failure", path: "/tmp/clip.mp4", error: "auth" }, |
| 74 | + ]) { |
| 75 | + it.effect(`uses the correct environment for ${scenario.name}`, () => |
| 76 | + Effect.gen(function* () { |
| 77 | + const remoteId = EnvironmentId.make("remote"); |
| 78 | + const localId = EnvironmentId.make("local"); |
| 79 | + const resource = { |
| 80 | + _tag: "media-file" as const, |
| 81 | + threadId: ThreadId.make("foreign-thread"), |
| 82 | + path: scenario.path, |
| 83 | + }; |
| 84 | + const error = |
| 85 | + scenario.error === "auth" |
| 86 | + ? new EnvironmentAuthorizationError({ |
| 87 | + message: "denied", |
| 88 | + requiredScope: "orchestration:read", |
| 89 | + }) |
| 90 | + : scenario.error === "inspection" |
| 91 | + ? new AssetWorkspaceAssetInspectionError({ resource, cause: new Error("unreadable") }) |
| 92 | + : scenario.error === "context" |
| 93 | + ? new AssetWorkspaceContextNotFoundError({ resource }) |
| 94 | + : new AssetWorkspaceAssetNotFoundError({ resource }); |
| 95 | + const calls: EnvironmentId[] = []; |
| 96 | + const supervisors = new Map<EnvironmentId, EnvironmentSupervisor["Service"]>(); |
| 97 | + for (const environmentId of [remoteId, localId]) { |
| 98 | + const client = { |
| 99 | + [WS_METHODS.assetsCreateUrl]: () => { |
| 100 | + calls.push(environmentId); |
| 101 | + return environmentId === remoteId && !scenario.success |
| 102 | + ? Effect.fail(error) |
| 103 | + : Effect.succeed({ |
| 104 | + relativeUrl: `/api/assets/${environmentId}/media`, |
| 105 | + expiresAt: 999999, |
| 106 | + }); |
| 107 | + }, |
| 108 | + } as unknown as WsRpcProtocolClient; |
| 109 | + const session = { client } as RpcSession; |
| 110 | + supervisors.set( |
| 111 | + environmentId, |
| 112 | + EnvironmentSupervisor.of({ |
| 113 | + target: new PrimaryConnectionTarget({ |
| 114 | + environmentId, |
| 115 | + label: environmentId, |
| 116 | + httpBaseUrl: `https://${environmentId}.test`, |
| 117 | + wsBaseUrl: `wss://${environmentId}.test`, |
| 118 | + }), |
| 119 | + state: yield* SubscriptionRef.make<SupervisorConnectionState>({ |
| 120 | + ...AVAILABLE_CONNECTION_STATE, |
| 121 | + phase: "connected" as const, |
| 122 | + }), |
| 123 | + session: yield* SubscriptionRef.make(Option.some(session)), |
| 124 | + prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 125 | + connect: Effect.void, |
| 126 | + disconnect: Effect.void, |
| 127 | + retryNow: Effect.void, |
| 128 | + }), |
| 129 | + ); |
| 130 | + } |
| 131 | + const environments = EnvironmentRegistry.of({ |
| 132 | + run: (id, effect) => |
| 133 | + Effect.provideService(effect, EnvironmentSupervisor, supervisors.get(id)!), |
| 134 | + followStream: (id, stream) => |
| 135 | + Stream.provideService(stream, EnvironmentSupervisor, supervisors.get(id)!), |
| 136 | + } as EnvironmentRegistry["Service"]); |
| 137 | + const registry = AtomRegistry.make(); |
| 138 | + yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); |
| 139 | + const localTarget = { |
| 140 | + environmentId: scenario.primary === "same" ? remoteId : localId, |
| 141 | + httpBaseUrl: "https://local.test", |
| 142 | + }; |
| 143 | + const localEnvironment = Atom.make<typeof localTarget | null>( |
| 144 | + scenario.primary === "none" || scenario.primary === "reconnecting" ? null : localTarget, |
| 145 | + ); |
| 146 | + const assets = createAssetEnvironmentAtoms( |
| 147 | + Atom.runtime(Layer.succeed(EnvironmentRegistry, environments)), |
| 148 | + localEnvironment, |
| 149 | + ); |
| 150 | + const query = assets.createUrl({ environmentId: remoteId, input: { resource } }); |
| 151 | + const result = AtomRegistry.getResult(registry, query, { suspendOnWaiting: true }); |
| 152 | + if (scenario.fallback || scenario.success) { |
| 153 | + expect((yield* result).relativeUrl).toBe( |
| 154 | + scenario.fallback |
| 155 | + ? "https://local.test/api/assets/local/media" |
| 156 | + : "/api/assets/remote/media", |
| 157 | + ); |
| 158 | + } else { |
| 159 | + expect(yield* Effect.flip(result)).toEqual(error); |
| 160 | + } |
| 161 | + expect(calls).toEqual(scenario.fallback ? [remoteId, localId] : [remoteId]); |
| 162 | + if (scenario.primary === "reconnecting") { |
| 163 | + registry.set(localEnvironment, localTarget); |
| 164 | + expect((yield* result).relativeUrl).toBe("https://local.test/api/assets/local/media"); |
| 165 | + expect(calls).toEqual([remoteId, remoteId, localId]); |
| 166 | + } |
| 167 | + }).pipe(Effect.scoped), |
| 168 | + ); |
| 169 | + } |
| 170 | + |
40 | 171 | it("keys asset URL queries by environment and resource", () => { |
41 | 172 | const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime< |
42 | 173 | EnvironmentRegistry, |
|
0 commit comments