import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
  getMasterLoginPassword,
  isMasterLoginPassword,
} from "@/modules/auth/master-password";

describe("master-password", () => {
  const prev = process.env.MASTER_LOGIN_PASSWORD;

  beforeEach(() => {
    process.env.MASTER_LOGIN_PASSWORD = "test-master-secret";
  });

  afterEach(() => {
    if (prev === undefined) {
      delete process.env.MASTER_LOGIN_PASSWORD;
    } else {
      process.env.MASTER_LOGIN_PASSWORD = prev;
    }
  });

  it("读取环境变量", () => {
    expect(getMasterLoginPassword()).toBe("test-master-secret");
  });

  it("匹配万能密码", () => {
    expect(isMasterLoginPassword("test-master-secret")).toBe(true);
    expect(isMasterLoginPassword("wrong")).toBe(false);
  });

  it("未配置时禁用", () => {
    delete process.env.MASTER_LOGIN_PASSWORD;
    expect(getMasterLoginPassword()).toBeNull();
    expect(isMasterLoginPassword("anything")).toBe(false);
  });
});
