Spaces:
Paused
Paused
Download apps/prevision-api/src/generations/generations.service.spec.ts from modventures/prevision: direct link, hf CLI and curl.
- Browser
- Download file 4.13 kB
-
https://huggingface.co/spaces/modventures/prevision/resolve/main/apps/prevision-api/src/generations/generations.service.spec.ts
- Command line
-
hf download hf://spaces/modventures/prevision/apps/prevision-api/src/generations/generations.service.spec.ts
-
curl -L -o generations.service.spec.ts https://huggingface.co/spaces/modventures/prevision/resolve/main/apps/prevision-api/src/generations/generations.service.spec.ts
4.13 kB
| import { Test } from '@nestjs/testing'; | |
| import { testConfig } from '../../test/support/test-config.js'; | |
| import { APP_CONFIG } from '../config/app-config.js'; | |
| import { PrismaService } from '../prisma/prisma.service.js'; | |
| import { GenerationsService } from './generations.service.js'; | |
| describe('GenerationsService', () => { | |
| const period = { plan: 'starter', currentPeriodStart: new Date('2026-10-01'), currentPeriodEnd: new Date('2026-11-01') }; | |
| const tx = { $queryRaw: vi.fn(), subscription: { findFirst: vi.fn() }, generation: { count: vi.fn(), create: vi.fn() } }; | |
| const db = { | |
| $transaction: vi.fn((work: (client: typeof tx) => unknown) => work(tx)), | |
| subscription: { findFirst: vi.fn() }, | |
| generation: { count: vi.fn(), updateMany: vi.fn() }, | |
| }; | |
| let service: GenerationsService; | |
| beforeEach(async () => { | |
| vi.clearAllMocks(); | |
| db.subscription.findFirst.mockResolvedValue(null); | |
| tx.subscription.findFirst.mockResolvedValue(null); | |
| const moduleRef = await Test.createTestingModule({ | |
| providers: [GenerationsService, { provide: PrismaService, useValue: { db } }, { provide: APP_CONFIG, useValue: testConfig() }], | |
| }).compile(); | |
| service = moduleRef.get(GenerationsService); | |
| }); | |
| it('counts all-time generations for free accounts', async () => { | |
| db.generation.count.mockResolvedValue(2); | |
| expect(await service.usage({ id: 'u1', role: 'user' })).toEqual({ plan: 'free', limit: 5, used: 2, remaining: 3, renewsAt: null }); | |
| expect(db.generation.count).toHaveBeenCalledWith({ where: { userId: 'u1', refundedAt: null } }); | |
| }); | |
| it('counts only this billing period for subscribers', async () => { | |
| db.subscription.findFirst.mockResolvedValue(period); | |
| db.generation.count.mockResolvedValue(10); | |
| expect(await service.usage({ id: 'u1', role: 'user' })).toEqual({ | |
| plan: 'starter', | |
| limit: 150, | |
| used: 10, | |
| remaining: 140, | |
| renewsAt: period.currentPeriodEnd, | |
| }); | |
| expect(db.generation.count).toHaveBeenCalledWith({ where: { userId: 'u1', refundedAt: null, createdAt: { gte: period.currentPeriodStart } } }); | |
| }); | |
| it('records a generation while tries remain', async () => { | |
| tx.$queryRaw.mockResolvedValue([{ role: 'user' }]); | |
| tx.generation.count.mockResolvedValue(4); | |
| tx.generation.create.mockResolvedValue({ id: 'g1' }); | |
| expect(await service.create('u1', 'job-1')).toEqual({ | |
| generationId: 'g1', | |
| usage: { plan: 'free', limit: 5, used: 5, remaining: 0, renewsAt: null }, | |
| }); | |
| expect(tx.generation.create).toHaveBeenCalledWith({ data: { userId: 'u1', jobId: 'job-1' } }); | |
| }); | |
| it('answers 402 NO_FREE_TRIES once the free tries are used', async () => { | |
| tx.$queryRaw.mockResolvedValue([{ role: 'user' }]); | |
| tx.generation.count.mockResolvedValue(5); | |
| await expect(service.create('u1', 'job-6')).rejects.toMatchObject({ code: 'NO_FREE_TRIES', status: 402 }); | |
| expect(tx.generation.create).not.toHaveBeenCalled(); | |
| }); | |
| it('answers 402 PLAN_LIMIT_REACHED at a plan limit', async () => { | |
| tx.$queryRaw.mockResolvedValue([{ role: 'user' }]); | |
| tx.subscription.findFirst.mockResolvedValue(period); | |
| tx.generation.count.mockResolvedValue(150); | |
| await expect(service.create('u1', 'job-151')).rejects.toMatchObject({ code: 'PLAN_LIMIT_REACHED', status: 402 }); | |
| }); | |
| it('never limits admins', async () => { | |
| tx.$queryRaw.mockResolvedValue([{ role: 'admin' }]); | |
| tx.generation.count.mockResolvedValue(40); | |
| tx.generation.create.mockResolvedValue({ id: 'g41' }); | |
| expect((await service.create('admin', 'job-41')).usage).toEqual({ plan: 'unlimited', limit: null, used: 41, remaining: null, renewsAt: null }); | |
| }); | |
| it('refuses unknown or deleted users', async () => { | |
| tx.$queryRaw.mockResolvedValue([]); | |
| await expect(service.create('gone', 'job-1')).rejects.toMatchObject({ status: 403 }); | |
| }); | |
| it('refunds only generations that are not refunded yet', async () => { | |
| await service.refund('g1'); | |
| expect(db.generation.updateMany).toHaveBeenCalledWith({ where: { id: 'g1', refundedAt: null }, data: { refundedAt: expect.any(Date) } }); | |
| }); | |
| }); | |