@sigil-dev/grimoire 0.9.2 → 0.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/index.ts +37 -35
  2. package/package.json +9 -12
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +31 -8
  6. package/src/client/enhance.ts +102 -98
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +5 -5
  9. package/src/client/router.ts +529 -371
  10. package/src/client/scope.ts +22 -22
  11. package/src/dev/compile-module.ts +163 -163
  12. package/src/dev/effect-registry.ts +23 -23
  13. package/src/dev/graph.ts +114 -114
  14. package/src/dev/hmr-client.ts +185 -185
  15. package/src/dev/loader.ts +47 -47
  16. package/src/dev/paths.ts +14 -14
  17. package/src/dev/runtime-bundle.ts +52 -52
  18. package/src/dev/watcher.ts +47 -46
  19. package/src/env/dynamic-private.ts +19 -19
  20. package/src/env/dynamic-public.ts +22 -22
  21. package/src/env/index.ts +25 -25
  22. package/src/env/plugin.ts +19 -19
  23. package/src/env/private.ts +5 -5
  24. package/src/env/public.ts +7 -7
  25. package/src/env/typegen.ts +61 -61
  26. package/src/headers.ts +114 -114
  27. package/src/integrations/vite.ts +73 -73
  28. package/src/logger/index.ts +4 -4
  29. package/src/logger/instance.ts +6 -6
  30. package/src/logger/logger.ts +113 -113
  31. package/src/logger/request-middleware.ts +24 -24
  32. package/src/logger/transports.ts +117 -117
  33. package/src/logger/types.ts +39 -39
  34. package/src/rendering/head.ts +47 -47
  35. package/src/rendering/hydrate.ts +146 -119
  36. package/src/rendering/index.ts +573 -372
  37. package/src/rendering/ssrPlugin.ts +69 -69
  38. package/src/routing/manifest-gen.ts +55 -42
  39. package/src/routing/router.ts +135 -109
  40. package/src/routing/scanner.ts +187 -141
  41. package/src/routing/transform-routes.ts +102 -102
  42. package/src/sentinels/error.ts +52 -52
  43. package/src/sentinels/fail.ts +41 -41
  44. package/src/sentinels/redirect.ts +35 -35
  45. package/src/server/build.ts +255 -219
  46. package/src/server/context.ts +10 -10
  47. package/src/server/cookie-utils.ts +66 -66
  48. package/src/server/coordinator.ts +364 -356
  49. package/src/server/hooks.ts +114 -114
  50. package/src/server/index.ts +677 -658
  51. package/src/server/interactive.ts +56 -56
  52. package/src/server/plugins.ts +121 -120
  53. package/src/server/worker.ts +58 -58
  54. package/src/typegen/index.ts +389 -356
  55. package/src/types.ts +8 -4
  56. package/test/client-router.test.ts +295 -35
  57. package/test/context.test.ts +52 -52
  58. package/test/env.test.ts +70 -70
  59. package/test/exports.test.ts +37 -37
  60. package/test/fail.test.ts +46 -46
  61. package/test/headers.test.ts +100 -100
  62. package/test/hydration.test.ts +119 -119
  63. package/test/middleware.test.ts +223 -223
  64. package/test/preload.ts +5 -5
  65. package/test/redirect-error.test.ts +112 -112
  66. package/test/rendering.test.ts +728 -579
  67. package/test/routing.test.ts +194 -81
  68. package/test/scanning.test.ts +254 -200
  69. package/test/scope.test.ts +195 -195
  70. package/test/server.test.ts +354 -249
  71. package/test/streaming.test.ts +242 -125
  72. package/test/transform-routes.test.ts +87 -84
  73. package/test/typegen.test.ts +697 -682
  74. package/tsconfig.json +10 -10
  75. package/.grimoire/_routes.dom.js +0 -8
  76. package/.grimoire/_routes.hydrate.js +0 -8
  77. package/.grimoire/tsconfig.generated.json +0 -11
  78. package/.grimoire/types/ambient.d.ts +0 -59
  79. package/.grimoire/types/api/hello/$types.d.ts +0 -50
  80. package/.grimoire/types/api/items/$types.d.ts +0 -50
  81. package/.grimoire/types/echo/$types.d.ts +0 -50
  82. package/.grimoire/types/env-private.d.ts +0 -5
  83. package/.grimoire/types/env-public.d.ts +0 -5
  84. package/.grimoire/types/mixed/$types.d.ts +0 -50
  85. package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
  86. package/.grimoire/types/reject/$types.d.ts +0 -50
  87. package/public/__grimoire__/hydrate.js +0 -585
  88. package/public/__grimoire__/index.js +0 -490
