diff --git a/src/csv.ts b/src/csv.ts index bbb9f3a..bb37990 100644 --- a/src/csv.ts +++ b/src/csv.ts @@ -1,4 +1,5 @@ import type { Database } from "bun:sqlite"; +import { formatCents } from "./money/index.ts"; interface ExpenseRow { createdAt: string; @@ -26,19 +27,15 @@ function fetchExpenseRows(db: Database, groupId: string): 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 stored as integer + * cents and formatted to a decimal string (1000 -> "10.00") only here, at the + * output boundary. */ 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/cli.test.ts b/tests/cli.test.ts index b87c924..1f0845a 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -38,6 +38,12 @@ describe("splitbill export", () => { const output = run(["export", "flat-4b"], setupDb()); expect(output.split("\n")[0]).toBe("date,payer,description,amount"); }); + + test("splitbill export prints formatted amounts", () => { + const output = run(["export", "flat-4b"], setupDb()); + expect(output).toContain(",Electricity bill,45.00"); + expect(output).not.toContain(",4500"); + }); }); describe("splitbill with no command", () => { diff --git a/tests/csv.test.ts b/tests/csv.test.ts new file mode 100644 index 0000000..3d02fbe --- /dev/null +++ b/tests/csv.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, test } from "bun:test"; +import { Database } from "bun:sqlite"; +import { initSchema } from "../src/db.ts"; +import { seedDemoData } from "../src/seed.ts"; +import { createExpense } from "../src/expenses.ts"; +import { exportCsv } from "../src/csv.ts"; + +function setupDb(): Database { + const db = new Database(":memory:"); + initSchema(db); + seedDemoData(db); + return db; +} + +function amountColumn(csv: string): string[] { + return csv + .trimEnd() + .split("\n") + .slice(1) + .map((line) => line.split(",").at(-1) as string); +} + +describe("exportCsv", () => { + test("exportCsv formats the amount column as a decimal amount", () => { + const csv = exportCsv(setupDb(), "flat-4b"); + const lines = csv.trimEnd().split("\n"); + expect(lines[0]).toBe("date,payer,description,amount"); + expect(lines).toHaveLength(4); + expect(amountColumn(csv)).toEqual(["45.00", "12.00", "30.00"]); + }); + + test("exportCsv keeps the cents of a non-round amount", () => { + const db = setupDb(); + createExpense(db, { + groupId: "flat-4b", + payerId: "flat-priya-id", + amountCents: 1005, + description: "Milk", + splitAmong: ["flat-priya-id", "flat-imran-id"], + createdAt: "2026-09-10T09:00:00.000Z", + }); + expect(amountColumn(exportCsv(db, "flat-4b")).at(-1)).toBe("10.05"); + }); +});