A local-first personal finance app
Fix flaky merkle test by enforcing deterministic key ordering (#6165)
Matiss Janis Aboltins957bfe240d6d
diff --git a/packages/crdt/src/crdt/merkle.ts b/packages/crdt/src/crdt/merkle.tsindex f9b48c8871..29328f94f0 100644--- a/packages/crdt/src/crdt/merkle.ts+++ b/packages/crdt/src/crdt/merkle.ts@@ -22,7 +22,7 @@ export type TrieNode = {type NumberTrieNodeKey = keyof Omit<TrieNode, 'hash'>;export function emptyTrie(): TrieNode {- return { hash: 0 };+ return createTrieNode({ hash: 0 });}function isNumberTrieNodeKey(input: string): input is NumberTrieNodeKey {@@ -33,6 +33,33 @@ export function getKeys(trie: TrieNode): NumberTrieNodeKey[] {return Object.keys(trie).filter(isNumberTrieNodeKey);}+/**+ * Creates a TrieNode with keys in deterministic sorted order:+ * numeric keys ('0', '1', '2') first in order, then 'hash' last.+ * This ensures consistent serialization for snapshots.+ */+function createTrieNode(props: TrieNode = {}): TrieNode {+ const result: TrieNode = {};++ // Add numeric keys in sorted order+ if (props['0'] !== undefined) {+ result['0'] = props['0'];+ }+ if (props['1'] !== undefined) {+ result['1'] = props['1'];+ }+ if (props['2'] !== undefined) {+ result['2'] = props['2'];+ }++ // Add hash last+ if (props.hash !== undefined) {+ result.hash = props.hash;+ }++ return result;+}+export function keyToTimestamp(key: string): number {// 16 is the length of the base 3 value of the current time in// minutes. Ensure it's padded to create the full value@@ -49,7 +76,12 @@ export function insert(trie: TrieNode, timestamp: Timestamp) {const hash = timestamp.hash();const key = Number(Math.floor(timestamp.millis() / 1000 / 60)).toString(3);- trie = Object.assign({}, trie, { hash: (trie.hash || 0) ^ hash });+ trie = createTrieNode({+ '0': trie['0'],+ '1': trie['1'],+ '2': trie['2'],+ hash: (trie.hash || 0) ^ hash,+ });return insertKey(trie, key, hash);}@@ -60,10 +92,19 @@ function insertKey(trie: TrieNode, key: string, hash: number): TrieNode {const c = key[0];const t = isNumberTrieNodeKey(c) ? trie[c] : undefined;const n = t || {};- return Object.assign({}, trie, {- [c]: Object.assign({}, n, insertKey(n, key.slice(1), hash), {- hash: (n.hash || 0) ^ hash,- }),+ const childWithInserted = insertKey(n, key.slice(1), hash);+ const updatedChild = createTrieNode({+ '0': childWithInserted['0'],+ '1': childWithInserted['1'],+ '2': childWithInserted['2'],+ hash: (n.hash || 0) ^ hash,+ });++ return createTrieNode({+ '0': c === '0' ? updatedChild : trie['0'],+ '1': c === '1' ? updatedChild : trie['1'],+ '2': c === '2' ? updatedChild : trie['2'],+ hash: trie.hash,});}@@ -147,7 +188,11 @@ export function prune(trie: TrieNode, n = 2): TrieNode {const keys = getKeys(trie);keys.sort();- const next: TrieNode = { hash: trie.hash };+ const prunedChildren: {+ '0'?: TrieNode;+ '1'?: TrieNode;+ '2'?: TrieNode;+ } = {};// Prune child nodes.for (const k of keys.slice(-n)) {@@ -157,10 +202,15 @@ export function prune(trie: TrieNode, n = 2): TrieNode {throw new Error(`TrieNode for key ${k} could not be found`);}- next[k] = prune(node, n);+ prunedChildren[k] = prune(node, n);}- return next;+ return createTrieNode({+ '0': prunedChildren['0'],+ '1': prunedChildren['1'],+ '2': prunedChildren['2'],+ hash: trie.hash,+ });}export function debug(trie: TrieNode, k = '', indent = 0): string {diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-cash-flow-graph-and-checks-visuals-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-cash-flow-graph-and-checks-visuals-1-chromium-linux.pngindex 7fd766a761..058cd7db7c 100644Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-cash-flow-graph-and-checks-visuals-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-cash-flow-graph-and-checks-visuals-1-chromium-linux.png differdiff --git a/upcoming-release-notes/6165.md b/upcoming-release-notes/6165.mdnew file mode 100644index 0000000000..ea0db15b74--- /dev/null+++ b/upcoming-release-notes/6165.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [MatissJanis]+---++Enforce trie object key ordering - improve unit test stability