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.
Before you start
Section titled “Before you start”- Vitest, and the packages
@pyn-backend/platform-harness-public,@pyn-backend/platform-service-test,@pyn-backend/service-cron-test,@pyn-backend/service-system-testand@pyn-backend/test-services-loggerfrom the repository. - An
AppSpecto test. The steps use the leave App from Add states and transitions.
-
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');});}); -
Create a Workspace. Call
harness.createWorkspace(). It creates a source Workspace, runs the App Hooks, and returnsread,writeandworkspaceId.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');});}); -
Write and read.
workspace.write(fn)runsfnin a write execution assystem;workspace.read(fn)runs it in a read execution. Whateverfnreturns is the result of the call. Reach the App throughctx.apps.<canonicalName>and invoke operations through theirhandler.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');});}); -
Assert the synchronous boundary. When
writeresolves, 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');});}); -
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 onharness.tickeras 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');});}); -
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 throughctx.now, a UTCDateTimefixed 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');});}); -
Run the conformance suites against a Runtime. Call
platformRuntimeCompliance(initialize, options)from@pyn-backend/platform-runtime-complianceinside adescribe, whereinitializereturns 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.
Verify
Section titled “Verify”From the repository root, run:
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.
Options
Section titled “Options”- Principal executions. The harness runs as
system, where Grants do not filter andctx.principalIdis unavailable. To test access, create an instance withplatform.createInstanceand aprincipalauth context as Grant access shows. - Event history. Run
workspace.write<void, SystemWriteRuntimeInstance>(fn)and callctx.selectEvents().itemId(id).execute()to assert which Events an Item has. - Retries and idempotency. Pass
{ maximumTransientRetries, idempotencyScope }as the fourth argument ofinstance.execute('readwrite', ...)on an instance fromplatform.createInstance. Unit of Work defines both.
Related
Section titled “Related”- Run and test it for the tutorial that introduces the harness.
- Execution model for the boundaries the assertions above follow.
- Runtime conformance for what each suite obliges a Runtime to do.
- Runtime implementations for the three Runtimes the suites run against.
PlatformInstanceandReadWriteExecuteOptionsforexecuteand its retry and idempotency options.

