const { test, describe } = require("node:test"); const assert = require("node:assert/strict"); const { SplitCalculator, Money } = require("./load"); describe("SplitCalculator.split", () => { test("an even chop hands everyone the same stack", () => { const result = SplitCalculator.split(90, 3, 1); assert.equal(result.each, 30); assert.equal(result.oddChips, 0); assert.equal(result.dust, 0); assert.equal(SplitCalculator.accountedFor(result), 90); }); test("what won't divide is called out as odd chips, not rolled into a stack", () => { // $100 three ways in $5 chips: 20 chips, 6 each, 2 left over. const result = SplitCalculator.split(100, 3, 5); assert.equal(result.each, 30); assert.equal(result.oddChips, 2); assert.equal(SplitCalculator.accountedFor(result), 100); }); test("a pot that isn't a whole number of chips surfaces the remainder", () => { // $47 in $5 chips: 9 chips ($45) and $2 that no chip in play can pay. const result = SplitCalculator.split(47, 2, 5); assert.equal(result.dust, 2, "the dealer picked a denomination the pot can't make"); assert.equal(SplitCalculator.accountedFor(result), 47); }); test("every split accounts for the whole pot", () => { const chips = [0.25, 0.5, 1, 5, 25, 100]; for (let pot = 1; pot <= 400; pot += 7) { for (let ways = 2; ways <= 6; ways++) { chips.forEach(chip => { const result = SplitCalculator.split(pot, ways, chip); assert.equal( Money.toCents(SplitCalculator.accountedFor(result)), Money.toCents(pot), `${pot} split ${ways} ways in ${chip} chips lost money` ); }); } } }); test("odd chips are always fewer than the number of players", () => { for (let pot = 1; pot <= 200; pot += 3) { for (let ways = 2; ways <= 8; ways++) { const result = SplitCalculator.split(pot, ways, 1); assert.ok(result.oddChips < ways, "a whole extra chip each would have been dealt out"); } } }); test("an empty pot isn't a valid split", () => { assert.equal(SplitCalculator.split(0, 3, 1).valid, false); assert.equal(SplitCalculator.split(-5, 3, 1).valid, false); }); test("nonsense inputs fall back to something a dealer can read", () => { const result = SplitCalculator.split(50, 0, 0); assert.equal(result.players, 1, "a split has to be at least one way"); assert.equal(result.chip, 0.01, "and at least one cent a chip"); assert.equal(SplitCalculator.accountedFor(result), 50); }); });