cc-haha/desktop/electron/services/appIdentity.test.ts
程序员阿江(Relakkes) ef87589923 fix(desktop): set Windows AppUserModelID and guard window navigation
- applyWindowsAppUserModelId() so Windows attributes toast notifications to the
  app (kept in sync with build.appId via a test); no-op on macOS/Linux
- main window: setWindowOpenHandler denies uncontrolled popups, routes http(s)
  links to the system browser; intentionally no will-navigate guard so dev HMR
  reloads keep working
- preview view: denies popups + blocks non-http(s) navigation (file:/custom
  schemes) while allowing in-page http(s) browsing

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-03 14:17:13 +08:00

29 lines
1.3 KiB
TypeScript

import { readFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it, vi } from 'vitest'
import { applyWindowsAppUserModelId, WINDOWS_APP_USER_MODEL_ID } from './appIdentity'
describe('applyWindowsAppUserModelId', () => {
it('sets the AppUserModelID on Windows so toast notifications are attributed to the app', () => {
const setAppUserModelId = vi.fn()
const result = applyWindowsAppUserModelId({ setAppUserModelId }, 'win32')
expect(result).toBe(true)
expect(setAppUserModelId).toHaveBeenCalledWith(WINDOWS_APP_USER_MODEL_ID)
})
it('is a no-op on macOS and Linux', () => {
for (const platform of ['darwin', 'linux'] as const) {
const setAppUserModelId = vi.fn()
expect(applyWindowsAppUserModelId({ setAppUserModelId }, platform)).toBe(false)
expect(setAppUserModelId).not.toHaveBeenCalled()
}
})
it('keeps the AppUserModelID in sync with build.appId in package.json', () => {
const packageJsonPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'package.json')
const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf8')) as { build?: { appId?: string } }
expect(WINDOWS_APP_USER_MODEL_ID).toBe(pkg.build?.appId)
})
})