Files
orca/src/main/startup/single-instance-lock.test.ts
T
Jinjingandbbingz 1d2aaf1bf5 Fix recipe serve desktop promotion (#8646)
* fix(runtime): preserve terminals during headless desktop activation

* rm design doc

* Fix desktop activation launch ordering and blocked-window status resolut

- Check desktopWindowStatus before spawning the Orca app so a blocked
  runtime no longer launches a doomed second instance.
- Reuse resolveDesktopWindowStatus for remote runtime status so it
  honors the same authoritativeWindowId fallback as local status.
- Re-check the authoritative window at spawn time instead of trusting
  a possibly-stale snapshot, since it can be destroyed mid-await.
- Harden the e2e activation spec against silent spawn failures.

---------

Co-authored-by: bbingz <zzb@gxsmjx.com>
2026-07-13 19:41:24 -07:00

145 lines
4.8 KiB
TypeScript

import type { App } from 'electron'
import { describe, expect, it, vi } from 'vitest'
import {
acquireSingleInstanceLock,
logSingleInstanceLockBypass,
logSingleInstanceLockFailure,
shouldBypassSingleInstanceLock,
shouldSkipSingleInstanceLock,
SINGLE_INSTANCE_LOCK_BYPASS_MESSAGE,
SINGLE_INSTANCE_LOCK_FAILURE_MESSAGE
} from './single-instance-lock'
type Listener = (...args: unknown[]) => void
function makeFakeApp(lockResult: boolean): {
app: App
requestSingleInstanceLock: ReturnType<typeof vi.fn>
on: ReturnType<typeof vi.fn>
listeners: Record<string, Listener[]>
} {
const listeners: Record<string, Listener[]> = {}
const requestSingleInstanceLock = vi.fn(() => lockResult)
const on = vi.fn((event: string, cb: Listener) => {
listeners[event] = listeners[event] ?? []
listeners[event].push(cb)
})
const app = {
requestSingleInstanceLock,
on
} as unknown as App
return { app, requestSingleInstanceLock, on, listeners }
}
describe('acquireSingleInstanceLock', () => {
it('returns false and does NOT register second-instance when the lock is held', () => {
const onSecondInstance = vi.fn()
const fake = makeFakeApp(false)
const acquired = acquireSingleInstanceLock(fake.app, onSecondInstance)
expect(acquired).toBe(false)
expect(fake.requestSingleInstanceLock).toHaveBeenCalledTimes(1)
// Why: if we registered the listener on a losing process, focusing the
// existing window would become our job even though the primary owns
// that UX surface. Verify no listener was added.
expect(fake.on).not.toHaveBeenCalled()
expect(fake.listeners['second-instance']).toBeUndefined()
})
it('returns true and registers exactly one second-instance listener when the lock is acquired', () => {
const onSecondInstance = vi.fn()
const fake = makeFakeApp(true)
const acquired = acquireSingleInstanceLock(fake.app, onSecondInstance)
expect(acquired).toBe(true)
expect(fake.requestSingleInstanceLock).toHaveBeenCalledTimes(1)
expect(fake.on).toHaveBeenCalledTimes(1)
expect(fake.on).toHaveBeenCalledWith('second-instance', onSecondInstance)
expect(fake.listeners['second-instance']).toHaveLength(1)
})
it('fires the registered callback when second-instance dispatches', () => {
const onSecondInstance = vi.fn()
const fake = makeFakeApp(true)
acquireSingleInstanceLock(fake.app, onSecondInstance)
const [registered] = fake.listeners['second-instance'] ?? []
expect(registered).toBeDefined()
registered?.()
expect(onSecondInstance).toHaveBeenCalledTimes(1)
})
})
describe('shouldSkipSingleInstanceLock', () => {
it('keeps ordinary dev multi-instance behavior but never skips for serve', () => {
expect(shouldSkipSingleInstanceLock({ isDev: true, isServeMode: false, env: {} })).toBe(true)
expect(shouldSkipSingleInstanceLock({ isDev: true, isServeMode: true, env: {} })).toBe(false)
expect(shouldSkipSingleInstanceLock({ isDev: false, isServeMode: false, env: {} })).toBe(false)
})
it('lets isolated E2E exercise the production single-instance path', () => {
expect(
shouldSkipSingleInstanceLock({
isDev: true,
isServeMode: false,
env: { ORCA_E2E_ENFORCE_SINGLE_INSTANCE_LOCK: '1' }
})
).toBe(false)
})
})
describe('logSingleInstanceLockFailure', () => {
it('emits a production-visible synchronous diagnostic for the early quit path', () => {
const write = vi.fn()
logSingleInstanceLockFailure(write)
expect(write).toHaveBeenCalledWith(2, `${SINGLE_INSTANCE_LOCK_FAILURE_MESSAGE}\n`)
expect(write.mock.calls[0]?.[1]).toContain('Electron/macOS single-instance lock failure')
})
})
describe('shouldBypassSingleInstanceLock', () => {
it('allows the hidden diagnostic bypass only for packaged macOS app launches', () => {
expect(
shouldBypassSingleInstanceLock({
env: { ORCA_BYPASS_SINGLE_INSTANCE_LOCK: '1' },
isDev: false,
isServeMode: false,
platform: 'darwin'
})
).toBe(true)
expect(
shouldBypassSingleInstanceLock({
env: { ORCA_BYPASS_SINGLE_INSTANCE_LOCK: '1' },
isDev: true,
isServeMode: false,
platform: 'darwin'
})
).toBe(false)
expect(
shouldBypassSingleInstanceLock({
env: { ORCA_BYPASS_SINGLE_INSTANCE_LOCK: '1' },
isDev: false,
isServeMode: false,
platform: 'linux'
})
).toBe(false)
})
})
describe('logSingleInstanceLockBypass', () => {
it('emits a warning when the diagnostic bypass is active', () => {
const write = vi.fn()
logSingleInstanceLockBypass(write)
expect(write).toHaveBeenCalledWith(2, `${SINGLE_INSTANCE_LOCK_BYPASS_MESSAGE}\n`)
expect(write.mock.calls[0]?.[1]).toContain('bypassing the packaged macOS single-instance lock')
})
})