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 { createCaptureWorkspace as createWorkspace } from "../extension/capture-workspace.js";

function createCaptureWorkspace(options) {
  return createWorkspace({ ...options, storage: options.storage || options.chromeApi.storage.local });
}

test("capture uses its injected library storage even when browser storage is unavailable", async () => {
  const stored = {}, chromeApi = fakeChrome(stored), storage = chromeApi.storage.local;
  Object.defineProperty(chromeApi, 'storage', { get() { throw new Error('browser storage bypass'); } });
  const workspace = createWorkspace({ chromeApi, storage, captureDraftStorageKey: 'captureDraft',
    ensureOffscreenDocument: async () => {}, deleteVisual: async () => {} });
  await workspace.dispatch('add-selection', { fragment: { text: '按活动资料库保存', sourceUrl: 'https://example.com' } });
  assert.equal(stored.captureDraft.fragments[0].text, '按活动资料库保存');
});

test("metadata writes merge into the latest draft instead of restoring a stale snapshot", async () => {
  const stored = {
    captureDraft: {
      id: "draft", title: "新标题",
      fragments: [{ id: "late", text: "Agent 稍后加入的内容", sourceUrl: "https://example.com" }],
      contentTypeId: "old-type"
    }
  };
  const workspace = createCaptureWorkspace({
    chromeApi: fakeChrome(stored), captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined, deleteVisual: async () => undefined
  });
  // A page holding an older/empty draft sends only its metadata patch; newer
  // content written meanwhile must survive.
  const result = await workspace.dispatch("persist-draft-metadata", {
    patch: { contentTypeId: "new-type", contentTypeExplicit: true }
  });
  assert.equal(result.ok, true);
  assert.equal(stored.captureDraft.fragments[0].text, "Agent 稍后加入的内容");
  assert.equal(stored.captureDraft.title, "新标题");
  assert.equal(stored.captureDraft.contentTypeId, "new-type");
  assert.equal(stored.captureDraft.contentTypeExplicit, true);
});

test("capture workspace persists a selected fragment behind one dispatch interface", async () => {
  const stored = {};
  const chromeApi = fakeChrome(stored);
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined,
    deleteVisual: async () => undefined
  });

  const result = await workspace.dispatch("add-selection", {
    fragment: {
      text: "保留这段提示词",
      sourceUrl: "https://example.com/post",
      sourceTitle: "案例"
    }
  });

  assert.equal(result.ok, true);
  assert.equal(result.added, true);
  assert.equal(result.wasEmpty, true);
  assert.equal(result.draft.fragments[0].text, "保留这段提示词");
  assert.equal(stored.captureDraft.fragments[0].sourceUrl, "https://example.com/post");
});

test("subsequent right-click additions report counts without treating the draft as empty", async () => {
  const stored = {};
  const workspace = createCaptureWorkspace({
    chromeApi: fakeChrome(stored), captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined, deleteVisual: async () => undefined
  });
  await workspace.dispatch("add-selection", { fragment: { text: "一", sourceUrl: "https://a.example" } });
  const result = await workspace.dispatch("add-selection", { fragment: { text: "二", sourceUrl: "https://b.example" } });
  assert.equal(result.wasEmpty, false);
  assert.equal(result.textCount, 2);
  assert.equal(result.imageCount, 0);
  assert.equal(result.message, "已加入 · 共 2 段文字 / 0 张图片");
});

test("automatic text capture reads only the active highlight", async () => {
  const stored = {};
  const chromeApi = fakeChrome(stored, {
    executeScript: async () => [{ result: "网页高亮文字" }]
  });
  const workspace = createCaptureWorkspace({
    chromeApi, captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined, deleteVisual: async () => undefined
  });

  const result = await workspace.dispatch("try-active-selection");

  assert.equal(result.added, true);
  assert.equal(result.candidateKind, "selection");
  assert.equal(result.draft.fragments[0].text, "网页高亮文字");
  assert.equal(result.draft.fragments[0].sourceKind, "selection");
  assert.equal(result.draft.fragments[0].sourceUrl, "https://example.com/");
});

