Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2
3import { chartChunkUrlFrom, createChartsLoader } from "./charts-loader";
4
5const ORIGIN = "https://trytanstackdb.com";
6/** Where this module ends up once built: a chunk in the assets folder. */
7const MODULE_URL = `${ORIGIN}/assets/lazy-charts-B1x9.js`;
8const CHUNK_URL = `${ORIGIN}/assets/charts-GJvtBBTf.js`;
9
10const chromium = (url: string) =>
11 new TypeError(`Failed to fetch dynamically imported module: ${url}`);
12const firefox = (url: string) => new TypeError(`error loading dynamically imported module: ${url}`);
13
14describe("chartChunkUrlFrom", () => {
15 it("finds the chart chunk a failed import names (Chromium, Firefox)", () => {
16 expect(chartChunkUrlFrom(chromium(CHUNK_URL), MODULE_URL, ORIGIN)).toBe(CHUNK_URL);
17 expect(chartChunkUrlFrom(firefox(CHUNK_URL), MODULE_URL, ORIGIN)).toBe(CHUNK_URL);
18 // Dev: the source module, next to this one, maybe with Vite's timestamp.
19 expect(
20 chartChunkUrlFrom(
21 chromium("http://pop-os:5179/src/components/notes/charts.tsx?t=1730730000"),
22 "http://pop-os:5179/src/components/notes/lazy-charts.tsx",
23 "http://pop-os:5179",
24 ),
25 ).toBe("http://pop-os:5179/src/components/notes/charts.tsx");
26 });
27
28 it("rejects another origin", () => {
29 expect(
30 chartChunkUrlFrom(chromium("https://attacker.example/evil.js"), MODULE_URL, ORIGIN),
31 ).toBeNull();
32 expect(
33 chartChunkUrlFrom(
34 chromium("https://attacker.example/assets/charts-GJvtBBTf.js"),
35 MODULE_URL,
36 ORIGIN,
37 ),
38 ).toBeNull();
39 });
40
41 it("rejects same-origin scripts that aren't the chart chunk", () => {
42 for (const path of [
43 "/uploads/payload.js",
44 "/uploads/charts-GJvtBBTf.js",
45 "/assets/payload.js",
46 "/assets/sub/charts-GJvtBBTf.js",
47 "/assets/../uploads/charts-x.js",
48 "/assets/charts-GJvtBBTf.js.map",
49 ]) {
50 expect(chartChunkUrlFrom(chromium(`${ORIGIN}${path}`), MODULE_URL, ORIGIN), path).toBeNull();
51 }
52 });
53
54 it("ignores errors that aren't failed imports, even if they mention the chunk", () => {
55 expect(
56 chartChunkUrlFrom(new TypeError("Importing a module script failed."), MODULE_URL, ORIGIN),
57 ).toBeNull();
58 expect(
59 chartChunkUrlFrom(
60 new TypeError(`x is not a function (${CHUNK_URL}:1:20)`),
61 MODULE_URL,
62 ORIGIN,
63 ),
64 ).toBeNull();
65 expect(chartChunkUrlFrom(chromium("not a url"), MODULE_URL, ORIGIN)).toBeNull();
66 });
67});
68
69describe("createChartsLoader", () => {
70 function setup(firstError: unknown) {
71 const module = { name: "charts" };
72 const importModule = vi.fn().mockRejectedValueOnce(firstError).mockResolvedValue(module);
73 const importUrl = vi.fn().mockResolvedValue(module);
74 const load = createChartsLoader({
75 importModule,
76 importUrl,
77 chunkUrlFrom: (error) => chartChunkUrlFrom(error, MODULE_URL, ORIGIN),
78 });
79 return { module, importModule, importUrl, load };
80 }
81
82 it("imports once for charts loading at the same time, and once per retry", async () => {
83 const { module, importModule, importUrl, load } = setup(chromium(CHUNK_URL));
84 const first = [load(), load(), load()];
85 expect(importModule).toHaveBeenCalledTimes(1);
86 for (const attempt of first) await expect(attempt).rejects.toThrow(CHUNK_URL);
87
88 // Every chart retries at once: one import, of the chunk under a new URL.
89 const retried = [load(), load(), load()];
90 expect(await Promise.all(retried)).toEqual([module, module, module]);
91 expect(importUrl.mock.calls).toEqual([[`${CHUNK_URL}?retry=1`]]);
92 expect(importModule).toHaveBeenCalledTimes(1);
93
94 // Loaded: no more imports.
95 expect(await load()).toBe(module);
96 expect(importUrl).toHaveBeenCalledTimes(1);
97 });
98
99 it("counts retries up while they keep failing", async () => {
100 const { importUrl, load } = setup(chromium(CHUNK_URL));
101 await expect(load()).rejects.toThrow();
102 importUrl.mockRejectedValueOnce(chromium(`${CHUNK_URL}?retry=1`));
103 await expect(load()).rejects.toThrow();
104 await load();
105 expect(importUrl.mock.calls).toEqual([[`${CHUNK_URL}?retry=1`], [`${CHUNK_URL}?retry=2`]]);
106 });
107
108 it("retries the normal way when the error names no chunk, or the wrong one", async () => {
109 for (const error of [
110 new TypeError("Importing a module script failed."),
111 chromium("https://attacker.example/evil.js"),
112 chromium(`${ORIGIN}/uploads/payload.js`),
113 ]) {
114 const { module, importModule, importUrl, load } = setup(error);
115 await expect(load()).rejects.toBe(error);
116 expect(await load()).toBe(module);
117 expect(importModule).toHaveBeenCalledTimes(2);
118 expect(importUrl).not.toHaveBeenCalled();
119 }
120 });
121});