Skip to content

Spot it. Save it. Free, local-first Chrome extension for images, videos and prompts. Explore 2,000+ curated cases and connect your library to AI Agents via MCP. 提示词导演

3.6 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";

const bootstrap = await readFile(new URL("../extension/theme-bootstrap.js", import.meta.url), "utf8");
const uiRuntime = await readFile(new URL("../extension/i18n.js", import.meta.url), "utf8");

const pages = [
  "library.html",
  "curated.html",
  "curated-skills.html",
  "composer.html",
  "skills.html",
  "collector.html"
];

test("theme bootstrap resolves a real first-paint background and color scheme before CSS", () => {
  assert.match(bootstrap, /matchMedia\("\(prefers-color-scheme: dark\)"\)/);
  assert.match(bootstrap, /dataset\.theme/);
  assert.match(bootstrap, /dataset\.resolvedTheme/);
  assert.match(bootstrap, /style\.backgroundColor\s*=/);
  assert.match(bootstrap, /style\.colorScheme\s*=/);
  assert.match(bootstrap, /document\.addEventListener\("DOMContentLoaded"/);
  assert.match(bootstrap, /root\.dataset\.theme !== theme/);
  assert.match(bootstrap, /: "dark"/);
  assert.match(bootstrap, /"#0f1113"/);
  assert.match(bootstrap, /"#e5e5e5"/);
  assert.match(bootstrap, /storedMotion === "none"[\s\S]*?"reduced"/);
});

test("ui initialization replaces provisional first-paint colors with live semantic theme roles", () => {
  assert.match(uiRuntime, /document\.documentElement\.style\.backgroundColor = "var\(--ui-browser\)"/);
  assert.match(uiRuntime, /document\.body\.style\.backgroundColor = "var\(--ui-page\)"/);
  assert.match(uiRuntime, /document\.body\.style\.color = "var\(--ui-text\)"/);
  assert.match(uiRuntime, /dataset\.resolvedTheme = preferences\.theme/);
});

test("every first-party shell loads bootstrap, shared foundation, then page styles", async () => {
  for (const page of pages) {
    const html = await readFile(new URL(`../extension/${page}`, import.meta.url), "utf8");
    const bootstrapIndex = html.indexOf('<script src="theme-bootstrap.js"></script>');
    const foundationIndex = html.indexOf('<link rel="stylesheet" href="ui-foundation.css" />');
    const stylesheetIndex = html.indexOf('<link rel="stylesheet"', foundationIndex + 1);
    assert.ok(bootstrapIndex >= 0, `${page} 缺少 theme bootstrap`);
    assert.ok(foundationIndex > bootstrapIndex, `${page} 应在 bootstrap 后加载共享视觉基础`);
    assert.ok(stylesheetIndex > foundationIndex, `${page} 应在共享视觉基础后加载页面样式`);
  }
});

test("pages opened after English use stay hidden until translated, so Chinese markup never flashes", async () => {
  const { runInNewContext } = await import("node:vm");
  const run = locale => {
    const appended = [];
    const timers = [];
    const document = {
      documentElement: { dataset: {}, style: {} },
      head: { append: node => appended.push(node) },
      createElement: () => ({ remove() { this.removed = true; } }),
      addEventListener() {}
    };
    const localStorage = { getItem: key => (key === "promptDirectorLocale" ? locale : null), setItem() {} };
    runInNewContext(bootstrap, { document, localStorage, setTimeout: (callback, delay) => timers.push({ callback, delay }) });
    return { appended, timers };
  };
  const english = run("en");
  assert.equal(english.appended.length, 1);
  assert.match(english.appended[0].textContent, /visibility: hidden/);
  english.timers[0].callback();
  assert.equal(english.appended[0].removed, true, "a page whose script fails must still become visible");
  assert.equal(run("zh-CN").appended.length, 0);
  assert.equal(run(null).appended.length, 0);
  assert.match(uiRuntime, /setItem\("promptDirectorLocale", activeLocale\)[\s\S]*?applyDocumentTranslations\(document\);\s*document\.getElementById\("promptdirector-locale-pending"\)\?\.remove\(\)/);
});