Simpl-Resultat/src/__integration__/split-offset-manual-flag.test.ts
le king fu 79abdecc9d test: share one node:sqlite harness for the integration tests (#392)
src/test/sqliteHarness.ts opens the schema of a new profile with its seed,
foreign keys on, and stands in for ../services/db: $N bound by name,
rowsAffected and lastInsertId, withTransaction on the same handle, getDb()
refused inside a transaction. split-offset-manual-flag.test.ts and
restore.test.ts drop their two copies of that adapter for it, and
sqliteHarness.test.ts proves its rules once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 22:31:54 -05:00

151 lines
6.3 KiB
TypeScript
Raw 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.

/**
* A split's offset line and « Catégoriser automatiquement » (#454, item 1 of
* the review of PR #515).
*
* `autoCategorizeTransactions` picks every row without a category whose
* `is_manually_categorized` is 0, the children of a split included. The offset
* line of a split cancels, in its parent's category, the part sent to the other
* categories: it takes its parent's manual flag when the split is written
* (`saveSplitAdjustment`) and when the parent changes category
* (`propagateCategoryToOffsets`), so that a parent set to no category by hand
* keeps its offset line with it. Without that, the keyword of the parent's
* description sends the offset line alone to its category.
*
* The services run as written on a real SQLite database, the schema of a new
* profile with its seed (`consolidated_schema.sql`), foreign keys on, as under
* sqlx: `../services/db` is replaced by the shared harness
* (`src/test/sqliteHarness.ts`, #392).
*/
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import type { DatabaseSync } from "node:sqlite";
vi.mock("../services/db", async () => (await import("../test/sqliteHarness")).dbModule());
import { openProfileDb, useDb } from "../test/sqliteHarness";
import {
autoCategorizeTransactions,
saveSplitAdjustment,
updateTransactionCategory,
} from "../services/transactionService";
/** « Épicerie régulière », where the seed's `METRO` keyword sends a description that holds METRO. */
const GROCERIES = 1111;
/** « Boucherie & poissonnerie », the category the split sends −50 to. */
const B = 1112;
const DESCRIPTION = "EPICERIE METRO";
/** The split of the reproduction of #454: −50 of the −120 to B, with the parent's description. */
const ENTRIES = [{ category_id: B, amount: -50, description: DESCRIPTION }];
let db: DatabaseSync;
beforeEach(() => {
db = useDb(openProfileDb());
});
afterEach(() => {
db.close();
});
/**
* A transaction of −120 as an import writes it: its category from the
* keywords, or none, and no manual flag (the column's default, 0).
*/
function imported(categoryId: number | null): number {
const result = db
.prepare(
`INSERT INTO transactions (date, description, amount, category_id, original_description)
VALUES ('2026-10-01', $description, -120, $category, $description)`,
)
.run({ $description: DESCRIPTION, $category: categoryId });
return Number(result.lastInsertRowid);
}
type Line = { category_id: number | null; is_manually_categorized: number };
/** The category and the manual flag of the parent, of its entry (−50) and of its offset line (+50). */
function split(parentId: number): { parent: Line; entry: Line; offset: Line } {
const line = (where: string) =>
({
...db
.prepare(`SELECT category_id, is_manually_categorized FROM transactions WHERE ${where}`)
.get({ $id: parentId }),
}) as Line;
return {
parent: line("id = $id"),
entry: line("parent_transaction_id = $id AND amount < 0"),
offset: line("parent_transaction_id = $id AND amount > 0"),
};
}
/**
* What the reports add up, per category (`none` for no category): every row,
* the split parent and its children included, the offset line cancelling in
* the parent's category the part sent to the others.
*/
function totals(): Record<string, number> {
const rows = db
.prepare("SELECT category_id, SUM(amount) AS total FROM transactions GROUP BY category_id")
.all() as Array<{ category_id: number | null; total: number }>;
return Object.fromEntries(rows.map((r) => [r.category_id === null ? "none" : String(r.category_id), r.total]));
}
describe("a split's offset line keeps its parent's manual flag (#454)", () => {
it("the seed sends « EPICERIE METRO » to « Épicerie régulière », by its METRO keyword", () => {
expect({ ...db.prepare("SELECT category_id FROM keywords WHERE keyword = 'METRO'").get() }).toEqual({
category_id: GROCERIES,
});
});
// Scenario R-S3 of the review of PR #515.
it("a split parent set to no category by hand keeps its offset line through « Catégoriser automatiquement »", async () => {
const parent = imported(GROCERIES);
await saveSplitAdjustment(parent, ENTRIES);
// « Aucune catégorie » in the Transactions list (TransactionTable → useTransactions).
await updateTransactionCategory(parent, null, true);
expect(totals()).toEqual({ none: -70, [B]: -50 });
const categorized = await autoCategorizeTransactions();
expect(totals()).toEqual({ none: -70, [B]: -50 });
expect(categorized).toBe(0);
expect(split(parent)).toEqual({
parent: { category_id: null, is_manually_categorized: 1 },
// The entries keep the flag 0: out of the scope of #454.
entry: { category_id: B, is_manually_categorized: 0 },
offset: { category_id: null, is_manually_categorized: 1 },
});
});
it("a split made under a parent set to no category by hand keeps its offset line through « Catégoriser automatiquement »", async () => {
const parent = imported(GROCERIES);
await updateTransactionCategory(parent, null, true);
await saveSplitAdjustment(parent, ENTRIES);
expect(totals()).toEqual({ none: -70, [B]: -50 });
const categorized = await autoCategorizeTransactions();
expect(totals()).toEqual({ none: -70, [B]: -50 });
expect(categorized).toBe(0);
expect(split(parent).offset).toEqual({ category_id: null, is_manually_categorized: 1 });
});
it("witness: a parent imported without a category and its offset line get the same category from « Catégoriser automatiquement », as before", async () => {
const parent = imported(null);
await saveSplitAdjustment(parent, ENTRIES);
expect(split(parent)).toEqual({
parent: { category_id: null, is_manually_categorized: 0 },
entry: { category_id: B, is_manually_categorized: 0 },
offset: { category_id: null, is_manually_categorized: 0 },
});
const categorized = await autoCategorizeTransactions();
expect(totals()).toEqual({ [GROCERIES]: -70, [B]: -50 });
expect(categorized).toBe(2);
expect(split(parent)).toEqual({
parent: { category_id: GROCERIES, is_manually_categorized: 0 },
entry: { category_id: B, is_manually_categorized: 0 },
offset: { category_id: GROCERIES, is_manually_categorized: 0 },
});
});
});