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

21.8 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";

import {
  addEntryMedia,
  caseCoverAsset,
  setCaseCover,
  addTimeNote,
  currentVideoReconstruction,
  editCurrentVideoReconstruction,
  mediaDescriptions,
  mediaKindFromFile,
  normalizeEntryMedia,
  normalizeMediaAsset,
  posterAssetForVideo,
  replaceCurrentVideoReconstruction,
  removeEntryMedia,
  setPrimaryMedia,
  updateLocalAssetReferenceMetadata
} from "../extension/media.js";
import { normalizeEntryVisuals, primaryVisionDescription, reorderEntryVisuals } from "../extension/visuals.js";

test("new visual analysis remains readable through reconstruction prompts without a legacy description", () => {
  const entry = normalizeEntryMedia({
    id: "case:analysis",
    mediaAssets: [{
      id: "image:analysis",
      kind: "image",
      usage: "content",
      storageMode: "managed",
      visionAnalysis: {
        quality: "complete",
        reconstructionPrompt: "一名披风角色站在雨夜霓虹街道中央,35mm 低机位电影镜头。",
        tags: [{ g: "style.render", t: "电影写实" }]
      }
    }]
  });
  assert.deepEqual(mediaDescriptions(entry), ["一名披风角色站在雨夜霓虹街道中央,35mm 低机位电影镜头。"]);
});

test("legacy partial visual output is not exposed as usable analysis", () => {
  const entry = {
    mediaAssets: [{
      id: "image-partial",
      kind: "image",
      usage: "content",
      visionAnalysis: {
        reconstructionPrompt: "只有半截的旧结果",
        quality: "partial",
        tags: []
      }
    }],
    primaryMediaId: "image-partial"
  };
  assert.deepEqual(mediaDescriptions(entry), []);
});

test("media normalization discards empty slots instead of crashing library startup", () => {
  assert.equal(normalizeMediaAsset(null), null);
  const entry = normalizeEntryMedia({
    id: "case:empty-media-slot",
    mediaAssets: [null, { id: "image:kept", kind: "image", usage: "content", storageMode: "managed" }]
  });
  assert.deepEqual(entry.mediaAssets.map((asset) => asset.id), ["image:kept"]);
});

test("legacy visuals migrate to canonical media assets without serializing legacy fields", () => {
  const entry = normalizeEntryVisuals({
    id: "case:one",
    visuals: [{ id: "image:one", mimeType: "image/webp", screenshotPath: "images/one.webp" }],
    primaryVisualId: "image:one"
  });
  assert.equal(entry.mediaAssets[0].kind, "image");
  assert.equal(entry.mediaAssets[0].assetPath, "images/one.webp");
  assert.equal(entry.primaryMediaId, "image:one");
  assert.equal(entry.visuals[0].id, "image:one");
  assert.equal(Object.hasOwn(JSON.parse(JSON.stringify(entry)), "visuals"), false);
});

test("legacy local screenshots with source URLs never become external references", () => {
  const repaired = normalizeEntryMedia({
    id: "case:web-image",
    mediaAssets: [{
      id: "image:web", kind: "image", storageMode: "reference", mimeType: "image/webp",
      sourceUrl: "https://example.com/original-image"
    }],
    primaryMediaId: "image:web"
  });
  assert.equal(repaired.mediaAssets[0].storageMode, "managed");
  assert.equal(repaired.mediaAssets[0].sourceUrl, "https://example.com/original-image");
});

test("one case can contain local video, image, document and ordered time notes", () => {
  let entry = normalizeEntryMedia({ id: "case:mixed" });
  entry = addEntryMedia(entry, { id: "video:one", kind: "video", storageMode: "managed", mimeType: "video/mp4", durationMs: 12_000 });
  entry = addEntryMedia(entry, { id: "image:one", kind: "image", storageMode: "managed", mimeType: "image/webp" });
  entry = addEntryMedia(entry, { id: "pdf:one", kind: "document", storageMode: "managed", mimeType: "application/pdf" });
  entry = addEntryMedia(entry, { id: "frame:one", kind: "image", storageMode: "managed", mimeType: "image/webp" });
  entry = addTimeNote(entry, { id: "note:later", assetId: "video:one", startMs: 8000, text: "结束状态" });
  entry = addTimeNote(entry, { id: "note:first", assetId: "video:one", frameAssetId: "frame:one", startMs: 1000, endMs: 4000, text: "可见变化" });
  assert.deepEqual(entry.mediaAssets.map((item) => item.kind), ["video", "image", "document", "image"]);
  assert.deepEqual(entry.timeNotes.map((item) => item.id), ["note:first", "note:later"]);
  assert.equal(entry.timeNotes[0].frameAssetId, "frame:one");
  entry = removeEntryMedia(entry, "video:one");
  assert.deepEqual(entry.timeNotes, []);
});

test("removing media also removes its position from the saved article", () => {
  const entry = removeEntryMedia(normalizeEntryMedia({
    id: "case:article",
    mediaAssets: [{ id: "image:article", kind: "image", storageMode: "managed", mimeType: "image/webp" }],
    articleDocument: {
      version: 1,
      blocks: [
        { id: "text", kind: "paragraph", text: "正文", sourceOrder: 0 },
        { id: "image", kind: "image", assetId: "image:article", sourceUrl: "https://example.com/image.webp", sourceOrder: 1 }
      ]
    }
  }), "image:article");

  assert.deepEqual(entry.articleDocument.blocks.map((block) => block.kind), ["paragraph"]);
});

test("video analysis keeps actual provider cost and routing metadata", () => {
  const entry = normalizeEntryMedia({
    id: "case:video-analysis",
    videoAnalyses: [{
      id: "video-analysis:one",
      assetId: "video:one",
      text: "00:01 opening",
      provider: "OpenRouter",
      model: "declared/video-model",
      usage: { totalTokens: 15 },
      cost: 0.012,
      routing: { provider: "declared-provider" }
    }]
  });
  assert.equal(entry.videoAnalyses[0].cost, 0.012);
  assert.deepEqual(entry.videoAnalyses[0].routing, { provider: "declared-provider" });
});

test("structured video reconstruction survives normalization and current selection stays asset-specific", () => {
  const entry = normalizeEntryMedia({
    id: "case:video-reconstruction",
    mediaAssets: [
      { id: "video:one", kind: "video", storageMode: "managed", mimeType: "video/mp4" },
      { id: "video:two", kind: "video", storageMode: "managed", mimeType: "video/mp4" }
    ],
    videoAnalyses: [
      {
        id: "legacy:unlinked", text: "旧版无资产记录", mode: "visual-reconstruction",
        createdAt: "2026-08-30T00:00:00.000Z"
      },
      {
        id: "reconstruction:old", assetId: "video:one", mode: "visual-reconstruction",
        requestId: "attempt:old", contractVersion: "visual-v3",
        reconstructionPrompt: "旧版逆推", tags: [{ g: "style.render", t: "电影写实" }],
        uncertainties: ["转场方式不确定"], includeTags: true, analysisScope: "visual",
        finishReason: "stop", provider: "智谱 GLM", model: "glm-5.3-flash",
        createdAt: "2026-08-30T00:01:00.000Z"
      },
      {
        id: "temporary:newer", assetId: "video:one", mode: "creative-breakdown",
        text: "临时创作拆解", createdAt: "2026-08-30T00:03:00.000Z"
      },
      {
        id: "reconstruction:current", assetId: "video:one", mode: "visual-reconstruction",
        requestId: "attempt:current", contractVersion: "visual-v3-1", batchJobId: "batch:one",
        reconstructionPrompt: "当前逆推", tags: [{ g: "composition.shot", t: "近景" }],
        uncertainties: [], includeTags: true, analysisScope: "visual", finishReason: "stop",
        userEdited: false, provider: "智谱 GLM", model: "glm-5.3-flash",
        createdAt: "2026-08-30T00:02:00.000Z"
      }
    ]
  });

  const current = currentVideoReconstruction(entry, "video:one");
  assert.equal(current.id, "reconstruction:current");
  assert.equal(current.reconstructionPrompt, "当前逆推");
  assert.equal(current.requestId, "attempt:current");
  assert.equal(current.batchJobId, "batch:one");
  assert.deepEqual(current.tags, [{ g: "composition.shot", t: "近景" }]);
  assert.equal(currentVideoReconstruction(entry, "video:two"), null);
  assert.equal(currentVideoReconstruction(entry, ""), null);
});

