fix: refuse a malformed format number with its own message, pin two adjustments in the export (#520)
A format_version that is not an integer of at least 1 now raises a SrefValidationError with the key invalidFormatVersion, translated in both locales (Max's answer of 2026-11-05 on PR #533); an envelope with missing fields keeps its raw message. The export test now seeds two adjustments, each with its own line, so a filter that handed every line to every adjustment goes red (review 31876 of #533). Refs #520 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e63029e7c3
commit
a8661907a6
4 changed files with 58 additions and 14 deletions
|
|
@ -792,7 +792,8 @@
|
|||
"orphanSplitLines": "This file cannot be imported: some split lines cannot find their original transaction. Use a JSON export. Nothing has been modified.",
|
||||
"danglingCategoryReference": "This file cannot be imported: some categories, keywords, or suppliers refer to a category missing from the file. Nothing has been modified.",
|
||||
"invalidBudgetRows": "This file cannot be imported: some budget, budget template, or adjustment lines are invalid or refer to a category missing from the file. Nothing has been modified.",
|
||||
"newerFormat": "This file comes from a newer version of Simpl'Résultat. Update the app to import it. Nothing has been modified."
|
||||
"newerFormat": "This file comes from a newer version of Simpl'Résultat. Update the app to import it. Nothing has been modified.",
|
||||
"invalidFormatVersion": "This file cannot be imported: its format number is invalid. Nothing has been modified."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -792,7 +792,8 @@
|
|||
"orphanSplitLines": "Ce fichier ne peut pas être importé : des lignes de répartition ne retrouvent pas leur transaction d'origine. Utilisez un export JSON. Rien n'a été modifié.",
|
||||
"danglingCategoryReference": "Ce fichier ne peut pas être importé : des catégories, mots-clés ou fournisseurs renvoient à une catégorie absente du fichier. Rien n'a été modifié.",
|
||||
"invalidBudgetRows": "Ce fichier ne peut pas être importé : des lignes de budget, de modèle de budget ou d'ajustement sont invalides ou visent une catégorie absente du fichier. Rien n'a été modifié.",
|
||||
"newerFormat": "Ce fichier vient d'une version plus récente de Simpl'Résultat. Mettez l'application à jour pour l'importer. Rien n'a été modifié."
|
||||
"newerFormat": "Ce fichier vient d'une version plus récente de Simpl'Résultat. Mettez l'application à jour pour l'importer. Rien n'a été modifié.",
|
||||
"invalidFormatVersion": "Ce fichier ne peut pas être importé : son numéro de format est invalide. Rien n'a été modifié."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1342,11 +1342,18 @@ describe("collectExportData gathers the arrays of each mode (#520)", () => {
|
|||
]);
|
||||
const [monthly] = db.tables.budget_templates;
|
||||
db.seed("budget_template_entries", [{ template_id: monthly.id, category_id: 1, amount: 400 }]);
|
||||
// Two adjustments, one line each: each must leave with its own line only
|
||||
// (review 31876 of #533: with one adjustment, a filter that hands every
|
||||
// line to every adjustment stayed green).
|
||||
db.seed("adjustments", [
|
||||
{ name: "Rattrapage", description: null, date: "2026-10-15", is_recurring: 1, recurrence_rule: null },
|
||||
{ name: "Remboursement", description: "Assurance", date: "2026-10-20", is_recurring: 0, recurrence_rule: null },
|
||||
]);
|
||||
const [adjustment, refund] = db.tables.adjustments;
|
||||
db.seed("adjustment_entries", [
|
||||
{ adjustment_id: adjustment.id, category_id: 1, amount: 15, description: "Crédit" },
|
||||
{ adjustment_id: refund.id, category_id: 1, amount: 20, description: null },
|
||||
]);
|
||||
const [adjustment] = db.tables.adjustments;
|
||||
db.seed("adjustment_entries", [{ adjustment_id: adjustment.id, category_id: 1, amount: 15, description: "Crédit" }]);
|
||||
});
|
||||
|
||||
it.each<[ExportMode, string[]]>([
|
||||
|
|
@ -1386,12 +1393,31 @@ describe("collectExportData gathers the arrays of each mode (#520)", () => {
|
|||
recurrence_rule: null,
|
||||
entries: [{ category_id: 1, amount: 15, description: "Crédit" }],
|
||||
},
|
||||
{
|
||||
name: "Remboursement",
|
||||
description: "Assurance",
|
||||
date: "2026-10-20",
|
||||
is_recurring: 0,
|
||||
recurrence_rule: null,
|
||||
entries: [{ category_id: 1, amount: 20, description: null }],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the format of a file is checked before anything is deleted (#520)", () => {
|
||||
const NEWER = "settings.dataManagement.import.errors.newerFormat";
|
||||
const MALFORMED = "settings.dataManagement.import.errors.invalidFormatVersion";
|
||||
|
||||
/** The error a call throws, or null. */
|
||||
const thrown = (call: () => unknown) => {
|
||||
try {
|
||||
call();
|
||||
} catch (e) {
|
||||
return e;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
it("accepts every format this build reads", () => {
|
||||
for (const version of [1, 2, SREF_FORMAT_VERSION]) {
|
||||
|
|
@ -1415,12 +1441,23 @@ describe("the format of a file is checked before anything is deleted (#520)", ()
|
|||
});
|
||||
|
||||
it.each([0, -1, 2.5, "3", null, Number.NaN, Number.POSITIVE_INFINITY])(
|
||||
"refuses a format_version of %s as a malformed file",
|
||||
"refuses a format_version of %s with its own translated message",
|
||||
(version) => {
|
||||
expect(() => validateImportedFormatVersion(version)).toThrow("Invalid export file format");
|
||||
const error = thrown(() => validateImportedFormatVersion(version));
|
||||
expect(error).toBeInstanceOf(SrefValidationError);
|
||||
expect((error as SrefValidationError).i18nKey).toBe(MALFORMED);
|
||||
expect((error as SrefValidationError).params).toEqual({});
|
||||
}
|
||||
);
|
||||
|
||||
it("keeps the raw message of an envelope with missing fields", () => {
|
||||
const error = thrown(() =>
|
||||
parseImportedJson(JSON.stringify({ export_type: "transactions_only", format_version: 2.5 }))
|
||||
);
|
||||
expect(error).not.toBeInstanceOf(SrefValidationError);
|
||||
expect((error as Error).message).toBe("Invalid export file format — missing required fields");
|
||||
});
|
||||
|
||||
it("refuses a newer file at PARSE time, before its mode is even read", () => {
|
||||
const json = JSON.stringify({
|
||||
export_type: "budget_only",
|
||||
|
|
@ -1434,7 +1471,9 @@ describe("the format of a file is checked before anything is deleted (#520)", ()
|
|||
|
||||
it("refuses a malformed format_version at PARSE time", () => {
|
||||
const json = JSON.stringify({ ...JSON.parse(serializeToJson("transactions_only", envelopeData(), "0.17.2")), format_version: 2.5 });
|
||||
expect(() => parseImportedJson(json)).toThrow("Invalid export file format");
|
||||
const error = thrown(() => parseImportedJson(json));
|
||||
expect(error).toBeInstanceOf(SrefValidationError);
|
||||
expect((error as SrefValidationError).i18nKey).toBe(MALFORMED);
|
||||
});
|
||||
|
||||
it("reads a file without format_version as format 1, and a file of this build at its format", () => {
|
||||
|
|
@ -1452,8 +1491,8 @@ describe("the format of a file is checked before anything is deleted (#520)", ()
|
|||
["categories only", (data: ExportEnvelope["data"], version: number) => importCategoriesOnly(data, version)],
|
||||
] as const)("%s: a newer format refused again at the restore, before a single DELETE", async (_name, restore) => {
|
||||
db.seed("categories", [{ name: "Existing" }]);
|
||||
await expect(restore(envelopeData(), SREF_FORMAT_VERSION + 1)).rejects.toBeInstanceOf(SrefValidationError);
|
||||
await expect(restore(envelopeData(), 0)).rejects.toThrow("Invalid export file format");
|
||||
await expect(restore(envelopeData(), SREF_FORMAT_VERSION + 1)).rejects.toMatchObject({ i18nKey: NEWER });
|
||||
await expect(restore(envelopeData(), 0)).rejects.toMatchObject({ i18nKey: MALFORMED });
|
||||
expect(db.log).toEqual([]);
|
||||
expect(db.tables.categories.map((c) => c.name)).toEqual(["Existing"]);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -196,14 +196,15 @@ export type SrefValidationErrorKey =
|
|||
| "settings.dataManagement.import.errors.orphanSplitLines"
|
||||
| "settings.dataManagement.import.errors.danglingCategoryReference"
|
||||
| "settings.dataManagement.import.errors.invalidBudgetRows"
|
||||
| "settings.dataManagement.import.errors.newerFormat";
|
||||
| "settings.dataManagement.import.errors.newerFormat"
|
||||
| "settings.dataManagement.import.errors.invalidFormatVersion";
|
||||
|
||||
/**
|
||||
* Thrown when a backup carries a configuration this build cannot store, split
|
||||
* lines that cannot find their parent (#518), categories, keywords or
|
||||
* suppliers that name a category the file does not hold (#521), budget lines,
|
||||
* templates or adjustments the restore could not write back, or a format newer
|
||||
* than this build (#520).
|
||||
* than this build or not a format number at all (#520).
|
||||
*
|
||||
* The `CHECK` added by migration v17 already refuses these values, but a SQLite
|
||||
* constraint error is not something a user can act on — and it would surface
|
||||
|
|
@ -605,7 +606,7 @@ export function validateImportedCategoryReferences(
|
|||
}
|
||||
}
|
||||
|
||||
/** The refusal of an envelope this app did not write (missing fields, unreadable format). */
|
||||
/** The refusal of an envelope this app did not write (missing fields). */
|
||||
const INVALID_ENVELOPE = "Invalid export file format — missing required fields";
|
||||
|
||||
/**
|
||||
|
|
@ -613,14 +614,16 @@ const INVALID_ENVELOPE = "Invalid export file format — missing required fields
|
|||
* deleted (#520): a `format_version` above `SREF_FORMAT_VERSION` comes from a
|
||||
* newer Simpl'Résultat, whose files may carry what this build would drop
|
||||
* without a word; one that is not an integer of at least 1 was not written by
|
||||
* the app at all, and gets the refusal of a malformed envelope.
|
||||
* the app at all, and gets its own translated refusal (Max's answer of
|
||||
* 2026-11-05 on PR #533, where it had shared the raw message of an envelope
|
||||
* with missing fields).
|
||||
*
|
||||
* PURE: no I/O, so it can be called from the parse step and again at the top of
|
||||
* each restore that is given the format, like `validateImportedFormatRows`.
|
||||
*/
|
||||
export function validateImportedFormatVersion(formatVersion: unknown): void {
|
||||
if (!Number.isInteger(formatVersion) || (formatVersion as number) < 1) {
|
||||
throw new Error(INVALID_ENVELOPE);
|
||||
throw new SrefValidationError("settings.dataManagement.import.errors.invalidFormatVersion", {});
|
||||
}
|
||||
if ((formatVersion as number) > SREF_FORMAT_VERSION) {
|
||||
throw new SrefValidationError("settings.dataManagement.import.errors.newerFormat", {});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue