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

assets.test.ts

120 lines3.4 KB

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,
78 input: {
79 resource: {
80 _tag: "project-favicon",
81 cwd: "/repo/original",
82 path: "brand/icon.svg",
83 },
84 },
85 }),
86 ).not.toBe(assets.createUrl(originalTarget));
87 expect(
88 assets.createUrl({
89 environmentId: EnvironmentId.make("environment-2"),
90 input: originalTarget.input,
91 }),
92 ).not.toBe(assets.createUrl(originalTarget));
93 });
94​
95 it("keys collections while preserving independent resource queries", () => {
96 const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime<
97 EnvironmentRegistry,
98 never
99 >;
100 const assets = createAssetEnvironmentAtoms(runtime);
101 const environmentId = EnvironmentId.make("environment-1");
102 const resources = [
103 { _tag: "attachment" as const, attachmentId: "attachment-1" },
104 { _tag: "attachment" as const, attachmentId: "attachment-2" },
105 ];
106​
107 expect(assets.createUrls({ environmentId, resources })).toBe(
108 assets.createUrls({
109 environmentId,
110 resources: resources.map((resource) => ({ ...resource })),
111 }),
112 );
113 expect(
114 assets.createUrls({
115 environmentId,
116 resources: [...resources].toReversed(),
117 }),
118 ).not.toBe(assets.createUrls({ environmentId, resources }));
119 });
120});