import { describe, expect, it, vi } from "vitest";
import { persistPageTitleAndNotifyDocumentsChanged } from "@/components/editor/document-content";

describe("persistPageTitleAndNotifyDocumentsChanged", () => {
  it("标题提交成功后应广播 documents-changed", async () => {
    const persistTitleCommand = vi.fn(async () => ({ ok: true as const }));
    const emitDocumentsChanged = vi.fn();

    const result = await persistPageTitleAndNotifyDocumentsChanged({
      documentId: "doc-1",
      workspaceId: "ws-1",
      title: "  新标题  ",
      persistTitleCommand,
      notifyDocumentsChanged: emitDocumentsChanged,
    });

    expect(result).toBe("新标题");
    expect(persistTitleCommand).toHaveBeenCalledWith({
      documentId: "doc-1",
      workspaceId: "ws-1",
      title: "新标题",
    });
    expect(emitDocumentsChanged).toHaveBeenCalledWith("doc-1");
  });
});
