import { describe, expect, it } from "vitest";
import { buildSidebarTreeSyncKey, buildMediaAssetListSyncKey } from "./sidebar-sync";
import type { SidebarTreeNode } from "@/lib/kernel-sidebar";
import type { MediaAsset } from "@/types/media";

function buildNode(overrides: Partial<SidebarTreeNode> = {}): SidebarTreeNode {
  return {
    id: "doc-1",
    workspace_id: "ws-1",
    title: "标题",
    parent_id: null,
    sort_order: 0,
    is_starred: false,
    is_template: false,
    access_scope: "private",
    created_at: "2026-04-21T00:00:00.000Z",
    updated_at: "2026-04-21T00:00:00.000Z",
    children: [],
    kernel: {
      nodeType: "page",
      depth: 0,
      position: 0,
      childCount: 0,
      expandedByDefault: true,
    },
    ...overrides,
  };
}

function buildAsset(overrides: Partial<MediaAsset> = {}): MediaAsset {
  return {
    id: "asset-1",
    workspace_id: "ws-1",
    document_id: "doc-1",
    asset_type: "image",
    file_url: "/files/a.png",
    thumbnail_url: null,
    bucket: null,
    storage_path: "uploads/a.png",
    file_name: "a.png",
    file_size: 1,
    mime_type: "image/png",
    ocr_payload: undefined,
    ocr_strategy: null,
    ocr_text: null,
    ocr_status: null,
    signed_url: null,
    created_at: "2026-04-21T00:00:00.000Z",
    updated_at: "2026-04-21T00:00:00.000Z",
    ...overrides,
  };
}

describe("sidebar-sync", () => {
  it("相同侧边栏树内容应产生相同 sync key", () => {
    const left = [buildNode({ children: [buildNode({ id: "doc-2", parent_id: "doc-1" })] })];
    const right = [buildNode({ children: [buildNode({ id: "doc-2", parent_id: "doc-1" })] })];

    expect(buildSidebarTreeSyncKey(left)).toBe(buildSidebarTreeSyncKey(right));
  });

  it("侧边栏树语义变化时应产生不同 sync key", () => {
    const left = [buildNode({ title: "标题 A" })];
    const right = [buildNode({ title: "标题 B" })];

    expect(buildSidebarTreeSyncKey(left)).not.toBe(buildSidebarTreeSyncKey(right));
  });

  it("相同附件列表内容应产生相同 sync key", () => {
    const left = [buildAsset()];
    const right = [buildAsset()];

    expect(buildMediaAssetListSyncKey(left)).toBe(buildMediaAssetListSyncKey(right));
  });

  it("附件列表语义变化时应产生不同 sync key", () => {
    const left = [buildAsset({ file_name: "a.png" })];
    const right = [buildAsset({ file_name: "b.png" })];

    expect(buildMediaAssetListSyncKey(left)).not.toBe(buildMediaAssetListSyncKey(right));
  });
});
