prevision / apps /prevision-api /test /generations.e2e-spec.ts
github-actions[bot]
Automated Deploy: 23110475c742a5d4f03930b55bca09042215c1dd
1fe7075
Raw History Blame Contribute Delete
3.54 kB
import type { INestApplication } from '@nestjs/common';
import { api, createApp, internalToken, resetDatabase, signIn, sql } from './helpers.js';
describe('Generations (e2e)', () => {
let app: INestApplication;
let userId: string;
let headers: Record<string, string>;
const create = async (jobId: string, token?: string) =>
api(app)
.post('/internal/generations')
.set('Authorization', `Bearer ${token ?? (await internalToken())}`)
.send({ userId, jobId });
beforeAll(async () => {
app = await createApp();
});
afterAll(() => app.close());
beforeEach(async () => {
await resetDatabase();
headers = await signIn('user_t');
userId = (await api(app).get('/api/me').set(headers)).body.data.user.id;
});
it('allows 5 generations, then answers 402', async () => {
for (let n = 1; n <= 5; n++) expect((await create(`job${n}`)).status).toBe(201);
const refused = await create('job6');
expect(refused.status).toBe(402);
expect(refused.body).toEqual({ success: false, error: { code: 'NO_FREE_TRIES', message: expect.any(String) } });
expect((await api(app).get('/api/me').set(headers)).body.data.usage).toEqual({ plan: 'free', limit: 5, used: 5, remaining: 0, renewsAt: null });
});
it('lets admins generate without a limit', async () => {
await sql("UPDATE users SET role = 'admin' WHERE id = $1", [userId]);
for (let n = 1; n <= 7; n++) expect((await create(`job${n}`)).status).toBe(201);
expect((await api(app).get('/api/me').set(headers)).body.data.usage).toEqual({ plan: 'unlimited', limit: null, used: 7, remaining: null, renewsAt: null });
});
it('gives a try back once when a generation is refunded', async () => {
const created = await create('job1');
expect(created.body.data).toEqual({ generationId: expect.any(String), usage: { plan: 'free', limit: 5, used: 1, remaining: 4, renewsAt: null } });
const token = await internalToken();
for (let n = 0; n < 2; n++) {
await api(app).post(`/internal/generations/${created.body.data.generationId}/refund`).set('Authorization', `Bearer ${token}`).expect(204);
}
expect((await api(app).get('/api/me').set(headers)).body.data.usage.used).toBe(0);
});
it('never passes the limit with simultaneous jobs', async () => {
const token = await internalToken();
const responses = await Promise.all(Array.from({ length: 12 }, (_, n) => create(`burst${n}`, token)));
expect(responses.filter((response) => response.status === 201)).toHaveLength(5);
expect(responses.filter((response) => response.status === 402)).toHaveLength(7);
});
it('accepts only the internal token', async () => {
await api(app).post('/internal/generations').send({ userId, jobId: 'j' }).expect(401);
expect((await create('j', await internalToken('prevision-internal', -120))).status).toBe(401);
const browserToken = (await api(app).post('/api/processing-token').set(headers)).body.data.token;
expect((await create('j', browserToken)).status).toBe(401);
});
it('validates the request', async () => {
const token = await internalToken();
await api(app).post('/internal/generations').set('Authorization', `Bearer ${token}`).send({ userId: 'nope', jobId: 'j' }).expect(400);
await api(app).post('/internal/generations').set('Authorization', `Bearer ${token}`).send({ userId, jobId: '../x' }).expect(400);
});
it('is not reachable through the /api proxy', async () => {
await api(app).post('/api/internal/generations').expect(404);
});
});