package/src/types.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { ServerWebSocket } from "bun";
2
- import type { RouteFile, RouteTree } from "./routing/scanner";
1
+ import type { Server, ServerWebSocket } from "bun";
3
2
  import type { ScopedLogger } from "./logger";
3
+ import type { RouteFile, RouteTree } from "./routing/scanner";
4
4
 
5
5
  /// <reference types="bun-types">
6
6
  declare global {
@@ -37,6 +37,8 @@ export interface LoadContext {
37
37
  url: URL;
38
38
  locals: Record<string, unknown>; // set by plugins/hooks
39
39
  fetch: typeof globalThis.fetch; // ← add
40
+ /** Data returned by parent layouts' loads, merged outermost-first. */
41
+ parent: () => Promise<Record<string, unknown>>;
40
42
  }
41
43
 
42
44
  export interface TypedLoadContext<
@@ -47,6 +49,8 @@ export interface TypedLoadContext<
47
49
  url: URL;
48
50
  locals: App.Locals;
49
51
  fetch: typeof fetch;
52
+ /** Data returned by parent layouts' loads, merged outermost-first. */
53
+ parent: () => Promise<Record<string, unknown>>;
50
54
  }
51
55
 
52
56
  export interface RenderContext {
@@ -67,7 +71,6 @@ export interface BuildResult {
67
71
  outputs: string[];
68
72
  errors: string[];
69
73
  }
70
- export type Server = { port: number; hostname: string; stop(): void };
71
74
 
72
75
  export interface GrimoirePlugin {
73
76
  name: string;
@@ -76,7 +79,8 @@ export interface GrimoirePlugin {
76
79
  onPluginsResolved?(ctx: PluginContext): void | Promise<void>;
77
80
 
78
81
  // server lifecycle
79
- onStart?(server: Server): void | Promise<void>;
82
+ // we dont care about what the impl puts as websocket data therefore unknown.
83
+ onStart?(server: Server<unknown>): void | Promise<void>;
80
84
  onStop?(reason: "shutdown" | "restart"): void | Promise<void>;
81
85
 
82
86
  // request pipeline
@@ -1,38 +1,298 @@
1
- import { describe, test, expect, beforeEach } from "bun:test";
2
- import { depends, hasDep, startCollecting, stopCollecting, clearDeps } from "../src/client/deps.ts";
1
+ import { beforeEach, describe, expect, test, vi } from "bun:test";
2
+ import type { Document, History, Location, Window } from "happy-dom";
3
+ import {
4
+ clearDeps,
5
+ depends,
6
+ hasDep,
7
+ setDeps,
8
+ startCollecting,
9
+ stopCollecting,
10
+ } from "../src/client/deps.ts";
3
11
 
4
- beforeEach(() => clearDeps("/"));
12
+ // test/preload.ts installs happy-dom globals at runtime; this package's
13
+ // compiler options have no DOM lib, so declare the shapes we rely on.
14
+ declare global {
15
+ // biome-ignore-all lint/style/noVar: ambient global declarations require var
16
+ var window: Window;
17
+ var document: Document;
18
+ var location: Location;
19
+ var history: History;
20
+ var MouseEvent: Window["MouseEvent"];
21
+ var Event: Window["Event"];
22
+ var requestAnimationFrame: Window["requestAnimationFrame"];
23
+ }
24
+
25
+ // router.ts reads `location` at module load — DOM globals must exist first.
26
+ // Dynamic import is required because the module under test captures location
27
+ // during evaluation, which static-hoisted imports would race. The default
28
+ // about:blank URL has origin "null", which breaks new URL(href, origin) —
29
+ // point the window at a real origin first.
30
+ window.location.href = "http://localhost/";
31
+ const globalEnv = globalThis as typeof globalThis & {
32
+ location: Location;
33
+ history: History;
34
+ requestAnimationFrame: Window["requestAnimationFrame"];
35
+ };
36
+ globalEnv.location = window.location;
37
+ globalEnv.history = window.history;
38
+ globalEnv.requestAnimationFrame = window.requestAnimationFrame.bind(window);
39
+ const router = await import("../src/client/router.ts");
40
+
41
+ function jsonResponse(pattern: string): Response {
42
+ return new Response(
43
+ JSON.stringify({
44
+ data: {},
45
+ layoutData: [],
46
+ params: {},
47
+ pattern,
48
+ head: "",
49
+ }),
50
+ { headers: { "content-type": "application/json" } },
51
+ );
52
+ }
53
+
54
+ function captureFetch(): {
55
+ calls: Array<{ input: string; init?: RequestInit }>;
56
+ restore: () => void;
57
+ } {
58
+ const calls: Array<{ input: string; init?: RequestInit }> = [];
59
+ const original = globalThis.fetch;
60
+ // test shim over the global — precise fetch typing is impossible here.
61
+ // Echoes the requested pathname as the route pattern, mirroring renderRoute.
62
+ globalThis.fetch = (async (
63
+ input: string,
64
+ init?: RequestInit,
65
+ ): Promise<Response> => {
66
+ calls.push({ input, init });
67
+ const pattern = new URL(input, "http://localhost").pathname;
68
+ return jsonResponse(pattern);
69
+ }) as unknown as typeof fetch;
70
+ return {
71
+ calls,
72
+ restore: () => {
73
+ globalThis.fetch = original;
74
+ },
75
+ };
76
+ }
5
77
 
6
78
  describe("depends", () => {
7
- test("no-op outside collection", () => {
8
- depends("posts");
9
- expect(hasDep("/", "posts")).toBe(false);
10
- });
11
-
12
- test("registers key during collection", () => {
13
- startCollecting();
14
- depends("posts");
15
- stopCollecting("/");
16
- expect(hasDep("/", "posts")).toBe(true);
17
- });
18
-
19
- test("multiple keys", () => {
20
- startCollecting();
21
- depends("posts");
22
- depends("tags");
23
- stopCollecting("/");
24
- expect(hasDep("/", "posts")).toBe(true);
25
- expect(hasDep("/", "tags")).toBe(true);
26
- });
27
-
28
- test("cleared on next collection", () => {
29
- startCollecting();
30
- depends("posts");
31
- stopCollecting("/");
32
- startCollecting();
33
- depends("comments");
34
- stopCollecting("/");
35
- expect(hasDep("/", "posts")).toBe(false);
36
- expect(hasDep("/", "comments")).toBe(true);
37
- });
38
- });
79
+ test("no-op outside collection", () => {
80
+ depends("posts");
81
+ expect(hasDep("/", "posts")).toBe(false);
82
+ });
83
+
84
+ test("registers key during collection", () => {
85
+ startCollecting();
86
+ depends("posts");
87
+ stopCollecting("/");
88
+ expect(hasDep("/", "posts")).toBe(true);
89
+ });
90
+
91
+ test("multiple keys", () => {
92
+ startCollecting();
93
+ depends("posts");
94
+ depends("tags");
95
+ stopCollecting("/");
96
+ expect(hasDep("/", "posts")).toBe(true);
97
+ expect(hasDep("/", "tags")).toBe(true);
98
+ });
99
+
100
+ test("cleared on next collection", () => {
101
+ startCollecting();
102
+ depends("posts");
103
+ stopCollecting("/");
104
+ startCollecting();
105
+ depends("comments");
106
+ stopCollecting("/");
107
+ expect(hasDep("/", "posts")).toBe(false);
108
+ expect(hasDep("/", "comments")).toBe(true);
109
+ });
110
+ });
111
+ describe("navigate — view transitions", () => {
112
+ test("falls back to direct swap when startViewTransition is missing", async () => {
113
+ const root = document.createElement("div");
114
+ root.id = "grimoire-root";
115
+ document.body.appendChild(root);
116
+ const fetchShim = captureFetch();
117
+ try {
118
+ router.initRouter({ "/": () => document.createElement("main") }, []);
119
+ await router.navigate("/");
120
+ expect(root.childNodes.length).toBeGreaterThan(0);
121
+ } finally {
122
+ fetchShim.restore();
123
+ root.remove();
124
+ }
125
+ });
126
+
127
+ test("routes the swap through startViewTransition when available", async () => {
128
+ const root = document.createElement("div");
129
+ root.id = "grimoire-root";
130
+ document.body.appendChild(root);
131
+ let vtCalled = false;
132
+ const docEnv = document as Document & {
133
+ startViewTransition?: (cb: () => void) => unknown;
134
+ };
135
+ docEnv.startViewTransition = (cb: () => void) => {
136
+ vtCalled = true;
137
+ cb();
138
+ return undefined;
139
+ };
140
+ const fetchShim = captureFetch();
141
+ try {
142
+ router.initRouter({ "/vt": () => document.createElement("main") }, []);
143
+ await router.navigate("/vt");
144
+ expect(vtCalled).toBe(true);
145
+ expect(root.childNodes.length).toBeGreaterThan(0);
146
+ } finally {
147
+ delete docEnv.startViewTransition;
148
+ fetchShim.restore();
149
+ root.remove();
150
+ }
151
+ });
152
+ });
153
+
154
+ describe("hover prefetch", () => {
155
+ // The 80ms debounce is real timer logic under test — fake timers advance
156
+ // it deterministically instead of sleeping past it.
157
+ test("plain internal link prefetches route data; preload=off skips", () => {
158
+ vi.useFakeTimers();
159
+ router.initRouter(
160
+ {
161
+ "/": () => document.createElement("div"),
162
+ "/off": () => document.createElement("div"),
163
+ },
164
+ [],
165
+ );
166
+ const fetchShim = captureFetch();
167
+ try {
168
+ const plain = document.createElement("a");
169
+ plain.setAttribute("href", "/");
170
+ document.body.appendChild(plain);
171
+ plain.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
172
+ vi.advanceTimersByTime(80);
173
+ expect(fetchShim.calls.length).toBe(1);
174
+ expect(fetchShim.calls[0]?.init?.headers).toEqual({
175
+ "x-grimoire-navigate": "1",
176
+ });
177
+
178
+ const optedOut = document.createElement("a");
179
+ optedOut.setAttribute("href", "/off");
180
+ optedOut.setAttribute("data-sigil-preload", "off");
181
+ document.body.appendChild(optedOut);
182
+ optedOut.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
183
+ vi.advanceTimersByTime(80);
184
+ expect(fetchShim.calls.length).toBe(1); // unchanged
185
+ } finally {
186
+ vi.useRealTimers();
187
+ fetchShim.restore();
188
+ }
189
+ });
190
+
191
+ test("external links are never prefetched", () => {
192
+ vi.useFakeTimers();
193
+ const fetchShim = captureFetch();
194
+ try {
195
+ const ext = document.createElement("a");
196
+ ext.setAttribute("href", "https://example.com/page");
197
+ document.body.appendChild(ext);
198
+ ext.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
199
+ vi.advanceTimersByTime(80);
200
+ expect(fetchShim.calls.length).toBe(0);
201
+ } finally {
202
+ vi.useRealTimers();
203
+ fetchShim.restore();
204
+ }
205
+ });
206
+ });
207
+
208
+ describe("enhance — named action URLs", () => {
209
+ test("action starting with ?/ submits to the current pathname", async () => {
210
+ // earlier navigate() tests moved history — reset to a clean path
211
+ window.history.pushState(null, "", "/");
212
+ const { enhance } = await import("../src/client/enhance.ts");
213
+ const calls: string[] = [];
214
+ const original = globalThis.fetch;
215
+ globalThis.fetch = (async (input: string) => {
216
+ calls.push(input);
217
+ return new Response(null, { status: 303, headers: { location: "/" } });
218
+ }) as unknown as typeof fetch;
219
+
220
+ const form = document.createElement("form");
221
+ document.body.appendChild(form);
222
+ const cleanup = enhance(form, { action: "?/create" });
223
+ try {
224
+ form.dispatchEvent(
225
+ new Event("submit", { bubbles: true, cancelable: true }),
226
+ );
227
+ await new Promise((r) => setTimeout(r, 0));
228
+ expect(calls).toEqual([`${window.location.pathname}?/create`]);
229
+ } finally {
230
+ cleanup();
231
+ form.remove();
232
+ globalThis.fetch = original;
233
+ }
234
+ });
235
+ });
236
+
237
+ describe("Page.load — parent()", () => {
238
+ test("parent() merges layoutData entries outermost-first", async () => {
239
+ let seenParent: Record<string, unknown> | undefined;
240
+ const PageWithLoad = Object.assign(
241
+ () => document.createElement("div"),
242
+ {
243
+ load: async (ctx: { parent: () => Promise<Record<string, unknown>> }) => {
244
+ seenParent = await ctx.parent();
245
+ return {};
246
+ },
247
+ },
248
+ );
249
+
250
+ const original = globalThis.fetch;
251
+ globalThis.fetch = (async (input: string) => {
252
+ const pattern = new URL(input, "http://localhost").pathname;
253
+ return new Response(
254
+ JSON.stringify({
255
+ data: {},
256
+ layoutData: [{ a: 1 }, { b: 2 }],
257
+ params: {},
258
+ pattern,
259
+ head: "",
260
+ }),
261
+ { headers: { "content-type": "application/json" } },
262
+ );
263
+ }) as unknown as typeof fetch;
264
+
265
+ try {
266
+ router.initRouter({ "/pl": PageWithLoad }, []);
267
+ await router.navigate("/pl");
268
+ expect(seenParent).toEqual({ a: 1, b: 2 });
269
+ } finally {
270
+ globalThis.fetch = original;
271
+ }
272
+ });
273
+ });
274
+
275
+ describe("invalidate", () => {
276
+ test("refetches only on key intersection; empty deps always refetch", async () => {
277
+ router.initRouter({ "/": () => document.createElement("div") }, []);
278
+ const shim = captureFetch();
279
+ try {
280
+ await router.navigate("/"); // establishes lastState.pattern = "/"
281
+
282
+ setDeps("/", ["user:1"]);
283
+ shim.calls.length = 0;
284
+ await router.invalidate("other");
285
+ expect(shim.calls.length).toBe(0); // no intersection → skip
286
+
287
+ await router.invalidate("user:1");
288
+ expect(shim.calls.length).toBe(1); // intersect → refetch
289
+
290
+ clearDeps("/");
291
+ await router.invalidate("anything");
292
+ expect(shim.calls.length).toBe(2); // no deps → conservative refetch
293
+ } finally {
294
+ shim.restore();
295
+ clearDeps("/");
296
+ }
297
+ });
298
+ });
@@ -1,52 +1,52 @@
1
- // packages/grimoire/src/context.test.ts
2
- import { expect, test } from "bun:test";
3
- import { createContext, getContext, setContext } from "@sigil-dev/runtime";
4
- import { runWithContext } from "../src/server/context";
5
-
6
- const UserKey = createContext<string>();
7
- const _ThemeKey = createContext<string>();
8
-
9
- // 1. Basic: does it work at all
10
- test("getContext returns value set in same context", async () => {
11
- await runWithContext(async () => {
12
- setContext(UserKey, "cane");
13
- expect(getContext(UserKey)).toBe("cane");
14
- });
15
- });
16
-
17
- // 2. Isolation: the whole point of AsyncLocalStorage
18
- test("concurrent requests do not bleed context", async () => {
19
- let resolveA!: () => void;
20
- let resolveB!: () => void;
21
-
22
- const barrier = {
23
- a: new Promise<void>((r) => (resolveA = r)),
24
- b: new Promise<void>((r) => (resolveB = r)),
25
- };
26
-
27
- const requestA = runWithContext(async () => {
28
- setContext(UserKey, "request-A");
29
- await barrier.a; // suspend, let B run
30
- // if context bleeds, this returns "request-B"
31
- return getContext(UserKey);
32
- });
33
-
34
- const requestB = runWithContext(async () => {
35
- setContext(UserKey, "request-B");
36
- resolveA(); // wake A up
37
- await barrier.b;
38
- return getContext(UserKey);
39
- });
40
-
41
- resolveB();
42
- const [a, b] = await Promise.all([requestA, requestB]);
43
-
44
- expect(a).toBe("request-A"); // fails if bleed
45
- expect(b).toBe("request-B");
46
- });
47
-
48
- // 3. Outside context: should not throw, returns undefined
49
- test("getContext outside runWithContext returns undefined gracefully", () => {
50
- const val = getContext(UserKey);
51
- expect(val).toBeUndefined();
52
- });
1
+ // packages/grimoire/src/context.test.ts
2
+ import { expect, test } from "bun:test";
3
+ import { createContext, getContext, setContext } from "@sigil-dev/runtime";
4
+ import { runWithContext } from "../src/server/context";
5
+
6
+ const UserKey = createContext<string>();
7
+ const _ThemeKey = createContext<string>();
8
+
9
+ // 1. Basic: does it work at all
10
+ test("getContext returns value set in same context", async () => {
11
+ await runWithContext(async () => {
12
+ setContext(UserKey, "cane");
13
+ expect(getContext(UserKey)).toBe("cane");
14
+ });
15
+ });
16
+
17
+ // 2. Isolation: the whole point of AsyncLocalStorage
18
+ test("concurrent requests do not bleed context", async () => {
19
+ let resolveA!: () => void;
20
+ let resolveB!: () => void;
21
+
22
+ const barrier = {
23
+ a: new Promise<void>((r) => (resolveA = r)),
24
+ b: new Promise<void>((r) => (resolveB = r)),
25
+ };
26
+
27
+ const requestA = runWithContext(async () => {
28
+ setContext(UserKey, "request-A");
29
+ await barrier.a; // suspend, let B run
30
+ // if context bleeds, this returns "request-B"
31
+ return getContext(UserKey);
32
+ });
33
+
34
+ const requestB = runWithContext(async () => {
35
+ setContext(UserKey, "request-B");
36
+ resolveA(); // wake A up
37
+ await barrier.b;
38
+ return getContext(UserKey);
39
+ });
40
+
41
+ resolveB();
42
+ const [a, b] = await Promise.all([requestA, requestB]);
43
+
44
+ expect(a).toBe("request-A"); // fails if bleed
45
+ expect(b).toBe("request-B");
46
+ });
47
+
48
+ // 3. Outside context: should not throw, returns undefined
49
+ test("getContext outside runWithContext returns undefined gracefully", () => {
50
+ const val = getContext(UserKey);
51
+ expect(val).toBeUndefined();
52
+ });
package/test/env.test.ts CHANGED
@@ -1,90 +1,90 @@
1
1
  // packages/grimoire/src/env/env.test.ts
2
- import { describe, test, expect, beforeEach, afterEach } from "bun:test";
2
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
3
3
 
4
4
  describe("$env/dynamic/public", () => {
5
- let orig: Record<string, string | undefined> = {};
5
+ let orig: Record<string, string | undefined> = {};
6
6
 
7
- beforeEach(() => {
8
- orig = {
9
- PUBLIC_APP_NAME: process.env.PUBLIC_APP_NAME,
10
- PUBLIC_API_URL: process.env.PUBLIC_API_URL,
11
- SECRET_KEY: process.env.SECRET_KEY,
12
- };
13
- });
7
+ beforeEach(() => {
8
+ orig = {
9
+ PUBLIC_APP_NAME: process.env.PUBLIC_APP_NAME,
10
+ PUBLIC_API_URL: process.env.PUBLIC_API_URL,
11
+ SECRET_KEY: process.env.SECRET_KEY,
12
+ };
13
+ });
14
14
 
15
- afterEach(() => {
16
- for (const [k, v] of Object.entries(orig)) {
17
- if (v === undefined) delete process.env[k];
18
- else process.env[k] = v;
19
- }
20
- });
15
+ afterEach(() => {
16
+ for (const [k, v] of Object.entries(orig)) {
17
+ if (v === undefined) delete process.env[k];
18
+ else process.env[k] = v;
19
+ }
20
+ });
21
21
 
22
- test("reads PUBLIC_ vars at call time", async () => {
23
- process.env.PUBLIC_APP_NAME = "grimoire";
24
- const { env } = await import("../src/env/dynamic-public.ts");
25
- expect(env.PUBLIC_APP_NAME).toBe("grimoire");
26
- });
22
+ test("reads PUBLIC_ vars at call time", async () => {
23
+ process.env.PUBLIC_APP_NAME = "grimoire";
24
+ const { env } = await import("../src/env/dynamic-public.ts");
25
+ expect(env.PUBLIC_APP_NAME).toBe("grimoire");
26
+ });
27
27
 
28
- test("reflects changes after import", async () => {
29
- const { env } = await import("../src/env/dynamic-public.ts");
30
- process.env.PUBLIC_APP_NAME = "changed";
31
- expect(env.PUBLIC_APP_NAME).toBe("changed");
32
- });
28
+ test("reflects changes after import", async () => {
29
+ const { env } = await import("../src/env/dynamic-public.ts");
30
+ process.env.PUBLIC_APP_NAME = "changed";
31
+ expect(env.PUBLIC_APP_NAME).toBe("changed");
32
+ });
33
33
 
34
- test("does not expose non-PUBLIC_ vars", async () => {
35
- process.env.SECRET_KEY = "supersecret";
36
- const { env } = await import("../src/env/dynamic-public.ts");
37
- expect(env.SECRET_KEY).toBeUndefined();
38
- });
34
+ test("does not expose non-PUBLIC_ vars", async () => {
35
+ process.env.SECRET_KEY = "supersecret";
36
+ const { env } = await import("../src/env/dynamic-public.ts");
37
+ expect(env.SECRET_KEY).toBeUndefined();
38
+ });
39
39
 
40
- test("returns empty string for missing PUBLIC_ key", async () => {
41
- delete process.env.PUBLIC_API_URL;
42
- const { env } = await import("../src/env/dynamic-public.ts");
43
- expect(env.PUBLIC_API_URL).toBe("");
44
- });
40
+ test("returns empty string for missing PUBLIC_ key", async () => {
41
+ delete process.env.PUBLIC_API_URL;
42
+ const { env } = await import("../src/env/dynamic-public.ts");
43
+ expect(env.PUBLIC_API_URL).toBe("");
44
+ });
45
45
 
46
- test("ownKeys only includes PUBLIC_ vars", async () => {
47
- process.env.PUBLIC_APP_NAME = "grimoire";
48
- process.env.SECRET_KEY = "secret";
49
- const { env } = await import("../src/env/dynamic-public.ts");
50
- const keys = Object.keys(env);
51
- expect(keys.every(k => k.startsWith("PUBLIC_"))).toBe(true);
52
- });
46
+ test("ownKeys only includes PUBLIC_ vars", async () => {
47
+ process.env.PUBLIC_APP_NAME = "grimoire";
48
+ process.env.SECRET_KEY = "secret";
49
+ const { env } = await import("../src/env/dynamic-public.ts");
50
+ const keys = Object.keys(env);
51
+ expect(keys.every((k) => k.startsWith("PUBLIC_"))).toBe(true);
52
+ });
53
53
  });
54
54
 
55
55
  describe("$env/dynamic/private", () => {
56
- let orig: string | undefined;
56
+ let orig: string | undefined;
57
57
 
58
- beforeEach(() => {
59
- orig = process.env.DATABASE_URL;
60
- });
58
+ beforeEach(() => {
59
+ orig = process.env.DATABASE_URL;
60
+ });
61
61
 
62
- afterEach(() => {
63
- if (orig === undefined) delete process.env.DATABASE_URL;
64
- else process.env.DATABASE_URL = orig;
65
- });
62
+ afterEach(() => {
63
+ if (orig === undefined) delete process.env.DATABASE_URL;
64
+ else process.env.DATABASE_URL = orig;
65
+ });
66
66
 
67
- test("reads any env var at call time", async () => {
68
- process.env.DATABASE_URL = "postgres://localhost/test";
69
- const { env } = await import("../src/env/dynamic-private.ts");
70
- expect(env.DATABASE_URL).toBe("postgres://localhost/test");
71
- });
67
+ test("reads any env var at call time", async () => {
68
+ process.env.DATABASE_URL = "postgres://localhost/test";
69
+ const { env } = await import("../src/env/dynamic-private.ts");
70
+ expect(env.DATABASE_URL).toBe("postgres://localhost/test");
71
+ });
72
72
 
73
- test("reflects changes after import", async () => {
74
- const { env } = await import("../src/env/dynamic-private.ts");
75
- process.env.DATABASE_URL = "postgres://localhost/changed";
76
- expect(env.DATABASE_URL).toBe("postgres://localhost/changed");
77
- });
73
+ test("reflects changes after import", async () => {
74
+ const { env } = await import("../src/env/dynamic-private.ts");
75
+ process.env.DATABASE_URL = "postgres://localhost/changed";
76
+ expect(env.DATABASE_URL).toBe("postgres://localhost/changed");
77
+ });
78
78
 
79
- test("returns empty string for missing key", async () => {
80
- delete process.env.DATABASE_URL;
81
- const { env } = await import("../src/env/dynamic-private.ts");
82
- expect(env.DATABASE_URL).toBe("");
83
- });
79
+ test("returns empty string for missing key", async () => {
80
+ delete process.env.DATABASE_URL;
81
+ const { env } = await import("../src/env/dynamic-private.ts");
82
+ expect(env.DATABASE_URL).toBe("");
83
+ });
84
84
 
85
- test("exposes PUBLIC_ vars too", async () => {
86
- process.env.PUBLIC_APP_NAME = "grimoire";
87
- const { env } = await import("../src/env/dynamic-private.ts");
88
- expect(env.PUBLIC_APP_NAME).toBe("grimoire");
89
- });
85
+ test("exposes PUBLIC_ vars too", async () => {
86
+ process.env.PUBLIC_APP_NAME = "grimoire";
87
+ const { env } = await import("../src/env/dynamic-private.ts");
88
+ expect(env.PUBLIC_APP_NAME).toBe("grimoire");
89
+ });
90
90
  });