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

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

import {
  COLLECTION_VISIBILITY,
  createCollection,
  deleteCollection,
  mergeOrganizerState,
  mergeOrganizerStateWithMap,
  normalizeOrganizerState,
  planCollectionAndEntriesDeletion,
  reorderCollections,
  removeEntriesFromOrganizer,
  replaceCollectionEntries,
  renameCollection,
  isEntryVisibleInLibrary,
  createLibraryVisibilityReader,
  collectionEntryIds,
  collectionPathLabel,
  collectionSelectorLabel,
  collectionSubtreeEntryIdsById,
  collectionSubtreeIds,
  moveCollection,
  moveEntriesBetweenCollections,
  setCollectionVisibility,
  setEntriesCollection
} from "../extension/organizer.js";

test("organizer normalizes duplicate and missing entry references", () => {
  const value = normalizeOrganizerState({
    collections: [{ id: "collection:a", name: "项目 A", entryIds: ["one", "missing", "one"] }]
  }, ["one"]);
  assert.deepEqual(value.collections[0].entryIds, ["one"]);
});

test("organizer drops obsolete saved searches and only keeps explicit projects", () => {
  const normalized = normalizeOrganizerState({
    savedViews: [{ id: "view:old", name: "视频参考", query: "type:video" }],
    collections: [{ id: "collection:a", name: "项目 A", entryIds: [] }]
  });
  assert.equal(normalized.version, 7);
  assert.deepEqual(Object.keys(normalized), ["version", "collections"]);
});

test("a case can belong to multiple project collections", () => {
  let state = createCollection({}, "广告参考").state;
  state = createCollection(state, "电影参考").state;
  state = setEntriesCollection(state, state.collections[0].id, ["one"], true);
  state = setEntriesCollection(state, state.collections[1].id, ["one"], true);
  assert.deepEqual(state.collections.map((item) => item.entryIds), [["one"], ["one"]]);
  assert.equal(renameCollection(state, state.collections[0].id, "广告成片").collections[0].name, "广告成片");
  assert.equal(deleteCollection(state, state.collections[0].id).collections.length, 1);
});

test("folder moves retain exactly one owner regardless of the originating view", () => {
  let state = createCollection({}, "来源").state;
  state = createCollection(state, "保留").state;
  state = createCollection(state, "目标").state;
  const [source, retained, target] = state.collections;
  state = setEntriesCollection(state, source.id, ["one"], true);
  state = setEntriesCollection(state, retained.id, ["one"], true);

  state = moveEntriesBetweenCollections(state, source.id, target.id, ["one"]);

  assert.deepEqual(state.collections.map((item) => item.entryIds), [[], [], ["one"]]);
});

test("moving from unassigned adds the target membership without a source collection", () => {
  const created = createCollection({}, "目标");
  const state = moveEntriesBetweenCollections(created.state, null, created.item.id, ["one"]);
  assert.deepEqual(state.collections[0].entryIds, ["one"]);
});

test("moving several cases is atomic, preserves visual order, and never duplicates target members", () => {
  const original = normalizeOrganizerState({ collections: [
    { id: "source", name: "来源", order: 0, entryIds: ["one", "two", "three", "four"] },
    { id: "retained", name: "保留", order: 1, entryIds: ["one", "four"] },
    { id: "target", name: "目标", order: 2, entryIds: ["existing", "two"] }
  ] });

  const moved = moveEntriesBetweenCollections(original, "source", "target", ["three", "two", "three"]);

  assert.deepEqual(original.collections.map((item) => item.entryIds), [
    ["one", "two", "three", "four"], ["one", "four"], ["existing", "two"]
  ]);
  assert.deepEqual(moved.collections.map((item) => item.entryIds), [
    ["one", "four"], ["one", "four"], ["existing", "two", "three"]
  ]);
});

test("adding cases to a project is repeatable without creating duplicate memberships", () => {
  const original = normalizeOrganizerState({ collections: [
    { id: "target", name: "目标", entryIds: ["one"] }
  ] });
  const once = setEntriesCollection(original, "target", ["one", "two", "two"], true);
  const twice = setEntriesCollection(once, "target", ["two"], true);

  assert.deepEqual(original.collections[0].entryIds, ["one"]);
  assert.deepEqual(once.collections[0].entryIds, ["one", "two"]);
  assert.deepEqual(twice.collections[0].entryIds, ["one", "two"]);
});

