Files
orca/config/scripts/desktop-distribution/runtime-verification/verify-packaged-daemon-entry.test.mjs
Vitalii KulishandNeil 4824562f3e fix(packaging): run the terminal host PTY probe in /bin/sh (#39602)
The packaged daemon PTY check types a POSIX arithmetic command and waits
for "OK 42". The daemon picked the PTY shell from the builder's $SHELL, so
a fish builder never printed it and Linux afterPack timed out. Pin the
probe daemon's SHELL to /bin/sh so the check exercises the packaged PTY,
not the builder's login shell.

Fixes #39591

Co-authored-by: Neil <neil@stably.ai>
2026-11-08 03:35:16 -08:00

137 lines
5.3 KiB
JavaScript

import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const require = createRequire(import.meta.url)
const { runTerminalHostRoundTrip } = require('./verify-packaged-daemon-entry.cjs')
describe.skipIf(process.platform === 'win32')('terminal host round trip', () => {
let root
beforeEach(() => {
root = mkdtempSync(join(tmpdir(), 'vpd-'))
})
afterEach(() => {
vi.unstubAllEnvs()
rmSync(root, { recursive: true, force: true })
})
// Listens, publishes its token, records its pid, then treats each connection with `onConnection`.
function fakeDaemon(onConnection) {
const entry = join(root, 'daemon.cjs')
writeFileSync(
entry,
[
"const net = require('node:net'), fs = require('node:fs')",
'const arg = (name) => process.argv[process.argv.indexOf(name) + 1]',
`fs.writeFileSync(${JSON.stringify(join(root, 'pid'))}, String(process.pid))`,
`fs.writeFileSync(${JSON.stringify(join(root, 'shell'))}, process.env.SHELL)`,
`net.createServer(${onConnection}).listen(arg('--socket'), () => fs.writeFileSync(arg('--token'), 'tok'))`
].join('\n')
)
return entry
}
async function roundTrip(entry, timeoutMs) {
const startedAt = Date.now()
const error = await runTerminalHostRoundTrip({
command: [process.execPath],
entry,
deadline: startedAt + timeoutMs,
stderr: [],
scratchPrefix: join(root, 'oth-')
}).then(
() => null,
(rejection) => rejection
)
const pid = Number(readFileSync(join(root, 'pid'), 'utf8'))
let daemonAlive = true
try {
process.kill(pid, 0)
} catch {
daemonAlive = false
}
return {
error,
elapsedMs: Date.now() - startedAt,
daemonAlive,
scratchDirs: readdirSync(root).filter((name) => name.startsWith('oth-'))
}
}
it('accepts a valid peer round trip and cleans up after observing the boot request', async () => {
const messagesPath = join(root, 'messages.jsonl')
const entry = fakeDaemon(`(socket) => {
let buffer = ''
socket.on('data', (chunk) => {
buffer += chunk
let newline
while ((newline = buffer.indexOf('\\n')) !== -1) {
const message = JSON.parse(buffer.slice(0, newline))
buffer = buffer.slice(newline + 1)
fs.appendFileSync(${JSON.stringify(messagesPath)}, JSON.stringify(message) + '\\n')
if (message.type === 'hello') {
if (message.role === 'stream') globalThis.stream = socket
socket.write(JSON.stringify({ ok: true }) + '\\n')
} else if (message.type === 'createOrAttach') {
globalThis.stream.write(JSON.stringify({ payload: { data: 'OK 42' } }) + '\\n')
}
}
})
}`)
vi.stubEnv('SHELL', '/usr/bin/fish')
const result = await roundTrip(entry, 1_500)
expect(result.error).toBe(null)
expect(readFileSync(join(root, 'shell'), 'utf8')).toBe('/bin/sh')
expect(result.daemonAlive).toBe(false)
expect(result.scratchDirs).toEqual([])
const messages = readFileSync(messagesPath, 'utf8').trim().split('\n').map(JSON.parse)
expect(messages.slice(0, 2).map(({ type, role, token }) => ({ type, role, token }))).toEqual([
{ type: 'hello', role: 'control', token: 'tok' },
{ type: 'hello', role: 'stream', token: 'tok' }
])
expect(messages[0].clientId).toBe(messages[1].clientId)
expect(messages[0].clientId).toMatch(/^[0-9a-f-]{36}$/)
expect(messages).toHaveLength(3)
expect(messages[2]).toMatchObject({
id: 'boot-1',
type: 'createOrAttach',
payload: {
sessionId: 'terminal-host-boot',
cols: 80,
rows: 24,
command: 'echo "OK $((6*7))" && exit'
}
})
expect(messages[2].payload.cwd.startsWith(join(root, 'oth-'))).toBe(true)
})
it('rejects by the deadline and cleans up when the daemon never answers the hello', async () => {
const result = await roundTrip(fakeDaemon('() => {}'), 1_500)
expect(result.error?.message).toMatch(/timed out waiting for the control hello reply/)
expect(result.elapsedMs).toBeLessThan(1_500 + 5_000)
expect(result.daemonAlive).toBe(false)
expect(result.scratchDirs).toEqual([])
})
it('rejects at once and cleans up when the daemon closes the connection', async () => {
const result = await roundTrip(fakeDaemon('(socket) => socket.destroy()'), 10_000)
// A destroyed socket may report a clean close, a reset, or a write after close.
expect(result.error?.message).toMatch(/closed the control connection|ECONNRESET|EPIPE/)
expect(result.elapsedMs).toBeLessThan(10_000)
expect(result.daemonAlive).toBe(false)
expect(result.scratchDirs).toEqual([])
})
it('rejects and cleans up when the daemon replies with malformed JSON', async () => {
const result = await roundTrip(fakeDaemon("(socket) => socket.write('{not json\\n')"), 10_000)
expect(result.error?.message).toMatch(/non-JSON control line: \{not json/)
expect(result.elapsedMs).toBeLessThan(10_000)
expect(result.daemonAlive).toBe(false)
expect(result.scratchDirs).toEqual([])
})
})