test("editing current video reconstruction changes only its reusable prompt and keeps request evidence", () => {
  const source = normalizeEntryMedia({
    id: "case:edit-video",
    mediaAssets: [{ id: "video:one", kind: "video", storageMode: "managed", mimeType: "video/mp4" }],
    text: "ORIGINAL_PROMPT_SENTINEL",
    videoAnalyses: [{
      id: "reconstruction:one", assetId: "video:one", mode: "visual-reconstruction",
      requestId: "attempt:one", contractVersion: "visual-v3-1", prompt: "ACTUAL_REQUEST_PROMPT",
      reconstructionPrompt: "模型原结果", tags: [], uncertainties: [], includeTags: false,
      analysisScope: "visual", finishReason: "stop", createdAt: "2026-08-30T00:00:00.000Z"
    }]
  });
  const edited = editCurrentVideoReconstruction(source, "video:one", "人工调整后的逆推提示词");
  const current = currentVideoReconstruction(edited, "video:one");
  assert.equal(current.reconstructionPrompt, "人工调整后的逆推提示词");
  assert.equal(current.prompt, "ACTUAL_REQUEST_PROMPT");
  assert.equal(current.requestId, "attempt:one");
  assert.equal(current.userEdited, true);
  assert.match(current.editedAt, /^\d{4}-\d{2}-\d{2}T/);
  assert.equal(edited.text, "ORIGINAL_PROMPT_SENTINEL");
});

test("a new video reverse-analysis replaces only the current AI result and preserves older compatibility data", () => {
  const source = normalizeEntryMedia({
    id: "case:replace-video",
    mediaAssets: [{ id: "video:one", kind: "video", storageMode: "managed", mimeType: "video/mp4" }],
    text: "ORIGINAL_PROMPT_SENTINEL",
    videoAnalyses: [
      { id: "legacy:review", assetId: "video:one", mode: "ad-review", text: "旧广告评价", createdAt: "2026-08-29T00:00:00.000Z" },
      {
        id: "reconstruction:current", assetId: "video:one", mode: "visual-reconstruction",
        requestId: "attempt:old", contractVersion: "visual-v3-1",
        reconstructionPrompt: "旧逆推", tags: [{ g: "style.render", t: "旧标签" }], uncertainties: [], includeTags: true,
        analysisScope: "visual", finishReason: "stop", createdAt: "2026-08-30T00:00:00.000Z", version: 3
      }
    ]
  });
  const replaced = replaceCurrentVideoReconstruction(source, "video:one", {
    id: "reconstruction:new-id-is-not-used",
    reconstructionPrompt: "新逆推", tags: [{ g: "style.render", t: "写实" }], uncertainties: [],
    requestId: "attempt:new", contractVersion: "visual-v3-1",
    includeTags: true, analysisScope: "visual", finishReason: "stop", createdAt: "2026-09-03T00:00:00.000Z"
  });

  assert.equal(replaced.videoAnalyses.length, 2);
  assert.equal(replaced.videoAnalyses[0].text, "旧广告评价");
  assert.equal(currentVideoReconstruction(replaced, "video:one").id, "reconstruction:current");
  assert.equal(currentVideoReconstruction(replaced, "video:one").version, 4);
  assert.equal(currentVideoReconstruction(replaced, "video:one").reconstructionPrompt, "新逆推");
  assert.equal(replaced.text, "ORIGINAL_PROMPT_SENTINEL");
});

