summaryrefslogtreecommitdiffhomepage
path: root/packages/desktop-electron/src/renderer/html.test.ts
diff options
context:
space:
mode:
authorLuke Parker <[email protected]>2026-03-12 14:19:44 +1000
committerGitHub <[email protected]>2026-03-12 12:19:44 +0800
commit54e7baa6cfff86627e5555842560b4a20e4be424 (patch)
tree901937a48c87d04a853ea4c62a79d2b86640ee2c /packages/desktop-electron/src/renderer/html.test.ts
parent1d7fcd40b45548e20b43e29cd16b83899c8f6eb1 (diff)
downloadopencode-54e7baa6cfff86627e5555842560b4a20e4be424.tar.gz
opencode-54e7baa6cfff86627e5555842560b4a20e4be424.zip
fix(desktop-electron): fix resource loading under file:// protocol (#17125)
Diffstat (limited to 'packages/desktop-electron/src/renderer/html.test.ts')
-rw-r--r--packages/desktop-electron/src/renderer/html.test.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/packages/desktop-electron/src/renderer/html.test.ts b/packages/desktop-electron/src/renderer/html.test.ts
new file mode 100644
index 000000000..bd8281c2f
--- /dev/null
+++ b/packages/desktop-electron/src/renderer/html.test.ts
@@ -0,0 +1,62 @@
+import { describe, expect, test } from "bun:test"
+import { join, dirname, resolve } from "node:path"
+import { existsSync } from "node:fs"
+import { fileURLToPath } from "node:url"
+
+const dir = dirname(fileURLToPath(import.meta.url))
+const root = resolve(dir, "../..")
+
+const html = async (name: string) => Bun.file(join(dir, name)).text()
+
+/**
+ * Electron loads renderer HTML via `win.loadFile()` which uses the `file://`
+ * protocol. Absolute paths like `src="/foo.js"` resolve to the filesystem root
+ * (e.g. `file:///C:/foo.js` on Windows) instead of relative to the app bundle.
+ *
+ * All local resource references must use relative paths (`./`).
+ */
+describe("electron renderer html", () => {
+ for (const name of ["index.html", "loading.html"]) {
+ describe(name, () => {
+ test("script src attributes use relative paths", async () => {
+ const content = await html(name)
+ const srcs = [...content.matchAll(/\bsrc=["']([^"']+)["']/g)].map((m) => m[1])
+ for (const src of srcs) {
+ expect(src).not.toMatch(/^\/[^/]/)
+ }
+ })
+
+ test("link href attributes use relative paths", async () => {
+ const content = await html(name)
+ const hrefs = [...content.matchAll(/<link[^>]+href=["']([^"']+)["']/g)].map((m) => m[1])
+ for (const href of hrefs) {
+ expect(href).not.toMatch(/^\/[^/]/)
+ }
+ })
+
+ test("no web manifest link (not applicable in Electron)", async () => {
+ const content = await html(name)
+ expect(content).not.toContain('rel="manifest"')
+ })
+ })
+ }
+})
+
+/**
+ * Vite resolves `publicDir` relative to `root`, not the config file.
+ * This test reads the actual values from electron.vite.config.ts to catch
+ * regressions where the publicDir path no longer resolves correctly
+ * after the renderer root is accounted for.
+ */
+describe("electron vite publicDir", () => {
+ test("configured publicDir resolves to a directory with oc-theme-preload.js", async () => {
+ const config = await Bun.file(join(root, "electron.vite.config.ts")).text()
+ const pub = config.match(/publicDir:\s*["']([^"']+)["']/)
+ const rendererRoot = config.match(/root:\s*["']([^"']+)["']/)
+ expect(pub).not.toBeNull()
+ expect(rendererRoot).not.toBeNull()
+ const resolved = resolve(root, rendererRoot![1], pub![1])
+ expect(existsSync(resolved)).toBe(true)
+ expect(existsSync(join(resolved, "oc-theme-preload.js"))).toBe(true)
+ })
+})