test("Jimeng highlight and smart visual capture reuse resolved work identity instead of the tab title", async () => {
  const stored = {};
  const sourceContext = {
    canonicalUrl: "https://jimeng.jianying.com/ai-tool/work-detail/7490123456789012345",
    displayTitle: "AIGC大叔 · 2026-05-30",
    sourceFacts: { provider: "jimeng", itemId: "7490123456789012345", author: "AIGC大叔", model: "即梦 4.7" }
  };
  const chromeApi = fakeChrome(stored, {
    query: async () => [{ id: 7, windowId: 3, url: sourceContext.canonicalUrl, title: "即梦AI - 一站式AI创作平台" }],
    executeScript: async () => [{ result: "网页高亮文字" }]
  });
  const workspace = createCaptureWorkspace({
    chromeApi, captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined, deleteVisual: async () => undefined,
    resolveSourceContext: async () => sourceContext
  });

  const result = await workspace.dispatch("try-active-selection");

  assert.equal(result.draft.title, "AIGC大叔 · 2026-05-30");
  assert.equal(result.draft.fragments[0].sourceTitle, "AIGC大叔 · 2026-05-30");
  assert.equal(result.draft.sourceContexts[0].sourceFacts.author, "AIGC大叔");
});

test("automatic text capture stays empty when there is no highlight", async () => {
  const stored = {};
  const chromeApi = fakeChrome(stored, {
    executeScript: async () => [{ result: "   " }]
  });
  const workspace = createCaptureWorkspace({
    chromeApi, captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined, deleteVisual: async () => undefined
  });

  const result = await workspace.dispatch("try-active-selection");

  assert.equal(result.added, false);
  assert.equal(result.reason, "empty-selection");
  assert.equal(result.draft.fragments.length, 0);
});

test("automatic text capture never reads or overwrites a non-empty draft", async () => {
  const stored = { captureDraft: { fragments: [{ id: "keep", text: "用户正在编辑" }] } };
  let scriptCalls = 0;
  const chromeApi = fakeChrome(stored, {
    executeScript: async () => {
      scriptCalls += 1;
      return [{ result: "网页高亮文字" }];
    }
  });
  const workspace = createCaptureWorkspace({
    chromeApi, captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined, deleteVisual: async () => undefined
  });

  const result = await workspace.dispatch("try-active-selection");

  assert.equal(result.added, false);
  assert.equal(result.reason, "draft-not-empty");
  assert.equal(scriptCalls, 0);
  assert.equal(result.draft.fragments[0].text, "用户正在编辑");
});

test("explicit clipboard extraction can reuse saved text without inventing a web source", async () => {
  const stored = {};
  const chromeApi = fakeChrome(stored, {
    executeScript: async () => [{ result: "" }]
  });
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined,
    deleteVisual: async () => undefined
  });

  const first = await workspace.dispatch("add-clipboard-text", { text: "  第一段剪贴板文字  " });
  assert.equal(first.added, true);
  assert.equal(first.draft.fragments[0].sourceKind, "clipboard");
  assert.equal(first.draft.fragments[0].sourceTitle, "剪贴板");
  assert.equal(first.draft.fragments[0].sourceUrl, "");
  stored.captureDraft = {};

  const repeated = await workspace.dispatch("add-clipboard-text", { text: "第一段剪贴板文字" });
  assert.equal(repeated.added, true);
  assert.equal(repeated.draft.fragments[0].text, "第一段剪贴板文字");
});

test("cancelled region capture does not create a visual or call offscreen", async () => {
  const stored = {};
  const calls = [];
  const chromeApi = fakeChrome(stored, {
    executeScript: async () => {
      calls.push("select");
      return [{ result: null }];
    },
    sendMessage: async (message) => {
      if (message?.target === "offscreen") calls.push("offscreen");
      return { ok: true };
    }
  });
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    captureClipboardFingerprintStorageKey: "lastCommittedClipboardFingerprint",
    ensureOffscreenDocument: async () => calls.push("ensure-offscreen"),
    deleteVisual: async () => calls.push("delete")
  });

  const result = await workspace.dispatch("capture-tab", {
    tab: { id: 7, windowId: 3, url: "https://example.com", title: "Example" }
  });

  assert.equal(result.ok, true);
  assert.equal(result.message, "已取消截图,草稿没有改变");
  assert.equal(result.draft.visuals.length, 0);
  assert.deepEqual(calls, ["select"]);
});