test("external video references keep only supported playback modes", () => {
  const youtube = normalizeMediaAsset({
    id: "video:youtube", kind: "video", storageMode: "reference",
    sourceUrl: "https://www.youtube.com/watch?v=abc", reference: { url: "https://www.youtube.com/watch?v=abc", playbackMode: "embed" }
  });
  assert.deepEqual(youtube.reference, {
    url: "https://www.youtube.com/watch?v=abc", provider: "youtube", playbackMode: "embed", metadataStatus: "partial"
  });
  const ordinary = normalizeMediaAsset({
    id: "video:source", kind: "video", storageMode: "reference",
    reference: { url: "https://example.com/watch", playbackMode: "embed" }
  });
  assert.equal(ordinary.reference.playbackMode, "source");
  assert.equal(ordinary.reference.provider, "generic");
});

test("video posters are managed display assets without becoming case content", () => {
  const poster = normalizeMediaAsset({
    id: "poster:one", kind: "image", storageMode: "managed", mimeType: "image/webp",
    usage: "poster", derivedFromAssetId: "video:one"
  });
  const video = normalizeMediaAsset({
    id: "video:one", kind: "video", storageMode: "managed", mimeType: "video/mp4",
    posterAssetId: "poster:one"
  });
  assert.equal(poster.usage, "poster");
  assert.equal(poster.derivedFromAssetId, "video:one");
  assert.equal(video.usage, "content");
  assert.equal(video.posterAssetId, "poster:one");
});

test("poster assets remain display-only even when relationships are stale or directly removed", () => {
  let entry = normalizeEntryMedia({
    id: "case:video",
    mediaAssets: [
      { id: "video:one", kind: "video", storageMode: "managed", mimeType: "video/mp4", posterAssetId: "poster:missing" },
      { id: "poster:one", kind: "image", usage: "poster", storageMode: "managed", mimeType: "image/webp", derivedFromAssetId: "video:one" }
    ]
  });
  assert.equal(entry.primaryMediaId, "video:one");
  assert.equal(posterAssetForVideo(entry, entry.mediaAssets[0])?.id, "poster:one");
  assert.throws(() => setPrimaryMedia(entry, "poster:one"), /没有找到这个媒体/);
  entry = normalizeEntryMedia({
    ...entry,
    mediaAssets: entry.mediaAssets.map((asset) => asset.id === "video:one"
      ? { ...asset, posterAssetId: "poster:one" }
      : asset)
  });
  entry = removeEntryMedia(entry, "poster:one");
  assert.equal(entry.mediaAssets[0].posterAssetId, undefined);
});

test("the image compatibility view never exposes or removes video posters", () => {
  const entry = normalizeEntryVisuals({
    id: "case", mediaAssets: [
      { id: "image", kind: "image", storageMode: "managed", mimeType: "image/webp" },
      { id: "video", kind: "video", storageMode: "managed", mimeType: "video/mp4", posterAssetId: "poster" },
      { id: "poster", kind: "image", usage: "poster", storageMode: "managed", mimeType: "image/webp", derivedFromAssetId: "video" }
    ], primaryMediaId: "image"
  });
  assert.deepEqual(entry.visuals.map((item) => item.id), ["image"]);
  const reordered = reorderEntryVisuals(entry, ["image"]);
  assert.equal(reordered.mediaAssets.some((item) => item.id === "poster" && item.usage === "poster"), true);
});

test("file media kinds are derived from actual file metadata and supported extensions", () => {
  assert.equal(mediaKindFromFile({ name: "clip.mp4", type: "video/mp4" }), "video");
  assert.equal(mediaKindFromFile({ name: "notes.pdf", type: "" }), "document");
  assert.equal(mediaKindFromFile({ name: "frame.webp", type: "image/webp" }), "image");
  assert.equal(mediaKindFromFile({ name: "motion.gif", type: "image/gif" }), "image");
  assert.equal(mediaKindFromFile({ name: "score.wav", type: "audio/wav" }), "audio");
  assert.equal(mediaKindFromFile({ name: "captions.srt", type: "text/plain" }), "document");
  assert.equal(mediaKindFromFile({ name: "layout.psd", type: "application/octet-stream" }), "attachment");
  assert.equal(mediaKindFromFile({ name: "font.otf", type: "font/otf" }), "attachment");
  assert.equal(normalizeMediaAsset({ id: "gif", kind: "image", mimeType: "image/gif" }).mimeType, "image/gif");
  assert.equal(mediaKindFromFile({ name: "archive.bin", type: "application/octet-stream" }), "");
});