test("a failed move target leaves the source organizer untouched", () => {
  const original = normalizeOrganizerState({ collections: [
    { id: "source", name: "来源", entryIds: ["one", "two"] }
  ] });

  assert.throws(
    () => moveEntriesBetweenCollections(original, "source", "missing", ["one"]),
    /目标项目不存在/
  );
  assert.deepEqual(original.collections[0].entryIds, ["one", "two"]);
});

test("organizer merge remaps entry ids and merges same-name collections", () => {
  const merged = mergeOrganizerState({
    collections: [{ id: "collection:local", name: "项目", order: 0, entryIds: ["local"] }]
  }, {
    collections: [{ id: "collection:remote", name: "项目", order: 0, entryIds: ["source"] }]
  }, { source: "imported" });
  assert.deepEqual(merged.collections[0].entryIds, ["local", "imported"]);
});

test("organizer merge exposes the actual destination id for every imported project", () => {
  const result = mergeOrganizerStateWithMap({
    collections: [{ id: "collection:local", name: "项目", order: 0, entryIds: ["local"] }]
  }, {
    collections: [
      { id: "collection:remote", name: "项目", order: 0, entryIds: ["source"] },
      { id: "collection:local", name: "另一个项目", order: 1, entryIds: ["second"] }
    ]
  }, { source: "imported", second: "imported-second" });

  assert.equal(result.collectionIdMap["collection:remote"], "collection:local");
  assert.notEqual(result.collectionIdMap["collection:local"], "collection:local");
  assert.deepEqual(result.state.collections[0].entryIds, ["local", "imported"]);
  assert.equal(result.state.collections.find((item) => item.id === result.collectionIdMap["collection:local"]).entryIds[0], "imported-second");
});

test("receiver-local project creation time is assigned only to newly imported projects", () => {
  const merged = mergeOrganizerState({
    collections: [{ id: "existing", name: "已有", order: 0, entryIds: ["one"], createdAt: "2026-01-01T00:00:00.000Z" }]
  }, {
    collections: [
      { id: "same-name", name: "已有", order: 0, entryIds: ["two"] },
      { id: "new", name: "新项目", order: 1, entryIds: ["three"], createdAt: "2025-01-01T00:00:00.000Z" }
    ]
  }, { two: "two", three: "three" }, { createdAt: "2026-08-22T00:00:00.000Z" });

  assert.equal(merged.collections[0].createdAt, "2026-01-01T00:00:00.000Z");
  assert.equal(merged.collections[1].createdAt, "2026-08-22T00:00:00.000Z");
});

test("removing a case also removes private organizer references", () => {
  const state = removeEntriesFromOrganizer({
    collections: [{ id: "collection:a", name: "A", entryIds: ["one", "two"] }]
  }, ["one"]);
  assert.deepEqual(state.collections[0].entryIds, ["two"]);
});

test("project and entries deletion is planned atomically and removes shared project references", () => {
  const organizer = {
    collections: [
      { id: "collection:delete", name: "AIArtWorks Midjourney", entryIds: ["one", "two"] },
      { id: "collection:keep", name: "保留项目", entryIds: ["two", "three"] }
    ]
  };
  const result = planCollectionAndEntriesDeletion(
    organizer,
    [{ id: "one" }, { id: "two" }, { id: "three" }],
    "collection:delete",
    "AIArtWorks Midjourney"
  );
  assert.deepEqual(result.deletedEntryIds, ["one", "two"]);
  assert.deepEqual(result.entries, [{ id: "three" }]);
  assert.deepEqual(result.organizerState.collections, [{
    id: "collection:keep",
    name: "保留项目",
    parentId: null,
    order: 0,
    entryIds: ["three"],
    visibility: COLLECTION_VISIBILITY.library
  }]);
});

test("project and entries deletion rejects a stale or mistyped confirmation name", () => {
  assert.throws(() => planCollectionAndEntriesDeletion({
    collections: [{ id: "collection:a", name: "准确名称", entryIds: ["one"] }]
  }, [{ id: "one" }], "collection:a", "错误名称"), /项目名称不匹配/);
});

test("project membership can be replaced atomically in visual selection order", () => {
  const state = replaceCollectionEntries({
    collections: [{ id: "collection:a", name: "A", entryIds: ["old", "keep"] }]
  }, "collection:a", ["keep", "new", "keep"]);
  assert.deepEqual(state.collections[0].entryIds, ["keep", "new"]);
});

