mirror of
https://github.com/bitbaum/petvity.git
synced 2026-12-04 13:47:03 +01:00
Two real defects in the repo I can actually reach, found by auditing my own
work rather than someone else's.
formatPrice divided by 100 unconditionally. That assumes every currency has two
decimals, and several have none: with NEXT_PUBLIC_APP_CURRENCY=JPY a 2999-unit
product rendered as ¥30 instead of ¥2,999 — a hundredfold error, silently, on
every price in the shop. KRW, VND and ISK behave the same way, and KWD errs in
the other direction with three decimals. APP_CURRENCY is an environment
variable, so this was one deployment away from being real.
It now delegates to commercekit's money module, which takes the exponent from
the currency. That also removes a second money formatter from a repo that had
just adopted the first — exactly the duplication this whole exercise is about,
introduced by me.
TEMPLATE_MAP typed its payload as `any`, so the cron dispatching welcome emails
could hand a template the wrong shape and the compiler would agree — on mail
that goes to real users, built from a jsonb column nothing else validates. All
four templates take the same WelcomePayload, so the `any` bought nothing.
Typing it immediately produced an error, which turned out to be a typing gap
rather than a live bug: the cron spread `item.payload as object`, erasing the
shape, while enqueueWelcomeSequence does write { name, email }. So the emails
were correct and the types were lying about why. The cast now narrows to
WelcomePayload and the compiler can see it.
Verification: lint, tsc, css-vars, 901 tests / 87 files (8 new, including the
JPY case that would have caught the price bug).
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
74 lines
2.4 KiB
TypeScript
74 lines
2.4 KiB
TypeScript
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
|
|
/**
|
|
* Prices must survive a currency whose minor unit is not 1/100.
|
|
*
|
|
* The old implementation divided by 100 unconditionally, so with
|
|
* NEXT_PUBLIC_APP_CURRENCY=JPY a ¥2,999 product rendered as ¥30 — a hundredfold
|
|
* error, silently, on every price in the shop. APP_CURRENCY is an environment
|
|
* variable, so this was one deployment away from being real.
|
|
*/
|
|
|
|
async function formatWith(currency: string) {
|
|
vi.resetModules();
|
|
vi.stubEnv("NEXT_PUBLIC_APP_CURRENCY", currency);
|
|
const mod = await import("./format");
|
|
return mod;
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
vi.resetModules();
|
|
});
|
|
|
|
describe("formatPrice respects the currency's exponent", () => {
|
|
it("renders two-decimal currencies unchanged", async () => {
|
|
const { formatPrice } = await formatWith("USD");
|
|
expect(formatPrice(2999, "en")).toContain("29.99");
|
|
});
|
|
|
|
it("renders a zero-decimal currency at full value, not 1/100th", async () => {
|
|
const { formatPrice } = await formatWith("JPY");
|
|
const out = formatPrice(2999, "en");
|
|
expect(out).toContain("2,999");
|
|
expect(out).not.toContain("30");
|
|
});
|
|
|
|
it("handles the other zero-decimal currencies too", async () => {
|
|
for (const code of ["KRW", "VND", "ISK"]) {
|
|
const { formatPrice } = await formatWith(code);
|
|
expect(formatPrice(2999, "en"), code).toMatch(/2[.,]999/);
|
|
}
|
|
});
|
|
|
|
it("renders a three-decimal currency with three decimals", async () => {
|
|
const { formatPrice } = await formatWith("KWD");
|
|
expect(formatPrice(2999, "en")).toContain("2.999");
|
|
});
|
|
|
|
it("formats zero", async () => {
|
|
const { formatPrice } = await formatWith("USD");
|
|
expect(formatPrice(0, "en")).toContain("0.00");
|
|
});
|
|
});
|
|
|
|
describe("formatAdoptionFee", () => {
|
|
it("says Free for nothing", async () => {
|
|
const { formatAdoptionFee } = await formatWith("USD");
|
|
expect(formatAdoptionFee(null)).toBe("Free");
|
|
expect(formatAdoptionFee(0)).toBe("Free");
|
|
});
|
|
|
|
it("drops minor units — a fee is a headline, not an invoice", async () => {
|
|
const { formatAdoptionFee } = await formatWith("USD");
|
|
const out = formatAdoptionFee(2500, "en");
|
|
expect(out).toContain("25");
|
|
expect(out).not.toContain(".00");
|
|
});
|
|
|
|
it("does not divide a zero-decimal currency by 100 either", async () => {
|
|
const { formatAdoptionFee } = await formatWith("JPY");
|
|
expect(formatAdoptionFee(2500, "en")).toContain("2,500");
|
|
});
|
|
});
|