PSA/packages/billing/tests/billingCurrencyActions.defaultCurrencyFallback.test.ts
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

201 lines
5.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
type MockTableState = {
client: Record<string, unknown> | null;
contracts: Array<Record<string, unknown>>;
billingSettings: Record<string, unknown> | null;
};
const mockState: MockTableState = {
client: null,
contracts: [],
billingSettings: null,
};
function createMockKnex(state: MockTableState) {
return (table: string) => {
if (table === 'clients') {
return {
where: vi.fn().mockReturnValue({
select: vi.fn().mockReturnValue({
first: vi.fn().mockResolvedValue(state.client),
}),
}),
};
}
if (table === 'client_contracts as cc') {
// Build a self-referencing chain for Knex query builder
const chain: any = {};
chain.join = vi.fn().mockReturnValue(chain);
chain.where = vi.fn().mockImplementation((arg: unknown) => {
if (typeof arg === 'function') arg.call(chain);
return chain;
});
chain.whereNull = vi.fn().mockReturnValue(chain);
chain.orWhere = vi.fn().mockReturnValue(chain);
chain.whereNotNull = vi.fn().mockReturnValue(chain);
chain.distinct = vi.fn().mockResolvedValue(state.contracts);
return chain;
}
if (table === 'default_billing_settings') {
return {
where: vi.fn().mockReturnValue({
select: vi.fn().mockReturnValue({
first: vi.fn().mockResolvedValue(state.billingSettings),
}),
}),
};
}
return {};
};
}
vi.mock('@alga-psa/db', () => ({
createTenantKnex: vi.fn(async () => ({ knex: createMockKnex(mockState) })),
}));
vi.mock('@alga-psa/auth', () => ({
withAuth: (fn: unknown) => fn,
}));
vi.mock('@alga-psa/auth/rbac', () => ({
hasPermission: vi.fn().mockResolvedValue(true),
}));
describe('resolveClientBillingCurrency — fallback chain', () => {
beforeEach(() => {
vi.clearAllMocks();
mockState.client = null;
mockState.contracts = [];
mockState.billingSettings = null;
});
it('returns contract currency when active contracts exist', async () => {
mockState.client = { default_currency_code: 'AUD' };
mockState.contracts = [{ currency_code: 'GBP' }];
mockState.billingSettings = { default_currency_code: 'EUR' };
const { resolveClientBillingCurrency } = await import(
'../src/actions/billingCurrencyActions'
);
const result = await resolveClientBillingCurrency(
{ user_id: 'user-1' },
{ tenant: 'tenant-1' },
'client-1'
);
expect(result).toBe('GBP');
});
it('returns client default when no active contracts', async () => {
mockState.client = { default_currency_code: 'AUD' };
mockState.contracts = [];
mockState.billingSettings = { default_currency_code: 'EUR' };
const { resolveClientBillingCurrency } = await import(
'../src/actions/billingCurrencyActions'
);
const result = await resolveClientBillingCurrency(
{ user_id: 'user-1' },
{ tenant: 'tenant-1' },
'client-1'
);
expect(result).toBe('AUD');
});
it('returns tenant billing settings default when no contract or client default', async () => {
mockState.client = { default_currency_code: null };
mockState.contracts = [];
mockState.billingSettings = { default_currency_code: 'EUR' };
const { resolveClientBillingCurrency } = await import(
'../src/actions/billingCurrencyActions'
);
const result = await resolveClientBillingCurrency(
{ user_id: 'user-1' },
{ tenant: 'tenant-1' },
'client-1'
);
expect(result).toBe('EUR');
});
it('returns USD as final fallback when nothing is configured', async () => {
mockState.client = { default_currency_code: null };
mockState.contracts = [];
mockState.billingSettings = null;
const { resolveClientBillingCurrency } = await import(
'../src/actions/billingCurrencyActions'
);
const result = await resolveClientBillingCurrency(
{ user_id: 'user-1' },
{ tenant: 'tenant-1' },
'client-1'
);
expect(result).toBe('USD');
});
it('falls back to billing settings when client is not found', async () => {
mockState.client = null;
mockState.contracts = [];
mockState.billingSettings = { default_currency_code: 'EUR' };
const { resolveClientBillingCurrency } = await import(
'../src/actions/billingCurrencyActions'
);
const result = await resolveClientBillingCurrency(
{ user_id: 'user-1' },
{ tenant: 'tenant-1' },
'client-1'
);
expect(result).toBe('EUR');
});
it('returns contract currency even when it matches client default', async () => {
mockState.client = { default_currency_code: 'GBP' };
mockState.contracts = [{ currency_code: 'GBP' }];
mockState.billingSettings = { default_currency_code: 'EUR' };
const { resolveClientBillingCurrency } = await import(
'../src/actions/billingCurrencyActions'
);
const result = await resolveClientBillingCurrency(
{ user_id: 'user-1' },
{ tenant: 'tenant-1' },
'client-1'
);
expect(result).toBe('GBP');
});
it('throws when client has contracts in multiple currencies', async () => {
mockState.client = { default_currency_code: 'AUD' };
mockState.contracts = [{ currency_code: 'GBP' }, { currency_code: 'EUR' }];
const { resolveClientBillingCurrency } = await import(
'../src/actions/billingCurrencyActions'
);
await expect(
resolveClientBillingCurrency(
{ user_id: 'user-1' },
{ tenant: 'tenant-1' },
'client-1'
)
).rejects.toThrow('Client has active contracts in multiple currencies');
});
});