web-accessible-resources.test.ts

2.7 KB · raw

1import { existsSync, readFileSync } from "node:fs";2import { resolve } from "node:path";3import { describe, expect, it } from "vitest";45type WebAccessibleResource = {6  matches: string[];7  resources: string[];8  use_dynamic_url?: boolean;9};1011type ExtensionManifest = {12  web_accessible_resources?: WebAccessibleResource[];13};1415// dist-chrome/ and dist-firefox/ are kohignored build outputs, so a clean16// checkout has no manifest to read. scripts/ci.ts builds both targets before17// the test step; with no artifacts present these suites skip instead of18// throwing ENOENT and passing/failing on nothing.19function manifestPath(target: "chrome" | "firefox"): string {20  return resolve(process.cwd(), `dist-${target}`, "manifest.json");21}2223function readManifest(target: "chrome" | "firefox"): ExtensionManifest {24  return JSON.parse(25    readFileSync(manifestPath(target), "utf8"),26  ) as ExtensionManifest;27}2829const HASHED_ASSET = /^assets\/[^/]+-[A-Za-z0-9_-]+\.(?:js|css)$/;3031describe.skipIf(!existsSync(manifestPath("chrome")))(32  "shipped Chrome web-accessible resources",33  () => {34    it("scopes page-reachable resources to content-hashed asset chunks", () => {35      const resources = readManifest("chrome").web_accessible_resources ?? [];3637      expect(resources).toHaveLength(1);38      expect(resources[0]?.matches).toEqual(["<all_urls>"]);39      expect(resources[0]?.resources.length).toBeGreaterThan(0);40      expect(41        resources[0]?.resources.every((file) => HASHED_ASSET.test(file)),42      ).toBe(true);43    });4445    // PB-26-314 is OPEN, not accepted. use_dynamic_url was enabled once and46    // reverted at v5.0.100 (save e1c5043e4311) because Chrome then denied the47    // dependent content-script chunks from page contexts, breaking the form48    // icons. This stays an expected failure so that enabling the flag turns49    // the suite red instead of silently shipping the finding.50    it.fails(51      "PB-26-314 (OPEN): Chrome serves web-accessible resources from a per-install dynamic URL",52      () => {53        const resources =54          readManifest("chrome").web_accessible_resources ?? [];5556        expect(resources[0]?.use_dynamic_url).toBe(true);57      },58    );59  },60);6162describe.skipIf(!existsSync(manifestPath("firefox")))(63  "shipped Firefox web-accessible resources",64  () => {65    it("keeps Firefox resources limited to content-script chunks with content hashes", () => {66      const resources = readManifest("firefox").web_accessible_resources ?? [];67      const files = resources.flatMap((entry) => entry.resources);6869      expect(files.length).toBeGreaterThan(0);70      expect(files.every((file) => HASHED_ASSET.test(file))).toBe(true);71      expect(files.every((file) => !file.includes("<dynamic_resource>"))).toBe(72        true,73      );74    });75  },76);