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

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

const source = await readFile(new URL("../extension/library.js", import.meta.url), "utf8");
const css = await readFile(new URL("../extension/organization.css", import.meta.url), "utf8");
const foundation = await readFile(new URL("../extension/ui-foundation.css", import.meta.url), "utf8");
const tagEditor = await readFile(new URL("../extension/tag-editor.js", import.meta.url), "utf8");

const quickOrganizer = source.slice(
  source.indexOf("function createDetailQuickOrganization"),
  source.indexOf("function createDetailDeleteAction")
);
const footerActions = source.slice(
  source.indexOf("function createDetailDeleteAction"),
  source.indexOf("function createComposerAction")
);
const editor = source.slice(
  source.indexOf("function createEntryEditor"),
  source.indexOf("function renderManager")
);
const attributes = source.slice(
  source.indexOf("function createDetailAttributes"),
  source.indexOf("function createDetailMetadata")
);

test("detail organization follows prompt and AI tags without an extra section title", () => {
  const detail = source.slice(source.indexOf("async function renderDetail"), source.indexOf("function createLocalDiscovery"));
  assert.ok(detail.indexOf("createDetailAttributes(entry)") < detail.indexOf("createDetailQuickOrganization(entry)"));
  assert.doesNotMatch(quickOrganizer, /"快捷整理"/);
  assert.match(quickOrganizer, /"项目"/);
  assert.match(quickOrganizer, /compact: true/);
  assert.doesNotMatch(quickOrganizer, /detail-organization-heading/);
  assert.doesNotMatch(quickOrganizer, /可选|任意输入|不用预先创建|还没有/);
  assert.match(quickOrganizer, /entry\.compoundCase\?\.customLabels \?\? entry\.customLabels/);
});

test("detail projects use a compact multi-project dropdown and can be created in place", () => {
  assert.match(quickOrganizer, /detail-project-menu/);
  assert.match(quickOrganizer, /"选择项目"/);
  assert.match(quickOrganizer, /t\("已加入 \{count\} 个项目", \{ count: selectedProjects\.length \}\)/);
  assert.match(quickOrganizer, /createDetailProjectSelector/);
  assert.match(quickOrganizer, /type: "REPLACE_COLLECTION_ENTRIES"/);
  assert.match(quickOrganizer, /type: "CREATE_COLLECTION"/);
  assert.match(quickOrganizer, /"新建并加入"/);
  assert.match(quickOrganizer, /expandLogicalCaseIds\(\[entry\.id\], compoundCases\)/);
});