test("audio and inert attachment assets survive canonical normalization", () => {
  const audio = normalizeMediaAsset({
    id: "audio:score",
    kind: "audio",
    storageMode: "managed",
    sourceTitle: "score.wav",
    mimeType: "audio/wav",
    durationMs: 3200
  });
  const attachment = normalizeMediaAsset({
    id: "attachment:scene",
    kind: "attachment",
    storageMode: "reference",
    sourceTitle: "scene.aep",
    relativePath: "project/scene.aep",
    mimeType: "application/x-after-effects"
  });

  assert.equal(audio.kind, "audio");
  assert.equal(audio.formatCategory, "audio");
  assert.equal(attachment.kind, "attachment");
  assert.equal(attachment.storageMode, "reference");
  assert.equal(attachment.sourceFormat, "aep");
  assert.equal(attachment.formatCategory, "motion-project");
  assert.equal(attachment.relativePath, "project/scene.aep");
  assert.equal(normalizeMediaAsset({
    id: "attachment:unsafe",
    kind: "attachment",
    sourceTitle: "payload.exe",
    mimeType: "application/octet-stream"
  }), null);
});

test("unknown local source references require record identity while packaged copies require a generic managed descriptor", () => {
  const reference = {
    id: "attachment:unknown",
    kind: "attachment",
    storageMode: "reference",
    recordType: "local-asset-reference",
    linkStatus: "linked",
    sourceTitle: "custom.zzz",
    relativePath: "sources/custom.zzz",
    sourceFormat: "zzz",
    formatCategory: "local-link",
    mimeType: "application/x-custom",
    byteSize: 20,
    sourceLastModified: 1_776_500_123_000,
    importFailure: {
      code: "unsupported_format",
      message: "暂不支持这种文件格式:custom.zzz",
      forceAllowed: false
    }
  };
  const normalized = normalizeMediaAsset(reference);
  const packaged = normalizeMediaAsset({
    id: "attachment:packaged",
    kind: "attachment",
    storageMode: "managed",
    sourceTitle: "custom.zzz",
    sourceFormat: "zzz",
    formatCategory: "other-source",
    mimeType: "application/octet-stream",
    byteSize: 20,
    assetPath: "attachments/case/custom.zzz"
  });

  assert.equal(normalized.recordType, "local-asset-reference");
  assert.equal(normalized.linkStatus, "linked");
  assert.equal(normalized.sourceLastModified, 1_776_500_123_000);
  assert.equal(normalized.importFailure.code, "unsupported_format");
  assert.equal(packaged.storageMode, "managed");
  assert.equal(packaged.formatCategory, "other-source");
  assert.equal(packaged.sourceFormat, "zzz");
  assert.equal(packaged.recordType, undefined);
  assert.equal(normalizeMediaAsset({ ...packaged, id: "attachment:reference", storageMode: "reference" }), null);
  assert.equal(normalizeMediaAsset({ ...packaged, id: "attachment:wrong-category", formatCategory: "local-link" }), null);
  assert.equal(normalizeMediaAsset({ ...packaged, id: "attachment:unsafe-format", sourceFormat: "../zzz" }), null);
  assert.equal(normalizeMediaAsset({ ...packaged, id: "attachment:specific-mime", mimeType: "application/x-custom" }), null);
  assert.equal(normalizeMediaAsset({
    ...packaged,
    id: "attachment:registered-mismatch",
    sourceTitle: "layout.psd",
    sourceFormat: "psd",
    mimeType: "text/html"
  }), null);
});

