← All exercises

Shipping tests that only confirm the code

The checkout service of an online store · Tests · difficulty 2/3 · ~8 min

The checkout service needs a shipping fee calculator. A teammate gave the AI assistant the pricing rules from the product spec and asked for "calculateShipping(order) with tests". Both the function and its tests came back in one go, and the test run is green.

You are reviewing the function and its tests together. The visible tests are the ones the assistant wrote; treat them as part of the code under review, not as an independent oracle.

The pricing rules from the spec:

Decide whether the tests actually prove the rules, and whether the implementation follows them.

Repair the code until the hidden tests pass.

code.ts

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

describe("calculateShipping", () => {
  it("ships free above the threshold", () => {
    expect(calculateShipping({ subtotal: 150, weightKg: 1 })).toBe(0);
  });

  it("charges the base rate plus the per-kg surcharge", () => {
    expect(calculateShipping({ subtotal: 40, weightKg: 1 })).toBe(BASE_RATE);
    expect(calculateShipping({ subtotal: 40, weightKg: 4 })).toBe(BASE_RATE + 2 * PER_EXTRA_KG);
  });

  it("adds the express supplement", () => {
    expect(calculateShipping({ subtotal: 40, weightKg: 1, express: true })).toBe(
      BASE_RATE + EXPRESS_SUPPLEMENT,
    );
  });
});