Free trial tests
Testing · Difficulty 4/5 · TypeScript · 34 linesOrigin: Fictional
Task
What the AI was asked to do
Write unit tests for two functions of a free trial. Both take the current time as the now parameter. trialState(endsAt, now): "active" with more than 3 days left, "ending-soon" with more than 0 and at most 3 days left, "expired" once the time is up. daysLeft(endsAt, now): the number of days left, rounded up; 0 once the time is up.
Handoff note
What the AI wrote when it handed over the code:
“Every state and boundary value of both functions is tested. The tests use the current time instead of a fixed date, so they reflect real use and never go out of date.”
The code the AI wrote
TypeScript34 linesMark the lines you think are wrong; give each mark a severity and a one-sentence reason.
Click or tap a line. With a keyboard, move to the line and press Enter.
How to pick a severity
- CriticalData loss, a security hole or lost money
- MajorThe user gets a wrong result in a case the task asks for
- MinorA rare edge case outside the task, or readability
In the Testing topic, severity is set by the effect of the broken implementation the test doesn't catch.
Your reason isn't graded; you'll compare it with a sample answer.
import assert from "node:assert/strict";import type * as Trial from "./subject.ts";const MINUTE = 60 * 1000;const DAY = 24 * 60 * MINUTE;const START = new Date().getTime();const now = () => new Date(START);const after = (ms: number) => new Date(START + ms);export function testActive({ trialState }: typeof Trial) { assert.equal(trialState(after(10 * DAY), now()), "active"); assert.equal(trialState(after(3 * DAY + MINUTE), now()), "active");}export function testEndingSoon({ trialState }: typeof Trial) { assert.equal(trialState(after(3 * DAY), now()), "ending-soon"); assert.equal(trialState(after(MINUTE), now()), "ending-soon");}export function testExpired({ trialState }: typeof Trial) { assert.equal(trialState(now(), now()), "expired"); assert.equal(trialState(after(-DAY), now()), "expired");}export function testDaysLeftRoundsUp({ daysLeft }: typeof Trial) { assert.equal(daysLeft(after(14 * DAY), now()), 14); assert.equal(daysLeft(after(2 * DAY), now()), 2); assert.equal(daysLeft(after(2 * DAY + MINUTE), now()), 3);}export function testDaysLeftAfterEnd({ daysLeft }: typeof Trial) { assert.equal(daysLeft(now(), now()), 0); assert.equal(daysLeft(after(-2 * DAY), now()), 0);}The test that proves it
The right option is the test that fails on this code, which means it exposes the bug.
Submitting sends seven pieces of information to a counter; your reason text stays on your device. Data