Skip to content

A local-first personal finance app

[AI] Fix CLI failing to push changes to end-to-end encrypted budgets (#8800)

Artem Ivanovff8de893e604
diff --git a/packages/cli/src/connection.test.ts b/packages/cli/src/connection.test.ts
index 8bb0ad4827..b111880378 100644
--- a/packages/cli/src/connection.test.ts
+++ b/packages/cli/src/connection.test.ts
@@ -146,7 +146,7 @@ describe('withConnection', () => {
expect(api.sync).toHaveBeenCalledTimes(1);
});
- it('encrypted budget forces a sync on a read inside the TTL', async () => {
+ it('encrypted budget syncs through downloadBudget on a read inside the TTL', async () => {
setConfig({ encryptionPassword: 'secret' });
writeCacheState(metaDirFor('sync-1'), {
version: 1,
@@ -157,9 +157,46 @@ describe('withConnection', () => {
lastDownloadedAt: Date.now(),
});
await withConnection({}, async () => 'ok', { mutates: false });
+ expect(api.downloadBudget).toHaveBeenCalledWith('sync-1', {
+ password: 'secret',
+ });
+ expect(api.loadBudget).not.toHaveBeenCalled();
+ });
+
+ it('encrypted budget registers the encryption key before a write pushes', async () => {
+ setConfig({ encryptionPassword: 'secret' });
+ writeCacheState(metaDirFor('sync-1'), {
+ version: 1,
+ syncId: 'sync-1',
+ budgetId: 'bud-disk-1',
+ serverUrl: 'http://test',
+ lastSyncedAt: Date.now(),
+ lastDownloadedAt: Date.now(),
+ });
+ await withConnection({}, async () => 'ok', { mutates: true });
+ // `loadBudget` would leave the key unregistered and the push below would
+ // fail with `encrypt-failure`.
+ expect(api.downloadBudget).toHaveBeenCalledWith('sync-1', {
+ password: 'secret',
+ });
+ expect(api.loadBudget).not.toHaveBeenCalled();
expect(api.sync).toHaveBeenCalledTimes(1);
});
+ it('unencrypted budget still syncs through loadBudget', async () => {
+ writeCacheState(metaDirFor('sync-1'), {
+ version: 1,
+ syncId: 'sync-1',
+ budgetId: 'bud-disk-1',
+ serverUrl: 'http://test',
+ lastSyncedAt: Date.now(),
+ lastDownloadedAt: Date.now(),
+ });
+ await withConnection({}, async () => 'ok', { mutates: true });
+ expect(api.loadBudget).toHaveBeenCalledWith('bud-disk-1');
+ expect(api.downloadBudget).not.toHaveBeenCalled();
+ });
+
it('invalidates cache when syncId changes', async () => {
writeCacheState(metaDirFor('sync-1'), {
version: 1,
diff --git a/packages/cli/src/connection.ts b/packages/cli/src/connection.ts
index 78aa167aac..05e028478c 100644
--- a/packages/cli/src/connection.ts
+++ b/packages/cli/src/connection.ts
@@ -127,6 +127,19 @@ export async function withConnection<T>(
info(`Using cached budget (synced ${age}s ago)...`, globalOpts.verbose);
await api.loadBudget(decision.state.budgetId);
state = decision.state;
+ } else if (config.encryptionPassword) {
+ info(`Syncing budget ${config.syncId}...`, globalOpts.verbose);
+ // `loadBudget` does not register the end-to-end encryption key, so a
+ // later push fails with `encrypt-failure` / `isMissingKey`. Reads still
+ // work, which makes the failure look like a broken budget file rather
+ // than a missing key. `downloadBudget` registers the key via `key-test`
+ // and, when the budget already exists locally, just loads and syncs it
+ // instead of re-downloading, so this costs nothing extra.
+ await api.downloadBudget(config.syncId, {
+ password: config.encryptionPassword,
+ });
+ state = { ...decision.state, lastSyncedAt: Date.now() };
+ writeCacheState(meta, state);
} else {
info(`Syncing budget ${config.syncId}...`, globalOpts.verbose);
await api.loadBudget(decision.state.budgetId);
diff --git a/upcoming-release-notes/8800.md b/upcoming-release-notes/8800.md
new file mode 100644
index 0000000000..b46f1cdc19
--- /dev/null
+++ b/upcoming-release-notes/8800.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [ivanovart]
+---
+
+Fix the CLI failing to push changes to end-to-end encrypted budgets