import { describe, expect, it } from "vitest";
import type {
  TreeProjectionItemBase,
  WorkspaceSource,
} from "@/lib/tree-protocol";

describe("tree-protocol", () => {
  it("冻结 WorkspaceSource 合同字段", () => {
    const source: WorkspaceSource = {
      sourceKind: "convex_workspace",
      rootUri: "convex://workspace/ws_1",
      workspaceId: "ws_1",
      capabilities: [
        "load-snapshot",
        "watch",
        "preflight-command",
        "execute-command",
        "resolve-page-aggregate",
      ],
    };

    expect(source.sourceKind).toBe("convex_workspace");
    expect(source.rootUri).toContain("workspace/ws_1");
    expect(source.capabilities).toContain("load-snapshot");
  });

  it("冻结共享 tree projection 字段与 file_tree 扩展语义", () => {
    const item: TreeProjectionItemBase = {
      nodeId: "doc_1",
      parentNodeId: null,
      nodeType: "page",
      projectionKind: "file_tree",
      title: "页面 1",
      depth: 0,
      position: 1,
      childCount: 2,
      expandable: true,
      expandedByDefault: true,
      capabilities: ["expand", "open", "drag", "drop", "select", "create-child"],
      resourceMeta: {
        resourceKind: "document",
        documentId: "doc_1",
        assetKind: "file",
        iconHint: "page",
        extra: {
          source: {
            sourceKind: "convex_workspace",
            sourceUri: "convex://workspace/ws_1",
            relativePath: "docs/doc_1.md",
            storageIdentity: "doc_1",
            operationProfile: "convex_workspace",
          },
        },
      },
      iconHint: "page",
    };

    expect(item.resourceMeta.resourceKind).toBe("document");
    expect(item.resourceMeta.assetKind).toBe("file");
    expect(item.resourceMeta.extra?.source).toMatchObject({
      sourceKind: "convex_workspace",
      sourceUri: "convex://workspace/ws_1",
      relativePath: "docs/doc_1.md",
      storageIdentity: "doc_1",
      operationProfile: "convex_workspace",
    });
    expect(item.capabilities).toContain("expand");
    expect(item.expandable).toBe(true);
    expect(item.iconHint).toBe("page");
  });
});
