import { describe, expect, it } from "vitest";
import {
  buildOnlyOfficeCallbackUrl,
  buildOnlyOfficeForcesaveUrl,
  buildOnlyOfficeOpenFileId,
  docTypeFromExt,
  resolveOnlyOfficeDocumentUrl,
} from "@/lib/onlyoffice/client-session";

describe("onlyoffice client session helpers", () => {
  it("docTypeFromExt maps office extensions", () => {
    expect(docTypeFromExt("docx")).toBe("word");
    expect(docTypeFromExt("xlsx")).toBe("cell");
    expect(docTypeFromExt("pptx")).toBe("slide");
    expect(docTypeFromExt("pdf")).toBe("pdf");
  });

  it("resolveOnlyOfficeDocumentUrl wraps signed url with proxy", () => {
    const result = resolveOnlyOfficeDocumentUrl({
      effectiveFileUrl: "https://public.example.com/storage/v1/object/sign/documents/a.docx?token=abc",
      proxyOrigin: "https://app.example.com",
      storageHostOverride: "",
      runtimeSupabaseUrl: "https://public.example.com",
      useConvex: false,
    });
    expect(result).toContain("https://app.example.com/api/onlyoffice/proxy?u=");
  });

  it("buildOnlyOfficeCallbackUrl keeps asset and user", () => {
    const result = buildOnlyOfficeCallbackUrl({
      callbackOrigin: "http://host.docker.internal:3000",
      proxyOrigin: "",
      windowOrigin: "https://app.example.com",
      assetId: "asset_1",
      userId: "user_1",
    });
    expect(result).toBe(
      "http://host.docker.internal:3000/api/onlyoffice/callback?assetId=asset_1&userId=user_1",
    );
  });

  it("buildOnlyOfficeForcesaveUrl keeps key", () => {
    const result = buildOnlyOfficeForcesaveUrl({
      windowOrigin: "https://app.example.com",
      assetId: "asset_1",
      key: "doc_key_1",
    });
    expect(result).toBe(
      "https://app.example.com/api/onlyoffice/forcesave?assetId=asset_1&key=doc_key_1",
    );
  });

  it("buildOnlyOfficeOpenFileId prefers asset id", () => {
    expect(
      buildOnlyOfficeOpenFileId({
        assetId: "asset_1",
        docKey: "doc_key_1",
        resolvedFileUrl: "https://app.example.com/file.docx",
        fileName: "file.docx",
      }),
    ).toBe("asset_1");
  });
});
