drop/server/test/unit/array.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

46 lines
1.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { lastItem, sum } from "../../../server/utils/array";
describe("sum", () => {
it("returns 0 for an empty array", () => {
expect(sum([])).toBe(0);
});
it("sums positive numbers", () => {
expect(sum([1, 2, 3, 4])).toBe(10);
});
it("sums negative and positive mixed", () => {
expect(sum([10, -5, 3])).toBe(8);
});
it("returns the single value for a one-element array", () => {
expect(sum([42])).toBe(42);
});
it("does not mutate the input array", () => {
const arr = [1, 2, 3];
const snapshot = [...arr];
sum(arr);
expect(arr).toEqual(snapshot);
});
});
describe("lastItem", () => {
it("returns undefined for an empty array", () => {
expect(lastItem([])).toBeUndefined();
});
it("returns the only element for a one-element array", () => {
expect(lastItem(["only"])).toBe("only");
});
it("returns the last element of a multi-element array", () => {
expect(lastItem([1, 2, 3])).toBe(3);
});
it("preserves object references", () => {
const obj = { id: 7 };
expect(lastItem([{ id: 1 }, obj])).toBe(obj);
});
});