Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/csv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Database } from "bun:sqlite";
import { formatCents } from "./money/cents.ts";

interface ExpenseRow {
createdAt: string;
Expand Down Expand Up @@ -26,19 +27,14 @@ 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 2-decimal string (1000 -> "10.00") only here.
*/
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" : "");
}
57 changes: 57 additions & 0 deletions tests/csv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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
.trim()
.split("\n")
.slice(1)
.map((line) => line.split(",").at(-1)!);
}

describe("exportCsv", () => {
test("formats the amount column as a 2-decimal amount", () => {
const db = setupDb();
const members = ["flat-priya-id", "flat-imran-id", "flat-sara-id"];
createExpense(db, {
groupId: "flat-4b",
payerId: "flat-priya-id",
amountCents: 1000,
description: "Plants",
splitAmong: members,
createdAt: "2026-09-10T09:00:00.000Z",
});
createExpense(db, {
groupId: "flat-4b",
payerId: "flat-sara-id",
amountCents: 1005,
description: "Soap",
splitAmong: members,
createdAt: "2026-09-11T09:00:00.000Z",
});

const amounts = amountColumn(exportCsv(db, "flat-4b"));
expect(amounts.slice(-2)).toEqual(["10.00", "10.05"]);
});

test("seeded flat-4b export uses formatted amounts", () => {
const csv = exportCsv(setupDb(), "flat-4b");
expect(csv.split("\n")[0]).toBe("date,payer,description,amount");
const amounts = amountColumn(csv);
expect(amounts).toEqual(["45.00", "12.00", "30.00"]);
for (const amount of amounts) {
expect(amount).toMatch(/^-?\d+\.\d{2}$/);
}
});
});
Loading