fix: keep budget sibling order distinct at numeric limit

This commit is contained in:
Lucas Winther
2026-09-29 20:36:33 +02:00
parent 3f976e0943
commit 390abf2be4
2 changed files with 21 additions and 1 deletions
+15
View File
@@ -2,6 +2,7 @@ import { test, expect, describe } from "bun:test";
import * as actions from "./actions.ts";
import { createSeedDataset } from "../domain/seed.ts";
import { occurrencesByMonth } from "../domain/occurrences.ts";
import { exportDatasetJson, parseDatasetJson } from "../export/json.ts";
import type { Dataset } from "../domain/types.ts";
const billInput = (postId: string, folderId: string | null = null) => ({
@@ -83,6 +84,20 @@ describe("budget folder placement", () => {
const bill = actions.addRecurringCost(data, billInput(post.id));
expect([folder.order, post.order, bill.order]).toEqual([4, 5, 6]);
});
test("adding after the largest finite sibling order stays importable", () => {
for (const add of [
(data: Dataset) => actions.addBudgetFolder(data, "Utilities", null),
(data: Dataset) => actions.addPost(data, "Bills", "DKK"),
(data: Dataset) => actions.addRecurringCost(data, billInput(data.posts[0]!.id)),
]) {
const data = createSeedDataset("2026-09");
data.posts[2]!.order = Number.MAX_VALUE;
const added = add(data);
expect(added.order).not.toBe(Number.MAX_VALUE);
expect(() => parseDatasetJson(exportDatasetJson(data))).not.toThrow();
}
});
});
/**
+6 -1
View File
@@ -64,7 +64,12 @@ export function budgetSiblings(draft: Dataset, parentId: FolderId | null): Budge
function nextSiblingOrder(draft: Dataset, parentId: FolderId | null): number {
const siblings = siblingRefs(draft, parentId);
return siblings.length ? siblings[siblings.length - 1]!.item.order + 1 : 0;
if (!siblings.length) return 0;
const highest = siblings[siblings.length - 1]!.item.order;
const next = highest + 1;
if (Number.isFinite(next) && next > highest) return next;
siblings.forEach(({ item }, order) => { item.order = order; });
return siblings.length;
}
function requireFolder(draft: Dataset, id: FolderId): BudgetFolder {