Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions test/fake-direct-run.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// The stand-in backend must start when the checkout path has a space or is reached through a symlink.
import assert from 'node:assert/strict'
import {spawn} from 'node:child_process'
import {mkdir, mkdtemp, rm, symlink} from 'node:fs/promises'
import os from 'node:os'
import {join} from 'node:path'
import {test} from 'node:test'
import {fileURLToPath} from 'node:url'

const fakePath = fileURLToPath(new URL('./fake-omarchyd.mjs', import.meta.url))

function started(script) {
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, [script, '0'], {stdio: ['ignore', 'pipe', 'pipe']})
let out = ''
let err = ''
let settled = false
const timer = setTimeout(() => done(new Error(`no startup line\n${out}\n${err}`)), 15_000)
const done = (error, text) => {
if (settled) return
settled = true
clearTimeout(timer)
child.kill('SIGTERM')
const kill = setTimeout(() => child.kill('SIGKILL'), 1000)
kill.unref()
if (child.exitCode != null || child.signalCode != null) {
error ? reject(error) : resolve(text)
return
}
child.on('exit', () => (error ? reject(error) : resolve(text)))
}
child.stdout.on('data', (b) => {
out += b
if (out.includes('fake omarchyd (TEST ONLY)')) done(null, out)
})
child.stderr.on('data', (b) => {
err += b
})
child.on('error', (error) => done(error))
child.on('exit', (code) => {
if (!out.includes('fake omarchyd (TEST ONLY)')) done(new Error(`exited ${code}\n${out}\n${err}`))
})
})
}

test('fake omarchyd starts through a symlink and a path with a space', async () => {
const dir = await mkdtemp(join(os.tmpdir(), 'omarchy-fake-'))
try {
const link = join(dir, 'fake-link.mjs')
await symlink(fakePath, link)
const spaced = join(dir, 'sp ace')
await mkdir(spaced)
const spacedFake = join(spaced, 'fake-omarchyd.mjs')
await symlink(fakePath, spacedFake)
for (const script of [link, spacedFake]) {
const out = await started(script)
assert.match(out, /fake omarchyd \(TEST ONLY\) at /)
}
} finally {
await rm(dir, {recursive: true, force: true})
}
})
14 changes: 13 additions & 1 deletion test/fake-omarchyd.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// server. It is never shipped or used by the app itself.

import http from 'node:http'
import {realpathSync} from 'node:fs'
import {readFile, readdir} from 'node:fs/promises'
import {randomBytes} from 'node:crypto'
import {fileURLToPath} from 'node:url'
import {WebSocketServer} from 'ws'

export async function startFakeserverd({
Expand Down Expand Up @@ -1172,7 +1174,17 @@ function rfbServer(ws, keys) {
}

// `node test/fake-omarchyd.mjs 7781` runs it standalone for UI development.
if (import.meta.url === `file://${process.argv[1]}`) {
// import.meta.url is the real path, percent-encoded. argv is the path as typed, so a
// space or a symlink used to make this comparison fail and the process exit 0.
function ranAsMain() {
if (typeof import.meta.main === 'boolean') return import.meta.main
try {
return Boolean(process.argv[1]) && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))
} catch {
return false
}
}
if (ranAsMain()) {
const fake = await startFakeserverd({port: Number(process.argv[2] ?? 7781)})
console.log(`fake omarchyd (TEST ONLY) at ${fake.url}, password "correct horse"`)
// Lets a browser test read which keys reached the fake VM.
Expand Down
Loading