← All exercises

The age validator that only tested the happy path

The signup form of a consumer web app · difficulty 1/3 · ~5 min

A teammate asked the AI assistant: "Write `parseAge(input)` for the signup form. It takes the raw string from the field and returns the age as a number. Reject anything that is not a whole number between 0 and 130: throw an Error so the form can show a message. Add tests." The code came back with a docstring that spells out the contract and three passing tests. You are reviewing both the function and its tests before they are merged. The contract promised by the docstring: - Surrounding whitespace is ignored. - Valid input is a whole number between 0 and `MAX_AGE` (130), inclusive. - Empty input, non-numeric text, decimals, negative values and values above `MAX_AGE` are rejected by throwing. - A "repaired" value must never be returned silently; the caller depends on the throw. The tests pass. Decide whether that means anything.

code.ts

Visible tests (they pass on the code as given)
import { describe, it, expect } from "@debunk/harness";
import { parseAge, validateAge } from "./code";

describe("parseAge", () => {
  it("parses a plain age", () => {
    expect(parseAge("42")).toBe(42);
  });

  it("ignores surrounding whitespace", () => {
    expect(parseAge("  27 ")).toBe(27);
  });

  it("validateAge reports non-numeric input as an error", () => {
    const r = validateAge("abc");
    expect(r.ok).toBe(false);
  });
});