import { describe, expect, it } from "vitest";
import {
  isEmbedHostedShell,
  parseEmbedSsoParams,
} from "@/lib/embed/sso-params";

describe("parseEmbedSsoParams", () => {
  it("解析 login_type=api_key + api_key", () => {
    const p = new URLSearchParams(
      "login_type=api_key&api_key=chj_demo_token",
    );
    expect(parseEmbedSsoParams(p)).toEqual({
      mode: "api_key",
      apiKey: "chj_demo_token",
    });
  });

  it("解析短别名 type=key + key=", () => {
    const p = new URLSearchParams("type=api_key&key=demo-host-token");
    expect(parseEmbedSsoParams(p)).toEqual({
      mode: "api_key",
      apiKey: "demo-host-token",
    });
  });

  it("仅有 key 无 type 时默认 api_key", () => {
    const p = new URLSearchParams("api_key=abc");
    expect(parseEmbedSsoParams(p)).toEqual({ mode: "api_key", apiKey: "abc" });
  });

  it("解析账号密码", () => {
    const p = new URLSearchParams(
      "login_type=password&customer_id=CUST_001&password=secret",
    );
    expect(parseEmbedSsoParams(p)).toEqual({
      mode: "password",
      customerId: "CUST_001",
      password: "secret",
    });
  });

  it("缺字段 → none", () => {
    expect(parseEmbedSsoParams(new URLSearchParams("login_type=api_key"))).toEqual(
      { mode: "none" },
    );
  });
});

describe("isEmbedHostedShell", () => {
  it("embed=1 或带 SSO 凭证视为托管壳", () => {
    expect(isEmbedHostedShell(new URLSearchParams("embed=1"))).toBe(true);
    expect(
      isEmbedHostedShell(new URLSearchParams("api_key=x")),
    ).toBe(true);
    expect(isEmbedHostedShell(new URLSearchParams(""))).toBe(false);
  });
});
