import { describe, expect, it } from "vitest";
import {
  FLOCK_RECORDING_TEST_ACCOUNT,
  getFlockQuotesPerEmail,
  getFlockTestAccount,
  isFlockFreshSessionPerQuote,
  isFlockRandomAccountPerQuote,
  isFlockWorkerReuseSession,
  shouldLoadFlockStorageState,
} from "@/lib/flock/env";

describe("getFlockTestAccount", () => {
  it("默认使用录制验证账号", () => {
    const prev = {
      random: process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE,
      fresh: process.env.FLOCK_FRESH_SESSION_PER_QUOTE,
      email: process.env.FLOCK_TEST_EMAIL,
      phone: process.env.FLOCK_TEST_PHONE,
    };
    delete process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE;
    delete process.env.FLOCK_FRESH_SESSION_PER_QUOTE;
    delete process.env.FLOCK_TEST_EMAIL;
    delete process.env.FLOCK_TEST_PHONE;

    const account = getFlockTestAccount();
    expect(account.email).toBe(FLOCK_RECORDING_TEST_ACCOUNT.email);
    expect(account.phone).toBe(FLOCK_RECORDING_TEST_ACCOUNT.phone);

    process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE = prev.random;
    process.env.FLOCK_FRESH_SESSION_PER_QUOTE = prev.fresh;
    process.env.FLOCK_TEST_EMAIL = prev.email;
    process.env.FLOCK_TEST_PHONE = prev.phone;
  });
});

describe("getFlockQuotesPerEmail", () => {
  it("未设置时不限次数", () => {
    const prev = process.env.FLOCK_QUOTES_PER_EMAIL;
    delete process.env.FLOCK_QUOTES_PER_EMAIL;
    expect(getFlockQuotesPerEmail()).toBe(Number.POSITIVE_INFINITY);
    process.env.FLOCK_QUOTES_PER_EMAIL = prev;
  });

  it("解析每邮箱查价次数", () => {
    const prev = process.env.FLOCK_QUOTES_PER_EMAIL;
    process.env.FLOCK_QUOTES_PER_EMAIL = "3";
    expect(getFlockQuotesPerEmail()).toBe(3);
    process.env.FLOCK_QUOTES_PER_EMAIL = prev;
  });
});

describe("isFlockWorkerReuseSession", () => {
  it("配置 FLOCK_QUOTES_PER_EMAIL 时默认开启", () => {
    const prev = {
      perEmail: process.env.FLOCK_QUOTES_PER_EMAIL,
      reuse: process.env.FLOCK_WORKER_REUSE_SESSION,
    };
    process.env.FLOCK_QUOTES_PER_EMAIL = "3";
    delete process.env.FLOCK_WORKER_REUSE_SESSION;
    expect(isFlockWorkerReuseSession()).toBe(true);
    process.env.FLOCK_QUOTES_PER_EMAIL = prev.perEmail;
    process.env.FLOCK_WORKER_REUSE_SESSION = prev.reuse;
  });

  it("显式 false 时关闭", () => {
    const prev = {
      perEmail: process.env.FLOCK_QUOTES_PER_EMAIL,
      reuse: process.env.FLOCK_WORKER_REUSE_SESSION,
    };
    process.env.FLOCK_QUOTES_PER_EMAIL = "3";
    process.env.FLOCK_WORKER_REUSE_SESSION = "false";
    expect(isFlockWorkerReuseSession()).toBe(false);
    process.env.FLOCK_QUOTES_PER_EMAIL = prev.perEmail;
    process.env.FLOCK_WORKER_REUSE_SESSION = prev.reuse;
  });
});

describe("flock env defaults (recording mode)", () => {
  it("默认不随机账号", () => {
    const prev = process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE;
    delete process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE;
    delete process.env.FLOCK_FRESH_SESSION_PER_QUOTE;
    expect(isFlockRandomAccountPerQuote()).toBe(false);
    process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE = prev;
  });

  it("无痕模式不加载 storage", () => {
    const prev = {
      fresh: process.env.FLOCK_FRESH_SESSION_PER_QUOTE,
      load: process.env.FLOCK_LOAD_STORAGE_STATE,
    };
    process.env.FLOCK_FRESH_SESSION_PER_QUOTE = "true";
    delete process.env.FLOCK_LOAD_STORAGE_STATE;
    expect(isFlockFreshSessionPerQuote()).toBe(true);
    expect(shouldLoadFlockStorageState(true)).toBe(false);
    process.env.FLOCK_FRESH_SESSION_PER_QUOTE = prev.fresh;
    process.env.FLOCK_LOAD_STORAGE_STATE = prev.load;
  });

  it("默认加载 storage（若文件存在）", () => {
    const prev = {
      load: process.env.FLOCK_LOAD_STORAGE_STATE,
      fresh: process.env.FLOCK_FRESH_SESSION_PER_QUOTE,
      random: process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE,
    };
    delete process.env.FLOCK_LOAD_STORAGE_STATE;
    delete process.env.FLOCK_FRESH_SESSION_PER_QUOTE;
    delete process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE;
    expect(shouldLoadFlockStorageState(true)).toBe(true);
    expect(shouldLoadFlockStorageState(false)).toBe(false);
    process.env.FLOCK_LOAD_STORAGE_STATE = prev.load;
    process.env.FLOCK_FRESH_SESSION_PER_QUOTE = prev.fresh;
    process.env.FLOCK_RANDOM_ACCOUNT_PER_QUOTE = prev.random;
  });
});
