Skip to content

Commit dcece50

Browse files
committed
refactor(host): read Linux, Vega, web and HarmonyOS configuration from an explicit host (#3794)
1 parent f6fc4f9 commit dcece50

22 files changed

Lines changed: 285 additions & 72 deletions

‎packages/daemon/src/__tests__/support/snapshot-runtime-fixture.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ export function snapshotRuntimeFixture(requestId?: string): Readonly<{
113113
// runner underneath is the suite's own mock.
114114
const runnerOptions = { signal: requestSignal };
115115
const appleInteractor = isApplePlatform(device.platform)
116-
? await applePlugin.createInteractor(device, runnerOptions)
116+
? await applePlugin.createInteractor(device, runnerOptions, {
117+
env: process.env,
118+
cwd: process.cwd(),
119+
})
117120
: undefined;
118121
const alertOptions = (input: AlertRuntimeInput) => ({
119122
...(input.timeoutMs === undefined ? {} : { timeoutMs: input.timeoutMs }),

‎packages/platform-harmonyos/src/__tests__/hdc.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ test('ensureHarmonyToolchainPathConfigured defaults to the host environment', as
144144
vi.stubEnv('PATH', '/usr/bin');
145145
vi.stubEnv('HDC_SDK_PATH', toolchains);
146146

147-
await ensureHarmonyToolchainPathConfigured();
147+
await ensureHarmonyToolchainPathConfigured(process.env);
148148

149149
assert.equal(process.env.PATH, [toolchains, '/usr/bin'].join(path.delimiter));
150150
});

‎packages/platform-harmonyos/src/hdc.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
runCmd,
1111
type ExecOptions,
1212
type ExecResult,
13-
commandHostEnvironment,
1413
} from '@agent-device/host-kit/command';
1514

1615
export type HarmonyHdcOptions = Pick<
@@ -49,9 +48,7 @@ export async function runHarmonyShell(
4948
* DevEco's command-line tools do not amend PATH for non-interactive processes.
5049
* Honor the documented roots so the daemon sees the same HDC binary as a shell.
5150
*/
52-
export async function ensureHarmonyToolchainPathConfigured(
53-
env: NodeJS.ProcessEnv = commandHostEnvironment().env,
54-
): Promise<void> {
51+
export async function ensureHarmonyToolchainPathConfigured(env: NodeJS.ProcessEnv): Promise<void> {
5552
const toolchainRoots = [
5653
env.HDC_SDK_PATH,
5754
env.DEVECO_SDK_HOME

‎packages/platform-web/src/__tests__/agent-browser-provider-boundary-faults.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ for (const [commandClass, runOperation] of Object.entries(COMMAND_CLASS_OPERATIO
2929
const stateDir = mkdtempForTestSync(`agent-device-exec-boundary-${commandClass}-`);
3030
try {
3131
installFakeManagedAgentBrowser(stateDir);
32-
const provider = createAgentBrowserWebProvider({ stateDir, session: 'boundary' });
32+
const provider = createAgentBrowserWebProvider({
33+
host: { env: process.env, cwd: process.cwd() },
34+
stateDir,
35+
session: 'boundary',
36+
});
3337
const failure = new AppError('COMMAND_FAILED', 'injected command failure', {
3438
hint: 'retry the boundary test',
3539
diagnosticId: `diag-${commandClass}`,

‎packages/platform-web/src/__tests__/agent-browser-provider-dispatch-disclosure.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ async function scrollRefusedOnThirdStep(): Promise<void> {
134134
/** The managed backend is not installed, so the CLI is never spawned. */
135135
async function openWithMissingBackend(): Promise<void> {
136136
const provider = createAgentBrowserWebProvider({
137+
host: { env: process.env, cwd: process.cwd() },
137138
stateDir: mkdtempForTestSync('agent-device-web-disclosure-missing-'),
138139
});
139140
await provider.open('https://example.test');

‎packages/platform-web/src/__tests__/agent-browser-provider-record-retention.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ async function withFleetRecordStore(
151151
store.replace({ kind: 'daemon' }, [seed]);
152152
await runScenario({
153153
provider: createAgentBrowserWebProvider({
154+
host: { env: process.env, cwd: process.cwd() },
154155
session: 'web-session',
155156
stateDir,
156157
openWebSessionNames: () => ['web-session'],

‎packages/platform-web/src/__tests__/doctor.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ test('web doctor lifecycle check reports live managed Chrome process count', asy
4242
],
4343
} satisfies AgentBrowserProcessSummary);
4444

45-
const check = await webBrowserLifecycleCheck(stateDir);
45+
const check = await webBrowserLifecycleCheck(stateDir, {
46+
env: process.env,
47+
cwd: process.cwd(),
48+
});
4649

4750
assert.equal(check.id, 'web-agent-browser-processes');
4851
assert.equal(check.status, 'info');
@@ -57,7 +60,10 @@ test('web doctor lifecycle check reports live managed Chrome process count', asy
5760
test('web doctor lifecycle check stays informational when managed backend is missing', async () => {
5861
const stateDir = mkdtempForTestSync('agent-device-web-doctor-missing-');
5962
try {
60-
const check = await webBrowserLifecycleCheck(stateDir);
63+
const check = await webBrowserLifecycleCheck(stateDir, {
64+
env: process.env,
65+
cwd: process.cwd(),
66+
});
6167

6268
assert.equal(check.status, 'info');
6369
assert.match(check.summary ?? '', /not installed/);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { test } from 'vitest';
2+
import assert from 'node:assert/strict';
3+
import { AppError } from '@agent-device/kernel/errors';
4+
import { resolveWebProvider, withWebProvider, type WebProvider } from '../provider.ts';
5+
6+
// The scope's un-composed fallback is the single policy for a request that reaches a web device
7+
// without a composed provider: it refuses with `web-provider-missing` instead of borrowing a host
8+
// it was never given (#3794).
9+
test('an un-scoped web provider resolves to a refusal naming the missing composition', async () => {
10+
const provider = resolveWebProvider();
11+
const operations: readonly [string, () => Promise<unknown>][] = [
12+
['open', () => provider.open('https://example.test')],
13+
['close', () => provider.close()],
14+
['snapshot', () => provider.snapshot()],
15+
['screenshot', () => provider.screenshot('/tmp/out.png')],
16+
['setViewport', () => provider.setViewport(800, 600)],
17+
['click', () => provider.click(1, 2)],
18+
['fill', () => provider.fill(1, 2, 'text')],
19+
['typeText', () => provider.typeText('text')],
20+
['scroll', () => provider.scroll('down')],
21+
];
22+
for (const [operation, call] of operations) {
23+
const error = await call().then(
24+
() => undefined,
25+
(error: unknown) => error,
26+
);
27+
assert.ok(error instanceof AppError, `${operation} refuses with an AppError`);
28+
assert.equal(error.details?.reason, 'web-provider-missing', operation);
29+
}
30+
});
31+
32+
test('a composed request provider answers instead of the fallback refusal', async () => {
33+
const opened: string[] = [];
34+
const provider: WebProvider = {
35+
open: async (target) => {
36+
opened.push(target);
37+
},
38+
close: async () => {},
39+
snapshot: async () => ({ nodes: [] }),
40+
screenshot: async () => {},
41+
setViewport: async () => {},
42+
click: async () => {},
43+
fill: async () => {},
44+
typeText: async () => {},
45+
scroll: async () => {},
46+
};
47+
await withWebProvider(provider, async () => {
48+
await resolveWebProvider().open('https://example.test');
49+
});
50+
assert.deepEqual(opened, ['https://example.test']);
51+
});

‎packages/platform-web/src/__tests__/test-utils.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ export async function withManagedAgentBrowserProvider(
6868
): Promise<void> {
6969
const stateDir = mkdtempForTestSync('agent device web provider ');
7070
installFakeManagedAgentBrowser(stateDir);
71-
await testFn(createAgentBrowserWebProvider({ ...options, stateDir }));
71+
await testFn(
72+
createAgentBrowserWebProvider({
73+
host: { env: process.env, cwd: process.cwd() },
74+
...options,
75+
stateDir,
76+
}),
77+
);
7278
}
7379

7480
/** The answer shape the managed backend answers every command with. */

‎packages/platform-web/src/agent-browser-install.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'node:path';
22
import { AppError } from '@agent-device/kernel/errors';
3-
import { runCmd, commandHostEnvironment } from '@agent-device/host-kit/command';
3+
import { runCmd } from '@agent-device/host-kit/command';
4+
import type { EnvMap } from '@agent-device/kernel/source-value';
45
import {
56
ensureHostDirectorySync,
67
hostFileStatSync,
@@ -20,11 +21,12 @@ export async function installManagedAgentBrowserPackage(params: {
2021
packageRoot: string;
2122
packageSpec: string;
2223
timeoutMs: number;
24+
env: Readonly<EnvMap>;
2325
}): Promise<void> {
2426
ensureHostDirectorySync(params.packageRoot);
2527
// `--no-global` keeps an ambient `npm_config_global` from redirecting the
2628
// install out of the managed prefix, where the backend entry would be missed.
27-
const npm = npmCommand([
29+
const npm = npmCommand(params.env, [
2830
'install',
2931
'--prefix',
3032
params.packageRoot,
@@ -35,7 +37,7 @@ export async function installManagedAgentBrowserPackage(params: {
3537
params.packageSpec,
3638
]);
3739
await runCmd(npm.command, npm.args, {
38-
env: commandHostEnvironment().env,
40+
env: params.env,
3941
timeoutMs: params.timeoutMs,
4042
});
4143
}
@@ -66,9 +68,9 @@ export function writeManagedAgentBrowserManifest(params: {
6668
* without a shell since the CVE-2024-27980 fix, so its JS entry runs under the
6769
* current Node runtime instead (#2022).
6870
*/
69-
function npmCommand(args: string[]): { command: string; args: string[] } {
71+
function npmCommand(env: Readonly<EnvMap>, args: string[]): { command: string; args: string[] } {
7072
if (hostPlatform() !== 'win32') return { command: 'npm', args };
71-
const npmCliScript = resolveWindowsNpmCliScript(commandHostEnvironment().env);
73+
const npmCliScript = resolveWindowsNpmCliScript(env);
7274
if (!npmCliScript) {
7375
throw new AppError('TOOL_MISSING', 'npm not found in PATH', {
7476
nodeExecPath: hostNodeExecutablePath(),

0 commit comments

Comments
 (0)