Joel Stevick

Chapter 3 · section 4 of 6

Proving it

The checker is pure: two strings in, a decision out, no network. That makes it the first thing in this pipeline with a real test, and the test is the record of every failure the checker exists to catch.

Listing: ask/check.test.ts

import test from "node:test";
import assert from "node:assert/strict";
import { checkAnswer, citedPages, pagesNamedIn } from "./check.js";
import type { Law } from "./corpus.js";

const HYDRANT: Law = {
  id: "MD-S01",
  law: "You may not stand or park a vehicle within 15 feet of a fire hydrant.",
  question: "How far from a fire hydrant must you park?",
  concept: "park-hydrant",
  citation: "Md. Code, Transp. § 21-1003(m) — 'A person may not stand or park a vehicle within 15 feet of a fire hydrant.'",
  kind: "statute",
};

const BEAMS: Law = {
  id: "MD-102",
  law: "You must change to low beams when following a vehicle that is 300 feet or less in front of you.",
  question: "How close behind a vehicle must you switch to low beams?",
  concept: "reduced-visibility",
  citation: "p.27 (printed) — 'Change to low beams when following a vehicle that is 300 feet or less in front of you.'",
};

const SPELLED: Law = {
  id: "XX-C-1",
  law: "A driver may not follow closer than five hundred feet behind a fire apparatus.",
  question: "",
  concept: "",
  citation: "Xx. Code § 1(a) — 'closer than five hundred feet'",
  kind: "statute",
};

const GIVEN = [HYDRANT, BEAMS, SPELLED];

const reply = (answer: string, sources: string[]) => ({
  kind: "reply" as const,
  scope: "road-law" as const,
  answer,
  sources,
});

test("an answer citing a law it was given, with that law's figure, is shown", () => {
  const checked = checkAnswer(reply("Stay at least 15 feet from a hydrant.", ["MD-S01"]), GIVEN);
  assert.equal(checked.ok, true);
  if (checked.ok) assert.deepEqual(checked.laws.map((law) => law.id), ["MD-S01"]);
});

test("a law the model was never given is refused as uncited", () => {
  const checked = checkAnswer(reply("Stay 15 feet away.", ["MD-S99"]), GIVEN);
  assert.deepEqual(checked, { ok: false, reason: "uncited" });
});

test("a figure no cited law states is refused as ungrounded", () => {
  const checked = checkAnswer(reply("Stay at least 20 feet from a hydrant.", ["MD-S01"]), GIVEN);
  assert.deepEqual(checked, { ok: false, reason: "ungrounded" });
});

test("the page reference grounds nothing: p.27 must not ground 27 feet", () => {
  const checked = checkAnswer(reply("Dim your lights within 27 feet.", ["MD-102"]), GIVEN);
  assert.deepEqual(checked, { ok: false, reason: "ungrounded" });
});

test("a figure the learner wrote may be said back", () => {
  const checked = checkAnswer(
    reply("Yes: 20 feet is more than the 15 feet the law asks for.", ["MD-S01"]),
    GIVEN,
    "can i park 20 feet from a hydrant",
  );
  assert.equal(checked.ok, true);
});

test("a spelled figure in the law grounds its digits in the answer", () => {
  const checked = checkAnswer(reply("Stay 500 feet behind a fire truck.", ["XX-C-1"]), GIVEN);
  assert.equal(checked.ok, true);
});

test("a page named in the answer that no cited law is on is refused", () => {
  const checked = checkAnswer(reply("See p.66: dim within 300 feet.", ["MD-102"]), GIVEN);
  assert.deepEqual(checked, { ok: false, reason: "uncited" });
});

test("an empty answer, or one with no sources, is not covered", () => {
  assert.deepEqual(checkAnswer(reply("", ["MD-S01"]), GIVEN), { ok: false, reason: "not-covered" });
  assert.deepEqual(checkAnswer(reply("15 feet.", []), GIVEN), { ok: false, reason: "not-covered" });
});

test("pages are read from citations and from prose", () => {
  assert.deepEqual(citedPages("pp.12-13 (printed) — '…'"), ["12", "13"]);
  assert.deepEqual(citedPages("Md. Code, Transp. § 21-1003(m) — '…'"), []);
  assert.deepEqual(pagesNamedIn("see pages 12 and 13, and p. 66"), ["12", "13", "66"]);
});

Run it:

npm test

Nine tests, green, in a few milliseconds, because nothing in them touched a network. Chapter 12 is about why.

Rule: A test that passes against the checker as written must fail against the checker with one check removed. Delete the pagesNamedIn line from checkAnswer and run the suite: the page test goes red. Put it back. A test that stays green with its check deleted is a test of nothing, and this is the cheapest moment to find out.