Simpl-Resultat/src/utils/sqlFlag.test.ts
le king fu f758282c2e fix(transactions): bind the manual-categorization flag as an integer (#390)
updateTransactionCategory bound the JS boolean `true`. tauri-plugin-sql
passes a bool to sqlx as a JSON value, so the column received the TEXT
'true', which the getting-started checklist's `is_manually_categorized = 1`
never matches: step 5 never ticked.

toSqlFlag turns a boolean, a number or a backup's string into 0/1. It is
used when a category is set by hand and when a JSON or CSV backup is
restored, since a backup exported while the bug stood carries "true"
(the CSV path read it as 0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 20:35:00 -04:00

49 lines
1.4 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { toSqlFlag } from "./sqlFlag";
describe("toSqlFlag (#390)", () => {
it("maps the UI boolean to an integer, never to the boolean itself", () => {
expect(toSqlFlag(true)).toBe(1);
expect(toSqlFlag(false)).toBe(0);
});
it("keeps database integers", () => {
expect(toSqlFlag(1)).toBe(1);
expect(toSqlFlag(0)).toBe(0);
});
it("treats any finite non-zero number as set, like SQLite", () => {
expect(toSqlFlag(2)).toBe(1);
expect(toSqlFlag(-1)).toBe(1);
});
it("maps NaN and infinities to 0", () => {
expect(toSqlFlag(Number.NaN)).toBe(0);
expect(toSqlFlag(Number.POSITIVE_INFINITY)).toBe(0);
});
it("reads the 'true' a backup carries from the time of the bug", () => {
expect(toSqlFlag("true")).toBe(1);
expect(toSqlFlag("TRUE")).toBe(1);
expect(toSqlFlag(" true ")).toBe(1);
});
it("reads a CSV cell", () => {
expect(toSqlFlag("1")).toBe(1);
expect(toSqlFlag(" 1 ")).toBe(1);
expect(toSqlFlag("0")).toBe(0);
expect(toSqlFlag("")).toBe(0);
});
it("maps every other string to 0", () => {
expect(toSqlFlag("false")).toBe(0);
expect(toSqlFlag("yes")).toBe(0);
expect(toSqlFlag("01")).toBe(0);
});
it("maps absent values to 0", () => {
expect(toSqlFlag(null)).toBe(0);
expect(toSqlFlag(undefined)).toBe(0);
expect(toSqlFlag({})).toBe(0);
});
});