html.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { describe, expect, test } from "bun:test"
  2. import { join, dirname, resolve } from "node:path"
  3. import { existsSync } from "node:fs"
  4. import { fileURLToPath } from "node:url"
  5. const dir = dirname(fileURLToPath(import.meta.url))
  6. const root = resolve(dir, "../..")
  7. const html = async (name: string) => Bun.file(join(dir, name)).text()
  8. /**
  9. * Packaged Electron windows load renderer HTML via the privileged `oc://`
  10. * protocol. Root-relative asset paths like `src="/foo.js"` would resolve from
  11. * the protocol origin root instead of relative to the current HTML entrypoint.
  12. *
  13. * All local resource references must use relative paths (`./`).
  14. */
  15. describe("electron renderer html", () => {
  16. for (const name of ["index.html", "loading.html"]) {
  17. describe(name, () => {
  18. test("script src attributes use relative paths", async () => {
  19. const content = await html(name)
  20. const srcs = [...content.matchAll(/\bsrc=["']([^"']+)["']/g)].map((m) => m[1])
  21. for (const src of srcs) {
  22. expect(src).not.toMatch(/^\/[^/]/)
  23. }
  24. })
  25. test("link href attributes use relative paths", async () => {
  26. const content = await html(name)
  27. const hrefs = [...content.matchAll(/<link[^>]+href=["']([^"']+)["']/g)].map((m) => m[1])
  28. for (const href of hrefs) {
  29. expect(href).not.toMatch(/^\/[^/]/)
  30. }
  31. })
  32. test("no web manifest link (not applicable in Electron)", async () => {
  33. const content = await html(name)
  34. expect(content).not.toContain('rel="manifest"')
  35. })
  36. })
  37. }
  38. })
  39. /**
  40. * Vite resolves `publicDir` relative to `root`, not the config file.
  41. * This test reads the actual values from electron.vite.config.ts to catch
  42. * regressions where the publicDir path no longer resolves correctly
  43. * after the renderer root is accounted for.
  44. */
  45. describe("electron vite publicDir", () => {
  46. test("configured publicDir resolves to a directory with oc-theme-preload.js", async () => {
  47. const config = await Bun.file(join(root, "electron.vite.config.ts")).text()
  48. const pub = config.match(/publicDir:\s*["']([^"']+)["']/)
  49. const rendererRoot = config.match(/root:\s*["']([^"']+)["']/)
  50. expect(pub).not.toBeNull()
  51. expect(rendererRoot).not.toBeNull()
  52. const resolved = resolve(root, rendererRoot![1], pub![1])
  53. expect(existsSync(resolved)).toBe(true)
  54. expect(existsSync(join(resolved, "oc-theme-preload.js"))).toBe(true)
  55. })
  56. })