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 src/services/timerNotificationService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
base64UrlToUint8Array,
cancelTimerNotification,
createScheduleCredentials,
configureTimerNotificationApiUrl,
getBackgroundAlertStatus,
Expand All @@ -9,9 +10,12 @@ import {
} from './timerNotificationService';

describe('timer notification service', () => {
const originalFetch = global.fetch;

afterEach(() => {
localStorage.clear();
configureTimerNotificationApiUrl(undefined);
global.fetch = originalFetch;
});

it('converts a URL-safe VAPID key into subscription bytes', () => {
Expand Down Expand Up @@ -75,4 +79,32 @@ describe('timer notification service', () => {

expect(requestAlerts).not.toHaveBeenCalled();
});

it('cancels a background schedule and forgets its credentials', async () => {
configureTimerNotificationApiUrl('https://worker.example');
createScheduleCredentials('timer-1', () => 'generated-token');
const fetchMock = jest.fn().mockResolvedValue({ ok: true } as Response);
global.fetch = fetchMock as unknown as typeof fetch;

await cancelTimerNotification('timer-1');

expect(fetchMock).toHaveBeenCalledWith('https://worker.example/v1/schedules/timer-1', {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ capability: 'generated-token' }),
});
expect(localStorage.getItem('timer-notification:timer-1')).toBeNull();
});

it('keeps credentials and stays quiet when cancelling fails offline', async () => {
configureTimerNotificationApiUrl('https://worker.example');
createScheduleCredentials('timer-1', () => 'generated-token');
const fetchMock = jest.fn().mockRejectedValue(new TypeError('Failed to fetch'));
global.fetch = fetchMock as unknown as typeof fetch;

await expect(cancelTimerNotification('timer-1')).resolves.toBeUndefined();

expect(fetchMock).toHaveBeenCalledTimes(1);
expect(localStorage.getItem('timer-notification:timer-1')).toContain('generated-token');
});
});
17 changes: 12 additions & 5 deletions src/services/timerNotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,18 @@ export const cancelTimerNotification = async (timerId: string) => {
if (!apiBaseUrl || !storedCredentials) return;

const credentials = JSON.parse(storedCredentials) as ScheduleCredentials;
await fetch(`${apiBaseUrl}/v1/schedules/${encodeURIComponent(credentials.scheduleId)}`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ capability: credentials.capability }),
});
try {
await fetch(`${apiBaseUrl}/v1/schedules/${encodeURIComponent(credentials.scheduleId)}`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ capability: credentials.capability }),
});
} catch (error) {
// Callers cancel as fire-and-forget, so an offline or backgrounded page must not
// reject. Keep the credentials so a later cancel can still delete the schedule.
console.debug('Unable to cancel background timer alert:', error);
return;
}
localStorage.removeItem(scheduleCredentialsKey(timerId));
};

Expand Down
Loading