/** #292: the self-host settings and config of the Chrome sidecar for `render_page`. */ import { describe, expect, it } from "vitest"; import { BROWSER_FLAGS, browserAdminBindings, browserServices, } from "../src/selfhost/browser-sidecar.ts"; import { secretsEnv } from "../src/selfhost/secrets.ts"; import { readSettings } from "../src/selfhost/settings.ts"; const ENV: Record = { ...Object.fromEntries( secretsEnv() .split("\n") .filter((l) => l && !l.startsWith("#")) .map((l) => [l.slice(0, l.indexOf("=")), l.slice(l.indexOf("=") + 1)]), ), CLOUDHAL_S3_ENDPOINT: "http://seaweedfs:8333", CLOUDHAL_S3_BUCKET: "cloudhal", }; describe("the Chrome sidecar settings", () => { it("is off without CLOUDHAL_BROWSER_URL", () => { const s = readSettings(ENV); expect(s.browser).toBeUndefined(); expect(browserAdminBindings(s.browser)).toEqual([]); expect(browserServices(s.browser)).toEqual([]); }); it("binds the admin Worker to the sidecar and opens the site at one origin for every instance", () => { const s = readSettings({ ...ENV, CLOUDHAL_BROWSER_URL: "http://chrome:9222" }); expect(s.browser).toEqual({ address: "chrome:9222", renderOrigin: "http://cloudhal:8788" }); expect(browserServices(s.browser)).toEqual([ '(name = "browser", external = (address = "chrome:9222", http = ()))', ]); expect(browserAdminBindings(s.browser)).toEqual([ '(name = "BROWSER_CDP", service = "browser")', '(name = "CLOUDHAL_RENDER_ORIGIN", text = "http://cloudhal:8788")', ]); expect(BROWSER_FLAGS).toEqual(["increase_websocket_message_size"]); const k8s = readSettings({ ...ENV, CLOUDHAL_BROWSER_URL: "http://ch-cloudhal-chrome", CLOUDHAL_RENDER_ORIGIN: "http://ch-cloudhal:8788/", CLOUDHAL_SITE_PORT: "9000", }); expect(k8s.browser).toEqual({ address: "ch-cloudhal-chrome:9222", renderOrigin: "http://ch-cloudhal:8788", }); }); it("refuses anything but a DevTools http:// address and an http(s) origin", () => { expect(() => readSettings({ ...ENV, CLOUDHAL_BROWSER_URL: "ws://chrome:9222" })).toThrow( /DevTools port over http/, ); expect(() => readSettings({ ...ENV, CLOUDHAL_BROWSER_URL: "http://chrome:9222/json/version" }), ).toThrow(/a scheme, a host and a port only/); expect(() => readSettings({ ...ENV, CLOUDHAL_BROWSER_URL: "http://chrome:9222", CLOUDHAL_RENDER_ORIGIN: "file://x", }), ).toThrow(/CLOUDHAL_RENDER_ORIGIN/); }); });