Files
0pt/tests/document-workbench/trial-credits.test.ts
T
admin dade37ee05
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 4s
masterplan-lock / check (push) Successful in 5s
CI (SIAX Cloud) / security (pull_request) Successful in 16s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
CI (SIAX Cloud) / integration (pull_request) Successful in 9m58s
CI (SIAX Cloud) / quality (pull_request) Successful in 10m25s
feat(credits): one-time trial grant so the ledger runs in production
- Ledger accepts kind='grant' (a server-owned balance grant, not a payment).
- OPT_TRIAL_CREDITS=N grants N credits once per tenant (idempotent key
  trial:<tenant>) on balance/structure, so reserve -> settle is exercised
  before any customer purchase.
- sql/020 extends the kind constraint (re-runnable).
- Tests for the grant helper; full suite 1345 passed.
2026-11-02 22:05:06 +01:00

26 lines
1.1 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { ensureTrialCredits, readTrialCredits } from '../../src/document-workbench/trial-credits';
describe('trial credits', () => {
it('is disabled by default and rejects invalid amounts', () => {
expect(readTrialCredits({} as unknown as NodeJS.ProcessEnv)).toBe(0);
expect(readTrialCredits({ OPT_TRIAL_CREDITS: '-5' } as unknown as NodeJS.ProcessEnv)).toBe(0);
expect(readTrialCredits({ OPT_TRIAL_CREDITS: 'abc' } as unknown as NodeJS.ProcessEnv)).toBe(0);
expect(readTrialCredits({ OPT_TRIAL_CREDITS: '100' } as unknown as NodeJS.ProcessEnv)).toBe(100);
});
it('applies one idempotent grant and does nothing for zero', async () => {
const apply = vi.fn(async () => ({ replay: false }));
await ensureTrialCredits({ apply }, 't_0pt', 100);
expect(apply).toHaveBeenCalledWith({
tenantId: 't_0pt',
idempotencyKey: 'trial:t_0pt',
kind: 'grant',
reference: 'trial:t_0pt',
amount: 100,
});
await ensureTrialCredits({ apply }, 't_0pt', 0);
expect(apply).toHaveBeenCalledTimes(1);
});
});