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

238 lines
6.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { app, mock, assert } = require('egg-mock/bootstrap');
const { Op } = require('sequelize');
const createFakeRedis = require('../../utils/fake-redis');
const { HEADER: INTERNAL } = require('../../utils/internal');
/** 用户 CRUD 属「内部接口」:请求都带上运维令牌 */
const http = {
get: url => app.httpRequest().get(url).set(INTERNAL),
post: url => app.httpRequest().post(url).set(INTERNAL),
put: url => app.httpRequest().put(url).set(INTERNAL),
delete: url => app.httpRequest().delete(url).set(INTERNAL),
};
/** 包装成「Sequelize 实例」的样子;data 里自带的 update / toJSON 优先 */
const instance = (data) => Object.assign(
{
toJSON: () => data,
update: async (fields) => Object.assign(data, fields),
},
data,
);
describe('test/app/controller/user.test.js', () => {
const row = { id: 1, username: 'zhangsan', nickname: '张三', phone: '13800000000', gender: 1, status: 1 };
let fakeRedis;
beforeEach(() => {
fakeRedis = createFakeRedis();
mock(app, 'redis', fakeRedis.client);
});
it('GET /api/users/:id 未命中缓存时回源数据库,并写入缓存', async () => {
let queries = 0;
mock(app, 'model', {
User: {
async findByPk(id) {
queries++;
assert(id === 1);
return instance(row);
},
},
});
const res = await http.get('/api/users/1').expect(200);
assert(res.body.data.nickname === '张三');
assert(queries === 1);
assert(fakeRedis.store.get('packs:user:1'), '详情应写入缓存');
// 第二次请求命中缓存,不再回源数据库
await http.get('/api/users/1').expect(200);
assert(queries === 1);
});
it('Redis 不可用时详情接口降级回源数据库,不报错', async () => {
mock(app, 'redis', {
async get() {
throw new Error('connect ECONNREFUSED 127.0.0.1:6379');
},
async setex() {
throw new Error('connect ECONNREFUSED 127.0.0.1:6379');
},
});
mock(app, 'model', {
User: {
async findByPk() {
return instance(row);
},
},
});
const res = await http.get('/api/users/1').expect(200);
assert(res.body.data.nickname === '张三');
});
it('GET /api/users/:id 用户不存在时返回 404', async () => {
mock(app, 'model', {
User: {
async findByPk() {
return null;
},
},
});
await http.get('/api/users/99').expect(404);
});
it('GET /api/users/:id id 非法时返回 400 且不查库', async () => {
mock(app, 'model', {
User: {
async findByPk() {
assert.fail('id 非法时不应该访问数据库');
},
},
});
await http.get('/api/users/abc').expect(400);
});
it('GET /api/users 支持 keyword + status 过滤与分页', async () => {
mock(app, 'model', {
User: {
async findAndCountAll(options) {
assert(options.limit === 10);
assert(options.offset === 10);
assert.deepStrictEqual(options.order, [[ 'id', 'DESC' ]]);
assert.strictEqual(options.where.status, 1);
assert.deepStrictEqual(options.where[Op.or], [
{ nickname: { [Op.like]: '%张%' } },
{ username: { [Op.like]: '%张%' } },
{ phone: { [Op.like]: '%张%' } },
{ email: { [Op.like]: '%张%' } },
]);
return { rows: [ instance(row) ], count: 1 };
},
},
});
const res = await http
.get(`/api/users?page=2&pageSize=10&status=1&keyword=${encodeURIComponent('张')}`)
.expect(200);
assert(res.body.data.total === 1);
assert(res.body.data.page === 2);
assert(res.body.data.list[0].nickname === '张三');
});
it('POST /api/users 缺少 nickname 时返回 422', async () => {
await http.post('/api/users').send({ phone: '13900000000' }).expect(422);
});
it('POST /api/users 创建成功返回 201', async () => {
mock(app, 'model', {
User: {
async findOne() {
return null;
},
async create(data) {
assert(data.nickname === '李四');
assert(data.gender === 2);
return instance(Object.assign({ id: 7 }, data));
},
},
});
const res = await http
.post('/api/users')
.send({ nickname: '李四', gender: '2' })
.expect(201);
assert(res.body.data.id === 7);
assert(res.body.data.gender === 2, 'gender 应归一化为数字');
});
it('POST /api/users 用户名已存在时返回 409', async () => {
mock(app, 'model', {
User: {
async findOne() {
return instance(row);
},
async create() {
assert.fail('唯一性冲突时不应该创建');
},
},
});
const res = await http
.post('/api/users')
.send({ nickname: '张三', username: 'zhangsan' })
.expect(409);
// 统一错误响应中间件:4xx 也返回 JSON
assert(res.body.ok === false);
assert(res.body.message.includes('username 已存在'), '应返回冲突原因');
});
it('PUT /api/users/:id 更新成功后清除该用户缓存', async () => {
fakeRedis.store.set('packs:user:1', JSON.stringify(row));
let updated = null;
const data = Object.assign({}, row);
mock(app, 'model', {
User: {
async findOne() {
return null;
},
async findByPk() {
return {
update: async fields => {
updated = fields;
Object.assign(data, fields);
},
toJSON: () => data,
};
},
},
});
const res = await http
.put('/api/users/1')
.send({ nickname: '张三丰' })
.expect(200);
assert(res.body.data.nickname === '张三丰');
assert.deepStrictEqual(updated, { nickname: '张三丰' });
assert(!fakeRedis.store.has('packs:user:1'), '更新后缓存应失效');
});
it('DELETE /api/users/:id 软删除并清除缓存', async () => {
fakeRedis.store.set('packs:user:1', JSON.stringify(row));
mock(app, 'model', {
User: {
async destroy(options) {
assert.deepStrictEqual(options.where, { id: 1 });
return 1;
},
},
});
await http.delete('/api/users/1').expect(200);
assert(!fakeRedis.store.has('packs:user:1'));
});
it('DELETE /api/users/:id 记录不存在返回 404', async () => {
mock(app, 'model', {
User: {
async destroy() {
return 0;
},
},
});
await http.delete('/api/users/1').expect(404);
});
});