mirror of
https://github.com/NanmiCoder/claude-code-haha.git
synced 2026-10-10 03:43:11 +08:00
386a41e606
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:package: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
173 lines
5.6 KiB
TypeScript
173 lines
5.6 KiB
TypeScript
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { tmpdir } from 'node:os'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
captureWindowState,
|
|
hasMeaningfulIntersection,
|
|
installWindowLifecycle,
|
|
isPersistableWindowState,
|
|
isWindowStateVisibleOnAnyDisplay,
|
|
readWindowState,
|
|
restoreWindowMaximized,
|
|
showMainWindow,
|
|
windowOptionsFromState,
|
|
windowStatePath,
|
|
writeWindowState,
|
|
} from './windows'
|
|
|
|
const fakeApp = (userData: string) => ({
|
|
getPath: vi.fn(() => userData),
|
|
})
|
|
|
|
describe('Electron window service', () => {
|
|
it('persists window state in CLAUDE_CONFIG_DIR when portable config is active', () => {
|
|
const tmp = mkdtempSync(path.join(tmpdir(), 'electron-window-state-'))
|
|
try {
|
|
const app = fakeApp(path.join(tmp, 'user-data'))
|
|
const state = { x: 10, y: 20, width: 1280, height: 820, maximized: false }
|
|
|
|
writeWindowState(app as never, state, { CLAUDE_CONFIG_DIR: tmp })
|
|
|
|
const statePath = windowStatePath(app as never, { CLAUDE_CONFIG_DIR: tmp })
|
|
expect(JSON.parse(readFileSync(statePath, 'utf-8'))).toEqual(state)
|
|
expect(app.getPath).not.toHaveBeenCalled()
|
|
} finally {
|
|
rmSync(tmp, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('rejects undersized or off-screen state before restore', () => {
|
|
expect(isPersistableWindowState({ x: 0, y: 0, width: 100, height: 100, maximized: false })).toBe(false)
|
|
expect(hasMeaningfulIntersection(
|
|
{ x: 5000, y: 5000, width: 1280, height: 820 },
|
|
{ x: 0, y: 0, width: 1440, height: 900 },
|
|
)).toBe(false)
|
|
expect(isWindowStateVisibleOnAnyDisplay(
|
|
{ x: 100, y: 100, width: 1280, height: 820, maximized: false },
|
|
[{ bounds: { x: 0, y: 0, width: 1440, height: 900 }, workArea: { x: 0, y: 0, width: 1440, height: 860 } }],
|
|
)).toBe(true)
|
|
})
|
|
|
|
it('reads only valid visible window state', () => {
|
|
const tmp = mkdtempSync(path.join(tmpdir(), 'electron-window-state-read-'))
|
|
try {
|
|
const app = fakeApp(tmp)
|
|
const state = { x: 50, y: 60, width: 1280, height: 820, maximized: true }
|
|
writeWindowState(app as never, state, {})
|
|
|
|
expect(readWindowState(
|
|
app as never,
|
|
[{ bounds: { x: 0, y: 0, width: 1440, height: 900 }, workArea: { x: 0, y: 0, width: 1440, height: 860 } }],
|
|
{},
|
|
)).toEqual(state)
|
|
expect(readWindowState(
|
|
app as never,
|
|
[{ bounds: { x: 3000, y: 3000, width: 1440, height: 900 }, workArea: { x: 3000, y: 3000, width: 1440, height: 860 } }],
|
|
{},
|
|
)).toBeNull()
|
|
} finally {
|
|
rmSync(tmp, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('does not capture minimized windows', () => {
|
|
const window = {
|
|
isMinimized: () => true,
|
|
isMaximized: () => false,
|
|
getBounds: () => ({ x: 0, y: 0, width: 1280, height: 820 }),
|
|
}
|
|
|
|
expect(captureWindowState(window as never)).toBeNull()
|
|
})
|
|
|
|
it('restores persisted bounds and maximized state when reopening the window', () => {
|
|
const state = { x: 12, y: 34, width: 1400, height: 900, maximized: true }
|
|
const maximize = vi.fn()
|
|
|
|
expect(windowOptionsFromState(state)).toEqual({
|
|
x: 12,
|
|
y: 34,
|
|
width: 1400,
|
|
height: 900,
|
|
})
|
|
|
|
restoreWindowMaximized({ maximize } as never, state)
|
|
expect(maximize).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('shows, restores, and focuses the hidden main window when a tray or notification action reopens it', () => {
|
|
const window = {
|
|
isVisible: () => false,
|
|
isMinimized: () => true,
|
|
show: vi.fn(),
|
|
restore: vi.fn(),
|
|
focus: vi.fn(),
|
|
}
|
|
|
|
showMainWindow(window as never)
|
|
|
|
expect(window.show).toHaveBeenCalledTimes(1)
|
|
expect(window.restore).toHaveBeenCalledTimes(1)
|
|
expect(window.focus).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('hides instead of closing until the app is explicitly quitting', () => {
|
|
const tmp = mkdtempSync(path.join(tmpdir(), 'electron-window-close-'))
|
|
try {
|
|
const handlers = new Map<string, (...args: never[]) => void>()
|
|
const preventDefault = vi.fn()
|
|
const window = {
|
|
on: vi.fn((event: string, handler: (...args: never[]) => void) => {
|
|
handlers.set(event, handler)
|
|
}),
|
|
hide: vi.fn(),
|
|
isMinimized: () => false,
|
|
isMaximized: () => false,
|
|
getBounds: () => ({ x: 0, y: 0, width: 1280, height: 820 }),
|
|
}
|
|
|
|
installWindowLifecycle({
|
|
app: fakeApp(tmp) as never,
|
|
window: window as never,
|
|
shouldQuit: () => false,
|
|
})
|
|
|
|
handlers.get('close')?.({ preventDefault } as never)
|
|
expect(preventDefault).toHaveBeenCalledTimes(1)
|
|
expect(window.hide).toHaveBeenCalledTimes(1)
|
|
} finally {
|
|
rmSync(tmp, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('allows the window to close normally once the app is explicitly quitting', () => {
|
|
const tmp = mkdtempSync(path.join(tmpdir(), 'electron-window-quit-'))
|
|
try {
|
|
const handlers = new Map<string, (...args: never[]) => void>()
|
|
const preventDefault = vi.fn()
|
|
const window = {
|
|
on: vi.fn((event: string, handler: (...args: never[]) => void) => {
|
|
handlers.set(event, handler)
|
|
}),
|
|
hide: vi.fn(),
|
|
isMinimized: () => false,
|
|
isMaximized: () => false,
|
|
getBounds: () => ({ x: 0, y: 0, width: 1280, height: 820 }),
|
|
}
|
|
|
|
installWindowLifecycle({
|
|
app: fakeApp(tmp) as never,
|
|
window: window as never,
|
|
shouldQuit: () => true,
|
|
})
|
|
|
|
handlers.get('close')?.({ preventDefault } as never)
|
|
expect(preventDefault).not.toHaveBeenCalled()
|
|
expect(window.hide).not.toHaveBeenCalled()
|
|
} finally {
|
|
rmSync(tmp, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|