test("smart visual capture verifies the active page, restores it, and persists the cropped result", async () => {
  const stored = {};
  const calls = [];
  let executeCount = 0;
  const chromeApi = fakeChrome(stored, {
    query: async () => {
      calls.push("active-tab");
      return [{ id: 7, windowId: 3, url: "https://example.com", title: "Example" }];
    },
    captureVisibleTab: async () => {
      calls.push("capture-visible-tab");
      return "data:image/png;base64,AA==";
    },
    executeScript: async () => {
      executeCount += 1;
      if (executeCount === 1) {
        calls.push("select-visual");
        return [{
          result: {
            captureToken: "capture-token",
            selections: [{ x: 10, y: 20, width: 600, height: 400 }]
          }
        }];
      }
      calls.push("restore-page");
      return [{ result: true }];
    },
    sendMessage: async (message) => {
      calls.push(message.type);
      return {
        ok: true,
        results: [{
          width: 600,
          height: 400,
          mimeType: "image/webp",
          byteSize: 1234,
          palette: { colors: ["#112233"] }
        }]
      };
    }
  });
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    captureClipboardFingerprintStorageKey: "lastCommittedClipboardFingerprint",
    ensureOffscreenDocument: async () => calls.push("ensure-offscreen"),
    deleteVisual: async () => calls.push("delete"),
    resolveSourceContext: async () => ({
      canonicalUrl: "https://example.com/",
      displayTitle: "作者甲 · 2026-08-13",
      sourceFacts: { provider: "example", itemId: "work-1", author: "作者甲" }
    }),
    createId: () => "visual-1",
    now: () => "2026-07-28T10:00:00.000Z"
  });

  const result = await workspace.dispatch("capture-visible-visuals", { tabId: 7 });

  assert.equal(result.ok, true);
  assert.equal(result.draft.visuals[0].id, "visual-1");
  assert.equal(result.draft.visuals[0].capturedAt, "2026-07-28T10:00:00.000Z");
  assert.equal(result.draft.visuals[0].sourceTitle, "作者甲 · 2026-08-13");
  assert.equal(result.draft.sourceContexts[0].sourceFacts.author, "作者甲");
  assert.deepEqual(calls, [
    "active-tab",
    "select-visual",
    "active-tab",
    "capture-visible-tab",
    "restore-page",
    "ensure-offscreen",
    "CROP_AND_STORE_SCREENSHOTS"
  ]);
  assert.equal(stored.captureDraft.visuals.length, 1);
});

test("region capture keeps its existing steps while attaching resolved work identity", async () => {
  const stored = {};
  let executeCount = 0;
  const chromeApi = fakeChrome(stored, {
    executeScript: async () => {
      executeCount += 1;
      if (executeCount === 1) {
        return [{ result: {
          captureToken: "region-token",
          x: 10, y: 20, width: 640, height: 480,
          viewportWidth: 1200, viewportHeight: 800
        } }];
      }
      return [{ result: true }];
    },
    captureVisibleTab: async () => "data:image/png;base64,AA==",
    sendMessage: async () => ({
      ok: true,
      width: 640,
      height: 480,
      mimeType: "image/webp",
      byteSize: 1234,
      palette: { colors: [] }
    })
  });
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined,
    deleteVisual: async () => undefined,
    resolveSourceContext: async () => ({
      canonicalUrl: "https://example.com/",
      displayTitle: "作者乙 · 2026-08-13",
      sourceFacts: { provider: "example", itemId: "work-2", author: "作者乙" }
    }),
    createId: () => "region-visual",
    now: () => "2026-08-13T00:00:00.000Z"
  });

  const result = await workspace.dispatch("capture-tab", {
    tab: { id: 7, windowId: 3, url: "https://example.com/", title: "Generic site title" }
  });

  assert.equal(result.ok, true);
  assert.equal(result.draft.visuals[0].sourceTitle, "作者乙 · 2026-08-13");
  assert.equal(result.draft.sourceContexts[0].sourceFacts.itemId, "work-2");
});

