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
Original file line number Diff line number Diff line change
@@ -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'));
Expand Down Expand Up @@ -52,15 +53,29 @@
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 () => {

Check failure on line 70 in src/backend/features/sync/recovery-sync/services/notification/show-recovery-sync-notification.test.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=internxt_drive-desktop&issues=AaJA3TXgcBfQQFaP7PPX&open=AaJA3TXgcBfQQFaP7PPX&pullRequest=1613
// 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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -84,13 +91,19 @@ export function showRecoverySyncNotification(outcome: RecoverySyncOutcome) {

if (destination) {
notification.on('click', () => {
void shell.openPath(win32.normalize(destination));
void openRecoveryFolder({ destination });
});
}

notification.on('failed', (error) => {
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 });
}
Loading