/**
 * 本地连通性冒烟：Prisma(MySQL) + Redis
 * 用法：npm run smoke:connect
 */
import { loadDevEnv } from "../lib/dev-env";

loadDevEnv();

import { prisma } from "../lib/prisma";
import { ensureRedisReady, getRedis } from "../lib/redis";

async function main() {
  if (!process.env.DATABASE_URL) {
    throw new Error("DATABASE_URL 未配置，请检查 .env");
  }
  if (!process.env.REDIS_URL) {
    throw new Error("REDIS_URL 未配置，请检查 .env");
  }

  await prisma.$connect();
  const userCount = await prisma.sysUser.count();
  console.log(`prisma ok（sys_user 行数: ${userCount}）`);

  await ensureRedisReady(8, 500);
  const pong = await getRedis().ping();
  console.log(`redis ok（ping: ${pong}）`);
}

main()
  .catch((error) => {
    console.error("连通性测试失败:", error);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
    getRedis().disconnect();
  });
