Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions browser/data-browser/src/helpers/managed/vaultAutoBackup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,41 @@ describe('restoreFromVault', () => {
throw new Error('403');
}),
});
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);

expect((await restoreFromVault(store, DRIVE, deps)).status).toBe('failed');
expect(store.isLocalOnlyDrive(DRIVE)).toBe(false);
// A real failure on a live page is still worth saying out loud.
expect(warn).toHaveBeenCalledWith(
'[cloud-vault] restore failed',
expect.anything(),
);
warn.mockRestore();
});

/**
* Navigating away cancels the restore's own requests, which reject as
* `TypeError: Failed to fetch`. Warning about that paints every interrupted
* restore as broken — and in a suite that fails on unexpected console output,
* as a red test.
*/
it('stays quiet when the document is discarded mid-restore', async () => {
const page = new EventTarget();
vi.stubGlobal('window', page);
const store = await signedInStore();
const deps = fakeDeps({
restoreDrive: vi.fn(async () => {
page.dispatchEvent(new Event('pagehide'));
throw new TypeError('Failed to fetch');
}),
});
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);

expect((await restoreFromVault(store, DRIVE, deps)).status).toBe('failed');
expect(warn).not.toHaveBeenCalled();

warn.mockRestore();
vi.unstubAllGlobals();
});
});

Expand Down
22 changes: 20 additions & 2 deletions browser/data-browser/src/helpers/managed/vaultAutoBackup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { driveDisplayMetadata } from './driveDisplayMetadata';
import { StoreEvents, isNotFound, type Store } from '@tomic/lib';
import {
StoreEvents,
isNotFound,
pageRequestSignal,
type Store,
} from '@tomic/lib';
import {
VaultSessionEndedError,
agentVaultProof,
Expand Down Expand Up @@ -441,6 +446,10 @@ export async function restoreFromVault(

if (!agent?.subject) return { status: 'no-backup', reason: 'not signed in' };

// Taken before the first request, so its pagehide listener is registered
// while this document is still the live one. The catch reads it.
const pageSignal = pageRequestSignal();

const absentFromNode = isNotFound(store.resources.get(driveSubject)?.error);

try {
Expand Down Expand Up @@ -514,7 +523,16 @@ export async function restoreFromVault(

return { status: 'restored', outcome };
} catch (error) {
console.warn('[cloud-vault] restore failed', error);
// A discarded document cancels its own in-flight requests, and they land
// here as `TypeError: Failed to fetch`, indistinguishable by type from a
// control plane that is really unreachable. The page is already gone, so
// there is nobody to warn and the warning reads as a defect on every
// navigation that happens to interrupt a restore. `Client
// .fetchResourceHTTP` draws the same line for reads, and the backup path
// above for its own cancellation.
if (!pageSignal?.aborted) {
console.warn('[cloud-vault] restore failed', error);
}

return {
status: 'failed',
Expand Down
1 change: 1 addition & 0 deletions browser/lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export * from './ontology.js';
export * from './invites.js';
export * from './pairing.js';
export * from './loro-loader.js';
export * from './page-request-signal.js';
export * from './presence.js';
export * from './CryptoProvider.js';
export { ClientDbWorker } from './client-db.js';
Expand Down