Give Your Agent Checks to Run

In the last video, an agent ran npm test, read the failure, edited the function, and ran the test again until it passed. For that loop, your agent has to know which command to run and be allowed to run it without your approval on every pass.

In this lesson, you’ll read and run your project’s tests, so you see what your agent sees. Then you’ll name the test commands in your instruction file, let your agent run them without asking, and end task 2’s done-when with a command. Start with the tests themselves and what a check says when it fails.

Read and Run the Project’s Tests

Before you hand a check to your agent, make sure you can read it, because its failures are what the agent works from. Here’s the start of the reference version’s test file after task 1. Your agent wrote yours, so its names and layout will differ:

Language: JavaScript Filename: test/check-room.test.js
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import {
  checkRoom,
  unreachablePuzzles,
} from "../src/check-room.js";

function loadRoom(file) {
  const path = new URL(`../rooms/${file}`, import.meta.url);
  return JSON.parse(readFileSync(path, "utf8"));
}

The first two imports are Node.js’s built-in testing tools: test() from node:test and assert from node:assert/strict. The file’s loadRoom() helper reads a sample room from rooms/ by filename.

The tests come next in the same file:

Language: JavaScript Filename: test/check-room.test.js
test("gives each sample room its verdict", () => {
  const study = loadRoom("lighthouse-keepers-study.json");
  const cellar = loadRoom("the-smugglers-cellar.json");
  assert.equal(checkRoom(study), "can be finished");
  assert.equal(checkRoom(cellar), "cannot be finished");
});

test("returns the ids of the puzzles no path reaches", () => {
  const study = loadRoom("lighthouse-keepers-study.json");
  const cellar = loadRoom("the-smugglers-cellar.json");
  assert.deepEqual(unreachablePuzzles(study), []);
  assert.deepEqual(unreachablePuzzles(cellar), [
    "strongbox",
    "trapdoor",
  ]);
});

Each test() call takes a name and a function. Inside the function, each assertion compares what the code returned, first, with what you expect, second.

Use assert.equal() for numbers and strings, and assert.deepEqual() for arrays and objects, because it compares their contents rather than asking whether they’re the same object.

Run the whole suite from your project folder:

Language: Shell
$ npm test

> [email protected] test
> node --test

✔ gives each sample room its verdict (2.026089ms)
✔ returns the ids of the puzzles no path reaches (1.287771ms)
ℹ tests 2
ℹ suites 0
ℹ pass 2
ℹ fail 0
...

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.