Skip to Content
harness-kit

Testing a configuration

Prove what your configuration refuses and lets through with runHook, in-process tests that answer calls exactly as the hook does.

You'll: write tests that prove which calls your configuration refuses and which everyday calls it lets through. @microeinhundert/harness-kit/testing answers a hook call in-process, as the real hook would, and returns its exit code, standard output and standard error.

Before you start

  • A harness.config.ts to test, and a test runner. The example uses @effect/vitest, whose it.effect runs an Effect as a test.
  • runHook runs every Policy of the configuration, the kit's own protection Policies included, and the Policies that act after the answer.
  • runHook runs commands and git where the call says. A test of a stop check or a session-start check needs a temporary repository of its own.

Test a refusal and a pass

This test proves the custom .env Policy: it refuses a write of .env with its reason and fix, and lets a write of .env.example through:

custom-policy.test.ts
import { assert, layer } from '@effect/vitest';
import { Effect, Layer, Option, Path } from '@microeinhundert/harness-kit/effect';
import { NodeServices } from '@microeinhundert/harness-kit/effect/platform-node';
import { checkoutAt, inMemorySessionState, preToolUse, runHook } from '@microeinhundert/harness-kit/testing';

/**
 * What the hook needs to run in a test: Node's file system and processes, and a session state in memory, with the
 * session working on a branch of a repository at a made-up path.
 */
const SERVICES = Layer.merge(NodeServices.layer, inMemorySessionState([checkoutAt('/work/repository', 'feat/abc-1-env')]));

/**
 * Answers a `PreToolUse` call of the `Write` tool on the given file as the hook would, from the configuration beside
 * this file.
 */
const answerWrite = Effect.fn('answerWrite')(function* (filePath: string) {
  const path = yield* Path.Path;
  const call = preToolUse('true');

  return yield* runHook(path.join(import.meta.dirname, 'custom-policy.config.ts'), {
    ...call,
    toolInput: {
      ...call.toolInput,
      filePath: Option.some(filePath),
    },
    toolName: 'Write',
  });
});

layer(SERVICES, {
  buildPerTest: true,
})('no-env-files', (it) => {
  it.effect('refuses a write of a .env file, with the reason and the fix', () => {
    return Effect.gen(function* () {
      const run = yield* answerWrite('/work/repository/.env');

      assert.strictEqual(run.exitCode, 2);
      assert.include(run.stderr, 'An agent does not write .env files, which hold secrets.\nFix: Write the variable into .env.example and ask a person to set it.');
    });
  });

  it.effect('lets a write of any other file through', () => {
    return Effect.gen(function* () {
      const run = yield* answerWrite('/work/repository/.env.example');

      assert.strictEqual(run.exitCode, 0);
    });
  });
});

The configuration is loaded from its file, just as the hook loads it, so the test also catches an option the configuration does not accept.

Decide what to test

  • Each refusal you rely on, with the text the agent reads. When an upgrade of the kit changes a message, the test tells you.
  • The everyday commands of your team (git status, npm test, your build), which must pass. A Policy that refuses the routine is worse than none.
  • That the configuration loads. A call that every Policy allows, such as preToolUse('touch notes.txt'), exits with 0 and writes nothing to standard error only when the configuration is valid; a broken one refuses it.

If something goes wrong

  • A test keeps running an old configuration. Node loads a harness.config.ts once per process and serves later loads from its cache, so a test that rewrites a configuration file in place keeps running the old one. Write each variant to a file of its own.
  • A call you expected to pass is refused. Run harness-kit explain with the same call, as Debugging a configuration shows.
  • A test of a stop or session-start check behaves differently on your machine. Give it a temporary repository so the real checkout does not leak in.

What the testing module exports

On this page