drop/server/test/unit/utils.test.ts
BillyOutlast 4a2202b7d7
test(server): add pure-function unit tests (array, tuple, colors, utils, prioritylist) (#32)
5 new test files in test/unit/, +31 tests, 57 total pass:

- array.test.ts (10 tests): sum() — empty, positive, mixed, single,
  no-mutation. lastItem() — empty, single, multi, object refs.
- tuple.test.ts (2 tests): x/y storage, toString format.
- colors.test.ts (6 tests): getBarColor — 0/70/71/90/91/100
  boundary cases.
- utils.test.ts (5 tests): getPercentage — value/total, 0/total,
  >100%, div-by-zero (documents current Infinity behavior), fractional.
- prioritylist.test.ts (8 tests): PriorityList — empty, insertion
  order, priority sort, pop, cache invalidation, find. PriorityListIndexed
  — index by property, remove from index on pop, empty-pop TypeError
  (pins current behavior; PR4 will add explicit guard).

Coverage:
- prioritylist.ts: 93.75% lines (was 0%)
- utils/ (array/colors/tuple/utils): 94.73% lines
- overall: 1.17% → 1.9% lines

Refs plan at .opencode/plans/hyperplan-dep-tdd-coverage.md PR7.

Co-authored-by: bot <ci@local>
2026-07-25 14:18:58 -04:00

27 lines
922 B
TypeScript

import { describe, expect, it } from "vitest";
import { getPercentage } from "../../../server/utils/utils";
describe("getPercentage", () => {
it("computes value/total as a percent", () => {
expect(getPercentage(50, 100)).toBe(50);
});
it("handles 0/total = 0", () => {
expect(getPercentage(0, 100)).toBe(0);
});
it("handles value > total (over 100%)", () => {
expect(getPercentage(150, 100)).toBe(150);
});
it("returns Infinity when total is 0 (current behavior — does not handle div-by-zero)", () => {
// Current code: 5*100/0 = Infinity. Number.isNaN(Infinity) is false,
// so the NaN-guard doesn't fire. The function returns Infinity, not 0.
// Documenting current behavior; a div-by-zero guard is a future fix.
expect(getPercentage(5, 0)).toBe(Infinity);
});
it("handles fractional values", () => {
expect(getPercentage(1, 3)).toBeCloseTo(33.333, 2);
});
});