From d88523263486ad1f96a7b9e1d41eb9e5f7bae3f6 Mon Sep 17 00:00:00 2001 From: Param Harrison Date: Tue, 13 Oct 2026 11:37:32 +0300 Subject: [PATCH 1/3] factory: claim #179 by runner-99539-1791880652136 From 5501ade54bd9b27ddcd0df3d7f665fb7f51d3afe Mon Sep 17 00:00:00 2001 From: Param Harrison Date: Tue, 13 Oct 2026 11:41:26 +0300 Subject: [PATCH 2/3] factory: build #179 --- src/csv.ts | 13 +++-------- tests/csv.test.ts | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 tests/csv.test.ts diff --git a/src/csv.ts b/src/csv.ts index bbb9f3a..04cf7f3 100644 --- a/src/csv.ts +++ b/src/csv.ts @@ -1,4 +1,5 @@ import type { Database } from "bun:sqlite"; +import { formatCents } from "./money/cents.ts"; interface ExpenseRow { createdAt: string; @@ -25,20 +26,12 @@ function fetchExpenseRows(db: Database, groupId: string): ExpenseRow[] { .all(groupId) as ExpenseRow[]; } -/** - * Build the CSV export for a group's expenses. - * - * SEEDED DEFECT (issue #3): the amount column prints the raw integer cents - * (e.g. "1000") instead of a formatted amount ("10.00"). Baseline tests - * only check the header row and the row count, not the amount column's - * formatting, so this ships undetected. The fix should reuse - * `formatCents` from src/money (see .claude/skills/handling-money). - */ +/** Build the CSV export for a group's expenses; amounts are formatted at this edge (1000 -> "10.00"). */ export function exportCsv(db: Database, groupId: string): string { const rows = fetchExpenseRows(db, groupId); const header = "date,payer,description,amount\n"; const lines = rows.map( - (r) => `${r.createdAt},${csvEscape(r.payerName)},${csvEscape(r.description)},${r.amountCents}`, + (r) => `${r.createdAt},${csvEscape(r.payerName)},${csvEscape(r.description)},${formatCents(r.amountCents)}`, ); return header + lines.join("\n") + (lines.length > 0 ? "\n" : ""); } diff --git a/tests/csv.test.ts b/tests/csv.test.ts new file mode 100644 index 0000000..ef7a548 --- /dev/null +++ b/tests/csv.test.ts @@ -0,0 +1,58 @@ +import { Database } from "bun:sqlite"; +import { describe, expect, test } from "bun:test"; +import { exportCsv } from "../src/csv.ts"; +import { initSchema } from "../src/db.ts"; +import { createExpense } from "../src/expenses.ts"; +import { seedDemoData } from "../src/seed.ts"; + +function setupDb(): Database { + const db = new Database(":memory:"); + initSchema(db); + seedDemoData(db); + return db; +} + +function amountColumn(csv: string): string[] { + return csv + .trim() + .split("\n") + .slice(1) + .map((line) => line.split(",").at(-1) ?? ""); +} + +describe("exportCsv", () => { + test("formats a 1000-cent expense as 10.00", () => { + const db = setupDb(); + createExpense(db, { + groupId: "flat-4b", + payerId: "flat-priya-id", + amountCents: 1000, + description: "Milk", + splitAmong: ["flat-priya-id"], + createdAt: "2026-10-01T09:00:00.000Z", + }); + const lines = exportCsv(db, "flat-4b").trim().split("\n"); + expect(lines.at(-1)).toBe("2026-10-01T09:00:00.000Z,Priya,Milk,10.00"); + }); + + test("formats non-round cent amounts with two decimals", () => { + const db = setupDb(); + [1005, 7].forEach((amountCents, i) => { + createExpense(db, { + groupId: "flat-4b", + payerId: "flat-priya-id", + amountCents, + description: `Odd ${i}`, + splitAmong: ["flat-priya-id"], + createdAt: `2026-10-0${i + 1}T09:00:00.000Z`, + }); + }); + expect(amountColumn(exportCsv(db, "flat-4b")).slice(-2)).toEqual(["10.05", "0.07"]); + }); + + test("seeded group export uses formatted amounts", () => { + const csv = exportCsv(setupDb(), "flat-4b"); + expect(csv.split("\n")[0]).toBe("date,payer,description,amount"); + expect(amountColumn(csv)).toEqual(["45.00", "12.00", "30.00"]); + }); +}); From fb33a25d457f2ac018ba33054a7b50c7af0957ae Mon Sep 17 00:00:00 2001 From: Param Harrison Date: Tue, 13 Oct 2026 12:24:23 +0300 Subject: [PATCH 3/3] factory: build #179 --- tests/csv.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/csv.test.ts b/tests/csv.test.ts index ef7a548..5fbc472 100644 --- a/tests/csv.test.ts +++ b/tests/csv.test.ts @@ -50,6 +50,20 @@ describe("exportCsv", () => { expect(amountColumn(exportCsv(db, "flat-4b")).slice(-2)).toEqual(["10.05", "0.07"]); }); + test("formats an amount under one dollar with a leading zero", () => { + const db = setupDb(); + createExpense(db, { + groupId: "flat-4b", + payerId: "flat-priya-id", + amountCents: 5, + description: "Sweet", + splitAmong: ["flat-priya-id"], + createdAt: "2026-10-01T09:00:00.000Z", + }); + const lines = exportCsv(db, "flat-4b").trim().split("\n"); + expect(lines.at(-1)).toBe("2026-10-01T09:00:00.000Z,Priya,Sweet,0.05"); + }); + test("seeded group export uses formatted amounts", () => { const csv = exportCsv(setupDb(), "flat-4b"); expect(csv.split("\n")[0]).toBe("date,payer,description,amount");