import { beforeEach, describe, expect, it, vi } from "vitest";

const mockSignOnlyOfficeConfig = vi.fn();

vi.mock("@/lib/api-utils", () => ({
  apiErrorResponse: vi.fn((message: string, status = 500, details?: unknown) => ({
    message,
    status,
    details,
  })),
}));

vi.mock("@/lib/onlyoffice/rust-adapter", () => ({
  signOnlyOfficeConfig: mockSignOnlyOfficeConfig,
}));

describe("/api/onlyoffice/sign route", () => {
  beforeEach(() => {
    mockSignOnlyOfficeConfig.mockReset();
  });

  it("delegates signing to rust adapter", async () => {
    const { POST } = await import("./route");
    mockSignOnlyOfficeConfig.mockResolvedValue({
      token: "token_all",
      documentToken: "token_doc",
      editorConfigToken: "token_editor",
    });

    const response = await POST(
      new Request("http://127.0.0.1:3001/api/onlyoffice/sign", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({
          config: {
            document: { title: "A" },
            editorConfig: { mode: "edit" },
          },
        }),
      }),
    );
    const payload = await response.json();

    expect(payload).toEqual({
      token: "token_all",
      documentToken: "token_doc",
      editorConfigToken: "token_editor",
    });
    expect(mockSignOnlyOfficeConfig).toHaveBeenCalledTimes(1);
  });
});
