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

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

test("data safety lives inside general settings without a top-level entry", async () => {
  const html = await readFile(new URL("../extension/library.html", import.meta.url), "utf8");
  const topbar = html.slice(html.indexOf('<header class="topbar">'), html.indexOf("</header>"));

  assert.doesNotMatch(topbar, /id="open-data-safety"|备份与同步/);
  assert.match(html, /id="data-safety-dialog"/);
  assert.equal((html.match(/data-i18n="备份与同步"/g) ?? []).length, 1);
  assert.doesNotMatch(topbar, /id="data-safety-status"/);
});

test("whole-library portable ZIP backup is removed while selected-case sharing remains", async () => {
  const background = await readFile(new URL("../extension/background.js", import.meta.url), "utf8");
  assert.doesNotMatch(background, /case "CREATE_PORTABLE_BACKUP"/);
  assert.match(background, /case "EXPORT_ARCHIVE"/);
  const exporter = background.slice(background.indexOf("async function exportArchive"), background.indexOf("async function exportProjectArchive"));
  assert.match(exporter, /!Array\.isArray\(requestedEntryIds\) \|\| !requestedEntryIds\.length/);
  assert.doesNotMatch(exporter, /exportState\s*=\s*sharing\s*\?/);
});

test("connecting a sync folder immediately states that it only verifies access", async () => {
  const library = await readFile(new URL("../extension/library.js", import.meta.url), "utf8");
  const actionBody = library.slice(
    library.indexOf("async function runDataSafetyAction"),
    library.indexOf("function showDataSafetyFeedback")
  );
  const sendIndex = actionBody.indexOf("await chrome.runtime.sendMessage(message)");
  const pendingIndex = actionBody.indexOf('showDataSafetyFeedback("正在验证文件夹与密码,不会读取或合并资料…")');

  assert.ok(pendingIndex >= 0, "连接同步时必须立即替换旧的备份或授权提示");
  assert.ok(pendingIndex < sendIndex, "验证文件夹前必须先显示准确状态");
  assert.match(actionBody, /button\.textContent\s*=\s*t\("正在连接…"\)/);
  assert.doesNotMatch(actionBody, /正在加密并写入同步文件夹|正在解锁并合并同步资料/);
});

test("a missing sync location shows a localized recovery action instead of the browser error", async () => {
  const library = await readFile(new URL("../extension/library.js", import.meta.url), "utf8");
  const renderBody = library.slice(
    library.indexOf("async function renderDataSafetyStatus"),
    library.indexOf("async function chooseSyncFolder")
  );
  const statusBody = library.slice(
    library.indexOf("function syncStatusMessage"),
    library.indexOf("async function maybeShowRestoreOnboarding")
  );

  assert.match(renderBody, /SYNC_ERROR_CODES\.LOCATION_NOT_FOUND/);
  assert.match(renderBody, /elements\.syncSettings\.open\s*=\s*true/);
  assert.match(renderBody, /"重新选择同步文件夹"/);
  assert.match(statusBody, /t\("同步文件夹中的文件或目录不存在,请重新选择同步文件夹后再同步"\)/);
  assert.doesNotMatch(statusBody, /return status\.lastErrorCode/);
});

test("ZIP import uses the local-file entry while settings retain backup and collapsed sync", async () => {
  const html = await readFile(new URL("../extension/library.html", import.meta.url), "utf8");
  const panel = html.slice(
    html.indexOf('<section id="data-safety-dialog"'),
    html.indexOf('<footer class="settings-about"')
  );

  assert.match(panel, /id="create-folder-backup"[\s\S]*?data-i18n="备份资料库"/);
  assert.match(panel, /id="restore-folder-backup"[\s\S]*?data-i18n="恢复资料库"/);
  assert.doesNotMatch(panel, /share-package-import|import-library-package|library-package-file/);
  assert.doesNotMatch(panel, /create-portable-backup|restore-portable-backup|小型 ZIP|从 ZIP/);
  assert.match(panel, /<details id="sync-settings"/);
  assert.match(panel, /<summary>[\s\S]*?data-i18n="跨设备同步"/);
  assert.equal((panel.match(/class="data-safety-primary-actions"/g) ?? []).length, 1);
  const primaryActions = panel.slice(
    panel.indexOf('class="data-safety-primary-actions"'),
    panel.indexOf("</div>", panel.indexOf('class="data-safety-primary-actions"'))
  );
  assert.equal((primaryActions.match(/<button\b/g) ?? []).length, 2, "资料库备份与恢复只能保留两个常驻动作");
  assert.doesNotMatch(panel, /id="restore-library-replacement-point"/);
});