test("failed smart visual storage removes every partial visual and leaves the draft unchanged", async () => {
  const stored = {};
  const deleted = [];
  let executeCount = 0;
  const chromeApi = fakeChrome(stored, {
    executeScript: async () => {
      executeCount += 1;
      if (executeCount === 1) {
        return [{
          result: {
            captureToken: "capture-token",
            selections: [{ x: 10, y: 20, width: 600, height: 400 }]
          }
        }];
      }
      return [{ result: true }];
    },
    sendMessage: async () => ({ ok: false, message: "磁盘写入失败" })
  });
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    captureClipboardFingerprintStorageKey: "lastCommittedClipboardFingerprint",
    ensureOffscreenDocument: async () => undefined,
    deleteVisual: async (visualId) => deleted.push(visualId),
    createId: () => "visual-failed"
  });

  await assert.rejects(
    () => workspace.dispatch("capture-visible-visuals", { tabId: 7 }),
    { message: "磁盘写入失败" }
  );
  assert.deepEqual(deleted, ["visual-failed"]);
  assert.equal((await workspace.getDraft()).visuals.length, 0);
});

test("smart visual selection with no candidates preserves the draft and offers region capture recovery", async () => {
  const stored = { captureDraft: { fragments: [{ id: "fragment-1", text: "keep me" }], visuals: [] } };
  const chromeApi = fakeChrome(stored, {
    executeScript: async () => [{ result: { captureToken: "capture-token", selections: [] } }]
  });
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    captureClipboardFingerprintStorageKey: "lastCommittedClipboardFingerprint",
    ensureOffscreenDocument: async () => undefined,
    deleteVisual: async () => undefined
  });

  const result = await workspace.dispatch("capture-visible-visuals", { tabId: 7 });

  assert.equal(result.ok, true);
  assert.equal(result.fallbackAction, "capture-region");
  assert.deepEqual(result.draft.fragments.map(({ id, text }) => ({ id, text })), [{ id: "fragment-1", text: "keep me" }]);
  assert.deepEqual(result.draft.visuals, []);
});

test("side-panel smart selection starts without a page toolbar and confirms through the same session", async () => {
  const stored = {};
  const calls = [];
  const chromeApi = fakeChrome(stored, {
    executeScript: async (details) => {
      calls.push({ type: "execute", options: details.args?.[0] });
      return [{ result: {
        empty: false,
        candidateCount: 2,
        selectedCount: 0,
        geometryRevision: 1,
        fullscreen: false
      } }];
    },
    sendTabMessage: async (_tabId, message) => {
      calls.push({ type: "tab-message", message });
      return {
        ok: true,
        selections: [{ x: 10, y: 20, width: 600, height: 400, viewportWidth: 1200, viewportHeight: 800 }]
      };
    },
    sendMessage: async (message) => {
      calls.push({ type: "runtime-message", message });
      return {
        ok: true,
        results: [{ width: 600, height: 400, mimeType: "image/webp", byteSize: 1234, palette: { colors: [] } }]
      };
    },
    captureVisibleTab: async () => "data:image/png;base64,AA=="
  });
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined,
    deleteVisual: async () => undefined,
    createId: (() => {
      const ids = ["selection-session", "selection-visual"];
      return () => ids.shift();
    })(),
    now: () => "2026-08-08T10:00:00.000Z"
  });

  const started = await workspace.dispatch("start-visible-visual-selection", { tabId: 7 });
  assert.equal(started.session.sessionId, "selection-session");
  assert.equal(started.session.candidateCount, 2);
  assert.equal(calls[0].options.externalControls, true);
  assert.equal(calls[0].options.hideFloatingControls, true);

  const confirmed = await workspace.dispatch("confirm-visible-visual-selection", { sessionId: "selection-session" });
  assert.equal(confirmed.ok, true);
  assert.equal(confirmed.draft.visuals[0].id, "selection-visual");
  const confirmCall = calls.find((call) => call.type === "tab-message");
  assert.deepEqual(confirmCall.message, {
    type: "PROMPTDIRECTOR_SMART_VISUAL_SELECTION",
    action: "confirm",
    sessionId: "selection-session"
  });
});

test("removing a visual never deletes its pixels before the updated draft is durable", async () => {
  const stored = {
    captureDraft: {
      visuals: [{ id: "visual-1", sourceUrl: "https://example.com", width: 600, height: 400 }]
    }
  };
  const calls = [];
  const chromeApi = fakeChrome(stored, {
    set: async () => {
      calls.push("persist");
      throw new Error("storage unavailable");
    }
  });
  const workspace = createCaptureWorkspace({
    chromeApi,
    captureDraftStorageKey: "captureDraft",
    captureClipboardFingerprintStorageKey: "lastCommittedClipboardFingerprint",
    ensureOffscreenDocument: async () => undefined,
    deleteVisual: async () => calls.push("delete")
  });

  await assert.rejects(
    () => workspace.dispatch("remove-visual", { visualId: "visual-1" }),
    { message: "storage unavailable" }
  );
  assert.deepEqual(calls, ["persist"]);
});

