Files
Web_BLV_OA_Exam_Prod/test/user-default-group.test.ts
XuJiacheng eb4504960e feat: 更新构建流程,添加 API 构建脚本和 SQL 文件复制脚本
- 修改 package.json,更新构建命令,添加 postbuild 脚本以复制 init.sql 文件。
- 新增 scripts/build-api.mjs,使用 esbuild 构建 API 代码。
- 新增 scripts/copy-init-sql.mjs,复制数据库初始化 SQL 文件到构建输出目录。
- 在 SubjectSelectionPage 组件中添加 totalScore 属性,增加历史最高分状态显示功能。
- 在 ExamSubjectPage 和 QuestionManagePage 中优化判断题答案处理逻辑。
- 在 OptionList 组件中将判断题选项文本从 'T' 和 'F' 改为 '对' 和 '错'。
- 在 QuizFooter 组件中调整样式,增加按钮和文本的可读性。
- 新增用户默认组测试用例,验证新用户创建后自动加入“全体用户”系统组。
- 新增 tsconfig.api.json,配置 API 相关 TypeScript 编译选项。
- 移除 vite.config.ts 中的 global 定义。
2025-12-30 20:33:14 +08:00

79 lines
2.8 KiB
TypeScript
Raw 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.
import test from 'node:test';
import assert from 'node:assert/strict';
process.env.NODE_ENV = 'test';
process.env.DB_PATH = ':memory:';
const jsonFetch = async (
baseUrl: string,
path: string,
options?: { method?: string; body?: unknown },
) => {
const res = await fetch(`${baseUrl}${path}`, {
method: options?.method ?? 'GET',
headers: options?.body ? { 'Content-Type': 'application/json' } : undefined,
body: options?.body ? JSON.stringify(options.body) : undefined,
});
const text = await res.text();
let json: any = null;
try {
json = text ? JSON.parse(text) : null;
} catch {
json = null;
}
return { status: res.status, json, text };
};
test('新用户创建后默认加入“全体用户”系统组(含 validate 自动创建与管理员导入)', async () => {
const { initDatabase } = await import('../api/database');
await initDatabase();
const { app } = await import('../api/server');
const server = app.listen(0);
try {
const addr = server.address();
assert.ok(addr && typeof addr === 'object');
const baseUrl = `http://127.0.0.1:${addr.port}`;
// 1) validate 自动创建用户
const created = await jsonFetch(baseUrl, '/api/users/validate', {
method: 'POST',
body: { name: '默认组用户', phone: '13800139001', password: '' },
});
assert.equal(created.status, 200);
assert.equal(created.json?.success, true);
const userId = created.json?.data?.id as string;
assert.ok(userId);
const list1 = await jsonFetch(baseUrl, '/api/admin/users?page=1&limit=20');
assert.equal(list1.status, 200);
assert.equal(list1.json?.success, true);
const row1 = (list1.json?.data as any[]).find((u) => u.id === userId);
assert.ok(row1);
assert.ok(Array.isArray(row1.groups));
assert.ok(row1.groups.some((g: any) => g.isSystem === 1 || g.isSystem === true));
// 2) 管理员导入用户(模拟 Excel走 importUsers 的解析逻辑不方便,这里直接调用 createUser 接口覆盖管理端创建路径)
const createdAdmin = await jsonFetch(baseUrl, '/api/admin/users', {
method: 'POST',
body: { name: '管理员创建用户', phone: '13800139002', password: '', groupIds: [] },
});
assert.equal(createdAdmin.status, 200);
assert.equal(createdAdmin.json?.success, true);
const userId2 = createdAdmin.json?.data?.id as string;
assert.ok(userId2);
const list2 = await jsonFetch(baseUrl, '/api/admin/users?page=1&limit=50');
assert.equal(list2.status, 200);
assert.equal(list2.json?.success, true);
const row2 = (list2.json?.data as any[]).find((u) => u.id === userId2);
assert.ok(row2);
assert.ok(Array.isArray(row2.groups));
assert.ok(row2.groups.some((g: any) => g.isSystem === 1 || g.isSystem === true));
} finally {
server.close();
}
});