Checking Accountā¦',
],
],
["~/children/[name=trn:id]", "volatile", null],
["~/children/[name=trn:date-entered]", "timestamp", null],
["~/children/[name=trn:slots]/children/[name=slot]", "upkeep", null],
]),
);
const slots = file.values.filter(({ pointer }) =>
pointer.includes("[name=book:slots]"),
);
expect(slots.map(({ note }) => note)).toEqual(["upkeep", "upkeep"]);
});
});
/** A split of a transaction: its id, which KMyMoney numbers anew in every transaction, amount, account and payee. */
const split = (id: string, value: string, account: string, payee: string) =>
``;
const transaction = (id: string, date: string, splits: string) =>
`${splits}`;
const WATER = transaction(
"T000000000000000001",
"2026-10-12",
split("S0001", "-209/5", "A000001", "P000002") +
split("S0002", "209/5", "A000011", "P000002"),
);
const ELECTRIC = transaction(
"T000000000000000002",
"2026-10-20",
split("S0001", "-921/10", "A000001", "P000003") +
split("S0002", "921/10", "A000011", "P000003"),
);
const PAYMENT = transaction(
"T000000000000000003",
"2026-11-03",
split("S0001", "-1749/20", "A000001", "P000003") +
split("S0002", "1749/20", "A000011", "P000003"),
);
/** A book: a file version whose id is a small number, payees, accounts the institution and parent list by id, and transactions. */
const kmy = (count: string, added: string) =>
`${WATER}${ELECTRIC}${added}`;
const SEED = kmy("2", "");
const END = kmy("3", PAYMENT);
const SPLITS =
"/root/children/[name=TRANSACTIONS]/children/[name=TRANSACTION][@postdate=2026-11-03]/children/[name=SPLITS]/children";
describe("draftTaskFiles for a transaction added to a book, as KMyMoney saves one", () => {
it("omits an oversized nested split field while retaining short checks and its note", () => {
const large = "x".repeat(100_000);
const end = kmy(
"3",
PAYMENT.replace(
'memo="" account="A000001"',
`memo="${large}" account="A000001"`,
),
);
const file = xmlDraft(SEED, end);
expect(
file.suggested
.filter((check) => JSON.stringify(check).length >= 10_000)
.map((check) => ({
kind: check.kind,
pointer: "pointer" in check ? check.pointer.slice(0, 220) : "",
})),
).toEqual([]);
expect(file.suggested).toEqual(
expect.arrayContaining([
expect.objectContaining({ kind: "value_equals", value: "-1749/20" }),
]),
);
expect(file.notes).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: "long_text",
pointers: expect.arrayContaining([
`${SPLITS}/[name=SPLIT][@account=A000001]/attributes/memo`,
]),
}),
]),
);
});
it("checks each split of the new transaction by the account it names, its amount and payee, not by the split id every transaction repeats", () => {
const file = xmlDraft(SEED, END);
const field = (account: string, key: string, value: string) => ({
kind: "value_equals",
file: "Documents/doc.xml",
pointer: `${SPLITS}/[name=SPLIT][@account=${account}]/attributes/${key}`,
value,
});
// `shares` repeats `value` in every split, and `memo`, `price` and `reconcileflag` are alike in all.
expect(file.suggested.slice(0, 4)).toEqual([
field("A000001", "value", "-1749/20"),
field("A000001", "payee", "P000003"),
field("A000011", "value", "1749/20"),
field("A000011", "payee", "P000003"),
]);
const checks = file.suggested.slice(0, 4);
expect(passing("Documents/doc.xml", xml(SEED), checks)).toEqual([
false,
false,
false,
false,
]);
expect(passing("Documents/doc.xml", xml(END), checks)).toEqual([
true,
true,
true,
true,
]);
// A wrong amount fails.
const wrong = END.replace('value="1749/20"', 'value="1700/20"');
expect(passing("Documents/doc.xml", xml(wrong), checks)).toEqual([
true,
true,
false,
true,
]);
});
});
describe("draftTaskFiles for references in KMyMoney transactions", () => {
it("names the account and payee each split refers to, the account over an id list naming it too, and no element a plain count equals", () => {
const file = xmlDraft(SEED, END);
const added = file.values.find(({ kind }) => kind === "added");
expect(
added?.refers_to?.map(({ attribute, value, element }) => [
attribute,
value,
element,
]),
).toEqual([
[
"account",
"A000001",
'',
],
["payee", "P000003", ''],
["account", "A000011", ''],
]);
// count="3" equals FIXVERSION's id, but `count` names no element.
const count = file.values.find(({ pointer }) =>
pointer.endsWith("/attributes/count"),
);
expect(count).toMatchObject({ before: "2", after: "3" });
expect(count?.refers_to).toBeUndefined();
});
it("checks a split moved to a new account, though its value equals the id the new account took, so a solve that never moves it fails", () => {
const opened = (seed: string) =>
seed
.replace(
'',
'',
)
.replace(
'',
'',
);
const moved = opened(SEED).replace(
'account="A000011" price="1/1" id="S0002" payee="P000002"',
'account="A000012" price="1/1" id="S0002" payee="P000002"',
);
const file = xmlDraft(SEED, moved);
const account = file.values.find(
({ pointer, after }) =>
pointer.endsWith("/attributes/account") && after === "A000012",
);
expect(account).toBeDefined();
expect(account?.note).toBeUndefined();
expect(
passing("Documents/doc.xml", xml(moved), file.suggested),
).not.toContain(false);
// Opening the account alone leaves the water bill where it was.
expect(
passing("Documents/doc.xml", xml(opened(SEED)), file.suggested),
).toContain(false);
});
});
describe("draftTaskFiles values' other pointers", () => {
it("lists a changed value's pointer by each other name its element holds alone, as an Inkscape text's label beside its id", () => {
const poster = (headline: string) =>
``;
const file = xmlDraft(poster("Jazz Night"), poster("Jazz Festival"));
const [changed] = file.values;
expect(changed).toMatchObject({
pointer:
"/root/children/[name=text][@id=text12]/children/[name=tspan]/children/0",
after: "Jazz Festival",
alternatives: [
"/root/children/[name=text][@inkscape:label=Headline]/children/[name=tspan]/children/0",
],
});
// Each reads the same value at the end.
expect(
resolvePointer(
xml(poster("Jazz Festival")),
changed?.alternatives?.[0] ?? "",
"xml",
),
).toBe("Jazz Festival");
});
});
describe("draftTaskFiles values in an archive's entries", () => {
it("lists a chapter title's pointer by the id its h1 holds, though its tag alone names it", () => {
const chapter = (title: string) =>
`${title}
The supply boat brought one letter.
`;
const epub = (title: string) => {
const entry = zipEntry(
{
name: "OEBPS/Text/Section0002.xhtml",
size: 1,
compressedSize: 1,
method: 8,
encrypted: false,
},
new TextEncoder().encode(chapter(title)),
);
return zipView([entry], 1);
};
const [file] = draftOf({
"Documents/book.epub": statePath(
"document",
"zip",
epub("A Letter from the Mainlnad"),
epub("A Letter from the Mainland"),
),
});
const h1 =
"/entries/[name=OEBPS~1Text~1Section0002.xhtml]/view/root/children/[name=body]/children/[name=h1]";
expect(file?.values[0]).toMatchObject({
pointer: `${h1}/children/0`,
alternatives: [
"/entries/[name=OEBPS~1Text~1Section0002.xhtml]/view/root/children/[name=body]/children/[name=h1][@id=chapter-two]/children/0",
],
});
});
});