Skip to content

Run and test it

Run examples/task-app.test.ts: a Vitest file that builds the App from Your first application, creates a Workspace on the test Runtime, completes a task and checks each effect at the moment it becomes visible. Each step adds one block to the test and shows the whole file with that block highlighted. At the end you know which assertions hold when a write returns and which hold only after the reactor runs.

  • examples/task-app.ts from the previous page.
  • The harness: createHarnessInstance(services, spec) builds the Definition, registers it on a test platform service and returns createWorkspace, tickAll and advance. It takes a system, logger, cron and platform service. The test implementation of each comes from the packages the Introduction lists.
  • The repository checkout with dependencies installed.

Create the four test services, pass them with taskApp to createHarnessInstance, and call harness.createWorkspace().

examples/task-app.test.ts
import type { SystemWriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';
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 { taskApp } from './task-app';
describe('task tutorial', () => {
it('completes a task, runs the Listener and appends the Event', async () => {
const system = createTestSystemService();
const logger = createTestLoggerService();
const cron = createTestCronService({ system });
const platform = createTestPlatformService({ cron, logger, system });
const harness = await createHarnessInstance({ cron, logger, platform, system }, taskApp);
const workspace = await harness.createWorkspace();
// Create a task below the Workspace Item.
const id = await workspace.write(async (ctx) => {
const parent = await ctx.getWorkspaceItem();
const task = await parent.createChild(ctx.apps.tutorialTasks.templates.task, {
key: 'firstTask',
properties: { title: 'Write the documentation' },
});
return task.id;
});
// Run the Mutation in its own write execution.
await workspace.write(async (ctx) => {
const current = ctx.apps.tutorialTasks;
await current.mutations.complete.handler({ id }, current, ctx);
});
// The transition committed with the write. The Listener has not run.
await workspace.read(async (ctx) => {
const current = ctx.apps.tutorialTasks;
const task = await current.templates.task.getItem(id);
expect(await current.queries.title.handler({ id }, current, ctx)).toBe('Write the documentation');
expect(task?.state?.value).toBe('done');
expect(task?.properties.completionNoted).toBeUndefined();
});
// Drive the reactor until it has no Events left to process.
await harness.tickAll();
await workspace.read(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
expect(task?.properties.completionNoted).toBe(true);
});
// The Event is durable history on the task.
await workspace.write<void, SystemWriteRuntimeInstance>(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
if (task === undefined) throw new Error('Task not found');
const events = await ctx.selectEvents().itemId(id).execute();
expect(events.results.some((event) => event.eventPrn === task.events.completed.prn)).toBe(true);
});
expect.assertions(5);
});
});

workspace exposes read(fn) and write(fn). Each call creates a Runtime instance for that Workspace and runs fn in a read or write execution. fn receives a ReadRuntimeInstance or a WriteRuntimeInstance. The App’s Items live under the Workspace Item that createWorkspace made.

In a write execution, get the Workspace Item with ctx.getWorkspaceItem() and call parent.createChild(template, { key, properties }) with the task Template from ctx.apps.tutorialTasks. Return the new Item’s id.

examples/task-app.test.ts
import type { SystemWriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';
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 { taskApp } from './task-app';
describe('task tutorial', () => {
it('completes a task, runs the Listener and appends the Event', async () => {
const system = createTestSystemService();
const logger = createTestLoggerService();
const cron = createTestCronService({ system });
const platform = createTestPlatformService({ cron, logger, system });
const harness = await createHarnessInstance({ cron, logger, platform, system }, taskApp);
const workspace = await harness.createWorkspace();
// Create a task below the Workspace Item.
const id = await workspace.write(async (ctx) => {
const parent = await ctx.getWorkspaceItem();
const task = await parent.createChild(ctx.apps.tutorialTasks.templates.task, {
key: 'firstTask',
properties: { title: 'Write the documentation' },
});
return task.id;
});
// Run the Mutation in its own write execution.
await workspace.write(async (ctx) => {
const current = ctx.apps.tutorialTasks;
await current.mutations.complete.handler({ id }, current, ctx);
});
// The transition committed with the write. The Listener has not run.
await workspace.read(async (ctx) => {
const current = ctx.apps.tutorialTasks;
const task = await current.templates.task.getItem(id);
expect(await current.queries.title.handler({ id }, current, ctx)).toBe('Write the documentation');
expect(task?.state?.value).toBe('done');
expect(task?.properties.completionNoted).toBeUndefined();
});
// Drive the reactor until it has no Events left to process.
await harness.tickAll();
await workspace.read(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
expect(task?.properties.completionNoted).toBe(true);
});
// The Event is durable history on the task.
await workspace.write<void, SystemWriteRuntimeInstance>(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
if (task === undefined) throw new Error('Task not found');
const events = await ctx.selectEvents().itemId(id).execute();
expect(events.results.some((event) => event.eventPrn === task.events.completed.prn)).toBe(true);
});
expect.assertions(5);
});
});

The task is a child of the Workspace Item with the key firstTask and one Property. It starts in the open state because setDefault marks it. Handlers reach the App by its canonical name, tutorialTasks, not by the dependency key tasks the root App chose. The write commits when the callback returns, and id addresses the task from then on.

