Skip to content

Currency conversion tests

Testing · Difficulty 3/5 · TypeScript · 39 linesOrigin: Fictional

Task

What the AI was asked to do

Write unit tests for convertPrice(amount, from, to, rates). The function gets the rate with rates.rate(from, to) (how many units of to one unit of from buys), converts the amount and rounds it to cents. With the same currency it returns the amount without asking for a rate; if the rate cannot be fetched, the error is passed on. In tests the rate service is a mock.

Handoff note

What the AI wrote when it handed over the code:

“Tested conversion, rounding to cents, the same currency and a failing rate service. The rate service is a mock, so the tests never touch the network.”

The code the AI wrote

TypeScript39 lines

Mark 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.

0 of 5 marks
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 Pricing from "./subject.ts";
3
const rates: Pricing.RateSource = {
  rate: async () => 1.25,
};
7
export async function testConvertsWithRate({ convertPrice }: typeof Pricing) {
  assert.equal(await convertPrice(10, "EUR", "USD", rates), 12.5);
}
11
export async function testRoundsToCents({ convertPrice }: typeof Pricing) {
  assert.equal(await convertPrice(0.99, "EUR", "USD", rates), 1.24);
  assert.equal(await convertPrice(10.01, "EUR", "USD", rates), 12.51);
}
16
export async function testSameCurrency({ convertPrice }: typeof Pricing) {
  let lookups = 0;
  const counting: Pricing.RateSource = {
    rate: async () => {
      lookups += 1;
      return 2;
    },
  };
  assert.equal(await convertPrice(10, "EUR", "EUR", counting), 10);
  assert.equal(lookups, 0);
}
28
export async function testRateFailure({ convertPrice }: typeof Pricing) {
  const failing: Pricing.RateSource = {
    rate: async () => {
      throw new Error("rate service down");
    },
  };
  await assert.rejects(
    convertPrice(10, "EUR", "USD", failing),
    /rate service down/,
  );
}

The test that proves it

The right option is the test that fails on this code, which means it exposes the bug.

Test options
Mark at least one line to submit.

Submitting sends seven pieces of information to a counter; your reason text stays on your device. Data