liufumingren bd2363bf00 chore: 初始化仓库(backend / frontend / extension 三块)
- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权;
  已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF),
  开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。
  单测 91 个通过(不需要本机数据库)。
- frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 /
  首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。
- extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。
- 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、
  .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
2026-10-17 08:50:32 +08:00

52 lines
1.6 KiB
JavaScript

'use strict';
const { app, mock } = require('egg-mock/bootstrap');
const createFakeRedis = require('../../utils/fake-redis');
describe('test/app/controller/health.test.js', () => {
beforeEach(() => {
mock(app, 'redis', createFakeRedis().client);
});
it('MySQL 与 Redis 正常时返回 200', async () => {
mock(app, 'model', {
async authenticate() {
return true;
},
});
const res = await app.httpRequest().get('/health').expect(200);
if (res.body.ok !== true) throw new Error('health 应为 ok');
if (res.body.dependencies.mysql.ok !== true) throw new Error('mysql 应可用');
if (res.body.dependencies.redis.ok !== true) throw new Error('redis 应可用');
});
it('MySQL 不可用时返回 503', async () => {
mock(app, 'model', {
async authenticate() {
throw new Error("Access denied for user 'packservice'@'localhost'");
},
});
const res = await app.httpRequest().get('/health').expect(503);
if (res.body.dependencies.mysql.ok !== false) throw new Error('mysql 应标记为不可用');
});
it('Redis 不可用时返回 503', async () => {
mock(app, 'model', {
async authenticate() {
return true;
},
});
mock(app, 'redis', {
async ping() {
throw new Error('NOAUTH Authentication required.');
},
});
const res = await app.httpRequest().get('/health').expect(503);
if (res.body.ok !== false) throw new Error('health 应为 ok:false');
if (res.body.dependencies.redis.ok !== false) throw new Error('redis 应标记为不可用');
});
});