cc-haha/desktop/electron/services/appMode.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

131 lines
4.7 KiB
TypeScript

import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import {
applyStartupPortableMode,
defaultPortableDir,
detectPortableDir,
determineStartupPortableDir,
dirHasPortableData,
getAppMode,
setAppMode,
type AppModeAppLike,
} from './appMode'
const tempDirs: string[] = []
function tempDir() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cc-haha-app-mode-'))
tempDirs.push(dir)
return dir
}
function app(root = tempDir()): AppModeAppLike & { root: string } {
const exe = path.join(root, 'Claude Code Haha.app', 'Contents', 'MacOS', 'Claude Code Haha')
const userData = path.join(root, 'user-data')
fs.mkdirSync(path.dirname(exe), { recursive: true })
fs.writeFileSync(exe, '')
return {
root,
getPath(name) {
return name === 'exe' ? exe : userData
},
}
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true })
}
})
describe('Electron app mode service', () => {
it('detects portable data using the same sentinel files and directories as Tauri', () => {
const root = tempDir()
expect(dirHasPortableData(root)).toBe(false)
fs.writeFileSync(path.join(root, 'settings.json'), '{}')
expect(dirHasPortableData(root)).toBe(true)
fs.rmSync(path.join(root, 'settings.json'))
fs.mkdirSync(path.join(root, 'projects'))
expect(dirHasPortableData(root)).toBe(true)
})
it('resolves startup portable mode from default portable data or app-mode config', () => {
const fakeApp = app()
const defaultDir = defaultPortableDir(fakeApp)
fs.mkdirSync(defaultDir, { recursive: true })
fs.writeFileSync(path.join(defaultDir, 'settings.json'), '{}')
expect(determineStartupPortableDir(fakeApp, {})).toBe(defaultDir)
expect(determineStartupPortableDir(fakeApp, { CLAUDE_CONFIG_DIR: '/external' })).toBeNull()
fs.writeFileSync(path.join(defaultDir, 'app-mode.json'), JSON.stringify({ mode: 'default' }))
expect(determineStartupPortableDir(fakeApp, {})).toBeNull()
})
it('sets portable environment variables before sidecars start', () => {
const fakeApp = app()
const env: NodeJS.ProcessEnv = {}
const defaultDir = defaultPortableDir(fakeApp)
fs.mkdirSync(defaultDir, { recursive: true })
fs.writeFileSync(path.join(defaultDir, 'settings.json'), '{}')
expect(applyStartupPortableMode(fakeApp, env)).toBe(defaultDir)
expect(env.CLAUDE_CONFIG_DIR).toBe(defaultDir)
expect(env.CC_HAHA_APP_PORTABLE_DIR).toBe('1')
expect(env.WEBVIEW2_USER_DATA_FOLDER).toBe(path.join(defaultDir, 'EBWebView'))
})
it('returns the active app mode shape expected by settingsStore', () => {
const fakeApp = app()
expect(getAppMode(fakeApp, {})).toEqual({
mode: 'default',
portableDir: defaultPortableDir(fakeApp),
defaultPortableDir: defaultPortableDir(fakeApp),
activeConfigDir: fakeApp.getPath('userData'),
configDirSource: 'system',
})
expect(getAppMode(fakeApp, { CLAUDE_CONFIG_DIR: '/portable', CC_HAHA_APP_PORTABLE_DIR: '1' })).toMatchObject({
mode: 'portable',
portableDir: '/portable',
activeConfigDir: '/portable',
configDirSource: 'portable',
})
expect(getAppMode(fakeApp, { CLAUDE_CONFIG_DIR: '/external' })).toMatchObject({
configDirSource: 'environment',
})
})
it('writes app-mode.json to active, target portable, and system config dirs', () => {
const fakeApp = app()
const active = tempDir()
const selected = path.join(tempDir(), 'portable')
setAppMode(fakeApp, { mode: 'portable', portableDir: selected }, { CLAUDE_CONFIG_DIR: active })
const expected = { mode: 'portable', portable_dir: selected }
expect(JSON.parse(fs.readFileSync(path.join(active, 'app-mode.json'), 'utf8'))).toEqual(expected)
expect(JSON.parse(fs.readFileSync(path.join(selected, 'app-mode.json'), 'utf8'))).toEqual(expected)
expect(JSON.parse(fs.readFileSync(path.join(fakeApp.getPath('userData'), 'app-mode.json'), 'utf8'))).toEqual(expected)
setAppMode(fakeApp, { mode: 'default', portableDir: null }, { CLAUDE_CONFIG_DIR: active })
expect(JSON.parse(fs.readFileSync(path.join(active, 'app-mode.json'), 'utf8'))).toEqual({
mode: 'default',
portable_dir: null,
})
})
it('reports whether the default portable dir already has data', () => {
const fakeApp = app()
expect(detectPortableDir(fakeApp)).toEqual({
defaultPortableDir: defaultPortableDir(fakeApp),
hasData: false,
})
fs.mkdirSync(defaultPortableDir(fakeApp), { recursive: true })
fs.writeFileSync(path.join(defaultPortableDir(fakeApp), '.mcp.json'), '{}')
expect(detectPortableDir(fakeApp).hasData).toBe(true)
})
})