Files
web-terminal/test/desktop/server-config.test.ts
Yaojia Wang cf8cfccab4 feat(desktop): Electron all-in-one desktop shell (Mac/Windows) embedding the server
Add a `desktop/` Electron app that embeds the existing Node server + node-pty
(all-in-one): the window loads http://127.0.0.1:<port>/ and reuses the frontend
unchanged; other LAN devices can still connect. The server needs zero changes —
startServer(cfg)/loadConfig already support programmatic embedding.

- Pure, unit-tested modules: port, shell, server-config, deep-link, notify-policy,
  notifications, live-poll, prefs, settings-store (94 tests; desktop/src ~97% cov)
- Electron glue: main/window/tray/menu/preload/embedded-server/logger
  (hardened: contextIsolation, sandbox, deny foreign-origin navigation)
- Native value: OS notifications driven by /live-sessions status, tray, deep links
- Packaging (electron-builder -> arm64 .dmg): ships dist/ + public/ + node_modules
  on-disk under Resources so the server resolves its deps from /Applications;
  node-pty rebuilt for the Electron ABI
- Docs: docs/DESKTOP_PLAN.md; PROGRESS_LOG updated

Verified: desktop tsc clean; 1401 tests pass; coverage >=80% (desktop/src 97/95/100/97);
.dmg built and launch-tested on arm64 (server boots, UI serves 200, node-pty loads).
2026-07-02 06:13:17 +02:00

102 lines
3.4 KiB
TypeScript

/**
* test/desktop/server-config.test.ts — unit tests for buildServerEnv (B1).
*
* Covers the PORT / BIND_HOST / SHELL_PATH / USE_TMUX mapping, the immutability
* guarantee (input.env untouched), and pass-through of unrelated env vars.
*/
import { describe, test, expect } from 'vitest'
import { buildServerEnv } from '../../desktop/src/server-config.js'
import type { DesktopPrefs } from '../../desktop/src/types.js'
const BASE_PREFS: DesktopPrefs = {
port: null,
lanSharing: false,
shellPath: null,
openAtLogin: false,
notifyOnApproval: true,
notifyOnStatusChange: true,
}
describe('buildServerEnv', () => {
test('sets PORT to the stringified port', () => {
const result = buildServerEnv({ prefs: BASE_PREFS, port: 4321, platform: 'darwin', env: {} })
expect(result.PORT).toBe('4321')
})
test('binds 127.0.0.1 when lanSharing is off', () => {
const result = buildServerEnv({ prefs: BASE_PREFS, port: 3000, platform: 'darwin', env: {} })
expect(result.BIND_HOST).toBe('127.0.0.1')
})
test('binds 0.0.0.0 when lanSharing is on', () => {
// Arrange
const prefs = { ...BASE_PREFS, lanSharing: true }
// Act
const result = buildServerEnv({ prefs, port: 3000, platform: 'darwin', env: {} })
// Assert
expect(result.BIND_HOST).toBe('0.0.0.0')
})
test('uses prefs.shellPath as SHELL_PATH when provided (overriding the platform default)', () => {
// Arrange
const prefs = { ...BASE_PREFS, shellPath: '/usr/bin/fish' }
// Act
const result = buildServerEnv({ prefs, port: 3000, platform: 'darwin', env: { SHELL: '/bin/zsh' } })
// Assert
expect(result.SHELL_PATH).toBe('/usr/bin/fish')
})
test('falls back to the platform default SHELL_PATH when prefs.shellPath is null', () => {
const win = buildServerEnv({ prefs: BASE_PREFS, port: 3000, platform: 'win32', env: {} })
expect(win.SHELL_PATH).toBe('powershell.exe')
const mac = buildServerEnv({ prefs: BASE_PREFS, port: 3000, platform: 'darwin', env: { SHELL: '/bin/zsh' } })
expect(mac.SHELL_PATH).toBe('/bin/zsh')
})
test('forces USE_TMUX off on win32 even when the ambient env enables it', () => {
const result = buildServerEnv({ prefs: BASE_PREFS, port: 3000, platform: 'win32', env: { USE_TMUX: '1' } })
expect(result.USE_TMUX).toBe('0')
})
test('passes ambient USE_TMUX through on non-Windows platforms', () => {
const result = buildServerEnv({ prefs: BASE_PREFS, port: 3000, platform: 'linux', env: { USE_TMUX: '1' } })
expect(result.USE_TMUX).toBe('1')
})
test('defaults USE_TMUX to off on non-Windows when the env is unset', () => {
const result = buildServerEnv({ prefs: BASE_PREFS, port: 3000, platform: 'linux', env: {} })
expect(result.USE_TMUX).toBe('0')
})
test('does not mutate the input env', () => {
// Arrange
const env = { PATH: '/usr/bin', USE_TMUX: '1' }
const snapshot = { ...env }
// Act
buildServerEnv({ prefs: BASE_PREFS, port: 3000, platform: 'win32', env })
// Assert
expect(env).toEqual(snapshot)
})
test('preserves unrelated ambient env vars', () => {
// Arrange
const env = { PATH: '/usr/bin', HOME: '/Users/me', CUSTOM: 'x' }
// Act
const result = buildServerEnv({ prefs: BASE_PREFS, port: 3000, platform: 'darwin', env })
// Assert
expect(result.PATH).toBe('/usr/bin')
expect(result.HOME).toBe('/Users/me')
expect(result.CUSTOM).toBe('x')
})
})