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

115 lines
3.4 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { handler } from '../src/handler.js';
import { createMockHostBindings, ExecuteRequest } from '@alga-psa/extension-runtime';
function makeRequest(overrides: Partial<ExecuteRequest> = {}): ExecuteRequest {
return {
context: {
tenantId: 'tenant-123',
extensionId: 'com.alga.sample.invoicing-demo',
requestId: 'req-1',
...overrides.context,
},
http: {
method: 'GET',
url: '/api/status',
headers: [],
...overrides.http,
},
};
}
describe('invoicing-demo handler', () => {
it('returns status for GET /api/status', async () => {
const host = createMockHostBindings({
logging: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
});
const response = await handler(makeRequest(), host);
expect(response.status).toBe(200);
const json = JSON.parse(new TextDecoder().decode(response.body ?? new Uint8Array()));
expect(json.status).toBe('healthy');
expect(json.tenant).toBe('tenant-123');
});
it('validates input and calls host.invoicing.createManualInvoice', async () => {
const createManualInvoice = vi.fn().mockResolvedValue({
success: true,
invoice: {
invoiceId: 'inv-1',
invoiceNumber: 'INV-0001',
status: 'draft',
subtotal: 100,
tax: 0,
total: 100,
},
});
const host = createMockHostBindings({
logging: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
invoicing: { createManualInvoice },
});
const body = {
clientId: 'client-1',
invoiceDate: '2026-01-14',
dueDate: '2026-01-14',
poNumber: 'PO-123',
items: [{ serviceId: 'svc-1', quantity: 2, description: 'Work', rate: 5000 }],
};
const response = await handler(
makeRequest({
http: {
method: 'POST',
url: '/api/create-manual-invoice',
body: new TextEncoder().encode(JSON.stringify(body)),
},
}),
host
);
expect(response.status).toBe(200);
expect(createManualInvoice).toHaveBeenCalledTimes(1);
expect(createManualInvoice).toHaveBeenCalledWith({
clientId: 'client-1',
invoiceDate: '2026-01-14',
dueDate: '2026-01-14',
poNumber: 'PO-123',
items: [{ serviceId: 'svc-1', quantity: 2, description: 'Work', rate: 5000 }],
});
});
it('returns 400 with fieldErrors for missing clientId/items', async () => {
const host = createMockHostBindings({
logging: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
invoicing: { createManualInvoice: vi.fn() },
});
const response = await handler(
makeRequest({
http: {
method: 'POST',
url: '/api/create-manual-invoice',
body: new TextEncoder().encode(JSON.stringify({ items: [] })),
},
}),
host
);
expect(response.status).toBe(400);
const json = JSON.parse(new TextDecoder().decode(response.body ?? new Uint8Array()));
expect(json.success).toBe(false);
expect(json.fieldErrors.clientId).toBeDefined();
expect(json.fieldErrors.items).toBeDefined();
});
it('returns 404 for unknown routes', async () => {
const host = createMockHostBindings({
logging: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
});
const response = await handler(makeRequest({ http: { method: 'GET', url: '/nope' } }), host);
expect(response.status).toBe(404);
});
});