1import { assert, describe, it } from "vite-plus/test";
2
3import { verifyPreloadBundle } from "./verify-preload-bundle.mjs";
4
5const validPreload = `
6 const electron = require("electron");
7 const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
8 electron.contextBridge.exposeInMainWorld("__clerk_internal_electron_passkeys", {});
9 electron.contextBridge.exposeInMainWorld("desktopBridge", {
10 getClientPlatform: () => process.platform,
11 getLocalEnvironmentBootstraps: () => [],
12 getPathForFile: () => "",
13 pickFolder: (options) => electron.ipcRenderer.invoke(PICK_FOLDER_CHANNEL, options),
14 });
15`;
16
17describe("desktop preload bundle verifier", () => {
18 it("rejects required API names that only appear in strings", () => {
19 assert.throws(
20 () =>
21 verifyPreloadBundle(`
22 "desktopBridge getClientPlatform getLocalEnvironmentBootstraps pickFolder";
23 "__clerk_internal_electron_passkeys";
24 require("electron");
25 `),
26 /missing executable APIs/,
27 );
28 });
29
30 it("rejects a required API whose exposed value is not callable", () => {
31 assert.throws(
32 () =>
33 verifyPreloadBundle(
34 validPreload.replace(
35 "getClientPlatform: () => process.platform,",
36 "getClientPlatform: undefined,",
37 ),
38 ),
39 /missing executable APIs: getClientPlatform/,
40 );
41 });
42
43 it("accepts a required API exposed through a function alias", () => {
44 assert.doesNotThrow(() =>
45 verifyPreloadBundle(`
46 const readClientPlatform = () => process.platform;
47 ${validPreload.replace(
48 "getClientPlatform: () => process.platform,",
49 "getClientPlatform: readClientPlatform,",
50 )}
51 `),
52 );
53 });
54
55 it("rejects dynamic imports with comments before the opening parenthesis", () => {
56 assert.throws(
57 () =>
58 verifyPreloadBundle(`${validPreload}\nimport /* @vite-ignore */("unsupported-module");`),
59 /dynamic import\(\)/,
60 );
61 });
62
63 it("rejects unsupported require calls with comments before the opening parenthesis", () => {
64 assert.throws(
65 () => verifyPreloadBundle(`${validPreload}\nrequire /* @__PURE__ */ ("node:fs");`),
66 /unsupported sandbox imports: node:fs/,
67 );
68 });
69
70 it("accepts Electron sandbox module aliases", () => {
71 assert.doesNotThrow(() =>
72 verifyPreloadBundle(`
73 ${validPreload}
74 require("electron/common");
75 require("electron/renderer");
76 require("node:events");
77 require("node:timers");
78 require("node:url");
79 `),
80 );
81 });
82
83 it("runs a preload that reads the page's DOM while loading", () => {
84 assert.doesNotThrow(() =>
85 verifyPreloadBundle(`
86 ${validPreload}
87 const report = () => document.fullscreenElement;
88 report();
89 document.addEventListener("fullscreenchange", report);
90 window.addEventListener("resize", report);
91 `),
92 );
93 });
94
95 it("ignores require-like text in strings and comments", () => {
96 assert.doesNotThrow(() =>
97 verifyPreloadBundle(`
98 ${validPreload}
99 const message = 'require("node:fs")';
100 // require("node:path")
101 `),
102 );
103 });
104});