统计面板继续迭代
This commit is contained in:
166
test/admin-task-stats.test.ts
Normal file
166
test/admin-task-stats.test.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
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('管理员任务分页统计接口返回结构正确', async () => {
|
||||
const { initDatabase, run } = 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}`;
|
||||
|
||||
const now = Date.now();
|
||||
const userA = { id: randomUUID(), name: '测试甲', phone: '13800138001', password: '' };
|
||||
const userB = { id: randomUUID(), name: '测试乙', phone: '13800138002', password: '' };
|
||||
const subjectId = randomUUID();
|
||||
const historyTaskId = randomUUID();
|
||||
const upcomingTaskId = randomUUID();
|
||||
const activeTaskId = randomUUID();
|
||||
|
||||
await run(`INSERT INTO users (id, name, phone, password) VALUES (?, ?, ?, ?)`, [
|
||||
userA.id,
|
||||
userA.name,
|
||||
userA.phone,
|
||||
userA.password,
|
||||
]);
|
||||
await run(`INSERT INTO users (id, name, phone, password) VALUES (?, ?, ?, ?)`, [
|
||||
userB.id,
|
||||
userB.name,
|
||||
userB.phone,
|
||||
userB.password,
|
||||
]);
|
||||
|
||||
await run(
|
||||
`INSERT INTO exam_subjects (id, name, type_ratios, category_ratios, total_score, duration_minutes)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
subjectId,
|
||||
'测试科目',
|
||||
JSON.stringify({ single: 100 }),
|
||||
JSON.stringify({ 通用: 100 }),
|
||||
100,
|
||||
60,
|
||||
],
|
||||
);
|
||||
|
||||
await run(
|
||||
`INSERT INTO questions (id, content, type, options, answer, score, category)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[randomUUID(), '题目1', 'single', JSON.stringify(['A', 'B']), 'A', 5, '通用'],
|
||||
);
|
||||
await run(
|
||||
`INSERT INTO questions (id, content, type, options, answer, score, category)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[randomUUID(), '题目2', 'single', JSON.stringify(['A', 'B']), 'B', 5, '数学'],
|
||||
);
|
||||
|
||||
const historyStartAt = new Date(now - 3 * 24 * 60 * 60 * 1000).toISOString();
|
||||
const historyEndAt = new Date(now - 2 * 24 * 60 * 60 * 1000).toISOString();
|
||||
const upcomingStartAt = new Date(now + 2 * 24 * 60 * 60 * 1000).toISOString();
|
||||
const upcomingEndAt = new Date(now + 3 * 24 * 60 * 60 * 1000).toISOString();
|
||||
const activeStartAt = new Date(now - 1 * 24 * 60 * 60 * 1000).toISOString();
|
||||
const activeEndAt = new Date(now + 1 * 24 * 60 * 60 * 1000).toISOString();
|
||||
|
||||
await run(
|
||||
`INSERT INTO exam_tasks (id, name, subject_id, start_at, end_at, selection_config)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
[historyTaskId, '历史任务', subjectId, historyStartAt, historyEndAt, null],
|
||||
);
|
||||
await run(
|
||||
`INSERT INTO exam_tasks (id, name, subject_id, start_at, end_at, selection_config)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
[upcomingTaskId, '未开始任务', subjectId, upcomingStartAt, upcomingEndAt, null],
|
||||
);
|
||||
await run(
|
||||
`INSERT INTO exam_tasks (id, name, subject_id, start_at, end_at, selection_config)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
[activeTaskId, '进行中任务', subjectId, activeStartAt, activeEndAt, null],
|
||||
);
|
||||
|
||||
const linkUserToTask = async (taskId: string, userId: string) => {
|
||||
await run(`INSERT INTO exam_task_users (id, task_id, user_id) VALUES (?, ?, ?)`, [
|
||||
randomUUID(),
|
||||
taskId,
|
||||
userId,
|
||||
]);
|
||||
};
|
||||
|
||||
await linkUserToTask(historyTaskId, userA.id);
|
||||
await linkUserToTask(historyTaskId, userB.id);
|
||||
await linkUserToTask(upcomingTaskId, userA.id);
|
||||
await linkUserToTask(activeTaskId, userA.id);
|
||||
|
||||
await run(
|
||||
`INSERT INTO quiz_records (id, user_id, subject_id, task_id, total_score, correct_count, total_count, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[randomUUID(), userA.id, subjectId, historyTaskId, 90, 18, 20, new Date(now - 2.5 * 24 * 60 * 60 * 1000).toISOString()],
|
||||
);
|
||||
|
||||
const overview = await jsonFetch(baseUrl, '/api/admin/dashboard/overview');
|
||||
assert.equal(overview.status, 200);
|
||||
assert.equal(overview.json?.success, true);
|
||||
assert.equal(typeof overview.json?.data?.totalUsers, 'number');
|
||||
assert.equal(typeof overview.json?.data?.activeSubjectCount, 'number');
|
||||
assert.ok(Array.isArray(overview.json?.data?.questionCategoryStats));
|
||||
assert.equal(typeof overview.json?.data?.taskStatusDistribution?.completed, 'number');
|
||||
assert.equal(typeof overview.json?.data?.taskStatusDistribution?.ongoing, 'number');
|
||||
assert.equal(typeof overview.json?.data?.taskStatusDistribution?.notStarted, 'number');
|
||||
|
||||
const history = await jsonFetch(baseUrl, '/api/admin/tasks/history-stats?page=1&limit=5');
|
||||
assert.equal(history.status, 200);
|
||||
assert.equal(history.json?.success, true);
|
||||
assert.ok(Array.isArray(history.json?.data));
|
||||
assert.equal(history.json?.pagination?.page, 1);
|
||||
assert.equal(history.json?.pagination?.limit, 5);
|
||||
assert.equal(history.json?.pagination?.total, 1);
|
||||
assert.equal(history.json?.pagination?.pages, 1);
|
||||
assert.equal(history.json?.data?.[0]?.taskName, '历史任务');
|
||||
|
||||
const upcoming = await jsonFetch(baseUrl, '/api/admin/tasks/upcoming-stats?page=1&limit=5');
|
||||
assert.equal(upcoming.status, 200);
|
||||
assert.equal(upcoming.json?.success, true);
|
||||
assert.ok(Array.isArray(upcoming.json?.data));
|
||||
assert.equal(upcoming.json?.pagination?.page, 1);
|
||||
assert.equal(upcoming.json?.pagination?.limit, 5);
|
||||
assert.equal(upcoming.json?.pagination?.total, 1);
|
||||
assert.equal(upcoming.json?.pagination?.pages, 1);
|
||||
assert.equal(upcoming.json?.data?.[0]?.taskName, '未开始任务');
|
||||
|
||||
const invalidPageFallback = await jsonFetch(baseUrl, '/api/admin/tasks/history-stats?page=0&limit=0');
|
||||
assert.equal(invalidPageFallback.status, 200);
|
||||
assert.equal(invalidPageFallback.json?.pagination?.page, 1);
|
||||
assert.equal(invalidPageFallback.json?.pagination?.limit, 5);
|
||||
} finally {
|
||||
await new Promise<void>((resolve) => server.close(() => resolve()));
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user