test("manual case order supports first middle and last positions without hidden step controls", () => {
  const initial = normalizeOrganizerState({ collections: [{
    id: "collection:a", name: "A", entryIds: ["one", "two", "three", "four"]
  }] });
  const first = replaceCollectionEntries(initial, "collection:a", ["four", "one", "two", "three"]);
  const middle = replaceCollectionEntries(first, "collection:a", ["four", "two", "one", "three"]);
  const last = replaceCollectionEntries(middle, "collection:a", ["two", "one", "three", "four"]);
  const unchanged = replaceCollectionEntries(last, "collection:a", last.collections[0].entryIds);

  assert.deepEqual(initial.collections[0].entryIds, ["one", "two", "three", "four"]);
  assert.deepEqual(first.collections[0].entryIds, ["four", "one", "two", "three"]);
  assert.deepEqual(middle.collections[0].entryIds, ["four", "two", "one", "three"]);
  assert.deepEqual(last.collections[0].entryIds, ["two", "one", "three", "four"]);
  assert.deepEqual(unchanged, last);
});

test("project order is normalized after deletion so a new project cannot reuse a stale order", () => {
  const initial = {
    collections: [
      { id: "collection:a", name: "A", order: 4, entryIds: [] },
      { id: "collection:b", name: "B", order: 9, entryIds: [] }
    ]
  };
  const afterDelete = deleteCollection(initial, "collection:a");
  assert.deepEqual(afterDelete.collections.map((item) => item.order), [0]);
  const created = createCollection(afterDelete, "C");
  assert.deepEqual(created.state.collections.map((item) => item.order), [0, 1]);
  assert.ok(Number.isFinite(Date.parse(created.item.createdAt)));
});

test("projects can be reordered without changing member order or inventing legacy creation time", () => {
  const reordered = reorderCollections({
    collections: [
      { id: "collection:a", name: "A", order: 0, entryIds: ["one", "two"] },
      { id: "collection:b", name: "B", order: 1, entryIds: ["three"], createdAt: "2026-08-22T01:02:03.000Z" }
    ]
  }, ["collection:b", "collection:a"]);
  assert.deepEqual(reordered.collections.map((item) => item.id), ["collection:b", "collection:a"]);
  assert.deepEqual(reordered.collections.map((item) => item.order), [0, 1]);
  assert.deepEqual(reordered.collections[1].entryIds, ["one", "two"]);
  assert.equal(Object.hasOwn(reordered.collections[1], "createdAt"), false);
  assert.equal(reordered.collections[0].createdAt, "2026-08-22T01:02:03.000Z");
});

test("project reorder rejects stale, incomplete, or duplicate project lists", () => {
  const state = {
    collections: [
      { id: "collection:a", name: "A", entryIds: [] },
      { id: "collection:b", name: "B", entryIds: [] }
    ]
  };
  assert.throws(() => reorderCollections(state, ["collection:a"]), /刷新后重试/);
  assert.throws(() => reorderCollections(state, ["collection:a", "collection:a"]), /刷新后重试/);
  assert.throws(() => reorderCollections(state, ["collection:a", "collection:missing"]), /刷新后重试/);
});

test("project-only cases leave the main library unless another visible project also contains them", () => {
  let state = createCollection({}, "字体参考").state;
  state = createCollection(state, "当前项目").state;
  state = setEntriesCollection(state, state.collections[0].id, ["font", "shared"], true);
  state = setEntriesCollection(state, state.collections[1].id, ["shared"], true);
  state = setCollectionVisibility(state, state.collections[0].id, COLLECTION_VISIBILITY.projectOnly);

  assert.equal(isEntryVisibleInLibrary(state, "font"), false);
  assert.equal(isEntryVisibleInLibrary(state, "shared"), true);
  assert.equal(isEntryVisibleInLibrary(state, "unassigned"), true);
  assert.equal(state.collections[0].visibility, COLLECTION_VISIBILITY.projectOnly);
});

test("browsing a whole library checks project visibility once while preserving shared and unassigned cases", () => {
  let reads = 0;
  const collections = [
    { id: "private", name: "项目内", visibility: "project-only", entryIds: ["hidden", "shared", "hidden"] },
    { id: "public", name: "图库内", parentId: "private", entryIds: ["shared", "visible"] }
  ];
  const state = { get collections() { reads += 1; return collections; } };
  const visible = createLibraryVisibilityReader(state);
  for (let i = 0; i < 5_000; i += 1) {
    assert.equal(visible("hidden"), false);
    assert.equal(visible("shared"), true);
    assert.equal(visible("visible"), true);
    assert.equal(visible(`unassigned-${i}`), true);
  }
  assert.equal(visible(""), false);
  assert.equal(visible(null), false);
  assert.equal(reads, 1, "Every case must use the same project snapshot rather than re-read all memberships");
  assert.deepEqual(collections[0].entryIds, ["hidden", "shared", "hidden"], "Opening does not rewrite project relations");
  collections[0].visibility = "library";
  assert.equal(visible("hidden"), false, "The current render keeps its consistent snapshot");
  assert.equal(createLibraryVisibilityReader(state)("hidden"), true, "The next render reflects project changes");
});

