diff --git a/src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.test.ts b/src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.test.ts index b912cd91b..880490a68 100644 --- a/src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.test.ts +++ b/src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.test.ts @@ -1,6 +1,7 @@ import { Notification, shell } from 'electron'; import { getLanguage } from '@/apps/main/config/language'; -import { call } from '@/tests/vitest/utils.helper.test'; +import { loggerMock } from '@/tests/vitest/mocks.helper.test'; +import { call, calls } from '@/tests/vitest/utils.helper.test'; import { getRecoverySyncNotificationText, showRecoverySyncNotification } from './show-recovery-sync-notification'; vi.mock(import('@/apps/main/config/language')); @@ -52,15 +53,29 @@ describe('show-recovery-sync-notification', () => { expect(body).toBe('Recovery sync stopped before finishing.'); }); - it('should open the first recovery folder when the notification is clicked', () => { + it('should open the first recovery folder when the notification is clicked', async () => { // Given const openPathMock = vi.mocked(shell.openPath); + openPathMock.mockResolvedValue(''); // When showRecoverySyncNotification({ status: 'completed', preserved: 1, destinations: ['C:/Recovery'] }); onMock.mock.calls.find(([event]) => event === 'click')?.[1](); // Then + await vi.waitFor(() => expect(openPathMock).toHaveBeenCalled()); expect(showMock).toHaveBeenCalledOnce(); call(openPathMock).toBe(String.raw`C:\Recovery`); + calls(loggerMock.error).toHaveLength(0); + }); + + it('should log when the recovery folder cannot be opened', async () => { + // Given + vi.mocked(shell.openPath).mockResolvedValue('Failed to open path'); + // When + showRecoverySyncNotification({ status: 'completed', preserved: 1, destinations: ['C:/Recovery'] }); + onMock.mock.calls.find(([event]) => event === 'click')?.[1](); + // Then + await vi.waitFor(() => calls(loggerMock.error).toHaveLength(1)); + call(loggerMock.error).toMatchObject({ msg: 'Error opening the recovery sync folder', error: 'Failed to open path' }); }); it('should not handle clicks when nothing was moved', () => { diff --git a/src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.ts b/src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.ts index c7ebf793d..49b864204 100644 --- a/src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.ts +++ b/src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.ts @@ -72,6 +72,13 @@ export function getRecoverySyncNotificationText({ status, preserved, destination return { title: texts.title[status], body: [texts[status], moved].filter(Boolean).join(' ') }; } +/** + * v2.7.4 Alexis Mora + * Electron can garbage collect a notification nothing references once it is shown, and its click listener goes with it, + * so clicking it did nothing. The last one shown is kept here until a new run replaces it. + */ +let lastNotification: Notification | undefined; + /** * v2.7.2 Alexis Mora * Shown once per manual run, after every context finished, so the user gets one outcome for the whole run. @@ -84,7 +91,7 @@ export function showRecoverySyncNotification(outcome: RecoverySyncOutcome) { if (destination) { notification.on('click', () => { - void shell.openPath(win32.normalize(destination)); + void openRecoveryFolder({ destination }); }); } @@ -92,5 +99,11 @@ export function showRecoverySyncNotification(outcome: RecoverySyncOutcome) { recoverySynclogger.error({ msg: 'Recovery sync notification failed', title, error }); }); - notification.show(); + lastNotification = notification; + lastNotification.show(); +} + +async function openRecoveryFolder({ destination }: { destination: string }) { + const error = await shell.openPath(win32.normalize(destination)); + if (error) recoverySynclogger.error({ msg: 'Error opening the recovery sync folder', destination, error }); }