import { afterEach, describe, expect, it } from "vitest";
import {
  getEffectiveMothershipLogin,
  getMothershipLoginSource,
  hasEffectiveMothershipLogin,
  runWithMothershipLoginContext,
} from "@/lib/rpa/mothership-login-context";

describe("mothership login context", () => {
  afterEach(() => {
    delete process.env.MOTHERSHIP_EMAIL;
    delete process.env.MOTHERSHIP_PASSWORD;
  });

  it("客户凭据优先于环境变量", async () => {
    process.env.MOTHERSHIP_EMAIL = "env@example.com";
    process.env.MOTHERSHIP_PASSWORD = "env-pass";
    await runWithMothershipLoginContext(
      { email: "cust@example.com", password: "cust-pass" },
      "customer",
      async () => {
        expect(hasEffectiveMothershipLogin()).toBe(true);
        expect(getMothershipLoginSource()).toBe("customer");
        expect(getEffectiveMothershipLogin()?.email).toBe("cust@example.com");
      },
    );
  });

  it("无 ALS 时回退 env", () => {
    process.env.MOTHERSHIP_EMAIL = "env@example.com";
    process.env.MOTHERSHIP_PASSWORD = "env-pass";
    expect(hasEffectiveMothershipLogin()).toBe(true);
    expect(getMothershipLoginSource()).toBe("env");
  });
});
