Skip to content

Commit 9918f7e

Browse files
committed
test(replay): cover publication through ensured daemon clients
1 parent 10b1746 commit 9918f7e

2 files changed

Lines changed: 53 additions & 30 deletions

File tree

‎test/integration/provider-scenarios/cli-record-publication.test.ts‎

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3-
import { expect, test, onTestFinished } from 'vitest';
4-
import type { AgentDeviceDaemonTransport } from '@agent-device/contracts/client';
3+
import { expect, test, onTestFinished, vi } from 'vitest';
4+
import * as codeSignature from '@agent-device/host-kit/code-signature';
5+
import { resolveLocalDaemonCodeIdentity } from '../../../src/daemon-client/daemon-launch-spec.ts';
56
import { parseReplayScriptDetailed } from '@agent-device/ad-script';
67
import { createProviderScenarioHarness } from './harness.ts';
78
import { PROVIDER_SCENARIO_ANDROID } from './fixtures.ts';
@@ -18,6 +19,13 @@ import { createCommandToolExecutor } from '../../../src/mcp/command-tools.ts';
1819
import { installIsolatedCliTestEnv } from '../../../src/__tests__/cli-test-env.ts';
1920

2021
async function repairPublication(root: string) {
22+
// The runtime runs inside Vitest, so publish the daemon entry's identity instead of Vitest's.
23+
const identity = await resolveLocalDaemonCodeIdentity();
24+
if (identity.origin !== 'checkout') throw new Error('expected checkout identity');
25+
const signature = vi
26+
.spyOn(codeSignature, 'resolveDaemonCodeSignature')
27+
.mockReturnValue(identity.codeSignature);
28+
onTestFinished(() => signature.mockRestore());
2129
const shellState = createAndroidProviderShellState();
2230
const daemon = await createProviderScenarioHarness({
2331
androidAdbProvider: () => ({
@@ -53,17 +61,8 @@ async function repairPublication(root: string) {
5361
}) => error.details.divergence.resume,
5462
);
5563
expect(divergence.repairSessionHeld).toBe(true);
56-
const transport: AgentDeviceDaemonTransport = async (req) => {
57-
const result = await daemon.callCommand(req.command, req.positionals, {
58-
...req.flags,
59-
...selection,
60-
});
61-
return result.json.error
62-
? { ok: false, error: result.json.error.data }
63-
: { ok: true, data: result.json.result.data };
64-
};
6564
return {
66-
transport,
65+
config: { stateDir: daemon.stateDir, daemonTransport: 'http' as const, ...selection },
6766
close: () => daemon.close(),
6867
commands: async () => {
6968
await client.replay.run({
@@ -88,17 +87,33 @@ test.each([false, true])(
8887
'CLI project record default retains a repair snapshot only with explicit opt-in=%s',
8988
async (explicit) => {
9089
const { root, home, project } = makeTempWorkspace();
91-
fs.writeFileSync(path.join(project, 'agent-device.json'), JSON.stringify({ record: true }));
9290
const publication = await repairPublication(root);
91+
fs.writeFileSync(path.join(project, 'agent-device.json'), JSON.stringify({ record: true }));
9392
onTestFinished(() => publication.close());
94-
const result = await runCliCapture(['snapshot', '--json', ...(explicit ? ['--record'] : [])], {
95-
cwd: project,
96-
env: { HOME: home, USERPROFILE: home },
97-
sendToDaemon: publication.transport,
98-
});
99-
expect(result.calls).toHaveLength(1);
100-
expect(result.calls[0]?.flags?.record).toBe(true);
101-
expect(await publication.commands()).toEqual(
93+
const result = await runCliCapture(
94+
[
95+
'snapshot',
96+
'--json',
97+
'--session',
98+
'default',
99+
'--state-dir',
100+
publication.config.stateDir,
101+
'--daemon-transport',
102+
'http',
103+
'--platform',
104+
'android',
105+
'--serial',
106+
publication.config.serial,
107+
...(explicit ? ['--record'] : []),
108+
],
109+
{
110+
cwd: project,
111+
env: { HOME: home, USERPROFILE: home },
112+
useRealDaemonClient: true,
113+
},
114+
);
115+
expect(result.code, result.stderr || result.stdout).not.toBe(1);
116+
expect(await publication.commands(), JSON.stringify(result)).toEqual(
102117
explicit ? ['snapshot', 'snapshot', 'is', 'close'] : ['snapshot', 'is', 'close'],
103118
);
104119
},
@@ -108,20 +123,26 @@ test.each([false, true])(
108123
'MCP project record default retains a repair snapshot only with explicit opt-in=%s',
109124
async (explicit) => {
110125
const { root, home, project } = makeTempWorkspace();
111-
fs.writeFileSync(path.join(project, 'agent-device.json'), JSON.stringify({ record: true }));
112126
const publication = await repairPublication(root);
127+
fs.writeFileSync(path.join(project, 'agent-device.json'), JSON.stringify({ record: true }));
113128
onTestFinished(() => publication.close());
114-
const restoreEnv = installIsolatedCliTestEnv({ HOME: home, USERPROFILE: home });
129+
const restoreEnv = installIsolatedCliTestEnv({
130+
HOME: home,
131+
USERPROFILE: home,
132+
AGENT_DEVICE_STATE_DIR: publication.config.stateDir,
133+
AGENT_DEVICE_DAEMON_TRANSPORT: 'http',
134+
});
115135
const previousCwd = process.cwd();
116136
process.chdir(project);
117137
try {
118-
const executor = createCommandToolExecutor({
119-
createClient: (config, ingress) =>
120-
createAgentDeviceClient(config, { ...ingress, transport: publication.transport }),
138+
const executor = createCommandToolExecutor();
139+
const result = await executor.execute('snapshot', {
140+
platform: 'android',
141+
serial: publication.config.serial,
142+
...(explicit ? { record: true } : {}),
121143
});
122-
const result = await executor.execute('snapshot', explicit ? { record: true } : {});
123-
expect(result.isError).toBe(false);
124-
expect(await publication.commands()).toEqual(
144+
expect(result.isError, JSON.stringify(result)).toBe(false);
145+
expect(await publication.commands(), JSON.stringify(result)).toEqual(
125146
explicit ? ['snapshot', 'snapshot', 'is', 'close'] : ['snapshot', 'is', 'close'],
126147
);
127148
} finally {
@@ -135,7 +156,7 @@ test('SDK per-call record explicitly retains a corrective snapshot', async () =>
135156
const { root } = makeTempWorkspace();
136157
const publication = await repairPublication(root);
137158
onTestFinished(() => publication.close());
138-
const client = createAgentDeviceClient({}, { transport: publication.transport });
159+
const client = createAgentDeviceClient(publication.config);
139160
await client.capture.snapshot({ record: true });
140161
expect(await publication.commands()).toEqual(['snapshot', 'snapshot', 'is', 'close']);
141162
});

‎test/integration/provider-scenarios/harness.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export type ProviderScenarioHarness = {
7070
) => Promise<ProviderScenarioRpcResult>;
7171
client: () => AgentDeviceClient;
7272
hasSession: (session?: string) => Promise<boolean>;
73+
stateDir: string;
7374
baseUrl: string;
7475
httpPort: number;
7576
token: string;
@@ -268,6 +269,7 @@ export async function createProviderScenarioHarness(
268269
client: () => client,
269270
hasSession: async (name = 'default') =>
270271
(await client.sessions.list()).some((session) => session.name === name),
272+
stateDir,
271273
baseUrl,
272274
httpPort: runtime.httpPort,
273275
token: runtime.token,

0 commit comments

Comments
 (0)