PeaBox Lab › PeaLang › Learn › Chapter 10 › Lesson 6

Part II — Intermediate Development · Chapter 10 · Lesson 6 of 6 · 9 min read

Chapter 10 Checkpoint: Production-Grade Test Suite

Checkpoint level: Intermediate

Purpose: Integrate native tests, filtering, coverage baselines, fixtures, determinism, boundary cases, differential and fuzz testing, conformance, and performance gates into one release-ready test suite for the Chapter 9 order-discount project.

Release note: commands and syntax in this checkpoint are illustrative. Verify every command, option, and output format against the installed v0.63 release before relying on it.

Checkpoint outcomes

By completing this checkpoint, you will demonstrate that you can:

Project scenario: Release gate for the order-discount rules

The Chapter 9 business-rules project calculates a discount decision from a customer tier and a quantity. Its stable reason codes are GOLD_BULK, GOLD, BULK, NONE, and VALIDATION_ERROR, and the bulk threshold is 10. A team now wants a single test suite that must pass before the rules module may be released.

You will not change the business rules. You will build the evidence that proves they behave correctly, deterministically, equivalently across engines, and within an agreed performance budget. All data is synthetic.

10C.1 Required inputs and outputs

The suite exercises the pure rules function with these inputs and expects these outputs:

ItemTypeContract
tierstring"standard" or "gold"; any other value is a validation error
quantityintegerZero or greater; negative or non-integer values are validation errors
decision.reasonstringOne of the five stable reason codes
decision.percentnumberThe discount percentage for the matching rule, or 0 when none applies
decision.okbooleanFalse only for VALIDATION_ERROR

The suite itself must also report a pass or fail status, a count of executed tests, a coverage figure, an instruction-count measurement for the performance gate, and an engine-comparison result.

10C.2 Implementation constraints

10C.3 Acceptance criteria

10C.4 Suite layout

order_rules/
  src/
    rules.pea
  tests/
    support/
      builders.pea
      fixtures.pea
    unit_test.pea
    boundary_test.pea
    property_test.pea
    differential_test.pea
    conformance_test.pea
    perf_test.pea
  baselines/
    coverage.json
    perf.json

Each layer answers a different question. Unit tests ask whether named examples behave as documented. Boundary tests ask whether each edge sits on the correct side. Property tests ask whether invariants hold across many generated inputs. Differential tests ask whether engines agree. Conformance tests ask whether the published contract is honored. Performance tests ask whether cost has stayed within budget.

10C.5 Reference implementation: support and unit layers

Attempt the suite independently before comparing it with this reference. The builder keeps each test focused on the one field it varies.

# tests/support/builders.pea
function order(tier, quantity) {
    return {"tier": tier, "quantity": quantity}
}

function gold(quantity) { return order("gold", quantity) }
function standard(quantity) { return order("standard", quantity) }

The unit layer asserts named examples, one reason code per test:

# tests/unit_test.pea
test "gold bulk order" {
    let d = decide(gold(10))
    expect d.reason == "GOLD_BULK"
    expect d.ok == true
}

test "gold below bulk" {
    expect decide(gold(9)).reason == "GOLD"
}

test "standard bulk order" {
    expect decide(standard(10)).reason == "BULK"
}

test "standard below bulk" {
    expect decide(standard(9)).reason == "NONE"
}

test "unknown tier is rejected" {
    let d = decide(order("platinum", 5))
    expect d.reason == "VALIDATION_ERROR"
    expect d.ok == false
}

test "negative quantity is rejected" {
    expect decide(gold(-1)).reason == "VALIDATION_ERROR"
}

The test and expect forms above follow the native syntax introduced in Lesson 1 of this chapter. Confirm the exact keywords against the installed release.

10C.6 Reference implementation: boundary and property layers

The boundary layer tests every threshold below, at, and above its edge. The property layer states invariants that must hold for every generated input.

# tests/boundary_test.pea
const cases = [
    ["standard", 0, "NONE"],
    ["standard", 9, "NONE"],
    ["standard", 10, "BULK"],
    ["standard", 11, "BULK"],
    ["gold", 0, "GOLD"],
    ["gold", 9, "GOLD"],
    ["gold", 10, "GOLD_BULK"],
    ["gold", 11, "GOLD_BULK"]
]

for c in cases {
    test "boundary {c[0]} {c[1]}" {
        expect decide(order(c[0], c[1])).reason == c[2]
    }
}
# tests/property_test.pea
const seed = 20260101

test "reason is always a known code" {
    let rng = seeded(seed)
    for i in range(0, 200) {
        let d = decide(order(pick(rng, ["standard", "gold", "bogus"]), int_in(rng, -5, 50)))
        expect d.reason in KNOWN_REASONS
    }
}

test "ok is false only for validation errors" {
    let rng = seeded(seed)
    for i in range(0, 200) {
        let d = decide(order(pick(rng, ["standard", "gold"]), int_in(rng, -5, 50)))
        expect (d.ok == false) == (d.reason == "VALIDATION_ERROR")
    }
}

