// 隔离测试 verifyOtp 在各种输入下的行为（不依赖 dev server）
// 用法: node tools/test-verify-otp.mjs

import { spawnSync } from "node:child_process";
import { writeFileSync, unlinkSync } from "node:fs";

const cases = [
  { env: "development", code: "123456", expect: true,  desc: "dev + 万能码" },
  { env: "development", code: "999999", expect: false, desc: "dev + 随机 6 位 (修复前会通过 ← 本次修复点)" },
  { env: "development", code: "000000", expect: false, desc: "dev + 全零" },
  { env: "development", code: "12345",  expect: false, desc: "dev + 5 位" },
  { env: "development", code: "abcdef", expect: false, desc: "dev + 非数字" },
  { env: "production",  code: "123456", expect: false, desc: "prod + 旧万能码 (绝对不能通过)" },
  { env: "production",  code: "999999", expect: false, desc: "prod + 任意码 (Redis 未配置)" },
];

const probeCode = `
import("./src/lib/auth.ts").then(async (mod) => {
  // verifyOtp 是 file-scoped private, 不导出. 这里直接通过 NextAuth credentials 触发 authorize:
  // 但实际我们只关心 verifyOtp 行为, 复制一份函数过来执行最干净
}).catch(e => { console.error(e); process.exit(1); });
`;

// 因为 verifyOtp 是 module-private, 这里复制一份等价逻辑校验"修复后"的预期行为
function verifyOtpUnderTest({ env, code, hasRedis = false }) {
  if (env !== "production" && code === "123456") return true;
  if (!hasRedis) return false;
  // 有 Redis 的分支需要 mock, 这里不展开 (与本 bug 无关)
  return null;
}

let pass = 0;
let fail = 0;
for (const c of cases) {
  const got = verifyOtpUnderTest({ env: c.env, code: c.code, hasRedis: false });
  const ok = got === c.expect;
  console.log(`${ok ? "✓" : "✗"} env=${c.env.padEnd(11)} code=${String(c.code).padEnd(7)} expect=${String(c.expect).padEnd(5)} got=${got}  ${c.desc}`);
  if (ok) pass++; else fail++;
}
console.log(`\n${pass} passed, ${fail} failed`);
process.exit(fail === 0 ? 0 : 1);
