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. 提示词导演

10.8 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { load } from "cheerio";
import { canonicalTextAnalysisInput } from "../extension/analysis-input.js";

const [library, html, css, background, composerHtml, composerCss, i18n] = await Promise.all([
  readFile(new URL("../extension/library.js", import.meta.url), "utf8"),
  readFile(new URL("../extension/library.html", import.meta.url), "utf8"),
  readFile(new URL("../extension/library.css", import.meta.url), "utf8"),
  readFile(new URL("../extension/background.js", import.meta.url), "utf8"),
  readFile(new URL("../extension/composer.html", import.meta.url), "utf8"),
  readFile(new URL("../extension/composer-page.css", import.meta.url), "utf8"),
  readFile(new URL("../extension/i18n.js", import.meta.url), "utf8")
]);

test("project visibility uses the clicked control and updates the wall without a redundant reload", () => {
  const projectFilters = library.slice(
    library.indexOf("function renderProjectFilters"),
    library.indexOf("async function createProjectCollection")
  );
  assert.match(projectFilters, /updateProjectVisibility\(collection, visibility\)/);
  assert.match(projectFilters, /perform\(button,\s*\{[\s\S]*SET_COLLECTION_VISIBILITY[\s\S]*\}, false\)/);
  assert.doesNotMatch(projectFilters, /perform\(null/);
});

test("structured filter clicks reuse the existing navigation instead of rebuilding it", () => {
  const structuredRender = library.slice(
    library.indexOf("function renderStructuredFilterResults"),
    library.indexOf("function updateLibrarySummary")
  );
  const filterRenderers = library.slice(
    library.indexOf("function renderContentFilters"),
    library.indexOf("function clearFilters")
  );
  assert.match(structuredRender, /renderGalleryResults\(\{ refreshNavigation: false \}\)/);
  assert.match(structuredRender, /syncStructuredFilterControls\(\)/);
  assert.match(filterRenderers, /renderStructuredFilterResults\(\)/);
  assert.doesNotMatch(filterRenderers, /addEventListener\("click",[^\n]+renderGallery\(\)/);
});

test("developer analysis import remains supported without appearing in the normal library UI", () => {
  assert.doesNotMatch(html, /id="import-candidates"|id="candidate-file"|高级工具|导入整库分析 JSON/);
  assert.match(background, /IMPORT_ANALYSIS_CANDIDATES/);
});

test("detail panels and selected projects use the themed surface and product accent", () => {
  assert.match(css, /\.detail-primary > \.detail-body\s*\{[^}]*min-height:\s*100dvh[^}]*align-self:\s*stretch/);
  assert.match(css, /\.project-filter\[aria-pressed="true"\]\s*\{[^}]*color:\s*var\(--accent-ink\)[^}]*background:\s*var\(--ui-project-active-surface\)[^}]*var\(--accent-emphasis\)/);
});

