Files
elysium/apps/web/test/format.spec.ts
T
hikari 69579e166a
Security Scan and Upload / Security & DefectDojo Upload (pull_request) Successful in 1m6s
CI / Lint, Build & Test (pull_request) Failing after 1m13s
fix: apply crystal multiplier to boss rewards, steepen prestige threshold, fix stale upgrade race condition, and fix companion format display
Closes #221
Closes #222
Closes #201
Closes #213
2026-04-06 13:22:15 -07:00

201 lines
6.1 KiB
TypeScript

/* eslint-disable max-lines -- Test suites naturally have many cases */
/* eslint-disable max-lines-per-function -- Test suites naturally have many cases */
/* eslint-disable max-nested-callbacks -- Vitest structure requires nesting */
/**
* @file Tests for number formatting utilities.
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { describe, expect, it } from "vitest";
import { formatInteger, formatNumber } from "../src/utils/format.js";
describe("formatNumber", () => {
describe("edge cases", () => {
it("should return '0' for NaN", () => {
expect(formatNumber(Number.NaN)).toBe("0");
});
it("should return '0' for Infinity", () => {
expect(formatNumber(Infinity)).toBe("0");
});
it("should return '0' for -Infinity", () => {
expect(formatNumber(-Infinity)).toBe("0");
});
it("should format negative numbers with a leading minus sign", () => {
expect(formatNumber(-1000)).toBe("-1.00K");
});
it("should format zero as '0.00'", () => {
expect(formatNumber(0)).toBe("0.00");
});
it("should format small decimal values with two decimal places", () => {
expect(formatNumber(0.01)).toBe("0.01");
});
});
describe("suffix format (default)", () => {
it("should format small numbers with one decimal place", () => {
expect(formatNumber(999)).toBe("999.0");
});
it("should format thousands with K suffix", () => {
expect(formatNumber(1000)).toBe("1.00K");
});
it("should format millions with M suffix", () => {
expect(formatNumber(1_000_000)).toBe("1.00M");
});
it("should format billions with B suffix", () => {
expect(formatNumber(1_000_000_000)).toBe("1.00B");
});
it("should format trillions with T suffix", () => {
expect(formatNumber(1e12)).toBe("1.00T");
});
it("should format quadrillions with Qa suffix", () => {
expect(formatNumber(1e15)).toBe("1.00Qa");
});
it("should format quintillions with Qi suffix", () => {
expect(formatNumber(1e18)).toBe("1.00Qi");
});
it("should format sextillions with Sx suffix", () => {
expect(formatNumber(1e21)).toBe("1.00Sx");
});
it("should format septillions with Sp suffix", () => {
expect(formatNumber(1e24)).toBe("1.00Sp");
});
it("should format octillions with Oc suffix", () => {
expect(formatNumber(1e27)).toBe("1.00Oc");
});
it("should format nonillions with No suffix", () => {
expect(formatNumber(1e30)).toBe("1.00No");
});
it("should format decillions with Dc suffix", () => {
expect(formatNumber(1e33)).toBe("1.00Dc");
});
it("should format values >= 1e36 with letter suffix 'a'", () => {
expect(formatNumber(1e36)).toBe("1.00a");
});
it("should format values >= 1e39 with letter suffix 'b'", () => {
expect(formatNumber(1e39)).toBe("1.00b");
});
it("should format values at 26th letter step with 'z'", () => {
expect(formatNumber(1e36 * Math.pow(10, 25 * 3))).toBe("1.00z");
});
it("should format values at 27th letter step with 'aa'", () => {
expect(formatNumber(1e36 * Math.pow(10, 26 * 3))).toBe("1.00aa");
});
});
describe("scientific format", () => {
it("should fall back to suffix format below 1e6", () => {
expect(formatNumber(500, "scientific")).toBe("500.0");
});
it("should format values >= 1e6 in scientific notation", () => {
expect(formatNumber(1_230_000, "scientific")).toBe("1.23e6");
});
it("should format large values in scientific notation", () => {
expect(formatNumber(1e18, "scientific")).toBe("1.00e18");
});
});
describe("engineering format", () => {
it("should fall back to suffix format below 1e6", () => {
expect(formatNumber(500, "engineering")).toBe("500.0");
});
it("should format values >= 1e6 with exponent multiple of 3", () => {
expect(formatNumber(1_230_000, "engineering")).toBe("1.23E6");
});
it("should format 1e9 correctly in engineering notation", () => {
expect(formatNumber(1e9, "engineering")).toBe("1.00E9");
});
it("should format 12350000 correctly in engineering notation", () => {
expect(formatNumber(12_350_000, "engineering")).toBe("12.35E6");
});
});
describe("unknown format (default branch)", () => {
it("should fall back to suffix format for an unrecognised format string", () => {
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Testing unreachable default branch */
expect(formatNumber(1000, "unknown" as never)).toBe("1.00K");
});
});
});
describe("formatInteger", () => {
describe("edge cases", () => {
it("should return '0' for NaN", () => {
expect(formatInteger(Number.NaN)).toBe("0");
});
it("should return '0' for Infinity", () => {
expect(formatInteger(Infinity)).toBe("0");
});
it("should format negative integers with a leading minus sign", () => {
expect(formatInteger(-1500)).toBe("-1K");
});
it("should format zero as '0'", () => {
expect(formatInteger(0)).toBe("0");
});
it("should format small integers without decimals", () => {
expect(formatInteger(42)).toBe("42");
});
});
describe("named suffixes", () => {
it("should format thousands with K suffix and no decimals", () => {
expect(formatInteger(1500)).toBe("1K");
});
it("should format millions with M suffix and no decimals", () => {
expect(formatInteger(2_500_000)).toBe("2M");
});
it("should format billions with B suffix", () => {
expect(formatInteger(3_000_000_000)).toBe("3B");
});
it("should format trillions with T suffix", () => {
expect(formatInteger(1e12)).toBe("1T");
});
it("should format quintillions with Qi suffix", () => {
expect(formatInteger(1e18)).toBe("1Qi");
});
});
describe("letter suffixes", () => {
it("should format values >= 1e36 with letter suffix 'a'", () => {
expect(formatInteger(1e36)).toBe("1a");
});
it("should format values >= 1e39 with letter suffix 'b'", () => {
expect(formatInteger(1e39)).toBe("1b");
});
});
});