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

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

import {
  composerPasteFiles,
  createTempReference,
  extractTempReferenceText,
  namePastedTempReferenceFile,
  tempReferenceAssetIds,
  unreadReferenceImageAssets,
  validateTempReferenceFile
} from "../extension/temp-references.js";
import { createComposerSession } from "../extension/composer.js";

const supportedFiles = [
  ["frame.png", "image/png"],
  ["frame.jpg", "image/jpeg"],
  ["frame.webp", "image/webp"],
  ["motion.gif", "image/gif"],
  ["treatment.pdf", "application/pdf"],
  ["notes.txt", "text/plain"],
  ["method.md", "text/markdown"],
  ["reference.html", "text/html"]
];

test("ordinary text paste remains the browser's default behavior", () => {
  const clipboard = {
    files: [],
    items: [{ kind: "string", type: "text/plain", getAsFile: () => null }]
  };

  assert.deepEqual(composerPasteFiles(clipboard), []);
});

test("a local MP4 can be attached as a temporary Composer video reference", () => {
  const file = new File([new Uint8Array([0, 1, 2, 3])], "reference.mp4", { type: "video/mp4" });
  const reference = createTempReference({
    file,
    assetId: "video-asset",
    referenceId: "temp-reference:video",
    alias: "@参考1"
  });

  assert.equal(validateTempReferenceFile(file).kind, "video");
  assert.equal(reference.referenceKind, "video_sources");
  assert.deepEqual(reference.assetRefs, [{ assetId: "video-asset", kind: "video", mimeType: "video/mp4", name: "reference.mp4", byteSize: 4 }]);
  assert.deepEqual(tempReferenceAssetIds(reference), ["video-asset"]);
});

test("an unnamed clipboard image receives a format-derived name before validation", () => {
  const pasted = namePastedTempReferenceFile(new File(["image"], "", { type: "image/png" }), "paste-one");
  assert.equal(pasted.name, "paste-one.png");
  assert.equal(validateTempReferenceFile(pasted).mimeType, "image/png");
});

test("the composer accepts its documented image and document formats", () => {
  for (const [name, type] of supportedFiles) {
    const file = new File(["content"], name, { type });
    assert.deepEqual(validateTempReferenceFile(file), {
      extension: name.split(".").at(-1),
      kind: type.startsWith("image/") ? "image" : "document",
      mimeType: type
    });
  }
});

test("files disguised with a mismatched extension are rejected", () => {
  assert.throws(
    () => validateTempReferenceFile(new File(["image"], "frame.png", { type: "text/html" })),
    /扩展名和文件格式不一致/
  );
});

test("temporary reference metadata survives composer session normalization", () => {
  const file = new File(["visual"], "lighting.webp", { type: "image/webp" });
  const reference = createTempReference({
    file,
    assetId: "temp-reference-asset:one",
    referenceId: "temp-reference:one",
    alias: "@参考1"
  });
  const [saved] = createComposerSession({ referenceSnapshots: [reference] }).referenceSnapshots;

  assert.equal(saved.sourceType, "temporary");
  assert.equal(saved.entryId, "temp-reference:one");
  assert.deepEqual(saved.assetRefs, [{
    assetId: "temp-reference-asset:one",
    kind: "image",
    mimeType: "image/webp",
    name: "lighting.webp",
    byteSize: 6
  }]);
  assert.deepEqual(saved.imageRefs, [{ visualId: "temp-reference-asset:one", mimeType: "image/webp" }]);
});

test("temporary reference backup paths keep safe relative paths and discard unsafe paths", () => {
  const base = {
    entryId: "temp-reference:backup",
    alias: "@参考1",
    sourceType: "temporary",
    assetRefs: [{ assetId: "temp-reference-asset:backup", kind: "document", mimeType: "text/plain", name: "notes.txt", byteSize: 5 }]
  };
  const [safe] = createComposerSession({
    referenceSnapshots: [{ ...base, assetRefs: [{ ...base.assetRefs[0], archivePath: "temporary/session/notes.txt" }] }]
  }).referenceSnapshots;
  const [unsafe] = createComposerSession({
    referenceSnapshots: [{ ...base, assetRefs: [{ ...base.assetRefs[0], archivePath: "../private/notes.txt" }] }]
  }).referenceSnapshots;

  assert.equal(safe.assetRefs[0].archivePath, "temporary/session/notes.txt");
  assert.equal(Object.hasOwn(unsafe.assetRefs[0], "archivePath"), false);
});


test("a partial temporary-image analysis remains eligible for completion", () => {
  const base = {
    referenceText: "已保存的可见描述",
    assetRefs: [{ assetId: "temp-reference-asset:partial", kind: "image", mimeType: "image/png" }],
    imageRefs: [{ visualId: "temp-reference-asset:partial", mimeType: "image/png" }]
  };
  const partial = {
    ...base,
    assets: [{
      assetId: "temp-reference-asset:partial",
      imageFingerprint: "image-hash",
      analysisImageFingerprint: "image-hash",
      analysisVersion: 2,
      analysisFingerprint: "profile-hash",
      reconstructionPrompt: ""
    }]
  };
  assert.deepEqual(unreadReferenceImageAssets(partial).map((item) => item.assetId), ["temp-reference-asset:partial"]);
  assert.deepEqual(unreadReferenceImageAssets({
    ...partial,
    assets: [{ ...partial.assets[0], reconstructionPrompt: "可独立复用的重建提示词" }]
  }), []);
});

test("a fingerprint-bound legacy description is usable without requesting another image analysis", () => {
  const reference = {
    referenceKind: "vision",
    referenceText: "被青绿色薄雾包围的古代庭院。",
    imageRefs: [{ visualId: "legacy-image", mimeType: "image/webp" }],
    assets: [{
      assetId: "legacy-image",
      imageFingerprint: "legacy-hash",
      analysisImageFingerprint: "legacy-hash",
      analysisVersion: 1,
      reconstructionPrompt: ""
    }]
  };
  assert.deepEqual(unreadReferenceImageAssets(reference), []);
  assert.deepEqual(unreadReferenceImageAssets({
    ...reference,
    assets: [{ ...reference.assets[0], analysisImageFingerprint: "other-image" }]
  }).map((item) => item.assetId), ["legacy-image"]);
});

test("text, Markdown, and HTML attachments expose readable text without script content", async () => {
  const parser = (html) => ({
    body: {
      textContent: html
        .replace(/<script\b[^>]*>[\s\S]*?<\/script>/giu, " ")
        .replace(/<style\b[^>]*>[\s\S]*?<\/style>/giu, " ")
        .replace(/<[^>]+>/gu, " ")
    }
  });
  assert.equal(await extractTempReferenceText(new File(["line one\nline two"], "notes.txt", { type: "text/plain" })), "line one\nline two");
  assert.equal(await extractTempReferenceText(new File(["# Method\nKeep contrast"], "method.md", { type: "text/markdown" })), "# Method\nKeep contrast");
  assert.equal(
    await extractTempReferenceText(new File(["<main>Visible</main><script>secret()</script>"], "brief.html", { type: "text/html" }), { parseHtml: parser }),
    "Visible"
  );
});

test("PDF attachments use the supplied PDF text extractor", async () => {
  const file = new File(["pdf"], "treatment.pdf", { type: "application/pdf" });
  const text = await extractTempReferenceText(file, { extractPdfText: async () => "Page one direction" });
  assert.equal(text, "Page one direction");
});