test("organizer upgrade permanently discards legacy project methods", () => {
  const normalized = normalizeOrganizerState({
    collections: [{
      id: "collection:a", name: "项目", entryIds: ["one"],
      projectMethods: { image: { systemInstruction: "legacy" } }
    }]
  }, ["one"]);
  assert.equal(Object.hasOwn(normalized.collections[0], "projectMethods"), false);
});

test("legacy flat projects migrate to roots without changing membership or order", () => {
  const normalized = normalizeOrganizerState({
    version: 6,
    collections: [
      { id: "collection:a", name: "A", order: 1, entryIds: ["one"] },
      { id: "collection:b", name: "B", order: 0, entryIds: ["one", "two"] }
    ]
  });
  assert.deepEqual(normalized.collections.map(({ id, parentId, order, entryIds }) => ({ id, parentId, order, entryIds })), [
    { id: "collection:b", parentId: null, order: 0, entryIds: ["one", "two"] },
    { id: "collection:a", parentId: null, order: 1, entryIds: ["one"] }
  ]);
});

test("projects move across parents and reject cycles", () => {
  const state = normalizeOrganizerState({ collections: [
    { id: "root", name: "根", order: 0, entryIds: ["one"] },
    { id: "peer", name: "同级", order: 1, entryIds: [] },
    { id: "child", name: "子项目", parentId: "root", order: 0, entryIds: ["two"] }
  ] });
  const moved = moveCollection(state, "peer", "root", 0);
  assert.deepEqual(moved.collections.map(({ id, parentId, order }) => ({ id, parentId, order })), [
    { id: "root", parentId: null, order: 0 },
    { id: "peer", parentId: "root", order: 0 },
    { id: "child", parentId: "root", order: 1 }
  ]);
  assert.throws(() => moveCollection(moved, "root", "child", 0), /自身或其子项目/);
});

test("tree helpers preserve paths and deduplicate subtree cases", () => {
  const state = { collections: [
    { id: "root", name: "根", entryIds: ["one"] },
    { id: "child", name: "素材", parentId: "root", entryIds: ["one", "two"] }
  ] };
  assert.deepEqual(collectionSubtreeIds(state, "root"), ["root", "child"]);
  assert.deepEqual(collectionEntryIds(state, "root", { subtree: true }), ["one", "two"]);
  assert.equal(collectionPathLabel(state, "child"), "根 / 素材");
  assert.deepEqual(collectionSubtreeEntryIdsById(state).get("root"), ["one", "two"]);
});

test("project selector labels distinguish literal separators from nested paths", () => {
  const state = { collections: [
    { id: "literal", name: "A › B", entryIds: [] },
    { id: "root", name: "A", entryIds: [] },
    { id: "child", name: "B", parentId: "root", entryIds: [] }
  ] };
  assert.equal(collectionSelectorLabel(state, "literal"), "A ›› B");
  assert.equal(collectionSelectorLabel(state, "child"), "A › B");
});

test("deep project trees normalize and traverse without recursive stack overflow", () => {
  const collections = Array.from({ length: 6000 }, (_, index) => ({
    id: `project:${index}`,
    name: `P${index}`,
    parentId: index ? `project:${index - 1}` : null,
    entryIds: index === 5999 ? ["case:deep"] : []
  }));
  const state = normalizeOrganizerState({ collections });
  assert.equal(state.collections.length, 6000);
  assert.equal(collectionSubtreeIds(state, "project:0").length, 6000);
  assert.deepEqual(collectionEntryIds(state, "project:0", { subtree: true }), ["case:deep"]);
});

test("project names are unique only among siblings", () => {
  let state = createCollection({}, "根 A").state;
  state = createCollection(state, "根 B").state;
  const [rootA, rootB] = state.collections;
  state = createCollection(state, "素材", rootA.id).state;
  state = createCollection(state, "素材", rootB.id).state;
  assert.equal(state.collections.filter((item) => item.name === "素材").length, 2);
  assert.throws(() => createCollection(state, "素材", rootA.id), /已经存在/);
});