Skip to content

Test an app

Test an App with the harness to run its Definition end to end in memory: write, tick the reactor, read, with the clock under your control.

  • Vitest, and the packages @pyn-backend/platform-harness-public, @pyn-backend/platform-service-test, @pyn-backend/service-cron-test, @pyn-backend/service-system-test and @pyn-backend/test-services-logger from the repository.
  • An AppSpec to test. The steps use the leave App from Add states and transitions.
  1. Create the harness. Call createHarnessInstance(dependencies, spec) with a test system service, a test cron service, a test logger and the test Runtime. The harness builds the Definition, registers the App with the Runtime and wires a reactor and a ticker to it.

    examples/testing/leave-app.test.ts
    import { createHarnessInstance } from '@pyn-backend/platform-harness-public';
    import { createTestPlatformService } from '@pyn-backend/platform-service-test';
    import { createTestCronService } from '@pyn-backend/service-cron-test';
    import { createTestSystemService } from '@pyn-backend/service-system-test';
    import { createTestLoggerService } from '@pyn-backend/test-services-logger';
    import { describe, expect, it } from 'vitest';
    import { leaveApp } from '../state-transitions/leave-app';
    const createHarness = async () => {
    const system = createTestSystemService();
    const logger = createTestLoggerService();
    const cron = createTestCronService({ system });
    const platform = createTestPlatformService({ cron, logger, system });
    return await createHarnessInstance({ cron, logger, platform, system }, leaveApp);
    };
    describe('leave App', () => {
    it('approves a request and notes the decision once the reactor has run', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    const id = await workspace.write(async (ctx) => {
    const parent = await ctx.getWorkspaceItem();
    const request = await parent.createChild(ctx.apps.leave.templates.leaveRequest, {
    key: 'summerHoliday',
    properties: { reason: 'Summer holiday' },
    });
    return request.id;
    });
    await workspace.write(async (ctx) => {
    const current = ctx.apps.leave;
    await current.mutations.approve.handler({ id }, current, ctx);
    });
    await workspace.read(async (ctx) => {
    const request = await ctx.getItemByKeyPath('summerHoliday');
    expect(request?.state?.value).toBe('approved');
    expect(request?.properties.decisionNoted).toBeUndefined();
    });
    await harness.tickAll();
    await workspace.read(async (ctx) => {
    const request = await ctx.getItem(id);
    expect(request?.properties.decisionNoted).toBe(true);
    });
    expect.assertions(3);
    });
    it('runs every execution at the time the harness clock reports', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    harness.advance('2026-03-02T09:00:00Z');
    const first = await workspace.read(async (ctx) => ctx.now.toISO());
    await harness.tickAll({ hours: 3 });
    const second = await workspace.read(async (ctx) => ctx.now.toISO());
    expect(first).toBe('2026-03-02T09:00:00.000Z');
    expect(second).toBe('2026-03-02T12:00:00.000Z');
    });
    });
  2. Create a Workspace. Call harness.createWorkspace(). It creates a source Workspace, runs the App Hooks, and returns read, write and workspaceId.

    examples/testing/leave-app.test.ts
    import { createHarnessInstance } from '@pyn-backend/platform-harness-public';
    import { createTestPlatformService } from '@pyn-backend/platform-service-test';
    import { createTestCronService } from '@pyn-backend/service-cron-test';
    import { createTestSystemService } from '@pyn-backend/service-system-test';
    import { createTestLoggerService } from '@pyn-backend/test-services-logger';
    import { describe, expect, it } from 'vitest';
    import { leaveApp } from '../state-transitions/leave-app';
    const createHarness = async () => {
    const system = createTestSystemService();
    const logger = createTestLoggerService();
    const cron = createTestCronService({ system });
    const platform = createTestPlatformService({ cron, logger, system });
    return await createHarnessInstance({ cron, logger, platform, system }, leaveApp);
    };
    describe('leave App', () => {
    it('approves a request and notes the decision once the reactor has run', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    const id = await workspace.write(async (ctx) => {
    const parent = await ctx.getWorkspaceItem();
    const request = await parent.createChild(ctx.apps.leave.templates.leaveRequest, {
    key: 'summerHoliday',
    properties: { reason: 'Summer holiday' },
    });
    return request.id;
    });
    await workspace.write(async (ctx) => {
    const current = ctx.apps.leave;
    await current.mutations.approve.handler({ id }, current, ctx);
    });
    await workspace.read(async (ctx) => {
    const request = await ctx.getItemByKeyPath('summerHoliday');
    expect(request?.state?.value).toBe('approved');
    expect(request?.properties.decisionNoted).toBeUndefined();
    });
    await harness.tickAll();
    await workspace.read(async (ctx) => {
    const request = await ctx.getItem(id);
    expect(request?.properties.decisionNoted).toBe(true);
    });
    expect.assertions(3);
    });
    it('runs every execution at the time the harness clock reports', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    harness.advance('2026-03-02T09:00:00Z');
    const first = await workspace.read(async (ctx) => ctx.now.toISO());
    await harness.tickAll({ hours: 3 });
    const second = await workspace.read(async (ctx) => ctx.now.toISO());
    expect(first).toBe('2026-03-02T09:00:00.000Z');
    expect(second).toBe('2026-03-02T12:00:00.000Z');
    });
    });
  3. Write and read. workspace.write(fn) runs fn in a write execution as system; workspace.read(fn) runs it in a read execution. Whatever fn returns is the result of the call. Reach the App through ctx.apps.<canonicalName> and invoke operations through their handler.

    examples/testing/leave-app.test.ts
    import { createHarnessInstance } from '@pyn-backend/platform-harness-public';
    import { createTestPlatformService } from '@pyn-backend/platform-service-test';
    import { createTestCronService } from '@pyn-backend/service-cron-test';
    import { createTestSystemService } from '@pyn-backend/service-system-test';
    import { createTestLoggerService } from '@pyn-backend/test-services-logger';
    import { describe, expect, it } from 'vitest';
    import { leaveApp } from '../state-transitions/leave-app';
    const createHarness = async () => {
    const system = createTestSystemService();
    const logger = createTestLoggerService();
    const cron = createTestCronService({ system });
    const platform = createTestPlatformService({ cron, logger, system });
    return await createHarnessInstance({ cron, logger, platform, system }, leaveApp);
    };
    describe('leave App', () => {
    it('approves a request and notes the decision once the reactor has run', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    const id = await workspace.write(async (ctx) => {
    const parent = await ctx.getWorkspaceItem();
    const request = await parent.createChild(ctx.apps.leave.templates.leaveRequest, {
    key: 'summerHoliday',
    properties: { reason: 'Summer holiday' },
    });
    return request.id;
    });
    await workspace.write(async (ctx) => {
    const current = ctx.apps.leave;
    await current.mutations.approve.handler({ id }, current, ctx);
    });
    await workspace.read(async (ctx) => {
    const request = await ctx.getItemByKeyPath('summerHoliday');
    expect(request?.state?.value).toBe('approved');
    expect(request?.properties.decisionNoted).toBeUndefined();
    });
    await harness.tickAll();
    await workspace.read(async (ctx) => {
    const request = await ctx.getItem(id);
    expect(request?.properties.decisionNoted).toBe(true);
    });
    expect.assertions(3);
    });
    it('runs every execution at the time the harness clock reports', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    harness.advance('2026-03-02T09:00:00Z');
    const first = await workspace.read(async (ctx) => ctx.now.toISO());
    await harness.tickAll({ hours: 3 });
    const second = await workspace.read(async (ctx) => ctx.now.toISO());
    expect(first).toBe('2026-03-02T09:00:00.000Z');
    expect(second).toBe('2026-03-02T12:00:00.000Z');
    });
    });
  4. Assert the synchronous boundary. When write resolves, Item changes and appended Events are committed, and nothing else has happened. Assert state and Properties here; a Listener effect is absent at this point.

    examples/testing/leave-app.test.ts
    import { createHarnessInstance } from '@pyn-backend/platform-harness-public';
    import { createTestPlatformService } from '@pyn-backend/platform-service-test';
    import { createTestCronService } from '@pyn-backend/service-cron-test';
    import { createTestSystemService } from '@pyn-backend/service-system-test';
    import { createTestLoggerService } from '@pyn-backend/test-services-logger';
    import { describe, expect, it } from 'vitest';
    import { leaveApp } from '../state-transitions/leave-app';
    const createHarness = async () => {
    const system = createTestSystemService();
    const logger = createTestLoggerService();
    const cron = createTestCronService({ system });
    const platform = createTestPlatformService({ cron, logger, system });
    return await createHarnessInstance({ cron, logger, platform, system }, leaveApp);
    };
    describe('leave App', () => {
    it('approves a request and notes the decision once the reactor has run', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    const id = await workspace.write(async (ctx) => {
    const parent = await ctx.getWorkspaceItem();
    const request = await parent.createChild(ctx.apps.leave.templates.leaveRequest, {
    key: 'summerHoliday',
    properties: { reason: 'Summer holiday' },
    });
    return request.id;
    });
    await workspace.write(async (ctx) => {
    const current = ctx.apps.leave;
    await current.mutations.approve.handler({ id }, current, ctx);
    });
    await workspace.read(async (ctx) => {
    const request = await ctx.getItemByKeyPath('summerHoliday');
    expect(request?.state?.value).toBe('approved');
    expect(request?.properties.decisionNoted).toBeUndefined();
    });
    await harness.tickAll();
    await workspace.read(async (ctx) => {
    const request = await ctx.getItem(id);
    expect(request?.properties.decisionNoted).toBe(true);
    });
    expect.assertions(3);
    });
    it('runs every execution at the time the harness clock reports', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    harness.advance('2026-03-02T09:00:00Z');
    const first = await workspace.read(async (ctx) => ctx.now.toISO());
    await harness.tickAll({ hours: 3 });
    const second = await workspace.read(async (ctx) => ctx.now.toISO());
    expect(first).toBe('2026-03-02T09:00:00.000Z');
    expect(second).toBe('2026-03-02T12:00:00.000Z');
    });
    });
  5. Drive the reactor. Call harness.tickAll(). It ticks the reactor and the Runtime until no work is left, so Listeners, parent Listeners and due schedules have run. Assert their effects afterward. For Metrics, register a projector on harness.ticker as Measure with metrics shows.

    examples/testing/leave-app.test.ts
    import { createHarnessInstance } from '@pyn-backend/platform-harness-public';
    import { createTestPlatformService } from '@pyn-backend/platform-service-test';
    import { createTestCronService } from '@pyn-backend/service-cron-test';
    import { createTestSystemService } from '@pyn-backend/service-system-test';
    import { createTestLoggerService } from '@pyn-backend/test-services-logger';
    import { describe, expect, it } from 'vitest';
    import { leaveApp } from '../state-transitions/leave-app';
    const createHarness = async () => {
    const system = createTestSystemService();
    const logger = createTestLoggerService();
    const cron = createTestCronService({ system });
    const platform = createTestPlatformService({ cron, logger, system });
    return await createHarnessInstance({ cron, logger, platform, system }, leaveApp);
    };
    describe('leave App', () => {
    it('approves a request and notes the decision once the reactor has run', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    const id = await workspace.write(async (ctx) => {
    const parent = await ctx.getWorkspaceItem();
    const request = await parent.createChild(ctx.apps.leave.templates.leaveRequest, {
    key: 'summerHoliday',
    properties: { reason: 'Summer holiday' },
    });
    return request.id;
    });
    await workspace.write(async (ctx) => {
    const current = ctx.apps.leave;
    await current.mutations.approve.handler({ id }, current, ctx);
    });
    await workspace.read(async (ctx) => {
    const request = await ctx.getItemByKeyPath('summerHoliday');
    expect(request?.state?.value).toBe('approved');
    expect(request?.properties.decisionNoted).toBeUndefined();
    });
    await harness.tickAll();
    await workspace.read(async (ctx) => {
    const request = await ctx.getItem(id);
    expect(request?.properties.decisionNoted).toBe(true);
    });
    expect.assertions(3);
    });
    it('runs every execution at the time the harness clock reports', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    harness.advance('2026-03-02T09:00:00Z');
    const first = await workspace.read(async (ctx) => ctx.now.toISO());
    await harness.tickAll({ hours: 3 });
    const second = await workspace.read(async (ctx) => ctx.now.toISO());
    expect(first).toBe('2026-03-02T09:00:00.000Z');
    expect(second).toBe('2026-03-02T12:00:00.000Z');
    });
    });
  6. Control the clock. Call harness.advance(to) with an ISO timestamp to set the clock or with a duration such as { hours: 3 } to move it. tickAll(to) advances first and then ticks. Every execution reads the clock through ctx.now, a UTC DateTime fixed for one attempt.

    examples/testing/leave-app.test.ts
    import { createHarnessInstance } from '@pyn-backend/platform-harness-public';
    import { createTestPlatformService } from '@pyn-backend/platform-service-test';
    import { createTestCronService } from '@pyn-backend/service-cron-test';
    import { createTestSystemService } from '@pyn-backend/service-system-test';
    import { createTestLoggerService } from '@pyn-backend/test-services-logger';
    import { describe, expect, it } from 'vitest';
    import { leaveApp } from '../state-transitions/leave-app';
    const createHarness = async () => {
    const system = createTestSystemService();
    const logger = createTestLoggerService();
    const cron = createTestCronService({ system });
    const platform = createTestPlatformService({ cron, logger, system });
    return await createHarnessInstance({ cron, logger, platform, system }, leaveApp);
    };
    describe('leave App', () => {
    it('approves a request and notes the decision once the reactor has run', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    const id = await workspace.write(async (ctx) => {
    const parent = await ctx.getWorkspaceItem();
    const request = await parent.createChild(ctx.apps.leave.templates.leaveRequest, {
    key: 'summerHoliday',
    properties: { reason: 'Summer holiday' },
    });
    return request.id;
    });
    await workspace.write(async (ctx) => {
    const current = ctx.apps.leave;
    await current.mutations.approve.handler({ id }, current, ctx);
    });
    await workspace.read(async (ctx) => {
    const request = await ctx.getItemByKeyPath('summerHoliday');
    expect(request?.state?.value).toBe('approved');
    expect(request?.properties.decisionNoted).toBeUndefined();
    });
    await harness.tickAll();
    await workspace.read(async (ctx) => {
    const request = await ctx.getItem(id);
    expect(request?.properties.decisionNoted).toBe(true);
    });
    expect.assertions(3);
    });
    it('runs every execution at the time the harness clock reports', async () => {
    const harness = await createHarness();
    const workspace = await harness.createWorkspace();
    harness.advance('2026-03-02T09:00:00Z');
    const first = await workspace.read(async (ctx) => ctx.now.toISO());
    await harness.tickAll({ hours: 3 });
    const second = await workspace.read(async (ctx) => ctx.now.toISO());
    expect(first).toBe('2026-03-02T09:00:00.000Z');
    expect(second).toBe('2026-03-02T12:00:00.000Z');
    });
    });
  7. Run the conformance suites against a Runtime. Call platformRuntimeCompliance(initialize, options) from @pyn-backend/platform-runtime-compliance inside a describe, where initialize returns the Runtime under test. Every Runtime in the repository runs the suites from its own file.

    examples/testing/conformance.ts
    import { type InitializeFn, platformRuntimeCompliance } from '@pyn-backend/platform-runtime-compliance';
    import { createTestPlatformService } from '@pyn-backend/platform-service-test';
    import { createTestCronService } from '@pyn-backend/service-cron-test';
    import { createTestSystemService } from '@pyn-backend/service-system-test';
    import { createTestLoggerService } from '@pyn-backend/test-services-logger';
    import { describe } from 'vitest';
    const initialize: InitializeFn = async (dependencies) => {
    const system = dependencies.system ?? createTestSystemService();
    const cron = dependencies.cron ?? createTestCronService({ system });
    const logger = dependencies.logger ?? createTestLoggerService();
    return createTestPlatformService({ cron, logger, system });
    };
    export const runConformance = () => {
    describe('test Runtime conformance', () => platformRuntimeCompliance(initialize));
    };

