cc-haha/desktop/electron/services/notifications.test.ts
程序员阿江(Relakkes) 386a41e606 feat(desktop): enable Electron migration path
Introduce the Electron desktop shell alongside the existing React renderer and local Bun server boundary. The migration keeps the DesktopHost contract explicit across Tauri, Electron, and browser runtimes while adding Electron main/preload services for dialogs, shell, notifications, updates, tray/window lifecycle, terminal, preview WebContentsView, app mode, and release/package validation.

The commit also carries the latest local main desktop command updates, including agent slash entries and hidden-by-default markdown thinking details, so the packaged Electron build matches the current main UX surface.

Constraint: React renderer, local Bun server, REST/WebSocket, and sidecar boundaries must remain reusable during the migration
Constraint: macOS dev packages are ad-hoc signed and cannot prove Developer ID notarization or Gatekeeper release launch
Rejected: Browser-only smoke validation | it cannot exercise native dialogs, keychain prompts, notification behavior, or packaged app startup
Confidence: medium
Scope-risk: broad
Directive: Do not remove Tauri host support until signed Electron release artifacts pass native OS smoke on macOS, Windows, and Linux
Tested: bun run check:desktop
Tested: cd desktop && bun run check:electron
Tested: CSC_IDENTITY_AUTO_DISCOVERY=false bun run electron📦dir
Tested: bun run test:package-smoke --platform macos --package-kind dir --artifacts-dir desktop/build-artifacts/electron
Tested: Computer Use read packaged Electron app window at desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app
Not-tested: Developer ID signed/notarized Gatekeeper launch
Not-tested: Real OS notification click-to-session action
Not-tested: Windows and Linux packaged app smoke on real hosts
2026-06-01 22:43:16 +08:00

123 lines
3.9 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import {
notificationPermissionState,
requestNotificationPermission,
sendDesktopNotification,
validateNotificationOptions,
type ElectronNotificationConstructor,
} from './notifications'
function fakeNotificationClass(supported = true) {
const handlers = new Map<string, () => void>()
const show = vi.fn()
const NotificationClass = vi.fn(function (_options: unknown) {
return {
show,
on(event: 'click' | 'close' | 'failed', handler: () => void) {
handlers.set(event, handler)
return this
},
}
}) as unknown as ElectronNotificationConstructor & ReturnType<typeof vi.fn>
NotificationClass.isSupported = () => supported
return { NotificationClass, handlers, show }
}
describe('Electron notification service', () => {
it('reports Electron notification support as host permission state', () => {
expect(notificationPermissionState(fakeNotificationClass(true).NotificationClass)).toBe('granted')
expect(requestNotificationPermission(fakeNotificationClass(false).NotificationClass)).toBe('denied')
})
it('validates notification payloads before constructing OS notifications', () => {
expect(validateNotificationOptions({ title: 'Done', body: 'Task complete' })).toBe(true)
expect(validateNotificationOptions({ title: '' })).toBe(false)
expect(validateNotificationOptions({ title: 'Done', extra: [] })).toBe(false)
})
it('shows a notification and forwards click targets through the host action channel', () => {
const { NotificationClass, handlers, show } = fakeNotificationClass(true)
const onAction = vi.fn()
const onLifecycle = vi.fn()
const target = { type: 'session', sessionId: 'session-1' }
expect(sendDesktopNotification({
NotificationClass,
options: {
id: 1,
title: 'Done',
body: 'Task complete',
extra: { ccHahaTarget: JSON.stringify(target) },
target,
},
onAction,
onLifecycle,
})).toBe(true)
expect(NotificationClass).toHaveBeenCalledWith({
title: 'Done',
body: 'Task complete',
icon: undefined,
})
expect(show).toHaveBeenCalledTimes(1)
expect(handlers.has('close')).toBe(true)
expect(handlers.has('failed')).toBe(true)
handlers.get('click')?.()
expect(onAction).toHaveBeenCalledWith({
id: 1,
extra: { ccHahaTarget: JSON.stringify(target) },
target,
action: 'click',
})
expect(onLifecycle).not.toHaveBeenCalled()
})
it('reports close and failed lifecycle events for smoke diagnostics', () => {
const { NotificationClass, handlers } = fakeNotificationClass(true)
const onLifecycle = vi.fn()
expect(sendDesktopNotification({
NotificationClass,
options: { title: 'Done' },
onAction: vi.fn(),
onLifecycle,
})).toBe(true)
handlers.get('close')?.()
expect(onLifecycle).toHaveBeenCalledWith('close')
expect(sendDesktopNotification({
NotificationClass,
options: { title: 'Done again' },
onAction: vi.fn(),
onLifecycle,
})).toBe(true)
handlers.get('failed')?.()
expect(onLifecycle).toHaveBeenCalledWith('failed')
})
it('does not construct notifications when the platform does not support them', () => {
const { NotificationClass } = fakeNotificationClass(false)
expect(sendDesktopNotification({
NotificationClass,
options: { title: 'Done' },
onAction: vi.fn(),
})).toBe(false)
expect(NotificationClass).not.toHaveBeenCalled()
})
it('rejects malformed notification payloads before constructing Electron notifications', () => {
const { NotificationClass } = fakeNotificationClass(true)
expect(() => sendDesktopNotification({
NotificationClass,
options: { body: 'Missing title' },
onAction: vi.fn(),
})).toThrow('Invalid Electron notification payload')
expect(NotificationClass).not.toHaveBeenCalled()
})
})