open-design/apps/web/tests/utils/notifications.test.ts
marco 5dd70b5016
Some checks failed
ci / Validate workspace (push) Successful in 12m32s
landing-page-ci / Validate landing page (push) Successful in 9m41s
landing-page-deploy / Deploy landing page (push) Failing after 5m23s
github-metrics / Generate repository metrics SVG (push) Failing after 2m3s
refresh-contributors-wall / Refresh contributors wall cache bust (push) Failing after 11s
Initial import: open-design source for helix-mind.ai distribution
This repository contains the open-design daemon CLI source code, built
and packaged at https://helix-mind.ai/cli/open-design/latest.tgz for use
by the HelixMind /design slash command.

Licenses: Apache-2.0 (root) + MIT (skills/*)
2026-05-06 20:50:24 +02:00

98 lines
3.0 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { showCompletionNotification } from '../../src/utils/notifications';
type NotificationOptionsWithRenotify = NotificationOptions & { renotify?: boolean };
class MockNotification {
static permission: NotificationPermission = 'granted';
static instances: MockNotification[] = [];
onclose: (() => void) | null = null;
onclick: (() => void) | null = null;
onerror: (() => void) | null = null;
constructor(
public title: string,
public options?: NotificationOptionsWithRenotify,
) {
MockNotification.instances.push(this);
}
close(): void {
// Fire synchronously so tests can observe cleanup without browser events.
this.onclose?.();
}
}
afterEach(() => {
vi.unstubAllGlobals();
MockNotification.permission = 'granted';
MockNotification.instances = [];
});
describe('showCompletionNotification', () => {
it('creates a renotifying desktop notification when permission is granted', async () => {
vi.stubGlobal('Notification', MockNotification as unknown as typeof Notification);
const result = await showCompletionNotification({
status: 'succeeded',
title: 'Task completed',
body: 'Done',
});
expect(result).toBe('shown');
expect(MockNotification.instances).toHaveLength(1);
expect(MockNotification.instances[0]!.title).toBe('Task completed');
expect(MockNotification.instances[0]!.options).toMatchObject({
body: 'Done',
tag: 'od-task-succeeded',
renotify: true,
});
});
it('uses the service worker notification API when available', async () => {
const showNotification = vi.fn().mockResolvedValue(undefined);
const registration = { showNotification };
const register = vi.fn().mockResolvedValue(registration);
vi.stubGlobal('Notification', MockNotification as unknown as typeof Notification);
vi.stubGlobal('navigator', {
serviceWorker: {
register,
ready: Promise.resolve(registration),
},
});
const result = await showCompletionNotification({
status: 'succeeded',
title: 'Task completed',
body: 'Done',
});
expect(result).toBe('shown');
expect(register).toHaveBeenCalledWith('/od-notifications-sw.js');
expect(showNotification).toHaveBeenCalledWith(
'Task completed',
expect.objectContaining({
body: 'Done',
tag: 'od-task-succeeded',
renotify: true,
}),
);
expect(MockNotification.instances).toHaveLength(0);
});
it('does not create a notification when permission is not granted', async () => {
MockNotification.permission = 'denied';
vi.stubGlobal('Notification', MockNotification as unknown as typeof Notification);
const result = await showCompletionNotification({
status: 'failed',
title: 'Task failed',
body: 'Error',
});
expect(result).toBe('permission-denied');
expect(MockNotification.instances).toHaveLength(0);
});
});