Set up harness with notification mocks, send seed message, return cleanup + count
(seedMessage: string)
| 389 | |
| 390 | /** Set up harness with notification mocks, send seed message, return cleanup + count */ |
| 391 | async function setupNotificationTest(seedMessage: string) { |
| 392 | const app = await createAppHarness({ branchPrefix: "compact-notify" }); |
| 393 | |
| 394 | // Mock document.hasFocus AFTER harness creates the DOM window |
| 395 | // Happy-dom's hasFocus returns !!this.activeElement - clear it to return false |
| 396 | const originalActiveElement = Object.getOwnPropertyDescriptor( |
| 397 | Object.getPrototypeOf(globalThis.document), |
| 398 | "activeElement" |
| 399 | ); |
| 400 | Object.defineProperty(globalThis.document, "activeElement", { |
| 401 | get: () => null, |
| 402 | configurable: true, |
| 403 | }); |
| 404 | |
| 405 | // Set Notification on the happy-dom window |
| 406 | (window as { Notification: unknown }).Notification = ( |
| 407 | globalThis as { Notification: unknown } |
| 408 | ).Notification; |
| 409 | |
| 410 | // Enable notifications via UI (click bell button in workspace header) |
| 411 | const notifyButton = app.view.container.querySelector( |
| 412 | '[data-testid="notify-on-response-button"]' |
| 413 | ); |
| 414 | if (!notifyButton) throw new Error("Notify button not found"); |
| 415 | fireEvent.click(notifyButton); |
| 416 | |
| 417 | // Send seed message and wait for notification |
| 418 | await app.chat.send(seedMessage); |
| 419 | await app.chat.expectTranscriptContains(`Mock response: ${seedMessage}`); |
| 420 | await waitFor(() => expect(notifications.length).toBeGreaterThanOrEqual(1), { timeout: 5_000 }); |
| 421 | |
| 422 | const countAfterSeed = notifications.length; |
| 423 | expect(countAfterSeed).toBe(1); |
| 424 | |
| 425 | const cleanup = async () => { |
| 426 | if (originalActiveElement) { |
| 427 | Object.defineProperty(globalThis.document, "activeElement", originalActiveElement); |
| 428 | } |
| 429 | await app.dispose(); |
| 430 | }; |
| 431 | |
| 432 | return { app, countAfterSeed, cleanup }; |
| 433 | } |
| 434 | |
| 435 | /** Wait for new notifications after seed, return count and last notification */ |
| 436 | async function waitForNewNotifications(countAfterSeed: number) { |
no test coverage detected