In a second write execution, take ctx.apps.tutorialTasks as current and call current.mutations.complete.handler({ id }, current, ctx).

examples/task-app.test.ts
import type { SystemWriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';
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 { taskApp } from './task-app';
describe('task tutorial', () => {
it('completes a task, runs the Listener and appends the Event', async () => {
const system = createTestSystemService();
const logger = createTestLoggerService();
const cron = createTestCronService({ system });
const platform = createTestPlatformService({ cron, logger, system });
const harness = await createHarnessInstance({ cron, logger, platform, system }, taskApp);
const workspace = await harness.createWorkspace();
// Create a task below the Workspace Item.
const id = await workspace.write(async (ctx) => {
const parent = await ctx.getWorkspaceItem();
const task = await parent.createChild(ctx.apps.tutorialTasks.templates.task, {
key: 'firstTask',
properties: { title: 'Write the documentation' },
});
return task.id;
});
// Run the Mutation in its own write execution.
await workspace.write(async (ctx) => {
const current = ctx.apps.tutorialTasks;
await current.mutations.complete.handler({ id }, current, ctx);
});
// The transition committed with the write. The Listener has not run.
await workspace.read(async (ctx) => {
const current = ctx.apps.tutorialTasks;
const task = await current.templates.task.getItem(id);
expect(await current.queries.title.handler({ id }, current, ctx)).toBe('Write the documentation');
expect(task?.state?.value).toBe('done');
expect(task?.properties.completionNoted).toBeUndefined();
});
// Drive the reactor until it has no Events left to process.
await harness.tickAll();
await workspace.read(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
expect(task?.properties.completionNoted).toBe(true);
});
// The Event is durable history on the task.
await workspace.write<void, SystemWriteRuntimeInstance>(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
if (task === undefined) throw new Error('Task not found');
const events = await ctx.selectEvents().itemId(id).execute();
expect(events.results.some((event) => event.eventPrn === task.events.completed.prn)).toBe(true);
});
expect.assertions(5);
});
});

This is the same call a Runtime makes when a principal invokes the Mutation. Inside the callback the handler executes the complete transition and publishes completed. Both belong to this write execution: the state change and the appended Event commit together when the callback returns, and neither exists if it throws.

In a read execution, invoke the title Query, then read the task through current.templates.task.getItem(id) and check its state and its completionNoted Property.

examples/task-app.test.ts
import type { SystemWriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';
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 { taskApp } from './task-app';
describe('task tutorial', () => {
it('completes a task, runs the Listener and appends the Event', async () => {
const system = createTestSystemService();
const logger = createTestLoggerService();
const cron = createTestCronService({ system });
const platform = createTestPlatformService({ cron, logger, system });
const harness = await createHarnessInstance({ cron, logger, platform, system }, taskApp);
const workspace = await harness.createWorkspace();
// Create a task below the Workspace Item.
const id = await workspace.write(async (ctx) => {
const parent = await ctx.getWorkspaceItem();
const task = await parent.createChild(ctx.apps.tutorialTasks.templates.task, {
key: 'firstTask',
properties: { title: 'Write the documentation' },
});
return task.id;
});
// Run the Mutation in its own write execution.
await workspace.write(async (ctx) => {
const current = ctx.apps.tutorialTasks;
await current.mutations.complete.handler({ id }, current, ctx);
});
// The transition committed with the write. The Listener has not run.
await workspace.read(async (ctx) => {
const current = ctx.apps.tutorialTasks;
const task = await current.templates.task.getItem(id);
expect(await current.queries.title.handler({ id }, current, ctx)).toBe('Write the documentation');
expect(task?.state?.value).toBe('done');
expect(task?.properties.completionNoted).toBeUndefined();
});
// Drive the reactor until it has no Events left to process.
await harness.tickAll();
await workspace.read(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
expect(task?.properties.completionNoted).toBe(true);
});
// The Event is durable history on the task.
await workspace.write<void, SystemWriteRuntimeInstance>(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
if (task === undefined) throw new Error('Task not found');
const events = await ctx.selectEvents().itemId(id).execute();
expect(events.results.some((event) => event.eventPrn === task.events.completed.prn)).toBe(true);
});
expect.assertions(5);
});
});

Three assertions hold at this boundary. The Query returns the title written in step 2. The state is done, because the transition committed with the Mutation’s write. completionNoted is undefined, because the Listener has not run: publish appended the Event, and the reactor has not processed it. A read execution sees committed data only.

Call await harness.tickAll(), then read the task again and check completionNoted.

