Write Tests for a Currency Exchange Calculator
You are given a CurrencyCalculator class in src/CurrencyCalculator.ts. It supports registering exchange rates and converting amounts between currencies.
Your job is to write a Vitest test suite in tests/CurrencyCalculator.test.ts that:
- Passes on the correct reference implementation.
- Catches each of the 4 known bugs described below.
The judge runs your tests against the reference and against 4 intentionally broken implementations. A test suite that only checks the happy path will miss the bugs.
The 4 bugs your tests must catch:
- Inverted rate —
convertapplies the rate in the wrong direction (divides instead of multiplies, or swaps from/to). - Missing rounding —
convertreturns a raw floating-point result without rounding to 2 decimal places. - No chaining —
convertonly looks up direct rates; it does not find a path through an intermediate currency. - Same-currency bug —
convert(amount, 'USD', 'USD')returns a wrong value instead ofamountrounded to 2 decimal places.
- Write tests in
tests/CurrencyCalculator.test.ts. The test file is the only editable file. - Tests must pass on the reference implementation — no false positives.
- At least one test must fail on the inverted-rate variant.
- At least one test must fail on the missing-rounding variant.
- At least one test must fail on the no-chaining variant.
- At least one test must fail on the same-currency-bug variant.
calc.setRate('USD', 'EUR', 1.1);
calc.convert(10, 'USD', 'EUR');11.00
10 * 1.1 = 11.0, rounded to 2 decimal places = 11.00. An inverted-rate bug would return ~9.09 instead.
calc.setRate('USD', 'EUR', 1.1);
calc.setRate('EUR', 'GBP', 0.9);
calc.convert(100, 'USD', 'GBP');99.00
100 USD × 1.1 = 110 EUR × 0.9 = 99.0 GBP. A no-chaining implementation throws or returns an error.
calc.setRate('USD', 'EUR', 3.33333);
calc.convert(1, 'USD', 'EUR');3.33
1 * 3.33333 = 3.33333 — must be rounded to 3.33. A missing-rounding bug returns 3.33333.
- Do not modify
src/CurrencyCalculator.ts. - Use Vitest imports (
describe,it,expect,beforeEach) — no other test frameworks. - Tests must be deterministic — no randomness or time-dependent assertions.
Your suite tests 4 known bugs. What other edge cases or invariants of a currency calculator are worth testing even when no specific bug has been reported?