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

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

const background = await readFile(new URL("../extension/background.js", import.meta.url), "utf8");

function functionBlock(name, nextName) {
  const start = background.indexOf(`async function ${name}(`);
  const end = background.indexOf(`async function ${nextName}(`, start + 1);
  assert.ok(start >= 0, `${name} should exist`);
  assert.ok(end > start, `${nextName} should follow ${name}`);
  return background.slice(start, end);
}

test("all user-facing case, media, and project deletion routes move metadata to trash", () => {
  assert.match(background, /case "DELETE_ENTRY":\s*return enqueue\(async \(\) => deleteEntry\(message\.entryId\)\)/);
  assert.match(background, /case "BATCH_MOVE_TO_TRASH":\s*return enqueue\(async \(\) => moveEntryBatchToTrash\(message\.entryIds\)\)/);
  assert.match(background, /case "DELETE_ENTRY_MEDIA":\s*return enqueue\(async \(\) => deleteEntryMedia/);
  assert.match(background, /case "DELETE_ENTRY_VISUAL":\s*return enqueue\(async \(\) => deleteEntryVisual/);
  assert.match(background, /case "DELETE_COLLECTION_WITH_ENTRIES":\s*return enqueue\(async \(\) => deleteCollectionWithEntries/);

  const caseDeletion = functionBlock("deleteEntry", "deleteCollectionWithEntries");
  assert.match(caseDeletion, /moveEntryBatchToTrash/);
  assert.doesNotMatch(caseDeletion, /deleteMediaBlob|deleteScreenshotBlob/);
  // Scope the safety assertion to the deletion function, not unrelated helpers inserted after it.
  const mediaDeletion = functionBlock("moveEntryMediaToTrash", "addUploadedMedia").split("\n}\n")[0];
  assert.match(mediaDeletion, /moveMediaToTrash/);
  assert.doesNotMatch(mediaDeletion, /deleteMediaBlob|deleteScreenshotBlob/);
  const projectDeletion = functionBlock("deleteCollectionWithEntries", "moveEntryBatchToTrash");
  assert.match(projectDeletion, /moveCollectionWithEntriesToTrash/);
  assert.match(projectDeletion, /collectionEntryIds\(organizerState, collection\.id, \{ subtree: true \}\)/);
  assert.doesNotMatch(projectDeletion, /deleteMediaBlob|deleteScreenshotBlob/);
});

test("undo of a saved case or import retains recoverable cases and media", () => {
  const saveUndo = functionBlock("undoLastSave", "deleteEntry").split("const current =")[0];
  assert.match(saveUndo, /assertCreatedEntryUndoSafe\(removed, undo\)/);
  assert.match(saveUndo, /moveEntriesToTrash/);
  assert.match(saveUndo, /\[STORAGE_KEYS\.trashState\]: moved\.trashState/);
  assert.doesNotMatch(saveUndo, /deleteUnreferencedMedia|screenshotStorageKey\(removed\.id\)/);

  const importUndo = functionBlock("undoImportJobAction", "recoverImportJobs");
  assert.match(importUndo, /entry\.importBatchId !== undone\.job\.importBatchId/);
  assert.match(importUndo, /moveEntriesToTrash/);
  assert.match(importUndo, /\[STORAGE_KEYS\.trashState\]: moved\.trashState/);
  assert.doesNotMatch(importUndo, /deleteUnreferencedMedia|deleteLocalAssetHandle/);
});

test("trash restore and irreversible cleanup have separate explicit message contracts", () => {
  for (const type of ["GET_TRASH_ITEMS", "RESTORE_TRASH_ITEMS", "PERMANENT_DELETE_TRASH_ITEMS", "EMPTY_TRASH"]) {
    assert.match(background, new RegExp(`case "${type}"`));
  }
  const cleanup = functionBlock("commitTrashCleanup", "restrictLocalStorageAccess");
  assert.match(cleanup, /await commitLocalChanges\(\{ \.\.\.taken\.changes, \[TRASH_CLEANUP_KEY\]: pending \}\)/);
  assert.match(cleanup, /await resumeTrashCleanup\(\)/);
  assert.match(cleanup, /deleteMediaBlobs/);
  assert.match(cleanup, /screenshotStorageKey/);
  assert.match(cleanup, /永久删除/);
});

test("project batch actions use independent copies and default moves", () => {
  for (const type of ["UPDATE_ENTRY_CUSTOM_LABELS", "BATCH_ADD_CUSTOM_LABELS", "BATCH_SET_PROJECT"]) {
    assert.match(background, new RegExp(`case "${type}"`));
  }
  const labels = functionBlock("updateEntryCustomLabels", "batchAddCustomLabels");
  assert.match(labels, /customLabels/);
  const projects = functionBlock("batchSetProject", "updateOrganizer");
  assert.match(projects, /planCaseCopies/);
  assert.match(projects, /message\.mode === "remove" \? "remove" : "move"/);
  assert.match(projects, /mode !== "remove"/);
  assert.match(projects, /moveEntriesBetweenCollections/);
  assert.match(projects, /message\.sourceCollectionId/);
  assert.match(projects, /setEntriesCollection/);
});