const assert = require("node:assert/strict");

const {
  auditSidebarMindmapGhostCandidates,
} = require("./task180-mindmap-ghost-candidate-audit");

const fixture = {
  documents: [
    { id: "doc_1", title: "带重复导图的页面" },
    { id: "doc_2", title: "正常页面" },
  ],
  mindmapAssets: [
    { id: "mind_a", document_id: "doc_1", asset_type: "mindmap", deleted_at: null },
    { id: "mind_b", document_id: "doc_1", asset_type: "mindmap", deleted_at: null },
    { id: "mind_b", document_id: "doc_1", asset_type: "mindmap", deleted_at: null },
    { id: "mind_c", document_id: "doc_2", asset_type: "mindmap", deleted_at: "2026-05-01" },
    { id: "mind_orphan", document_id: "doc_missing", asset_type: "mindmap", deleted_at: null },
  ],
  kernelFileTreeProjection: {
    items: [
      {
        rowId: "asset:mind_a",
        rowKind: "asset",
        title: "mind_a",
        resourceMeta: { resourceKind: "mindmap", documentId: "doc_1", assetId: "mind_a" },
      },
      {
        rowId: "asset:mind_a:dup",
        rowKind: "asset",
        title: "mind_a duplicate",
        resourceMeta: { resourceKind: "mindmap", documentId: "doc_1", assetId: "mind_a" },
      },
    ],
  },
};

const result = auditSidebarMindmapGhostCandidates(fixture);

assert.equal(result.ok, true);
assert.equal(result.summary.activeMindmapAssets, 4);
assert.equal(result.summary.documentGroupsWithMultipleActiveMindmaps, 1);
assert.equal(result.summary.duplicateActiveAssetIds, 1);
assert.equal(result.summary.activeAssetsWithMissingDocument, 1);
assert.equal(result.summary.duplicateFileTreeMindmapRows, 1);
assert.deepEqual(result.candidates.multipleActiveMindmapsByDocument[0].mindmapIds, [
  "mind_a",
  "mind_b",
  "mind_b",
]);
assert.deepEqual(result.candidates.duplicateActiveAssetIds[0].assetId, "mind_b");
assert.deepEqual(result.candidates.activeAssetsWithMissingDocument[0].assetId, "mind_orphan");
assert.deepEqual(result.candidates.duplicateFileTreeMindmapRows[0].assetId, "mind_a");

const fileProjectionResult = auditSidebarMindmapGhostCandidates({
  ok: true,
  result: {
    items: [
      {
        rowId: "resource:mind_projection",
        title: "导图",
        resourceMeta: { resourceKind: "mindmap", documentId: "doc_projection", assetId: "mind_projection" },
      },
      {
        rowId: "resource:mind_projection:duplicate",
        title: "导图副本",
        resourceMeta: { resourceKind: "mindmap", documentId: "doc_projection", assetId: "mind_projection" },
      },
    ],
  },
});

assert.equal(fileProjectionResult.summary.documents, 0);
assert.equal(fileProjectionResult.summary.activeMindmapAssets, 0);
assert.equal(fileProjectionResult.summary.fileTreeMindmapRows, 2);
assert.equal(fileProjectionResult.summary.duplicateFileTreeMindmapRows, 1);
assert.deepEqual(fileProjectionResult.candidates.duplicateFileTreeMindmapRows[0].rowIds, [
  "resource:mind_projection",
  "resource:mind_projection:duplicate",
]);

const directItemsResult = auditSidebarMindmapGhostCandidates({
  items: [
    {
      row_id: "resource:mind_direct",
      resource_meta: { resource_kind: "mindmap", document_id: "doc_direct", asset_id: "mind_direct" },
    },
  ],
});

assert.equal(directItemsResult.summary.fileTreeMindmapRows, 1);
assert.equal(directItemsResult.summary.duplicateFileTreeMindmapRows, 0);

console.log("task180 mindmap ghost candidate audit self-test passed");
