Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import { EnvironmentId } from "@t3tools/contracts";
3import * as Layer from "effect/Layer";
4import { Atom } from "effect/unstable/reactivity";
5
6import type { EnvironmentRegistry } from "../connection/registry.ts";
7import {
8 createAssetEnvironmentAtoms,
9 InvalidAssetCollectionKeyError,
10 parseAssetCollectionKey,
11} from "./assets.ts";
12
13describe("asset collection keys", () => {
14 it("preserves malformed JSON and its native cause", () => {
15 const key = "not-json";
16 let error: unknown;
17
18 try {
19 parseAssetCollectionKey(key);
20 } catch (cause) {
21 error = cause;
22 }
23
24 expect(error).toBeInstanceOf(InvalidAssetCollectionKeyError);
25 expect(error).toMatchObject({ key, cause: expect.any(SyntaxError) });
26 });
27
28 it("rejects invalid asset collection shapes", () => {
29 const key = JSON.stringify(["environment-1", [{ _tag: "unknown" }]]);
30
31 expect(() => parseAssetCollectionKey(key)).toThrowError(InvalidAssetCollectionKeyError);
32 });
33});
34
35describe("createAssetEnvironmentAtoms", () => {
36 it("keys asset URL queries by environment and resource", () => {
37 const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime<
38 EnvironmentRegistry,
39 never
40 >;
41 const assets = createAssetEnvironmentAtoms(runtime);
42 const environmentId = EnvironmentId.make("environment-1");
43 const originalTarget = {
44 environmentId,
45 input: {
46 resource: {
47 _tag: "project-favicon" as const,
48 cwd: "/repo/original",
49 },
50 },
51 };
52
53 expect(assets.createUrl(originalTarget)).toBe(
54 assets.createUrl({
55 environmentId,
56 input: {
57 resource: {
58 _tag: "project-favicon",
59 cwd: "/repo/original",
60 },
61 },
62 }),
63 );
64 expect(
65 assets.createUrl({
66 environmentId,
67 input: {
68 resource: {
69 _tag: "project-favicon",
70 cwd: "/repo/next",
71 },
72 },
73 }),
74 ).not.toBe(assets.createUrl(originalTarget));
75 expect(
76 assets.createUrl({
77 environmentId: EnvironmentId.make("environment-2"),
78 input: originalTarget.input,
79 }),
80 ).not.toBe(assets.createUrl(originalTarget));
81 });
82
83 it("keys collections while preserving independent resource queries", () => {
84 const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime<
85 EnvironmentRegistry,
86 never
87 >;
88 const assets = createAssetEnvironmentAtoms(runtime);
89 const environmentId = EnvironmentId.make("environment-1");
90 const resources = [
91 { _tag: "attachment" as const, attachmentId: "attachment-1" },
92 { _tag: "attachment" as const, attachmentId: "attachment-2" },
93 ];
94
95 expect(assets.createUrls({ environmentId, resources })).toBe(
96 assets.createUrls({
97 environmentId,
98 resources: resources.map((resource) => ({ ...resource })),
99 }),
100 );
101 expect(
102 assets.createUrls({
103 environmentId,
104 resources: [...resources].toReversed(),
105 }),
106 ).not.toBe(assets.createUrls({ environmentId, resources }));
107 });
108});