File size: 4,129 Bytes
1fe7075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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) } });
  });
});