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

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

import {
  LIBRARY_RETURN_STORAGE_KEY,
  buildNavigationState,
  buildNavigationUrl,
  deriveNavigationSnapshot,
  createLibraryReturnRestore,
  parseLibraryReturnSnapshot,
  serializeLibraryReturnSnapshot
} from "../extension/navigation-state.js";

function normalizeRoute(route) {
  const view = ["list", "detail", "editor", "refine", "create"].includes(route?.view) ? route.view : "list";
  const skillId = view === "list" || view === "create" ? "" : String(route?.skillId ?? "");
  return skillId ? { view, skillId } : view === "create" ? { view: "create", skillId: "" } : { view: "list", skillId: "" };
}

function parseRoute(search) {
  const params = new URLSearchParams(search);
  return { view: params.get("view"), skillId: params.get("skill") };
}

test("deriveNavigationSnapshot restores same-tab history state without resetting depth", () => {
  const snapshot = deriveNavigationSnapshot({
    stateKey: "skillPage",
    historyState: { skillPage: true, depth: 3, sourcePage: "composer", view: "detail", skillId: "skill:42" },
    locationSearch: "?source=library&view=list",
    parseRoute,
    normalizeRoute
  });

  assert.deepEqual(snapshot.route, { view: "detail", skillId: "skill:42" });
  assert.equal(snapshot.sourcePage, "composer");
  assert.equal(snapshot.depth, 3);
  assert.equal(snapshot.replace, false);
});

test("deriveNavigationSnapshot falls back to URL params and seeds a new state on first load", () => {
  const snapshot = deriveNavigationSnapshot({
    stateKey: "skillPage",
    historyState: null,
    locationSearch: "?source=composer&view=detail&skill=skill:7",
    parseRoute,
    normalizeRoute
  });

  assert.deepEqual(snapshot.route, { view: "detail", skillId: "skill:7" });
  assert.equal(snapshot.sourcePage, "composer");
  assert.equal(snapshot.depth, 0);
  assert.equal(snapshot.replace, true);
});

test("deriveNavigationSnapshot canonicalizes invalid stored route or source", () => {
  const snapshot = deriveNavigationSnapshot({
    stateKey: "skillPage",
    historyState: { skillPage: true, depth: 9, sourcePage: "unknown", view: "broken", skillId: "skill:9" },
    locationSearch: "?source=unknown&view=broken",
    parseRoute,
    normalizeRoute
  });

  assert.deepEqual(snapshot.route, { view: "list", skillId: "" });
  assert.equal(snapshot.sourcePage, "library");
  assert.equal(snapshot.depth, 9);
  assert.equal(snapshot.replace, true);
});

test("buildNavigationState and buildNavigationUrl preserve source, route, and depth contract", () => {
  const state = buildNavigationState({
    stateKey: "skillPage",
    route: { view: "editor", skillId: "skill:12" },
    sourcePage: "composer",
    depth: 4
  });

  assert.deepEqual(state, {
    skillPage: true,
    depth: 4,
    sourcePage: "composer",
    view: "editor",
    skillId: "skill:12"
  });

  const href = buildNavigationUrl("chrome-extension://example/skills.html?view=list", {
    route: { view: "editor", skillId: "skill:12" },
    sourcePage: "composer"
  });
  assert.match(href, /skills\.html\?source=composer&view=editor&skill=skill%3A12$/);
});

test("library return snapshots serialize canonical collection, facets, query, and scroll position", () => {
  const serialized = serializeLibraryReturnSnapshot({
    collectionId: "collection:7",
    contentId: "content:prompt:image",
    facetNodeIds: ["facet:2", "facet:1", "facet:2"],
    pendingOnly: true,
    query: "银色角色",
    scrollY: 428.7,
    sortMode: "updated-desc",
    projectSortMode: "recent"
  });

  assert.deepEqual(JSON.parse(serialized), {
    collectionId: "collection:7",
    unassignedViewActive: false,
    contentId: "content:prompt:image",
    facetNodeIds: ["facet:2", "facet:1"],
    pendingOnly: true,
    query: "银色角色",
    scrollY: 429,
    sortMode: "updated-desc"
  });
});

