Files
GandClaude Opus 5 c65ac4b7d7 chore: add a formatter (#71)
* chore: add a formatter, matching the style this repo already writes

singleQuote=false was chosen by counting this repo's own
imports, not by fleet decree. The fleet is genuinely split and the two repos
that already had a .prettierrc disagreed with each other, so there was no
standard to restore. Quote style does not cross repo boundaries; having a
gate does.

Markdown is ignored for now — prettier rewraps prose, which would bury the
real diff.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* style: format with prettier (252 files)

Mechanical. No behaviour change. This SHA is listed in
.git-blame-ignore-revs so `git blame` skips it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: teach git blame to skip the reformat

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-29 10:05:01 +00:00

251 lines
7.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import {
formatDateShort,
formatWeight,
formatTemperature,
formatRelativeDate,
formatPetAgeShort,
formatPetAge,
formatPrice,
formatAdoptionFee,
formatIsoDate,
formatIsoDateTime,
} from "./format";
describe("formatDateShort", () => {
it("formats a date string with locale-appropriate month name (en)", () => {
const result = formatDateShort("2026-01-15", "en");
expect(result).toContain("15");
expect(result).toContain("2026");
expect(result).toMatch(/Jan/);
});
it("handles December correctly with explicit locale (en)", () => {
const result = formatDateShort("2025-12-31", "en");
expect(result).toContain("31");
expect(result).toContain("2025");
expect(result).toMatch(/Dec/);
});
it("respects locale parameter (ja → Japanese month notation)", () => {
const result = formatDateShort("2026-01-15", "ja");
expect(result).toContain("15");
expect(result).toContain("2026");
// Japanese formats month with 月 character
expect(result).toMatch(/月/);
});
});
describe("formatWeight", () => {
it("shows grams for values under 1 kg", () => {
expect(formatWeight(500)).toBe("500 g");
expect(formatWeight(999)).toBe("999 g");
});
it("shows kg for values 1000 g and above", () => {
expect(formatWeight(1000)).toBe("1.0 kg");
expect(formatWeight(3500)).toBe("3.5 kg");
expect(formatWeight(25000)).toBe("25.0 kg");
});
it("rounds to one decimal place", () => {
expect(formatWeight(1234)).toBe("1.2 kg");
});
});
describe("formatTemperature", () => {
it("converts centidegrees to degrees with one decimal", () => {
expect(formatTemperature(3850)).toBe("38.5°C");
expect(formatTemperature(4100)).toBe("41.0°C");
expect(formatTemperature(3700)).toBe("37.0°C");
});
});
/**
* Both formatPetAgeShort and formatPetAge use Date.now() internally.
* Pin system time to a fixed date so the tests are deterministic.
* now = 2026-04-24T12:00:00Z
*
* These functions now require a `t` function (from next-intl). We pass
* a minimal English mock so tests remain self-contained.
*/
const mockTShort = (key: string, v?: Record<string, number>) =>
({
petAgeShortLess1Month: "< 1mo",
petAgeShortMonths: `${v?.n}mo`,
petAgeShortYears: `${v?.n}yr`,
})[key] ?? key;
const mockT = (key: string, v?: Record<string, number>) =>
({
petAgeLess1Month: "< 1 month old",
petAgeMonths: `${v?.n} month${v?.n === 1 ? "" : "s"} old`,
petAgeYears: `${v?.n} year${v?.n === 1 ? "" : "s"} old`,
petAgeYearsMonths: `${v?.years}y ${v?.months}mo old`,
})[key] ?? key;
describe("formatPetAgeShort", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-24T12:00:00Z"));
});
afterEach(() => vi.useRealTimers());
it("returns empty string for null", () => {
expect(formatPetAgeShort(null, mockTShort)).toBe("");
});
it("returns '< 1mo' for a pet born less than 1 month ago", () => {
expect(formatPetAgeShort("2026-04-21", mockTShort)).toBe("< 1mo");
});
it("returns months for 1–11 month old pets", () => {
expect(formatPetAgeShort("2026-01-24", mockTShort)).toBe("2mo");
expect(formatPetAgeShort("2025-07-24", mockTShort)).toBe("9mo");
});
it("returns years for pets 12+ months old", () => {
expect(formatPetAgeShort("2025-04-24", mockTShort)).toBe("1yr");
expect(formatPetAgeShort("2024-04-24", mockTShort)).toBe("2yr");
});
});
describe("formatPetAge", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-24T12:00:00Z"));
});
afterEach(() => vi.useRealTimers());
it("returns empty string for null", () => {
expect(formatPetAge(null, mockT)).toBe("");
});
it("returns '< 1 month old' for a very young pet", () => {
expect(formatPetAge("2026-04-21", mockT)).toBe("< 1 month old");
});
it("returns 'N months old' for 1–11 month old pets", () => {
expect(formatPetAge("2026-01-24", mockT)).toBe("2 months old");
});
it("returns 'N year(s) old' for exactly whole years", () => {
expect(formatPetAge("2025-04-24", mockT)).toBe("1 year old");
expect(formatPetAge("2024-04-24", mockT)).toBe("2 years old");
});
it("returns 'Ny Nmo old' when years + remaining months", () => {
expect(formatPetAge("2023-10-24", mockT)).toBe("2y 6mo old");
});
});
describe("formatPrice", () => {
it("formats zero as $0.00", () => {
expect(formatPrice(0, "en")).toBe("$0.00");
});
it("formats a whole-dollar amount with two decimal places", () => {
expect(formatPrice(1000, "en")).toBe("$10.00");
expect(formatPrice(100, "en")).toBe("$1.00");
});
it("formats cents correctly", () => {
expect(formatPrice(2999, "en")).toBe("$29.99");
expect(formatPrice(1, "en")).toBe("$0.01");
expect(formatPrice(50, "en")).toBe("$0.50");
});
it("handles large amounts with thousands separator", () => {
expect(formatPrice(100000, "en")).toBe("$1,000.00");
});
});
describe("formatAdoptionFee", () => {
it("returns 'Free' for null", () => {
expect(formatAdoptionFee(null, "en")).toBe("Free");
});
it("returns 'Free' for zero", () => {
expect(formatAdoptionFee(0, "en")).toBe("Free");
});
it("returns whole-dollar amount for non-zero fee", () => {
expect(formatAdoptionFee(2500, "en")).toBe("$25");
expect(formatAdoptionFee(10000, "en")).toBe("$100");
expect(formatAdoptionFee(5000, "en")).toBe("$50");
});
it("rounds sub-dollar amounts to nearest dollar", () => {
expect(formatAdoptionFee(250, "en")).toBe("$3");
expect(formatAdoptionFee(150, "en")).toBe("$2");
});
});
describe("formatIsoDate", () => {
it("extracts date portion from ISO string (en locale)", () => {
const result = formatIsoDate("2026-01-15T10:30:00Z", "en");
expect(result).toContain("15");
expect(result).toContain("2026");
expect(result).toMatch(/Jan/);
});
it("uses runtime locale when none supplied", () => {
const result = formatIsoDate("2026-06-20T00:00:00Z");
expect(result).toContain("2026");
expect(typeof result).toBe("string");
});
});
describe("formatIsoDateTime", () => {
it("includes month, day, hour, and minute (en locale)", () => {
const result = formatIsoDateTime("2026-01-15T14:30:00Z", "en");
expect(result).toContain("15");
expect(result).toMatch(/Jan/);
expect(result).toMatch(/\d{1,2}:\d{2}/);
});
it("returns a non-empty string for any valid ISO input", () => {
const result = formatIsoDateTime("2026-06-20T09:05:00Z");
expect(typeof result).toBe("string");
expect(result.length).toBeGreaterThan(0);
});
});
describe("formatRelativeDate", () => {
const now = new Date("2026-01-15T12:00:00Z");
it("returns 'today' for the current date", () => {
expect(formatRelativeDate("2026-01-15", "en", now)).toBe("today");
});
it("returns 'tomorrow' for the next day", () => {
expect(formatRelativeDate("2026-01-16", "en", now)).toBe("tomorrow");
});
it("returns 'yesterday' for the previous day", () => {
expect(formatRelativeDate("2026-01-14", "en", now)).toBe("yesterday");
});
it("returns 'in N days' for future dates", () => {
expect(formatRelativeDate("2026-01-22", "en", now)).toBe("in 7 days");
});
it("returns 'N days ago' for past dates", () => {
expect(formatRelativeDate("2026-01-08", "en", now)).toBe("7 days ago");
});
it("returns plural 'N days ago' for large past offsets", () => {
expect(formatRelativeDate("2025-12-16", "en", now)).toBe("30 days ago");
});
it("returns plural 'in N days' for large future offsets", () => {
expect(formatRelativeDate("2026-02-14", "en", now)).toBe("in 30 days");
});
it("formats in German when locale is 'de'", () => {
expect(formatRelativeDate("2026-01-15", "de", now)).toBe("heute");
expect(formatRelativeDate("2026-01-16", "de", now)).toBe("morgen");
expect(formatRelativeDate("2026-01-14", "de", now)).toBe("gestern");
});
});