Switch to double quotes and reformat codebase with Biome
This commit is contained in:
@@ -5,111 +5,115 @@
|
||||
* must not discover/kill processes via home-scoped `ps` command heuristics.
|
||||
*/
|
||||
|
||||
import assert from 'node:assert'
|
||||
import { spawn, type ChildProcess } from 'node:child_process'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { $ } from 'zx'
|
||||
import assert from "node:assert";
|
||||
import { spawn, type ChildProcess } from "node:child_process";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { $ } from "zx";
|
||||
|
||||
$.verbose = false
|
||||
$.verbose = false;
|
||||
|
||||
const testEnv = {
|
||||
PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD: process.env.PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD ?? '0',
|
||||
PASEO_DICTATION_ENABLED: process.env.PASEO_DICTATION_ENABLED ?? '0',
|
||||
PASEO_VOICE_MODE_ENABLED: process.env.PASEO_VOICE_MODE_ENABLED ?? '0',
|
||||
}
|
||||
PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD: process.env.PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD ?? "0",
|
||||
PASEO_DICTATION_ENABLED: process.env.PASEO_DICTATION_ENABLED ?? "0",
|
||||
PASEO_VOICE_MODE_ENABLED: process.env.PASEO_VOICE_MODE_ENABLED ?? "0",
|
||||
};
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function isProcessRunning(pid: number): boolean {
|
||||
if (!Number.isInteger(pid) || pid <= 0) {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
process.kill(pid, 0)
|
||||
return true
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForRunning(pid: number, timeoutMs: number): Promise<void> {
|
||||
const deadline = Date.now() + timeoutMs
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
if (isProcessRunning(pid)) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
await sleep(50)
|
||||
await sleep(50);
|
||||
}
|
||||
throw new Error(`Process ${pid} did not become running in time`)
|
||||
throw new Error(`Process ${pid} did not become running in time`);
|
||||
}
|
||||
|
||||
console.log('=== Daemon Stop Ownership Regression ===\n')
|
||||
console.log("=== Daemon Stop Ownership Regression ===\n");
|
||||
|
||||
const paseoHome = await mkdtemp(join(tmpdir(), 'paseo-stop-ownership-'))
|
||||
let decoyProcess: ChildProcess | null = null
|
||||
const paseoHome = await mkdtemp(join(tmpdir(), "paseo-stop-ownership-"));
|
||||
let decoyProcess: ChildProcess | null = null;
|
||||
|
||||
try {
|
||||
console.log('Test 1: start decoy process with daemon-like command markers')
|
||||
console.log("Test 1: start decoy process with daemon-like command markers");
|
||||
|
||||
decoyProcess = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
'-e',
|
||||
"-e",
|
||||
// Keep the process alive long enough for stop command assertions.
|
||||
'setInterval(() => {}, 1000)',
|
||||
'daemon-runner.ts',
|
||||
"setInterval(() => {}, 1000)",
|
||||
"daemon-runner.ts",
|
||||
],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
PASEO_HOME: paseoHome,
|
||||
},
|
||||
stdio: 'ignore',
|
||||
detached: process.platform !== 'win32',
|
||||
}
|
||||
)
|
||||
decoyProcess.unref()
|
||||
stdio: "ignore",
|
||||
detached: process.platform !== "win32",
|
||||
},
|
||||
);
|
||||
decoyProcess.unref();
|
||||
|
||||
const decoyPid = decoyProcess.pid
|
||||
assert(Number.isInteger(decoyPid) && (decoyPid ?? 0) > 0, 'decoy pid should exist')
|
||||
await waitForRunning(decoyPid!, 5000)
|
||||
console.log(`✓ decoy process started (${decoyPid})\n`)
|
||||
const decoyPid = decoyProcess.pid;
|
||||
assert(Number.isInteger(decoyPid) && (decoyPid ?? 0) > 0, "decoy pid should exist");
|
||||
await waitForRunning(decoyPid!, 5000);
|
||||
console.log(`✓ decoy process started (${decoyPid})\n`);
|
||||
|
||||
console.log('Test 2: daemon stop should report not_running and leave decoy untouched')
|
||||
console.log("Test 2: daemon stop should report not_running and leave decoy untouched");
|
||||
|
||||
const stopResult =
|
||||
await $`PASEO_HOME=${paseoHome} PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD=${testEnv.PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD} PASEO_DICTATION_ENABLED=${testEnv.PASEO_DICTATION_ENABLED} PASEO_VOICE_MODE_ENABLED=${testEnv.PASEO_VOICE_MODE_ENABLED} npx paseo daemon stop --home ${paseoHome} --json`.nothrow()
|
||||
assert.strictEqual(stopResult.exitCode, 0, `stop should succeed: ${stopResult.stderr}`)
|
||||
await $`PASEO_HOME=${paseoHome} PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD=${testEnv.PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD} PASEO_DICTATION_ENABLED=${testEnv.PASEO_DICTATION_ENABLED} PASEO_VOICE_MODE_ENABLED=${testEnv.PASEO_VOICE_MODE_ENABLED} npx paseo daemon stop --home ${paseoHome} --json`.nothrow();
|
||||
assert.strictEqual(stopResult.exitCode, 0, `stop should succeed: ${stopResult.stderr}`);
|
||||
|
||||
const parsed = JSON.parse(stopResult.stdout) as { action?: unknown }
|
||||
assert.strictEqual(parsed.action, 'not_running', `stop should not target decoy process: ${stopResult.stdout}`)
|
||||
assert(isProcessRunning(decoyPid!), 'decoy process must remain alive after stop')
|
||||
const parsed = JSON.parse(stopResult.stdout) as { action?: unknown };
|
||||
assert.strictEqual(
|
||||
parsed.action,
|
||||
"not_running",
|
||||
`stop should not target decoy process: ${stopResult.stdout}`,
|
||||
);
|
||||
assert(isProcessRunning(decoyPid!), "decoy process must remain alive after stop");
|
||||
|
||||
console.log('✓ stop is ownership-driven and does not kill decoy process\n')
|
||||
console.log("✓ stop is ownership-driven and does not kill decoy process\n");
|
||||
} finally {
|
||||
if (decoyProcess?.pid && isProcessRunning(decoyProcess.pid)) {
|
||||
try {
|
||||
process.kill(decoyProcess.pid, 'SIGTERM')
|
||||
process.kill(decoyProcess.pid, "SIGTERM");
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
await sleep(100)
|
||||
await sleep(100);
|
||||
if (isProcessRunning(decoyProcess.pid)) {
|
||||
try {
|
||||
process.kill(decoyProcess.pid, 'SIGKILL')
|
||||
process.kill(decoyProcess.pid, "SIGKILL");
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await $`PASEO_HOME=${paseoHome} npx paseo daemon stop --home ${paseoHome} --force`.nothrow()
|
||||
await rm(paseoHome, { recursive: true, force: true })
|
||||
await $`PASEO_HOME=${paseoHome} npx paseo daemon stop --home ${paseoHome} --force`.nothrow();
|
||||
await rm(paseoHome, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
console.log('=== Daemon stop ownership regression test passed ===')
|
||||
console.log("=== Daemon stop ownership regression test passed ===");
|
||||
|
||||
Reference in New Issue
Block a user