test: pin the export of each mode and a recurring adjustment in backups (#520)
- useDataExport.test.ts (new): "Exporter" writes, for each mode, the arrays collectExportData gathers, at the format of this build; a CSV still starts with its id column. - restore.test.ts: the adjustment of the test budget is recurring and carries a recurrence rule, so that a restore which drops either shows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
09c7b32dd1
commit
b45616fa1d
2 changed files with 116 additions and 6 deletions
|
|
@ -928,9 +928,10 @@ type BudgetCategories = { groceries: number; rent: number; electricity: number }
|
|||
/**
|
||||
* A budget on `on`, written by the real services of the Budget and Adjustments
|
||||
* pages: three budget lines, the template « Mois type » and its two lines, the
|
||||
* adjustment « Rattrapage » and its two lines.
|
||||
* recurring adjustment « Rattrapage » and its two lines. Its recurrence rule,
|
||||
* which no page writes yet, is set in `db`, so that it travels too.
|
||||
*/
|
||||
async function writeBudget(on: BudgetCategories): Promise<void> {
|
||||
async function writeBudget(db: DatabaseSync, on: BudgetCategories): Promise<void> {
|
||||
await upsertBudgetEntry(on.groceries, 2026, 10, 450, "Octobre");
|
||||
await upsertBudgetEntry(on.groceries, 2026, 11, 470);
|
||||
await upsertBudgetEntry(on.rent, 2026, 10, 1000);
|
||||
|
|
@ -942,7 +943,11 @@ async function writeBudget(on: BudgetCategories): Promise<void> {
|
|||
name: "Rattrapage",
|
||||
description: "Crédit sur facture",
|
||||
date: "2026-10-15",
|
||||
is_recurring: false,
|
||||
is_recurring: true,
|
||||
});
|
||||
db.prepare("UPDATE adjustments SET recurrence_rule = $rule WHERE id = $id").run({
|
||||
$rule: '{"every":"month"}',
|
||||
$id: adjustment,
|
||||
});
|
||||
await createEntry({ adjustment_id: adjustment, category_id: on.electricity, amount: -30, description: "Crédit" });
|
||||
await createEntry({ adjustment_id: adjustment, category_id: on.groceries, amount: 15 });
|
||||
|
|
@ -1019,7 +1024,7 @@ describe("restoring a backup with its budget, its templates and its adjustments
|
|||
let before: BudgetState;
|
||||
|
||||
beforeEach(async () => {
|
||||
await writeBudget(SEED);
|
||||
await writeBudget(profile, SEED);
|
||||
before = budgetOf(profile);
|
||||
expect(before.entries).toHaveLength(3);
|
||||
expect(before.templates.map((t) => [t.name, t.entries.length])).toEqual([["Mois type", 2]]);
|
||||
|
|
@ -1142,7 +1147,7 @@ describe("a budget the restore could not write back, or a newer file, is refused
|
|||
let before: BudgetState;
|
||||
|
||||
beforeEach(async () => {
|
||||
await writeBudget({ groceries: GROCERIES, rent: RENT, electricity: ELECTRICITY });
|
||||
await writeBudget(profile, { groceries: GROCERIES, rent: RENT, electricity: ELECTRICITY });
|
||||
before = budgetOf(profile);
|
||||
});
|
||||
|
||||
|
|
@ -1305,7 +1310,7 @@ describe("« Rétablir la sauvegarde » after the categories migration (#520)",
|
|||
// A profile on the former categories: « Réinitialiser » puts their seed back.
|
||||
const { reinitializeCategories } = await import("../services/categoryService");
|
||||
await reinitializeCategories();
|
||||
await writeBudget(V2);
|
||||
await writeBudget(profile, V2);
|
||||
former = budgetOf(profile);
|
||||
});
|
||||
|
||||
|
|
|
|||
105
src/hooks/useDataExport.test.ts
Normal file
105
src/hooks/useDataExport.test.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
// useDataExport — « Exporter » writes the arrays `collectExportData` gathers
|
||||
// for its mode (#520): the budget, its templates and the adjustments with the
|
||||
// categories, the same arrays as the backup taken before the categories
|
||||
// migration. No jsdom: the hook runs as a plain function under the shared
|
||||
// harness (src/test/hookHarness.ts, #409); the file dialog and the write are
|
||||
// fakes, the serialization is real.
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
vi.mock("react", async (orig) => (await import("../test/hookHarness")).fakeReact(await orig()));
|
||||
const { invoke } = vi.hoisted(() => ({ invoke: vi.fn() }));
|
||||
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
|
||||
vi.mock("@tauri-apps/api/app", () => ({ getVersion: vi.fn(async () => "0.17.2") }));
|
||||
const { collectExportData } = vi.hoisted(() => ({ collectExportData: vi.fn() }));
|
||||
vi.mock("../services/dataExportService", async (orig) => ({
|
||||
...(await orig<typeof import("../services/dataExportService")>()),
|
||||
collectExportData,
|
||||
}));
|
||||
|
||||
import { hookHarness } from "../test/hookHarness";
|
||||
import { useDataExport } from "./useDataExport";
|
||||
import { SREF_FORMAT_VERSION, type ExportEnvelope, type ExportMode } from "../services/dataExportService";
|
||||
|
||||
const harness = hookHarness();
|
||||
|
||||
/** One render of the mounted hook. */
|
||||
const render = () => harness.render(() => useDataExport());
|
||||
|
||||
const TRANSACTION = {
|
||||
id: 1,
|
||||
date: "2026-10-04",
|
||||
description: "EPICERIE",
|
||||
amount: -50,
|
||||
category_id: 1111,
|
||||
category_name: "Épicerie",
|
||||
original_description: null,
|
||||
notes: null,
|
||||
is_manually_categorized: 0,
|
||||
is_split: 0,
|
||||
parent_transaction_id: null,
|
||||
};
|
||||
|
||||
/** What `collectExportData` gathers for `mode`, the budget with the categories. */
|
||||
function gathered(mode: ExportMode): ExportEnvelope["data"] {
|
||||
const categories = {
|
||||
categories: [],
|
||||
suppliers: [],
|
||||
keywords: [],
|
||||
budget_entries: [{ category_id: 1111, year: 2026, month: 10, amount: 450, notes: null }],
|
||||
budget_templates: [{ name: "Mois type", description: null, entries: [{ category_id: 1111, amount: 400 }] }],
|
||||
adjustments: [],
|
||||
};
|
||||
const transactions = { transactions: [TRANSACTION], import_sources: [], import_config_templates: [] };
|
||||
if (mode === "categories_only") return categories;
|
||||
if (mode === "transactions_only") return transactions;
|
||||
return { ...categories, ...transactions };
|
||||
}
|
||||
|
||||
/** The content « Exporter » writes, once the user picked `path`. */
|
||||
function saving(path: string): () => string {
|
||||
let written = "";
|
||||
invoke.mockImplementation(async (cmd: string, args?: { content?: string }) => {
|
||||
if (cmd === "pick_save_file") return path;
|
||||
if (cmd === "write_export_file") {
|
||||
written = args?.content ?? "";
|
||||
return undefined;
|
||||
}
|
||||
throw new Error(`unexpected invoke: ${cmd}`);
|
||||
});
|
||||
return () => written;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
harness.clear();
|
||||
invoke.mockReset();
|
||||
collectExportData.mockReset();
|
||||
collectExportData.mockImplementation(async (mode: ExportMode) => gathered(mode));
|
||||
});
|
||||
|
||||
describe("useDataExport writes what collectExportData gathers for the mode (#520)", () => {
|
||||
it.each<ExportMode>(["transactions_with_categories", "categories_only", "transactions_only"])(
|
||||
"%s: the JSON carries the arrays of its mode, at the format of this build",
|
||||
async (mode) => {
|
||||
const written = saving("/tmp/export.json");
|
||||
|
||||
await render().performExport(mode, "json");
|
||||
|
||||
expect(collectExportData).toHaveBeenCalledWith(mode);
|
||||
const envelope = JSON.parse(written());
|
||||
expect(envelope.export_type).toBe(mode);
|
||||
expect(envelope.format_version).toBe(SREF_FORMAT_VERSION);
|
||||
expect(envelope.data).toEqual(gathered(mode));
|
||||
expect(render().state.status).toBe("success");
|
||||
},
|
||||
);
|
||||
|
||||
it("a CSV writes the transactions of the mode, as before", async () => {
|
||||
const written = saving("/tmp/export.csv");
|
||||
|
||||
await render().performExport("transactions_only", "csv");
|
||||
|
||||
const [header, row] = written().split(/\r?\n/);
|
||||
expect(header.split(",")[0]).toBe("id");
|
||||
expect(row.split(",").slice(0, 3)).toEqual(["1", "2026-10-04", "EPICERIE"]);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue