74 lines
2.6 KiB
TypeScript
74 lines
2.6 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
import request from 'supertest';
|
|
import { setupApp, teardown, resetDb } from './setup.js';
|
|
import { makePayload } from './fixtures.js';
|
|
|
|
const TOKEN = 'Bearer dev-ingest-token-1234';
|
|
|
|
let app: any;
|
|
|
|
beforeAll(async () => {
|
|
({ app } = await setupApp());
|
|
}, 90000);
|
|
afterAll(async () => {
|
|
await teardown();
|
|
});
|
|
|
|
describe('ingest auth/validation', () => {
|
|
it('401 without token', async () => {
|
|
const res = await request(app).post('/api/ingest').send(makePayload());
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('400 bad payload', async () => {
|
|
const res = await request(app)
|
|
.post('/api/ingest')
|
|
.set('Authorization', 'Bearer dev-ingest-token-1234')
|
|
.send({ inquiry: {} });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|
|
|
|
describe('ingest basic insert', () => {
|
|
it('accepts valid payload, inserts 2 messages', async () => {
|
|
await resetDb();
|
|
const res = await request(app).post('/api/ingest')
|
|
.set('Authorization', 'Bearer dev-ingest-token-1234').send(makePayload());
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.inserted).toBe(2);
|
|
expect(res.body.earlyReturn).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('ingest idempotency + increment', () => {
|
|
it('push N → push N again early-returns', async () => {
|
|
await resetDb();
|
|
const p = makePayload();
|
|
const r1 = await request(app).post('/api/ingest').set('Authorization', TOKEN).send(p);
|
|
expect(r1.status).toBe(200);
|
|
expect(r1.body.inserted).toBe(2);
|
|
expect(r1.body.earlyReturn).toBe(false);
|
|
const r2 = await request(app).post('/api/ingest').set('Authorization', TOKEN).send(p);
|
|
expect(r2.body.earlyReturn).toBe(true);
|
|
});
|
|
|
|
it('push N → push N+1 → push N+1 again early-returns (messagesCount=total regression)', async () => {
|
|
await resetDb();
|
|
const base = makePayload();
|
|
await request(app).post('/api/ingest').set('Authorization', TOKEN).send(base);
|
|
const withOneMore = makePayload({
|
|
messages: [
|
|
...base.messages,
|
|
{ messageId: 'm3', sendTime: '2026-07-13T02:13:00Z', sender: 'x', direction: 'incoming', content: 'c3', translation: '' },
|
|
],
|
|
});
|
|
const r3 = await request(app).post('/api/ingest').set('Authorization', TOKEN).send(withOneMore);
|
|
expect(r3.body.inserted).toBe(3); // 总量 3,不是 delta 1
|
|
expect(r3.body.earlyReturn).toBe(false);
|
|
// 再推同样 3 条 → 早返回(若 messagesCount 退化成 delta,这里 earlyReturn 会是 false)
|
|
const r4 = await request(app).post('/api/ingest').set('Authorization', TOKEN).send(withOneMore);
|
|
expect(r4.body.earlyReturn).toBe(true);
|
|
expect(r4.body.inserted).toBe(3);
|
|
});
|
|
});
|