test("library refresh waits for detail rebuilding before returning control", () => {
  const refreshLibrary = library.slice(
    library.indexOf("async function refreshLibrary"),
    library.indexOf("async function loadImageDerivedMetadata")
  );
  assert.match(refreshLibrary, /await renderOpenLibraryPanels\(\{\s*detailChanged: !progressive && !preserveCards\s*\}\)/);
  const panels = library.slice(library.indexOf("async function renderOpenLibraryPanels"), library.indexOf("async function applyLibraryEntriesChange"));
  assert.match(panels, /if \(detailChanged && currentDetailId[\s\S]*await renderDetail\(\)/);
});

test("vision batch dialog only revives running or paused jobs and otherwise shows the fresh preview", () => {
  const previewSelectedVisionBatch = library.slice(
    library.indexOf("async function previewSelectedVisionBatch"),
    library.indexOf("function renderVisionBatchDialog")
  );
  const renderVisionBatchDialog = library.slice(
    library.indexOf("function renderVisionBatchDialog"),
    library.indexOf("async function startSelectedVisionBatch")
  );
  assert.match(previewSelectedVisionBatch, /visionBatchJob && \["running", "paused"\]\.includes\(visionBatchJob\.status\)/);
  assert.match(renderVisionBatchDialog, /const activeJob = visionBatchJob && \["running", "paused"\]\.includes\(visionBatchJob\.status\) \? visionBatchJob : null/);
  assert.match(renderVisionBatchDialog, /const source = activeJob \?\? preview \?\? visionBatchJob \?\? \{\}/);
  assert.match(renderVisionBatchDialog, /elements\.visionBatchStart\.hidden = Boolean\(job\)/);
  assert.match(renderVisionBatchDialog, /elements\.visionBatchPause\.hidden = !activeJob \|\| activeJob\.status !== "running"/);
  assert.match(renderVisionBatchDialog, /elements\.visionBatchResume\.hidden = !activeJob \|\| activeJob\.status !== "paused"/);
  assert.match(renderVisionBatchDialog, /elements\.visionBatchCancel\.hidden = !active/);
  assert.match(renderVisionBatchDialog, /elements\.visionBatchProgress\.hidden = !job/);
  assert.match(renderVisionBatchDialog, /setTaskProgress\(elements\.visionBatchProgressBar, \{ completed: processedCount, total: requestCount \|\| job\?\.requestCount/);
  assert.match(renderVisionBatchDialog, /Math\.min\([\s\S]*requestCount,[\s\S]*counts\.succeeded[\s\S]*counts\.failed/);
  assert.doesNotMatch(previewSelectedVisionBatch, /type: "CANCEL_VISION_BATCH"/, "opening a running task must not cancel it after a service setting changes");
  assert.match(previewSelectedVisionBatch, /revision !== batchDialog\.revision/, "a previous scope's reply must not overwrite the latest preview");
  assert.match(library, /visionBatchJob = response\.visionBatchJob;\s*mediaBatchPreview = null;/, "the completed task must not fall back to its old paid preview");
});

test("image-analysis UI keeps one prompt surface instead of duplicating the reconstruction prompt", () => {
  const createPromptSection = library.slice(
    library.indexOf("function createPromptSection"),
    library.indexOf("async function analyzeEntryVisualSet")
  );
  const createEntryEditor = library.slice(
    library.indexOf("function createEntryEditor"),
    library.indexOf("function removeReferenceCard")
  );
  assert.match(html, /id="vision-batch-reanalyze"[\s\S]*重新分析已有反推提示词/);
  assert.doesNotMatch(html, /id="vision-batch-reanalyze"[\s\S]*重新分析已有画面描述/);
  assert.match(html, /id="vision-batch-progress"[\s\S]*id="vision-batch-progress-bar"/);
  assert.doesNotMatch(library, /function createVisionDescription|vision-description|撤回本次分析/);
  assert.match(createPromptSection, /createMediaPromptSection/);
  assert.match(createPromptSection, /createPromptCopyAction/);
  assert.doesNotMatch(createEntryEditor, /UPDATE_VISION_RECONSTRUCTION_PROMPT|vision-edit-field/);
  assert.doesNotMatch(createEntryEditor, /反推提示词/);
  assert.doesNotMatch(createEntryEditor, /画面描述/);
  assert.match(i18n, /"重新分析已有反推提示词": "Reanalyze existing reconstruction prompts"/);
  assert.match(i18n, /"反推提示词": "Reconstruction prompt"/);
});

test("analysis progress reports failures without exposing incomplete-result wording", () => {
  const renderAnalysisBatch = library.slice(
    library.indexOf("function renderAnalysisBatch"),
    library.indexOf("function analysisFailureSummary")
  );
  assert.doesNotMatch(renderAnalysisBatch, /重试失败\/待补全|部分完成/);
  assert.match(renderAnalysisBatch, /失败 \{failed\}/);
  assert.doesNotMatch(renderAnalysisBatch, /\$\{job\.counts\.partial \|\| 0\} partial/);
});

test("detail text analysis follows the currently displayed image prompt", () => {
  const media = library.slice(library.indexOf("function createMediaPromptSection"), library.indexOf("function createTextAnalysisAction"));
  const action = library.slice(library.indexOf("function createTextAnalysisAction"), library.indexOf("function createCodeSourceSection"));
  assert.match(media, /createTextAnalysisAction\(entry, asset\.id\)/);
  const entry = { id: 'case', text: 'Shared case context', mediaAssets: [{ id: 'first', kind: 'image' }, { id: 'second', kind: 'image' }],
    mediaPrompts: [{ assetId: 'first', text: 'First image original', source: 'manual' }, { assetId: 'second', text: 'Second image original', source: 'manual' }] };
  const before = structuredClone(entry), calls = [];
  const createAction = new Function('canonicalTextAnalysisInput', 'promptIconButton', 't', 'analyzeSingleEntry',
    action + '; return createTextAnalysisAction;')(canonicalTextAnalysisInput,
    () => ({ addEventListener(event, callback) { assert.equal(event, 'click'); this.click = callback; } }), value => value,
    (actualEntry, button, input) => calls.push({ actualEntry, button, input }));
  for (const asset of entry.mediaAssets) {
    const button = createAction(entry, asset.id); button.click();
    const call = calls.at(-1);
    assert.equal(call.actualEntry, entry); assert.equal(call.button, button);
    assert.equal(call.input.assetId, asset.id);
    assert.equal(call.input.text, entry.mediaPrompts.find(prompt => prompt.assetId === asset.id).text);
  }
  assert.equal(createAction({ text: '', mediaAssets: [] }), null, 'empty text must not become an analysis request');
  assert.deepEqual(entry, before, 'analysis selection never edits the originals');
  const analyzeSingleEntry = library.slice(library.indexOf("async function analyzeSingleEntry"), library.indexOf("async function privateAiSettings"));
  assert.match(analyzeSingleEntry, /fingerprint = await textFingerprint\(analysisInput\.text\)/);
  assert.match(analyzeSingleEntry, /assetId: analysisInput\.assetId/);
  assert.doesNotMatch(analyzeSingleEntry, /textFingerprint\(entry\.text\)/);
});

test("composer groups direction and reference inputs in their menus and secondary choices in settings", () => {
  assert.match(composerHtml, /class="composer-options-panel"/);
  assert.match(composerHtml, /id="composer-applied-skills"[^>]*hidden/);
  assert.match(composerHtml, /id="composer-attachment-local"/);
  assert.match(composerHtml, /id="composer-reference-tab-skills"/);
  assert.doesNotMatch(composerHtml, /id="composer-skill-manage"|id="composer-attachment-menu"/);
  const $ = load(composerHtml);
  assert.equal($('#composer-direction .composer-type-switch').length, 1);
  assert.equal($('#composer-reference-open').closest('.composer-options-panel').length, 0);
  for (const id of ['composer-send-images', 'composer-send-videos']) {
    assert.equal($(`#composer-reference-inputs #${id}`).length, 1);
  }
  for (const id of ['composer-production-review', 'composer-thinking', 'composer-create-image']) {
    assert.equal($(`#${id}`).closest('details').attr('id'), 'composer-options');
  }
  for (const id of ['composer-route', 'composer-platform', 'composer-output-language']) {
    assert.equal($(`#${id}`).length, 0, 'Task, target and language are inferred for new UI conversations');
  }
  assert.equal($('.composer-option-toggles > .composer-option-toggle').length, 3);
  assert.equal($('#composer-create-image').attr('aria-describedby'), 'composer-create-image-note');
  assert.match(composerCss, /\.composer-input-tools\s*\{[^}]*flex-wrap:\s*nowrap/);
});