Appendix B · section 12 of 32
ask/check.test.ts
From The Check That Is Not the Model.
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"]);
});