Skip to content
Closed
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
11 changes: 10 additions & 1 deletion src/daemon-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type DaemonClientSettings = {
};

const REQUEST_TIMEOUT_MS = resolveDaemonRequestTimeoutMs();
const DAEMON_STARTUP_TIMEOUT_MS = 5000;
const DAEMON_STARTUP_TIMEOUT_MS = resolveDaemonStartupTimeoutMs();
const DAEMON_TAKEOVER_TERM_TIMEOUT_MS = 3000;
const DAEMON_TAKEOVER_KILL_TIMEOUT_MS = 1000;
const IOS_RUNNER_XCODEBUILD_KILL_PATTERNS = [
Expand Down Expand Up @@ -650,6 +650,15 @@ export function resolveDaemonRequestTimeoutMs(raw: string | undefined = process.
return Math.max(1000, Math.floor(parsed));
}

export function resolveDaemonStartupTimeoutMs(
raw: string | undefined = process.env.AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS,
): number {
if (!raw) return 15000;
const parsed = Number(raw);
if (!Number.isFinite(parsed)) return 15000;
return Math.max(1000, Math.floor(parsed));
}

export function resolveDaemonStartupHint(
state: { hasInfo: boolean; hasLock: boolean },
paths: Pick<DaemonPaths, 'infoPath' | 'lockPath'> = resolveDaemonPaths(process.env.AGENT_DEVICE_STATE_DIR),
Expand Down
17 changes: 16 additions & 1 deletion src/utils/__tests__/daemon-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { spawn } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { computeDaemonCodeSignature, resolveDaemonRequestTimeoutMs, resolveDaemonStartupHint } from '../../daemon-client.ts';
import {
computeDaemonCodeSignature,
resolveDaemonRequestTimeoutMs,
resolveDaemonStartupHint,
resolveDaemonStartupTimeoutMs,
} from '../../daemon-client.ts';
import { resolveDaemonPaths } from '../../daemon/config.ts';
import {
isProcessAlive,
Expand All @@ -23,6 +28,16 @@ test('resolveDaemonRequestTimeoutMs enforces minimum timeout', () => {
assert.equal(resolveDaemonRequestTimeoutMs('invalid'), 90000);
});

test('resolveDaemonStartupTimeoutMs defaults to 15000', () => {
assert.equal(resolveDaemonStartupTimeoutMs(undefined), 15000);
});

test('resolveDaemonStartupTimeoutMs enforces minimum timeout', () => {
assert.equal(resolveDaemonStartupTimeoutMs('100'), 1000);
assert.equal(resolveDaemonStartupTimeoutMs('20000'), 20000);
assert.equal(resolveDaemonStartupTimeoutMs('invalid'), 15000);
});

test('resolveDaemonStartupHint prefers stale lock guidance when lock exists without info', () => {
const hint = resolveDaemonStartupHint({ hasInfo: false, hasLock: true });
assert.match(hint, /daemon\.lock/i);
Expand Down
11 changes: 9 additions & 2 deletions test/integration/android.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import test from 'node:test';
import { createIntegrationTestContext, runCliJson } from './test-helpers.ts';
import path from 'node:path';
import { cleanupDaemonMetadata, createIntegrationTestContext, runCliJson } from './test-helpers.ts';

const session = ['--session', 'android-test'];
const stateDir = path.resolve('test/.state/android-integration');
const session = ['--session', 'android-test', '--state-dir', stateDir];
const settingsSectionLabels = [
'Apps',
'Apps & notifications',
Expand All @@ -22,8 +24,13 @@ const settingsCrashDialogSelector = settingsCrashDialogLabels
.map((label) => (label.includes(' ') ? `label="${label}"` : `label=${label}`))
.join(' || ');

test.before(() => {
cleanupDaemonMetadata(stateDir);
});

test.after(() => {
runCliJson(['close', '--platform', 'android', ...session]);
cleanupDaemonMetadata(stateDir);
});

test('android settings commands', () => {
Expand Down
25 changes: 21 additions & 4 deletions test/integration/ios.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import test from 'node:test';
import { existsSync } from 'node:fs';
import path from 'node:path';
import { createIntegrationTestContext, runCliJson } from './test-helpers.ts';
import { cleanupDaemonMetadata, createIntegrationTestContext, runCliJson } from './test-helpers.ts';

const session = ['--session', 'ios-test'];
const stateDir = path.resolve('test/.state/ios-integration');
const daemonState = ['--state-dir', stateDir];
const session = ['--session', 'ios-test', ...daemonState];
const iosTarget = ['--platform', 'ios'];
const iosPhysicalUdid = process.env.IOS_UDID?.trim();
let didRunIosPhysicalSession = false;

test.before(() => {
cleanupDaemonMetadata(stateDir);
});

test.after(() => {
runCliJson(['close', ...iosTarget, '--json', ...session]);
if (iosPhysicalUdid && didRunIosPhysicalSession) {
runCliJson(['close', '--platform', 'ios', '--udid', iosPhysicalUdid, '--json', '--session', 'ios-device-test']);
runCliJson([
'close',
'--platform',
'ios',
'--udid',
iosPhysicalUdid,
'--json',
'--session',
'ios-device-test',
...daemonState,
]);
}
cleanupDaemonMetadata(stateDir);
});

test('ios settings commands', { skip: shouldSkipIos() }, async () => {
Expand Down Expand Up @@ -99,7 +116,7 @@ test('ios physical device core lifecycle', { skip: shouldSkipIosPhysicalDevice()
platform: 'ios',
testName: 'ios physical device core lifecycle',
});
const deviceSession = ['--session', 'ios-device-test'];
const deviceSession = ['--session', 'ios-device-test', ...daemonState];
const target = ['--platform', 'ios', '--udid', iosPhysicalUdid as string];
didRunIosPhysicalSession = true;

Expand Down
17 changes: 16 additions & 1 deletion test/integration/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'node:assert/strict';
import { mkdirSync, writeFileSync } from 'node:fs';
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { resolveDaemonPaths } from '../../src/daemon/config.ts';
import { runCmdSync } from '../../src/utils/exec.ts';

export type CliJsonResult = {
Expand Down Expand Up @@ -73,6 +74,20 @@ export function formatResultDebug(step: string, args: string[], result: CliJsonR
].join('\n');
}

export function cleanupDefaultDaemonMetadata(): void {
cleanupDaemonMetadata(process.env.AGENT_DEVICE_STATE_DIR);
}

export function cleanupDaemonMetadata(stateDir?: string): void {
const { infoPath, lockPath } = resolveDaemonPaths(stateDir);
if (existsSync(infoPath)) {
rmSync(infoPath, { force: true });
}
if (existsSync(lockPath)) {
rmSync(lockPath, { force: true });
}
}

export function createIntegrationTestContext(options: IntegrationTestContextOptions) {
const { platform, testName } = options;
const stepHistory: StepRecord[] = [];
Expand Down
Loading