Files
alibb-server/backend/tests/stats.test.ts
T
2026-08-27 16:11:49 +08:00

59 lines
1.8 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import request from 'supertest';
import { setupApp, resetDb, teardown } from './setup.js';
import { makePayload } from './fixtures.js';
let app: any;
beforeAll(async () => {
({ app } = await setupApp());
}, 90000);
afterAll(async () => {
await teardown();
});
async function login() {
return (
await request(app)
.post('/api/auth/login')
.send({ username: 'admin', password: 'admin-password-1234' })
).body.token;
}
async function pushInquiry(p: any) {
await request(app)
.post('/api/ingest')
.set('Authorization', 'Bearer dev-ingest-token-1234')
.send(p);
}
describe('stats overview', () => {
it('returns total / countryTop10 / byManager / monthly', async () => {
await resetDb();
await pushInquiry(makePayload({ customer: { country: 'PE', name: 'A' } }));
await pushInquiry(
makePayload({
inquiry: { inquiryId: 'X2' },
customer: { customerId: 'c2', country: 'US', name: 'B' },
}),
);
const token = await login();
const res = await request(app)
.get('/api/stats/overview')
.set('Authorization', 'Bearer ' + token);
expect(res.status).toBe(200);
expect(res.body.total).toBe(2);
expect(Array.isArray(res.body.countryTop10)).toBe(true);
expect(res.body.countryTop10.some((c: any) => c.country === 'PE')).toBe(true);
expect(Array.isArray(res.body.byManager)).toBe(true);
expect(Array.isArray(res.body.monthly)).toBe(true);
// 确认 _count 已重命名为 count(前端友好)
expect(res.body.countryTop10[0]).toHaveProperty('count');
expect(res.body.countryTop10[0].count).not.toBeUndefined();
});
it('401 without jwt', async () => {
const res = await request(app).get('/api/stats/overview');
expect(res.status).toBe(401);
});
});