test("share-package batch import preserves local configuration and accepts historical package versions", async () => {
  const library = await readFile(new URL("../extension/library.js", import.meta.url), "utf8");
  const parser = await readFile(new URL("../extension/library-package.js", import.meta.url), "utf8");
  const migrations = await readFile(new URL("../extension/library-package-migrations.js", import.meta.url), "utf8");
  const format = await readFile(new URL("../extension/library-package-format.js", import.meta.url), "utf8");
  const action = library.slice(
    library.indexOf("async function openLibraryPackageBatch"),
    library.indexOf("function backupMediaPaths")
  );
  assert.match(action, /previewLibraryImportInWorker/);
  assert.match(action, /GET_FOLDER_BACKUP_STATE/);
  assert.match(action, /APPLY_LIBRARY_IMPORT_BATCH/);
  assert.equal((action.match(/preserveLibraryConfiguration:\s*true/g) ?? []).length, 1);
  assert.match(action, /plan:\s*batch\.preview\.plan/);
  assert.match(action, /files\.map\(\(item\) =>/);
  assert.match(parser, /prepareLibraryPackageDraft\(value\)/);
  assert.match(migrations, /isSupportedLibraryPackageVersion\(value\.version\)/);
  assert.match(format, /SUPPORTED_LIBRARY_PACKAGE_VERSIONS\s*=\s*Object\.freeze\(\[1, 2, 3, 4, 5\]\)/);
});

test("multi-ZIP import uses one wide preflight with blocking failures and one batch apply", async () => {
  const html = await readFile(new URL("../extension/library.html", import.meta.url), "utf8");
  const library = await readFile(new URL("../extension/library.js", import.meta.url), "utf8");
  const dialog = html.slice(
    html.indexOf('id="library-package-import-dialog"'),
    html.indexOf('id="drawer-backdrop"')
  );
  const flow = library.slice(
    library.indexOf("async function openLibraryPackageBatch"),
    library.indexOf("function createLibraryImportOperationId")
  );

  assert.match(html, /id="media-file"[^>]*multiple/);
  assert.match(dialog, /library-package-import-package-count/);
  assert.match(dialog, /library-package-import-case-count/);
  assert.match(dialog, /library-package-import-project-count/);
  assert.match(dialog, /library-package-import-media-count/);
  assert.match(dialog, /library-package-import-byte-size/);
  assert.doesNotMatch(dialog, /library-package-import-select-all/);
  assert.doesNotMatch(dialog, /library-package-import-remove/);
  assert.match(dialog, /class="data-safety-actions import-actions ui-dialog-footer"/);
  assert.doesNotMatch(dialog, /type="checkbox"/);
  assert.match(flow, /for \(const item of batch\.items\)/);
  assert.match(flow, /await inspectLibraryPackageBatchItem\(batch, item\)/);
  assert.match(flow, /batch\.controller\.signal/);
  assert.match(flow, /previewLibraryImportInWorker/);
  assert.match(flow, /previewController\?\.abort/);
  assert.match(flow, /APPLY_LIBRARY_IMPORT_BATCH/);
  assert.match(flow, /assertStorageCapacity\(estimate, plannedBytes\)/);
  const plan = flow.slice(flow.indexOf("async function refreshLibraryPackageBatchPlan"), flow.indexOf("function libraryPackageMessage"));
  assert.ok(plan.indexOf("batch.preview = response") > plan.indexOf("assertStorageCapacity(estimate, plannedBytes)"), "publish readiness only after the capacity check");
  assert.match(flow, /retryLibraryPackageItem/);
  assert.match(flow, /removeLibraryPackageItem/);
  assert.match(flow, /batch\.preview\?\.unresolvedConflicts/);
  assert.match(flow, /await savePortableAssetBlobs\(resourceWrites,/);
  assert.match(flow, /readZipResources\(file,/);
});

test("data safety cannot be dismissed accidentally while a storage operation is running", async () => {
  const library = await readFile(new URL("../extension/library.js", import.meta.url), "utf8");
  const bindings = library.slice(
    library.indexOf("elements.settingsClose.addEventListener"),
    library.indexOf("elements.connectSyncFolder.addEventListener")
  );

  assert.match(bindings, /elements\.settingsDialog\.addEventListener\("cancel"/);
  assert.match(bindings, /dataSafetyOperationActive[^\n]*event\.preventDefault\(\)/);
  assert.match(library, /elements\.settingsClose\.disabled\s*=\s*active/);
});

test("folder backup self-check covers project and media trash snapshots plus their relationships", async () => {
  const library = await readFile(new URL("../extension/library.js", import.meta.url), "utf8");
  const roundtrip = library.slice(
    library.indexOf("function assertFolderBackupRoundtrip"),
    library.indexOf("async function localAssetReferenceBackupBlob")
  );
  assert.match(roundtrip, /expected\.kind === "collection"/);
  assert.match(roundtrip, /expected\.kind === "media"/);
  assert.match(roundtrip, /expected\.relationships/);
});

test("folder restore exposes safe merge and exact replace with aggregate capacity and a reversible recovery point", async () => {
  const html = await readFile(new URL("../extension/library.html", import.meta.url), "utf8");
  const library = await readFile(new URL("../extension/library.js", import.meta.url), "utf8");
  const restore = library.slice(
    library.indexOf("async function restoreCompleteFolderBackup"),
    library.indexOf("async function openLibraryPackageBatch")
  );

  assert.match(restore, /LIBRARY_TRANSFER_MODES\.SAFE_MERGE/);
  assert.match(restore, /LIBRARY_TRANSFER_MODES\.EXACT_REPLACE/);
  assert.match(restore, /libraryTransferWriteBytes\(/);
  assert.match(restore, /navigator\.storage\.estimate\(\)/);
  assert.match(restore, /assertStorageCapacity\(/);
  assert.match(restore, /conflictResolutions/);
  assert.match(restore, /showAppDialog\(/);
  assert.doesNotMatch(html, /id="library-recovery-actions"|id="restore-library-replacement-point"/);
  assert.match(library, /button.id = "restore-library-replacement-point"/);
  assert.match(library, /RESTORE_LIBRARY_REPLACEMENT_POINT/);
  assert.match(library, /canRestoreReplacementPoint/);
});