The suite files and the command that runs each of them:

Runtime Suite file Command Suites opted out
Test src/alpyne/runtime/test/index.test.ts pnpm run cli test src/alpyne/runtime/test/ none
PostgreSQL in process (PGlite) src/alpyne/runtime/postgresql/index.test.ts pnpm run cli test src/alpyne/runtime/postgresql/ concurrency, relationTransactional, stateMachine
PostgreSQL server src/alpyne/runtime/postgresql/index.spec.ts pnpm run pyn spec --local src/alpyne/runtime/postgresql/ none
SQLite src/alpyne/runtime/sqlite/index.test.ts pnpm run cli test src/alpyne/runtime/sqlite/ concurrency, relationTransactional, stateMachine

The three optional suites open two overlapping transactions. A single-connection backend opts out of them with { concurrency: false, relationTransactional: false, stateMachine: false }; the PostgreSQL server runs them all. Every other suite is mandatory for every Runtime.

From the repository root, run:

Terminal window
pnpm run cli test alpynehq/alpyne/website/examples/testing/

You should see:

Test Files 1 passed (1)
Tests 2 passed (2)

The first test asserts at two boundaries: when write resolves, the state is approved and decisionNoted is absent; after tickAll, decisionNoted is true. The second test sets the clock to 09:00, reads ctx.now, moves three hours while ticking, and reads 12:00.

  • Principal executions. The harness runs as system, where Grants do not filter and ctx.principalId is unavailable. To test access, create an instance with platform.createInstance and a principal auth context as Grant access shows.
  • Event history. Run workspace.write<void, SystemWriteRuntimeInstance>(fn) and call ctx.selectEvents().itemId(id).execute() to assert which Events an Item has.
  • Retries and idempotency. Pass { maximumTransientRetries, idempotencyScope } as the fourth argument of instance.execute('readwrite', ...) on an instance from platform.createInstance. Unit of Work defines both.