test("invalid library return snapshot data is rejected instead of leaking broken state", () => {
  assert.equal(parseLibraryReturnSnapshot("{"), null);
  assert.equal(parseLibraryReturnSnapshot(JSON.stringify({
    collectionId: 7,
    contentId: "",
    facetNodeIds: "facet:1",
    pendingOnly: false,
    query: null,
    scrollY: "far"
  })), null);
});

test("library return restore waits for initialization readiness and only applies once per page boot", () => {
  const storage = new Map();
  storage.set(LIBRARY_RETURN_STORAGE_KEY, serializeLibraryReturnSnapshot({
    collectionId: "collection:1",
    contentId: "",
    facetNodeIds: ["facet:1"],
    pendingOnly: false,
    query: "雾夜",
    scrollY: 320
  }));
  const applied = [];
  const restore = createLibraryReturnRestore({
    storage: {
      getItem: (key) => storage.get(key) ?? null,
      setItem: (key, value) => storage.set(key, value)
    },
    applySnapshot: (snapshot) => applied.push(snapshot)
  });

  assert.equal(restore(false), false);
  assert.deepEqual(applied, []);
  assert.equal(restore(true), true);
  assert.deepEqual(applied, [{
    collectionId: "collection:1",
    unassignedViewActive: false,
    contentId: "",
    facetNodeIds: ["facet:1"],
    pendingOnly: false,
    query: "雾夜",
    scrollY: 320,
    sortMode: "added-desc"
  }]);
  assert.equal(restore(true), false);
  assert.equal(storage.has(LIBRARY_RETURN_STORAGE_KEY), true);
});

test("old library return snapshots gain safe default sorting", () => {
  assert.deepEqual(parseLibraryReturnSnapshot(JSON.stringify({
    collectionId: "",
    contentId: "",
    facetNodeIds: [],
    pendingOnly: false,
    query: "",
    scrollY: 0
  })), {
    collectionId: "",
    unassignedViewActive: false,
    contentId: "",
    facetNodeIds: [],
    pendingOnly: false,
    query: "",
    scrollY: 0,
    sortMode: "added-desc"
  });
});

test("header ascending sorting survives return from creation", () => {
  const snapshot = parseLibraryReturnSnapshot(JSON.stringify({
    collectionId: "",
    contentId: "",
    facetNodeIds: [],
    pendingOnly: false,
    query: "",
    scrollY: 0,
    sortMode: "added-asc",
    projectSortMode: "manual"
  }));
  assert.equal(snapshot.sortMode, "added-asc");
});

test("returning from creation preserves the unassigned workspace alongside its filters", () => {
  const snapshot = parseLibraryReturnSnapshot(serializeLibraryReturnSnapshot({
    collectionId: "", unassignedViewActive: true, contentId: "content:prompt:image",
    facetNodeIds: ["style:film"], pendingOnly: false, query: "雨夜", scrollY: 2800,
    sortMode: "title", caseOrderManagementActive: true
  }));
  assert.equal(snapshot.unassignedViewActive, true);
  assert.equal(snapshot.query, "雨夜");
  assert.equal(snapshot.scrollY, 2800);
  assert.equal(snapshot.sortMode, "title");
  assert.equal("caseOrderManagementActive" in snapshot, false);
});

test("a saved project takes precedence over a contradictory unassigned flag", () => {
  const snapshot = parseLibraryReturnSnapshot(serializeLibraryReturnSnapshot({
    collectionId: "collection:1", unassignedViewActive: true, contentId: "",
    facetNodeIds: [], pendingOnly: false, query: "", scrollY: 0
  }));
  assert.equal(snapshot.collectionId, "collection:1");
  assert.equal(snapshot.unassignedViewActive, false);
});

test("unavailable session storage does not block opening the library", () => {
  const applied = [];
  const restore = createLibraryReturnRestore({
    storage: { getItem() { throw new Error("storage unavailable"); } },
    applySnapshot: snapshot => applied.push(snapshot)
  });
  assert.equal(restore(true), false);
  assert.deepEqual(applied, []);
});