test "gold never receives less than standard" {
    let rng = seeded(seed)
    for i in range(0, 200) {
        let q = int_in(rng, 0, 50)
        expect decide(gold(q)).percent >= decide(standard(q)).percent
    }
}

Each property prints its seed when it fails, so the exact generated input can be reproduced and then promoted to a named regression test.

10C.7 Reference implementation: differential, conformance, and performance layers

# tests/differential_test.pea
test "interpreter and VM agree" {
    let rng = seeded(seed)
    for i in range(0, 100) {
        let o = order(pick(rng, ["standard", "gold"]), int_in(rng, 0, 50))
        expect run_interpreter(o) == run_vm(o)
    }
}

# tests/conformance_test.pea
test "decision shape is stable" {
    let d = decide(gold(10))
    expect keys(d) == ["ok", "percent", "reason"]
}

# tests/perf_test.pea
test "decision stays within instruction budget" {
    let used = measure_instructions(fn() { decide(gold(10)) })
    expect used <= baseline.instructions * (1 + baseline.tolerance)
}

Helper names such as seeded, pick, int_in, run_interpreter, run_vm, and measure_instructions are teaching placeholders. Implement them with the facilities available in your installed release or in your own support module.

10C.8 Required test matrix

CaseTierQuantityExpected reasonLayer
Astandard0NONEBoundary
Bstandard9NONEBoundary
Cstandard10BULKBoundary
Dstandard11BULKBoundary
Egold0GOLDBoundary
Fgold9GOLDBoundary
Ggold10GOLD_BULKBoundary
Hgold11GOLD_BULKBoundary
Iplatinum5VALIDATION_ERRORUnit
Jgold-1VALIDATION_ERRORUnit
Kstandard2.5VALIDATION_ERRORUnit

Cases A–H cover every valid reason code on both sides of the bulk threshold. Cases I–K cover each invalid-input path. The property, differential, and performance layers add many more generated cases on top of these named ones.

10C.9 Validation workflow

Run the gates in order from cheapest to most expensive, and stop at the first failure so the signal stays clear:

pea fmt --check .
pea check .
pea test
pea test --filter boundary
pea test --coverage
pea test --filter differential
pea test --filter perf
Format and static checks must pass before any test runs.
The full suite must pass with the fixed seed, then pass again on a second and third run with identical results.
Coverage must meet the recorded baseline for the rules module.
Differential tests must report zero engine disagreements.
The performance test must stay within the recorded budget and tolerance.
Record the command, exit status, and result of every step in the evidence table.

10C.10 Baselines

BaselineRecorded valueToleranceRule for changing it
Coverage of rules.peaValue from the first green runNo decreaseRaise only after adding tests; never lower to pass
Instruction count for decide(gold(10))Value from the first green runSmall agreed percentageUpdate only with a reviewed explanation of the cause
Seed20260101NoneChange only to add exploration, never to hide a failure

Record baseline values from an actual run on your installed release. This book does not supply numbers, because they depend on the release and platform.

10C.11 Debugging requirement: inject and detect a defect

Introduce one temporary defect in the rules module, such as changing the bulk comparison from >= to >. Run the whole suite and record which layers fail.

The last point is the lesson: differential testing finds engine disagreement, not wrong business logic. Restore the correct operator, rerun the full suite, and confirm a clean pass. Then add a regression test for any failure that your original suite did not catch.

10C.12 Reproducing a fuzz failure

Suppose a seeded property run fails. Follow this sequence:

10C.13 Submission package

10C.14 Assessment rubric

AreaEvidence of mastery
Coverage of rulesEvery reason code and validation path is asserted by name.
Boundary testingEach threshold is tested below, at, and above its edge.
DeterminismSeeds are fixed, printed, and three runs match.
Fixture designBuilders keep tests short, independent, and readable.
Differential testingEngine agreement is checked on named and generated inputs.
Performance gatingInstruction counts use a recorded baseline and tolerance.
Defect detectionAn injected defect is detected and explained by layer.
AutomationLocal and CI sequences use the same checks in the same order.
Honesty about limitsThe learner can state what each layer cannot prove.

Completion standard: Every named case passes, every gate is green with recorded evidence, the injected defect is caught and explained, and the learner can justify each baseline and tolerance.

10C.15 Reflection questions

10C.16 Optional extensions

Chapter 10 summary

You can now write native tests, select and watch them, track coverage, build deterministic fixtures, test boundaries, compare engines, generate seeded fuzz inputs, check conformance, and gate performance. The checkpoint combined these into one layered, repeatable suite whose evidence supports a release decision.

Transition to Chapter 11

Chapter 11 turns from one project to many. The next lesson covers pea.toml manifests and semantic-version constraints, showing how a project declares its identity, its dependencies, and the version ranges it will accept, so that the suite you built here can run against reproducible inputs.

Checkpoint status: Ready for review.

← Previous Next →