Fix: Enforce single default storage connection

Previously, `StorageService.saveConnection` allowed multiple connections to be marked as `defaultForUploads: true` because it did not unset the flag on existing connections. This caused `loadFromStorage` (which uses `find`) to potentially pick up an old, invalid connection instead of the newly configured one, leading to a loop where the user is asked to configure credentials again.

This change modifies `saveConnection` to iterate through existing connections and unset `defaultForUploads` if the new connection is being saved as the default. This ensures the "default" invariant is maintained.

Added a unit test case to verify this behavior.
This commit is contained in:
google-labs-jules[bot] committed 2026-01-25 17:40:16 +00:00
1 parent e162ee355d
commit ea026552ce
2 files changed
+27

No files matched your search

+10
View File
@@ -418,6 +418,16 @@ export class StorageService {
account.connections = {};
}
// If this connection is being set as default, unset others
if (meta.defaultForUploads) {
for (const key in account.connections) {
const conn = account.connections[key];
if (conn.meta) {
conn.meta.defaultForUploads = false;
}
}
}
account.connections[connectionId] = {
id: connectionId,
provider: payload.provider || meta.provider,
+17
View File
@@ -155,4 +155,21 @@ describe("StorageService", () => {
assert.strictEqual(c1.meta.defaultForUploads, false);
assert.strictEqual(c2.meta.defaultForUploads, true);
});
test("saveConnection() with defaultForUploads=true clears other defaults", async () => {
await storageService.unlock(pubkey, { signer: mockSigner });
// Save first connection as default
await storageService.saveConnection(pubkey, "conn_1", { provider: PROVIDERS.R2 }, { defaultForUploads: true });
// Save second connection as default
await storageService.saveConnection(pubkey, "conn_2", { provider: PROVIDERS.S3 }, { defaultForUploads: true });
const conns = await storageService.listConnections(pubkey);
const c1 = conns.find(c => c.id === "conn_1");
const c2 = conns.find(c => c.id === "conn_2");
assert.strictEqual(c1.meta.defaultForUploads, false);
assert.strictEqual(c2.meta.defaultForUploads, true);
});
});