Files
Netcatty/infrastructure/services/cloudSync/disconnectProvider.test.ts
T
Netcatty-Bot 2cfe2a05ea fix(sync-ui): clarify disconnect and preserve failure retry
Make provider disconnect visible and explain the local credentials removed and the vault, backup history, shared sync password and cloud copies retained. Report failed disconnects and preserve a retry path instead of implying success.

Validation: focused sync and dialog checks, isolated Electron cancel/failure/retry/reload against WebDAV, two independent final full-diff reviews, exact-head Codex review and current GitHub checks; the main integration tree also passed focused checks.

Related to #3688. Device handover cleanup remains outside this patch and the issue remains open.
2026-10-16 10:45:01 +08:00

82 lines
3.5 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { providerConnectionStorageKey, type ProviderConnection } from '../../../domain/sync';
import { disconnectProviderImpl } from './authMethods';
import { saveProviderConnectionImpl } from './stateAndSecurityMethods';
function harness(failure?: 'throw' | 'quota') {
const original: ProviderConnection = {
provider: 'webdav', status: 'connected',
config: { endpoint: 'https://fixture.invalid', username: 'fixture', password: 'fixture', authType: 'basic' },
};
const storage = new Map<string, unknown>([
[providerConnectionStorageKey('webdav'), original],
['backup-history', ['fixture-backup']],
['sync-password', 'fixture-password'],
['auto-sync', true],
['base', {}], ['convergent-base', {}], ['anchor', {}], ['account', 'fixture'],
]);
let notifications = 0;
let signOuts = 0;
let fail = failure;
const adapter = { signOut: () => { signOuts += 1; } };
const manager = {
adapters: new Map([['webdav', adapter]]),
providerDecryptSeq: { webdav: 0 },
providerWriteSeq: { webdav: 0 },
state: { providers: { webdav: original }, syncState: 'IDLE' },
cancelProviderAuthAttempt() {},
loadFromStorage: (key: string) => storage.get(key) ?? null,
saveToStorage(key: string, value: unknown) {
if (fail === 'throw') throw new Error('storage denied');
if (fail === 'quota') return false;
storage.set(key, value);
return true;
},
removeFromStorage: (key: string) => { storage.delete(key); },
syncBaseKey: () => 'base',
convergentProviderBaselineKey: () => 'convergent-base',
clearSyncAnchor: () => { storage.delete('anchor'); },
providerAccountIdKey: () => 'account',
exitBlockedState() {},
notifyStateChange: () => { notifications += 1; },
async saveProviderConnection(_provider: string, connection: ProviderConnection) {
await saveProviderConnectionImpl.call(manager, 'webdav', connection);
},
};
return {
manager, storage, original,
recover: () => { fail = undefined; },
notifications: () => notifications,
signOuts: () => signOuts,
};
}
test('disconnect clears provider connection and merge state while retaining local data', async () => {
const h = harness();
await disconnectProviderImpl.call(h.manager, 'webdav');
assert.deepEqual(h.storage.get(providerConnectionStorageKey('webdav')), { provider: 'webdav', status: 'disconnected' });
for (const key of ['base', 'convergent-base', 'anchor', 'account']) assert.equal(h.storage.has(key), false);
assert.deepEqual(h.storage.get('backup-history'), ['fixture-backup']);
assert.equal(h.storage.get('sync-password'), 'fixture-password');
assert.equal(h.storage.get('auto-sync'), true);
assert.equal(h.notifications(), 1);
assert.equal(h.signOuts(), 1);
});
for (const failure of ['throw', 'quota'] as const) {
test(`disconnect rejects ${failure} save failures and allows retry`, async () => {
const h = harness(failure);
await assert.rejects(disconnectProviderImpl.call(h.manager, 'webdav'));
assert.equal(h.storage.get(providerConnectionStorageKey('webdav')), h.original);
assert.equal(h.manager.state.providers.webdav, h.original);
assert.equal(h.notifications(), 0);
assert.equal(h.signOuts(), 0);
h.recover();
await disconnectProviderImpl.call(h.manager, 'webdav');
assert.equal(h.manager.state.providers.webdav.status, 'disconnected');
assert.equal(h.signOuts(), 1);
assert.equal(h.notifications(), 1);
});
}