test("relinking an inert local source updates only verifiable file metadata", () => {
  const original = normalizeEntryMedia({
    id: "entry:source",
    primaryMediaId: "attachment:unknown",
    mediaAssets: [{
      id: "attachment:unknown",
      kind: "attachment",
      storageMode: "reference",
      recordType: "local-asset-reference",
      linkStatus: "relink-required",
      sourceTitle: "custom.zzz",
      relativePath: "old/custom.zzz",
      sourceFormat: "zzz",
      mimeType: "application/x-custom",
      byteSize: 20,
      importFailure: {
        code: "unsupported_format",
        message: "暂不支持这种文件格式:custom.zzz",
        forceAllowed: false
      }
    }]
  });
  const updated = updateLocalAssetReferenceMetadata(original, "attachment:unknown", {
    sourceTitle: "custom-v2.zzz",
    relativePath: "new/custom-v2.zzz",
    sourceFormat: "zzz",
    mimeType: "application/x-custom",
    byteSize: 32,
    sourceLastModified: 1_776_500_999_000
  });

  assert.equal(updated.asset.linkStatus, "linked");
  assert.equal(updated.asset.sourceTitle, "custom-v2.zzz");
  assert.equal(updated.asset.relativePath, "new/custom-v2.zzz");
  assert.equal(updated.asset.byteSize, 32);
  assert.equal(updated.asset.sourceLastModified, 1_776_500_999_000);
  assert.equal(updated.entry.primaryMediaId, "attachment:unknown");
  assert.throws(() => updateLocalAssetReferenceMetadata(original, "attachment:unknown", {
    sourceTitle: "custom.zzz", relativePath: "../custom.zzz", byteSize: 20, sourceLastModified: 1
  }), /安全相对路径/u);
});


test("saved X embed records keep their identity and source while losing unsupported embed mode", () => {
  const url = "https://x.com/director/status/123";
  const asset = normalizeMediaAsset({ id: "saved:x-video", kind: "video", storageMode: "reference", sourceUrl: url, playbackCapability: "embedded",
    reference: { url, provider: "x", playbackMode: "embed" } });
  assert.equal(asset.id, "saved:x-video");
  assert.equal(asset.sourceUrl, url);
  assert.equal(asset.reference.playbackMode, "source");
  assert.equal(asset.playbackCapability, "external");
});


test("an explicitly cleared image prompt does not revive a legacy description on reuse", () => {
  const entry = normalizeEntryMedia({ mediaAssets: [{ id: "image:cleared", kind: "image", usage: "content",
    visionAnalysis: { reconstructionPrompt: "", description: "旧分析文本", userEdited: true, quality: "complete", tags: [] } }] });
  assert.deepEqual(mediaDescriptions(entry), []);
  assert.equal(primaryVisionDescription(entry), "");
  assert.equal(entry.mediaAssets[0].visionAnalysis.description, "旧分析文本");
});


test("case covers use available images and preserve the video primary and article reading order", () => {
  const entry = normalizeEntryMedia({ id: "cover-case", primaryMediaId: "video", text: "Original text",
    mediaAssets: [{ id: "video", kind: "video", posterAssetId: "poster" }, { id: "poster", kind: "image", usage: "poster", derivedFromAssetId: "video" }, { id: "image", kind: "image" }],
    articleDocument: { version: 1, blocks: [{ id: "p", kind: "paragraph", text: "Original text" }, { id: "img", kind: "image", assetId: "image" }] } });
  assert.equal(caseCoverAsset(entry).id, "poster");
  const chosen = setCaseCover(entry, "image");
  assert.equal(caseCoverAsset(chosen).id, "image");
  assert.equal(chosen.primaryMediaId, "video");
  assert.deepEqual(chosen.articleDocument, entry.articleDocument);
  assert.equal(caseCoverAsset(removeEntryMedia(chosen, "image")).id, "poster");
  assert.equal(removeEntryMedia(chosen, "image").coverVisualId, undefined);
  assert.equal(caseCoverAsset({ mediaAssets: [{ id: "pdf", kind: "document" }, { id: "i", kind: "image" }], primaryMediaId: "pdf" }).id, "i");
  assert.equal(caseCoverAsset({ text: "text only" }), null);
  assert.throws(() => setCaseCover(entry, "video"), /没有找到这张图片/);
});