Files
orca/config/scripts/desktop-distribution/runtime-verification/verify-packaged-daemon-entry.test.mjs
OrcaWinandOrcaWin 6c3ca0ab6d test: remove 34 low-value suites and redundant cases (#40115)
* test: remove low-value ACP, CLI and simulated scroll suites

* test: remove redundant runtime, session composition and timing suites

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-11-08 20:10:32 -08:00

111 lines
3.9 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,
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)
})
})