PSA/packages/billing/tests/accounting/companySyncAdapters.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

132 lines
5.1 KiB
TypeScript

/**
* Unit tests for the company-sync accounting adapters
* (packages/billing/src/services/companySync/adapters/).
*
* The adapters bridge normalized company payloads to the QBO/Xero client
* services. The external client services are mocked; we verify factory
* wiring (including null-realm passthrough) and delegation.
*/
import { describe, expect, it, vi } from 'vitest';
// The real client services pull axios/secret providers; keep the unit
// boundary at the injected client factory.
vi.mock('@alga-psa/integrations/lib/qbo/qboClientService', () => ({
QboClientService: { create: vi.fn() },
}));
vi.mock('@alga-psa/integrations/lib/xero/xeroClientService', () => ({
XeroClientService: { create: vi.fn() },
}));
import { QuickBooksOnlineCompanyAdapter } from '../../src/services/companySync/adapters/quickBooksCompanyAdapter';
import { XeroCompanyAdapter } from '../../src/services/companySync/adapters/xeroCompanyAdapter';
import type { NormalizedCompanyPayload } from '../../src/services/companySync/companySync.types';
const payload: NormalizedCompanyPayload = { companyId: 'co-1', name: 'Acme Co' };
describe('QuickBooksOnlineCompanyAdapter', () => {
it('passes a null realm to the factory when no target realm is provided', async () => {
// The adapter no longer enforces a realm up front; QboClientService.create
// resolves the tenant's default realm when given null.
const client = {
findCustomerByDisplayName: vi.fn(async () => null),
createOrUpdateCustomer: vi.fn(),
};
const factory = vi.fn(async () => client);
const adapter = new QuickBooksOnlineCompanyAdapter(factory as any);
const result = await adapter.findExternalCompany(payload, { tenantId: 'tenant-1', targetRealm: null });
expect(factory).toHaveBeenCalledWith('tenant-1', null);
expect(client.findCustomerByDisplayName).toHaveBeenCalledWith('Acme Co');
expect(result).toBeNull();
});
it('looks up customers by display name through a realm-scoped client', async () => {
const found = { externalId: 'qbo-5', displayName: 'Acme Co' };
const client = {
findCustomerByDisplayName: vi.fn(async () => found),
createOrUpdateCustomer: vi.fn(),
};
const factory = vi.fn(async () => client);
const adapter = new QuickBooksOnlineCompanyAdapter(factory as any);
const result = await adapter.findExternalCompany(payload, {
tenantId: 'tenant-1',
targetRealm: 'realm-9',
});
expect(factory).toHaveBeenCalledWith('tenant-1', 'realm-9');
expect(client.findCustomerByDisplayName).toHaveBeenCalledWith('Acme Co');
expect(result).toBe(found);
});
it('delegates createOrUpdate with the full normalized payload', async () => {
const created = { externalId: 'qbo-6', displayName: 'Acme Co' };
const client = {
findCustomerByDisplayName: vi.fn(),
createOrUpdateCustomer: vi.fn(async () => created),
};
const adapter = new QuickBooksOnlineCompanyAdapter(vi.fn(async () => client) as any);
const result = await adapter.createOrUpdateExternalCompany(payload, {
tenantId: 'tenant-1',
targetRealm: 'realm-9',
});
expect(client.createOrUpdateCustomer).toHaveBeenCalledWith(payload);
expect(result).toBe(created);
});
it('identifies itself as the quickbooks_online adapter', () => {
expect(new QuickBooksOnlineCompanyAdapter(vi.fn() as any).type).toBe('quickbooks_online');
});
});
describe('XeroCompanyAdapter', () => {
it('works without a target realm, passing a null connection id to the factory', async () => {
const client = {
findContactByName: vi.fn(async () => null),
createOrUpdateContact: vi.fn(),
};
const factory = vi.fn(async () => client);
const adapter = new XeroCompanyAdapter(factory as any);
const result = await adapter.findExternalCompany(payload, { tenantId: 'tenant-1' });
expect(factory).toHaveBeenCalledWith('tenant-1', null);
expect(client.findContactByName).toHaveBeenCalledWith('Acme Co');
expect(result).toBeNull();
});
it('passes the target realm through as the Xero connection id', async () => {
const client = {
findContactByName: vi.fn(async () => null),
createOrUpdateContact: vi.fn(),
};
const factory = vi.fn(async () => client);
const adapter = new XeroCompanyAdapter(factory as any);
await adapter.findExternalCompany(payload, { tenantId: 'tenant-1', targetRealm: 'conn-3' });
expect(factory).toHaveBeenCalledWith('tenant-1', 'conn-3');
});
it('delegates createOrUpdate with the full normalized payload', async () => {
const created = { externalId: 'xero-1', displayName: 'Acme Co' };
const client = {
findContactByName: vi.fn(),
createOrUpdateContact: vi.fn(async () => created),
};
const adapter = new XeroCompanyAdapter(vi.fn(async () => client) as any);
const result = await adapter.createOrUpdateExternalCompany(payload, { tenantId: 'tenant-1' });
expect(client.createOrUpdateContact).toHaveBeenCalledWith(payload);
expect(result).toBe(created);
});
it('identifies itself as the xero adapter', () => {
expect(new XeroCompanyAdapter(vi.fn() as any).type).toBe('xero');
});
});