@sigil-dev/grimoire 0.9.14 → 0.9.16

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.
@@ -0,0 +1,96 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { preloadModules, startWarming } from "../src/server/warm.ts";
3
+
4
+ describe("preloadModules", () => {
5
+ test("imports each file once and reports failures without throwing", async () => {
6
+ const seen: string[] = [];
7
+ const warnings: string[] = [];
8
+ const loaded = await preloadModules(
9
+ ["/a.tsx", "/b.tsx", "/a.tsx", "/bad.tsx"],
10
+ async (f) => {
11
+ seen.push(f);
12
+ if (f === "/bad.tsx") throw new Error("boom");
13
+ },
14
+ (msg) => warnings.push(msg),
15
+ );
16
+ expect(loaded).toBe(2);
17
+ expect(seen.sort()).toEqual(["/a.tsx", "/b.tsx", "/bad.tsx"]);
18
+ expect(warnings).toEqual(["could not preload /bad.tsx:"]);
19
+ });
20
+ });
21
+
22
+ describe("startWarming", () => {
23
+ test("requests paths in order, with the given headers, and drains bodies", async () => {
24
+ const calls: Array<{ path: string; header: string | null }> = [];
25
+ let drained = 0;
26
+ const warmer = startWarming({
27
+ paths: ["/one", "/two"],
28
+ origin: "http://127.0.0.1:1234",
29
+ headers: { "x-grimoire-internal": "s3cret" },
30
+ handle: async (req) => {
31
+ calls.push({
32
+ path: new URL(req.url).pathname,
33
+ header: req.headers.get("x-grimoire-internal"),
34
+ });
35
+ const stream = new ReadableStream({
36
+ pull(c) {
37
+ drained++;
38
+ c.enqueue(new TextEncoder().encode("x"));
39
+ c.close();
40
+ },
41
+ });
42
+ return new Response(stream);
43
+ },
44
+ log: () => {},
45
+ });
46
+ await warmer.firstPass;
47
+ warmer.stop();
48
+ expect(calls).toEqual([
49
+ { path: "/one", header: "s3cret" },
50
+ { path: "/two", header: "s3cret" },
51
+ ]);
52
+ expect(drained).toBe(2);
53
+ });
54
+
55
+ test("a failing path is logged and does not stop the rest", async () => {
56
+ const logs: string[] = [];
57
+ const paths: string[] = [];
58
+ const warmer = startWarming({
59
+ paths: ["/bad", "/good"],
60
+ origin: "http://x",
61
+ handle: async (req) => {
62
+ const p = new URL(req.url).pathname;
63
+ paths.push(p);
64
+ if (p === "/bad") throw new Error("nope");
65
+ return new Response("ok");
66
+ },
67
+ log: (m) => logs.push(m),
68
+ });
69
+ await warmer.firstPass;
70
+ warmer.stop();
71
+ expect(paths).toEqual(["/bad", "/good"]);
72
+ expect(logs[0]).toContain("/bad failed: nope");
73
+ expect(logs[1]).toContain("warmed /good (200)");
74
+ });
75
+
76
+ test("every repeats the pass until stopped", async () => {
77
+ let n = 0;
78
+ const warmer = startWarming({
79
+ paths: ["/p"],
80
+ every: 20,
81
+ origin: "http://x",
82
+ handle: async () => {
83
+ n++;
84
+ return new Response("ok");
85
+ },
86
+ log: () => {},
87
+ });
88
+ await warmer.firstPass;
89
+ await Bun.sleep(90);
90
+ warmer.stop();
91
+ const seen = n;
92
+ expect(seen).toBeGreaterThanOrEqual(3);
93
+ await Bun.sleep(60);
94
+ expect(n).toBe(seen);
95
+ });
96
+ });