examples/task-app.test.ts
import type { SystemWriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';
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 { taskApp } from './task-app';
describe('task tutorial', () => {
it('completes a task, runs the Listener and appends the Event', async () => {
const system = createTestSystemService();
const logger = createTestLoggerService();
const cron = createTestCronService({ system });
const platform = createTestPlatformService({ cron, logger, system });
const harness = await createHarnessInstance({ cron, logger, platform, system }, taskApp);
const workspace = await harness.createWorkspace();
// Create a task below the Workspace Item.
const id = await workspace.write(async (ctx) => {
const parent = await ctx.getWorkspaceItem();
const task = await parent.createChild(ctx.apps.tutorialTasks.templates.task, {
key: 'firstTask',
properties: { title: 'Write the documentation' },
});
return task.id;
});
// Run the Mutation in its own write execution.
await workspace.write(async (ctx) => {
const current = ctx.apps.tutorialTasks;
await current.mutations.complete.handler({ id }, current, ctx);
});
// The transition committed with the write. The Listener has not run.
await workspace.read(async (ctx) => {
const current = ctx.apps.tutorialTasks;
const task = await current.templates.task.getItem(id);
expect(await current.queries.title.handler({ id }, current, ctx)).toBe('Write the documentation');
expect(task?.state?.value).toBe('done');
expect(task?.properties.completionNoted).toBeUndefined();
});
// Drive the reactor until it has no Events left to process.
await harness.tickAll();
await workspace.read(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
expect(task?.properties.completionNoted).toBe(true);
});
// The Event is durable history on the task.
await workspace.write<void, SystemWriteRuntimeInstance>(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
if (task === undefined) throw new Error('Task not found');
const events = await ctx.selectEvents().itemId(id).execute();
expect(events.results.some((event) => event.eventPrn === task.events.completed.prn)).toBe(true);
});
expect.assertions(5);
});
});

tickAll ticks the registered components in a loop until none reports work. The reactor claims the committed completed Event and runs noteCompletion in a write execution of its own. That execution sets completionNoted to true and commits. The fourth assertion holds only after this call. A Listener effect is never visible to the read that follows the Mutation, whatever the Runtime.

In a write execution typed as SystemWriteRuntimeInstance, select the task’s Events with ctx.selectEvents().itemId(id).execute() and check that one carries the completed Event’s PRN.

examples/task-app.test.ts
import type { SystemWriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';
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 { taskApp } from './task-app';
describe('task tutorial', () => {
it('completes a task, runs the Listener and appends the Event', async () => {
const system = createTestSystemService();
const logger = createTestLoggerService();
const cron = createTestCronService({ system });
const platform = createTestPlatformService({ cron, logger, system });
const harness = await createHarnessInstance({ cron, logger, platform, system }, taskApp);
const workspace = await harness.createWorkspace();
// Create a task below the Workspace Item.
const id = await workspace.write(async (ctx) => {
const parent = await ctx.getWorkspaceItem();
const task = await parent.createChild(ctx.apps.tutorialTasks.templates.task, {
key: 'firstTask',
properties: { title: 'Write the documentation' },
});
return task.id;
});
// Run the Mutation in its own write execution.
await workspace.write(async (ctx) => {
const current = ctx.apps.tutorialTasks;
await current.mutations.complete.handler({ id }, current, ctx);
});
// The transition committed with the write. The Listener has not run.
await workspace.read(async (ctx) => {
const current = ctx.apps.tutorialTasks;
const task = await current.templates.task.getItem(id);
expect(await current.queries.title.handler({ id }, current, ctx)).toBe('Write the documentation');
expect(task?.state?.value).toBe('done');
expect(task?.properties.completionNoted).toBeUndefined();
});
// Drive the reactor until it has no Events left to process.
await harness.tickAll();
await workspace.read(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
expect(task?.properties.completionNoted).toBe(true);
});
// The Event is durable history on the task.
await workspace.write<void, SystemWriteRuntimeInstance>(async (ctx) => {
const task = await ctx.apps.tutorialTasks.templates.task.getItem(id);
if (task === undefined) throw new Error('Task not found');
const events = await ctx.selectEvents().itemId(id).execute();
expect(events.results.some((event) => event.eventPrn === task.events.completed.prn)).toBe(true);
});
expect.assertions(5);
});
});

The Event is durable history beside the task’s current state; processing it did not remove it. task.events.completed.prn is prn::tutorialTasks:template/task/event/completed, so the test compares PRNs instead of string literals. selectEvents is a system-context method, which is why this block declares the context type. expect.assertions(5) confirms that every expect inside the callbacks ran.

From the repository root, run the file with the repository test command.

Terminal window
pnpm run cli test alpynehq/alpyne/website/examples/task-app.test.ts

You should see:

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

The projector updates the Metric from step 7 of the previous page after the write commits, separately from the Mutation and from the reactor. Measure with metrics queries it.

  • Steps 1 to 3 create a Workspace and run a Mutation through the harness. Test an app explains the harness, advance and running against other Runtimes.
  • Steps 4 and 5 show the two boundaries: a write’s own changes are visible when it returns, and a Listener’s changes are visible after tickAll. Execution model explains executions, commit and the Event lifecycle.
  • Step 6 reads the Event history that publish appended. Handle events explains publish, immediate and Listener failure.
  • Test an app to choose assertions per boundary and run conformance suites.
  • Handle events to react to Events with Listeners, parent Listeners and schedules.
  • Execution model for read and write executions, attempts and retries.
  • Grant access to grant the Mutation and Query to Roles and test them as a principal.
  • PlatformInstance for execute, the method the harness calls for every read and write.