test("detail uses the shared removable multi-tag editor", () => {
  assert.match(quickOrganizer, /createTagEditor/);
  assert.match(tagEditor, /\["Enter", ",", ","\]\.includes\(event\.key\)/);
  assert.match(tagEditor, /splitTagInput/);
  assert.match(tagEditor, /删除标签/);
  assert.match(source, /type: "UPDATE_ENTRY_CUSTOM_LABELS"/);
  assert.match(source, /type: "UPDATE_COMPOUND_CASE"/);
  assert.match(foundation, /\.tag-editor-chip\s*\{/);
});

test("case deletion is a visible one-step recycle-bin action and no longer lives in the editor", () => {
  const detail = source.slice(source.indexOf("async function renderDetail"), source.indexOf("function createLocalDiscovery"));
  const metadata = source.slice(source.indexOf("function createDetailMetadata"), source.indexOf("function createFullAnalysis"));
  assert.match(quickOrganizer, /createDetailDeleteAction\(entry\)/);
  assert.match(quickOrganizer, /createDetailSourceAction\(entry\)/);
  assert.match(footerActions, /"移入回收站"/);
  assert.match(footerActions, /createUiIcon\("trash-2"\)/);
  assert.match(footerActions, /案例及其媒体会移入回收站,可随时恢复/);
  assert.match(footerActions, /deleteCaseIncrementally\(deleteButton, entry\.id\)/);
  assert.doesNotMatch(detail, /createDetailFooterActions|includeDelete/);
  assert.doesNotMatch(metadata, /createDetailDeleteAction/);
  assert.doesNotMatch(editor, /DELETE_ENTRY|deleteCaseIncrementally|删除这个案例/);
});

test("original prompts have one detail surface; case editing contains no duplicated original editor", () => {
  const prompt = source.slice(source.indexOf("function createMediaPromptSection"), source.indexOf("async function analyzeEntryVisualSet"));
  assert.doesNotMatch(editor, /entry-original|createPromptPanel|提示词范围/);
  assert.match(prompt, /sharedOriginalPrompt\(entry\)/);
  assert.match(prompt, /'原始提示词'/);
  assert.match(prompt, /'当前媒体'/);
  assert.match(prompt, /setAttribute\('role', 'tablist'\)/);
  assert.match(prompt, /type: 'UPDATE_ENTRY_TEXT'/);
  assert.match(prompt, /type: 'UPDATE_ENTRY_MEDIA_PROMPT'/);
  assert.doesNotMatch(prompt, /prompt-more|replaceWith\(panel\)/);

});

test("entry title editing keeps one visible heading and an inline save action", () => {
  assert.match(editor, /textEl\("h4", "", "案例标题"\)/);
  assert.match(editor, /textEl\("span", "sr-only", "案例标题"\)/);
  assert.match(editor, /el\("div", "entry-edit-row"\)/);
  assert.match(editor, /const saveTitle = textEl\("button", "button-secondary", "保存"\)/);
  assert.doesNotMatch(editor, /保存标题/);
});

test("checkboxes are not stretched by the global text-input rule", () => {
  assert.match(foundation, /input:not\(\[type="checkbox"\]\):not\(\[type="radio"\]\):not\(\[type="file"\]\), select, textarea/);
  assert.match(css, /\.detail-project-option input\s*\{[^}]*width:\s*16px/);
});

test("AI tags show only leaf labels while preserving their full path for context", () => {
  assert.match(attributes, /"AI 标签"/);
  assert.match(attributes, /label: item\.name, path: item\.path/);
  assert.match(source, /function detailTag\(\{ facet, label, path = label \}\)[\s\S]*setAttribute\("aria-label", fullPath\)/);
  assert.doesNotMatch(attributes, /entry\.customLabels|customFacet|自定义标签/);
  assert.match(editor, /"创作标签"/);
  assert.match(editor, /"添加标签"/);
});

test("detail project checkbox changes only this case and keeps refreshes caused by others", () => {
  // A whole-list replace from a stale local organizer would drop a case an
  // agent or another tab added to the same project meanwhile.
  assert.match(quickOrganizer, /type: "BATCH_SET_PROJECT", collectionId, entryIds, mode: checked \? "move" : "remove"/);
  assert.doesNotMatch(quickOrganizer, /nextIds/);
  assert.match(quickOrganizer, /const mark = externalLibraryRefreshMark\(\);[\s\S]*consumePendingExternalLibraryRefresh\(mark\)/);
  const consume = source.slice(source.indexOf("function consumePendingExternalLibraryRefresh"), source.indexOf("async function previewDeepSeekAnalysisBatch"));
  assert.match(consume, /if \(mark\.pending \|\| workspaceLibraryRevision - mark\.revision > 1\) return;/);
  assert.doesNotMatch(source, /consumePendingExternalLibraryRefresh\(\)/);
});

test("detail label edits apply only the user's add or remove onto the latest labels", () => {
  const start = source.indexOf("async function saveDetailCustomLabels");
  const save = source.slice(start, source.indexOf("\n}\n", start));
  // While a draft label holds detail refreshes, the editor list can be stale;
  // pressing Enter must not delete a label a batch or agent added meanwhile.
  assert.match(tagEditor, /options\.onChange\?\.\(requested, trigger, previous\)/);
  assert.match(quickOrganizer, /saveDetailCustomLabels\(entry, trigger, values, previous\)/);
  // Only the delta is sent; the background applies it to the latest stored labels (label-edits.test.js).
  assert.match(save, /const addLabels = requested\.filter\(\(label\) => !before\.has\(label\)\)/);
  assert.match(save, /const removeLabels = \[\.\.\.before\]\.filter\(\(label\) => !after\.has\(label\)\)/);
  assert.doesNotMatch(save, /customLabels\s*\n?\s*\}, false\)/, "the page must not send a whole label list built from a stale view");
  assert.match(save, /consumePendingExternalLibraryRefresh\(mark\)/);
});