// 真实跑 otp-store 的 store/consume 逻辑
import { storeOtp, consumeOtp } from "../src/lib/otp-store.ts";

const phone = "13900000000";
const cases = [];

await storeOtp(phone, "654321");
cases.push(["wrong code 111111 → false", await consumeOtp(phone, "111111"), false]);
cases.push(["right code 654321 → true",  await consumeOtp(phone, "654321"), true]);
cases.push(["replay 654321 → false",      await consumeOtp(phone, "654321"), false]);
cases.push(["never-stored phone → false", await consumeOtp("13800000001", "654321"), false]);

// 过期测试
await storeOtp(phone, "999000");
// 直接改 globalThis 里的 expiresAt 模拟过期
globalThis.__otpMemStore.set(phone, { code: "999000", expiresAt: Date.now() - 1 });
cases.push(["expired code → false", await consumeOtp(phone, "999000"), false]);

let pass = 0, fail = 0;
for (const [desc, got, expect] of cases) {
  const ok = got === expect;
  console.log(`${ok ? "✓" : "✗"} ${desc.padEnd(35)} got=${got}`);
  ok ? pass++ : fail++;
}
console.log(`\n${pass} passed, ${fail} failed`);
process.exit(fail === 0 ? 0 : 1);