test("discard keeps only screenshots whose pixels could not be deleted", async () => {
  const stored = {
    captureDraft: {
      fragments: [{ id: "fragment-1", text: "discard" }],
      visuals: [
        { id: "visual-deleted", sourceUrl: "https://example.com/a", width: 600, height: 400 },
        { id: "visual-retained", sourceUrl: "https://example.com/b", width: 600, height: 400 }
      ]
    }
  };
  const workspace = createCaptureWorkspace({
    chromeApi: fakeChrome(stored),
    captureDraftStorageKey: "captureDraft",
    ensureOffscreenDocument: async () => undefined,
    deleteVisual: async (id) => {
      if (id === "visual-retained") throw new Error("storage busy");
    }
  });

  await assert.rejects(() => workspace.dispatch("cancel"), /1 张截图未能删除/);
  assert.deepEqual(stored.captureDraft.fragments, []);
  assert.deepEqual(stored.captureDraft.visuals.map((item) => item.id), ["visual-retained"]);
});

function fakeChrome(stored, overrides = {}) {
  return {
    storage: {
      local: {
        get: async (key) => ({ [key]: stored[key] }),
        set: overrides.set ?? (async (values) => Object.assign(stored, values))
      }
    },
    tabs: {
      query: overrides.query ?? (async () => [{ id: 7, windowId: 3, url: "https://example.com", title: "Example" }]),
      captureVisibleTab: overrides.captureVisibleTab ?? (async () => "data:image/png;base64,AA=="),
      sendMessage: overrides.sendTabMessage ?? (async () => ({ ok: true }))
    },
    scripting: {
      insertCSS: async () => undefined,
      removeCSS: async () => undefined,
      executeScript: overrides.executeScript ?? (async () => [{ result: null }])
    },
    runtime: {
      sendMessage: overrides.sendMessage ?? (async () => ({ ok: true }))
    },
    i18n: {
      getUILanguage: () => "zh-CN"
    }
  };
}

test('returning to a saved clipboard selection does not refill the draft, while explicit capture still works', async () => {
  const { CAPTURE_SAVED_TEXT_KEY, createTextCandidate } = await import('../extension/capture-text-candidate.js');
  const candidate = await createTextCandidate({clipboard:'saved text'});
  const stored = { [CAPTURE_SAVED_TEXT_KEY]: [candidate.textFingerprint] };
  const workspace = createCaptureWorkspace({chromeApi:fakeChrome(stored,{executeScript:async()=>[{result:'saved text'}]}),
    captureDraftStorageKey:'captureDraft',ensureOffscreenDocument:async()=>undefined,deleteVisual:async()=>undefined});
  const automatic=await workspace.dispatch('try-active-selection');
  assert.equal(automatic.added,false);
  assert.equal(automatic.reason,'already-saved-selection');
  assert.equal((await workspace.dispatch('add-active-selection')).added,true);
});

test('page save clears only persisted fragments and suppresses their automatic return', async () => {
  const stored = {};
  const workspace = createCaptureWorkspace({chromeApi:fakeChrome(stored,{executeScript:async()=>[{result:'saved page text'}]}),
    captureDraftStorageKey:'captureDraft',ensureOffscreenDocument:async()=>undefined,deleteVisual:async()=>undefined});
  const first = await workspace.dispatch('add-selection', {fragment:{text:'saved page text'}});
  await workspace.dispatch('add-selection', {fragment:{text:'unsaved text'}});
  const cleared = await workspace.dispatch('clear-saved-fragments', {fragmentIds:[first.draft.fragments[0].id]});
  assert.deepEqual(cleared.draft.fragments.map(item=>item.text), ['unsaved text']);
  await workspace.dispatch('remove-fragment', {fragmentId:cleared.draft.fragments[0].id});
  assert.equal((await workspace.dispatch('try-active-selection')).added, false);
  assert.equal((await workspace.dispatch('add-active-selection')).added, true);
});