|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { test } from 'node:test' |
| 3 | +import { add, greet, calculateCompoundInterest, clamp } from './index.ts' |
| 4 | + |
| 5 | +await test('add has JSDoc comment', () => { |
| 6 | + const source = add.toString() |
| 7 | + // Function should work correctly |
| 8 | + assert.strictEqual(add(2, 3), 5, '🚨 add(2, 3) should return 5') |
| 9 | + assert.strictEqual(add(-1, 1), 0, '🚨 add(-1, 1) should return 0') |
| 10 | +}) |
| 11 | + |
| 12 | +await test('greet has JSDoc comment and works correctly', () => { |
| 13 | + assert.strictEqual( |
| 14 | + greet('Alice'), |
| 15 | + 'Hello, Alice!', |
| 16 | + '🚨 greet("Alice") should return "Hello, Alice!"', |
| 17 | + ) |
| 18 | + assert.strictEqual( |
| 19 | + greet('World'), |
| 20 | + 'Hello, World!', |
| 21 | + '🚨 greet("World") should return "Hello, World!"', |
| 22 | + ) |
| 23 | +}) |
| 24 | + |
| 25 | +await test('calculateCompoundInterest works correctly', () => { |
| 26 | + // $1000 at 5% for 10 years = $1000 * (1.05)^10 ≈ $1628.89 |
| 27 | + const result = calculateCompoundInterest(1000, 0.05, 10) |
| 28 | + assert.ok( |
| 29 | + Math.abs(result - 1628.89) < 0.1, |
| 30 | + `🚨 calculateCompoundInterest(1000, 0.05, 10) should be approximately 1628.89, got ${result}`, |
| 31 | + ) |
| 32 | + |
| 33 | + // $100 at 10% for 1 year = $110 |
| 34 | + const simpleResult = calculateCompoundInterest(100, 0.1, 1) |
| 35 | + assert.ok( |
| 36 | + Math.abs(simpleResult - 110) < 0.01, |
| 37 | + `🚨 calculateCompoundInterest(100, 0.1, 1) should be approximately 110, got ${simpleResult}`, |
| 38 | + ) |
| 39 | +}) |
| 40 | + |
| 41 | +await test('clamp constrains values correctly', () => { |
| 42 | + // Value above max should return max |
| 43 | + assert.strictEqual( |
| 44 | + clamp(15, 0, 10), |
| 45 | + 10, |
| 46 | + '🚨 clamp(15, 0, 10) should return 10 (value above max)', |
| 47 | + ) |
| 48 | + |
| 49 | + // Value below min should return min |
| 50 | + assert.strictEqual( |
| 51 | + clamp(-5, 0, 10), |
| 52 | + 0, |
| 53 | + '🚨 clamp(-5, 0, 10) should return 0 (value below min)', |
| 54 | + ) |
| 55 | + |
| 56 | + // Value within range should return unchanged |
| 57 | + assert.strictEqual( |
| 58 | + clamp(5, 0, 10), |
| 59 | + 5, |
| 60 | + '🚨 clamp(5, 0, 10) should return 5 (value within range)', |
| 61 | + ) |
| 62 | + |
| 63 | + // Edge cases |
| 64 | + assert.strictEqual( |
| 65 | + clamp(0, 0, 10), |
| 66 | + 0, |
| 67 | + '🚨 clamp(0, 0, 10) should return 0 (value at min)', |
| 68 | + ) |
| 69 | + assert.strictEqual( |
| 70 | + clamp(10, 0, 10), |
| 71 | + 10, |
| 72 | + '🚨 clamp(10, 0, 10) should return 10 (value at max)', |
| 73 | + ) |
| 74 | +}) |
0 commit comments