Skip to content

Testing package

@knetikdotdev/testing runs a module through the real executor on in-memory SQLite, with the agent framework in process. The model is scripted and connectors are mocks. No server, no Docker, no network and no API key. A normal test takes a fraction of a second.

Run tests with knetik test (see The knetik CLI); they use Node’s built-in test runner.

module.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mockConnector, runModule } from '@knetikdotdev/testing';
import mod from './knetik.module.ts';
import { Greeter } from './agents/greeter.ts';
test('posts to Slack only when connected and a channel is configured', async () => {
const slack = mockConnector({ 'chat.postMessage': { ok: true } });
const r = await runModule(mod, {
agents: { greeter: Greeter },
input: { name: 'Ada' },
model: [greetingsJson],
gates: { choose: 'warm' },
config: { slackChannel: '#welcome' },
connectors: { slack },
});
assert.equal(r.status, 'completed', r.error ?? '');
assert.equal(slack.calls.length, 1);
});
Option Meaning
agents The module’s agents keyed by file name without extension
input The start form values
config Module configuration values
model Scripted replies for agents (see below)
gates Decisions by gate stage id
connectors Mock connectors that count as connected. Anything not listed is not connected.
ai Scripted ctx.ai: chat, search and image
pages Pages ctx.fetchPage and the fetch_page tool can read, by URL, or a function
timeoutMs Time limit

Runs are queued one at a time, because the agent framework’s runtime is process-wide.

model is an ordered list of replies. Each reply is a string, or a function of the prompt and transcript. One function can answer every call instead. A reply can also be a tool call for the agent to make: { tool: 'web_search', arguments: { query: '...' } }.

gates maps a gate stage id to a decision: a choice string, or { choice, note, edits }. A list queues several decisions, for example a send-back followed by a pick. A gate with no decision fails with a message naming the stage and its options, instead of hanging.

gates: { 'pick-angle': [{ choice: 'regenerate', note: 'more practical' }, 'a2'] }

mockConnector({ operation: handlerOrValue }) returns a connector. A handler is a function of the params, or a fixed value. It records every call in .calls. An operation you did not list throws, so a test notices an unexpected call.

Property Meaning
status completed, failed or rejected
error The failure message, or null
outputs Stage outputs by stage id
gates Decisions by gate id
events The run timeline: { type, stage, data }
artifacts Files by name, each with contentType, size, bytes and text
prompts The prompts the model was asked, in order
aiCalls What code stages asked of ctx.ai and ctx.fetchPage

Connector calls appear in events as connector.call, so you can assert that they are recorded.

  • The happy path, with gate decisions, checking artifacts.
  • A model reply that is not valid JSON fails the run with “could not parse agent output”.
  • An optional connector present and absent.
  • A gate refusing an unknown choice.
  • A send-back, checking ctx.feedback reached the prompt (r.prompts).