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

125 lines
4.0 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { handler } from '../src/handler';
import type { ExecuteRequest, HostBindings } from '@alga-psa/extension-runtime';
const mockHost: HostBindings = {
context: { get: vi.fn() },
secrets: {
get: vi.fn(),
list: vi.fn().mockResolvedValue([]),
},
http: { fetch: vi.fn() },
storage: {
get: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
list: vi.fn(),
},
logging: {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
uiProxy: { callRoute: vi.fn() },
user: { get: vi.fn() },
scheduler: {
list: vi.fn().mockResolvedValue([]),
get: vi.fn(),
create: vi.fn().mockResolvedValue({ success: true, scheduleId: 'test-123' }),
update: vi.fn(),
delete: vi.fn().mockResolvedValue({ success: true }),
getEndpoints: vi.fn().mockResolvedValue([]),
},
};
function makeRequest(overrides: Partial<ExecuteRequest> = {}): ExecuteRequest {
return {
context: {
tenantId: 'test-tenant',
extensionId: 'com.alga.sample.scheduler-demo',
requestId: 'req-123',
...overrides.context,
},
http: {
method: 'GET',
url: '/api/status',
headers: [],
...overrides.http,
},
};
}
describe('scheduler-demo handler', () => {
it('returns status for GET /api/status', async () => {
const request = makeRequest({ http: { method: 'GET', url: '/api/status', headers: [] } });
const response = await handler(request, mockHost);
expect(response.status).toBe(200);
const body = JSON.parse(new TextDecoder().decode(response.body));
expect(body.status).toBe('healthy');
expect(body.tenant).toBe('test-tenant');
});
it('lists schedules for GET /api/schedules', async () => {
const request = makeRequest({ http: { method: 'GET', url: '/api/schedules', headers: [] } });
const response = await handler(request, mockHost);
expect(response.status).toBe(200);
const body = JSON.parse(new TextDecoder().decode(response.body));
expect(body.count).toBe(0);
expect(body.schedules).toEqual([]);
});
it('supports proxy-style method override from body for schedule listing', async () => {
const request = makeRequest({
http: {
method: 'POST',
url: '/api/schedules',
headers: [],
body: new TextEncoder().encode(JSON.stringify({ __method: 'GET' })),
},
});
const response = await handler(request, mockHost);
expect(response.status).toBe(200);
const body = JSON.parse(new TextDecoder().decode(response.body));
expect(body.count).toBe(0);
});
it('supports proxy-style method override from body for schedule delete', async () => {
const request = makeRequest({
http: {
method: 'POST',
url: '/api/schedules/sched-1',
headers: [],
body: new TextEncoder().encode(JSON.stringify({ __method: 'DELETE' })),
},
});
const response = await handler(request, mockHost);
expect(response.status).toBe(200);
const body = JSON.parse(new TextDecoder().decode(response.body));
expect(body.scheduleId).toBe('sched-1');
});
it('returns 404 for unknown routes', async () => {
const request = makeRequest({ http: { method: 'GET', url: '/unknown', headers: [] } });
const response = await handler(request, mockHost);
expect(response.status).toBe(404);
});
it('handles POST /api/setup', async () => {
const createSpy = vi.spyOn(mockHost.scheduler!, 'create');
const request = makeRequest({ http: { method: 'POST', url: '/api/setup', headers: [] } });
const response = await handler(request, mockHost);
expect(response.status).toBeGreaterThanOrEqual(200);
expect(response.status).toBeLessThan(300);
const body = JSON.parse(new TextDecoder().decode(response.body));
expect(body.results).toBeDefined();
expect(createSpy).toHaveBeenCalledWith(expect.objectContaining({ endpoint: '/api/heartbeat' }));
expect(createSpy).toHaveBeenCalledWith(expect.objectContaining({ endpoint: '/api/status' }));
});
});