Skip to content

Commit b826de0

Browse files
committed
fix(ci): cover, exempt, and pack the new ai-sdk subpath
Fixes the remaining CI failures on the ai-sdk subpath commit: - Coverage: src/ai-sdk/index.ts had no dedicated unit test (only manual/ integration verification), so changed-line coverage sat at 6.9% against the 70% gate. Adds src/ai-sdk/__tests__/index.test.ts (core vs 'all' tool filtering, session/platform pinning and schema hiding, error normalization, toolApproval passthrough) with createCommandToolExecutor and createAgentDeviceClient mocked the same way command-tools.test.ts does, plus a dedicated missing-peer-dependency.test.ts that mocks `ai` itself to throw, isolated to its own file so it doesn't affect the other tests' use of the real, installed `ai` package. Changed-line coverage is now 29/29 (100%). - Fallow Code Quality: src/ai-sdk/index.ts and examples/sdk/ai-sdk-tools.ts are entry points with no in-repo importer (reached only via package.json exports / run directly), and the new subpath's exports are unused internally by design - both need the same treatment src/sdk/*.ts and its examples already have in .fallowrc.json. - Integration Tests: test/integration/installed-package-metro.test.ts and src/__tests__/package-exports.test.ts each hand-list every published subpath and smoke-check it from a real packed install; added ./ai-sdk to both so the new subpath is actually exercised, not just silently passing.
1 parent 52e21de commit b826de0

5 files changed

Lines changed: 166 additions & 1 deletion

File tree

‎.fallowrc.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"src/sdk/contracts.ts",
1414
"src/sdk/selectors.ts",
1515
"src/sdk/finders.ts",
16+
"src/ai-sdk/index.ts",
1617
"src/bin.ts",
1718
"src/client/companion-tunnel.ts",
1819
"src/daemon.ts",
@@ -32,6 +33,7 @@
3233
"examples/sdk/metro-runtime.ts",
3334
"examples/sdk/contracts-result.ts",
3435
"examples/sdk/batch-orchestration.ts",
36+
"examples/sdk/ai-sdk-tools.ts",
3537
"test/scripts/metro-prepare-packaged-smoke.mjs",
3638
"test/integration/*.test.ts",
3739
"website/docs/404.mdx",
@@ -96,7 +98,7 @@
9698
},
9799
{
98100
"comment": "Published package surface (see package.json#exports); consumed by SDK users, not internally.",
99-
"file": "src/sdk/*.ts",
101+
"file": "{src/sdk/*.ts,src/ai-sdk/index.ts}",
100102
"exports": ["*"]
101103
},
102104
{

‎src/__tests__/package-exports.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const supportedSubpaths = [
3333
'./contracts',
3434
'./selectors',
3535
'./finders',
36+
'./ai-sdk',
3637
];
3738
const typeOnlySubpaths = new Set(['./remote-config']);
3839

‎src/ai-sdk/__tests__/index.test.ts‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import assert from 'node:assert/strict';
2+
import { afterEach, test, vi } from 'vitest';
3+
import { AppError } from '@agent-device/kernel/errors';
4+
import type { AgentDeviceClient } from '../../client/client-types.ts';
5+
import type { ToolResult } from '../../mcp/command-tools.ts';
6+
7+
const createClientCalls: unknown[] = [];
8+
const executorCalls: Array<{ name: string; input: Record<string, unknown> }> = [];
9+
10+
const defaultExecuteResult: ToolResult = {
11+
isError: false,
12+
structuredContent: { ok: true },
13+
content: [{ type: 'text', text: 'ok' }],
14+
};
15+
let executeResult: (
16+
name: string,
17+
input: Record<string, unknown>,
18+
) => Promise<ToolResult> = async () => defaultExecuteResult;
19+
20+
vi.mock('../../agent-device-client.ts', () => ({
21+
createAgentDeviceClient: (config: unknown) => {
22+
createClientCalls.push(config);
23+
return { marker: 'stub-client' } as unknown as AgentDeviceClient;
24+
},
25+
}));
26+
27+
vi.mock('../../mcp/command-tools.ts', async (importOriginal) => {
28+
const actual = await importOriginal<typeof import('../../mcp/command-tools.ts')>();
29+
return {
30+
...actual,
31+
createCommandToolExecutor: () => ({
32+
execute: async (name: string, input: Record<string, unknown>) => {
33+
executorCalls.push({ name, input });
34+
return executeResult(name, input);
35+
},
36+
}),
37+
};
38+
});
39+
40+
const { createAgentDeviceTools } = await import('../index.ts');
41+
42+
afterEach(() => {
43+
createClientCalls.length = 0;
44+
executorCalls.length = 0;
45+
executeResult = async () => defaultExecuteResult;
46+
});
47+
48+
// jsonSchema() (from the real, installed `ai` package) stores the raw schema
49+
// under `.jsonSchema`, so this reads exactly what the model would see.
50+
function rawSchemaOf(
51+
tools: Awaited<ReturnType<typeof createAgentDeviceTools>>['tools'],
52+
name: string,
53+
) {
54+
return (
55+
tools[name] as unknown as {
56+
inputSchema: { jsonSchema: { properties: Record<string, unknown> } };
57+
}
58+
).inputSchema.jsonSchema;
59+
}
60+
61+
function executeOf(
62+
tools: Awaited<ReturnType<typeof createAgentDeviceTools>>['tools'],
63+
name: string,
64+
) {
65+
return tools[name]!.execute as (input: Record<string, unknown>) => Promise<unknown>;
66+
}
67+
68+
test('defaults to the core tool set and pins the session for every call', async () => {
69+
const { tools, client, toolApproval } = await createAgentDeviceTools({ session: 'my-session' });
70+
71+
assert.deepEqual(createClientCalls, [{ session: 'my-session' }]);
72+
assert.equal((client as unknown as { marker: string }).marker, 'stub-client');
73+
assert.equal(toolApproval, undefined);
74+
75+
assert.ok('snapshot' in tools, 'snapshot is core-tier');
76+
assert.equal(
77+
'devices' in tools,
78+
false,
79+
'devices is extended-tier, excluded from the core default',
80+
);
81+
82+
const schema = rawSchemaOf(tools, 'snapshot');
83+
assert.equal('session' in schema.properties, false, 'session is always hidden from the model');
84+
assert.equal('mcpOutputFormat' in schema.properties, false, 'mcpOutputFormat is always hidden');
85+
assert.ok('platform' in schema.properties, 'platform stays visible when not pinned');
86+
87+
await executeOf(tools, 'snapshot')({ interactiveOnly: true });
88+
assert.deepEqual(executorCalls, [
89+
{ name: 'snapshot', input: { interactiveOnly: true, session: 'my-session' } },
90+
]);
91+
});
92+
93+
test("set: 'all' includes extended-tier commands too", async () => {
94+
const { tools } = await createAgentDeviceTools({ session: 's', set: 'all' });
95+
assert.ok('devices' in tools, 'set: all reaches every MCP-exposed command, not just core');
96+
assert.ok(Object.keys(tools).length > 20);
97+
});
98+
99+
test('platform is pinned per call and removed from the input schema', async () => {
100+
const { tools } = await createAgentDeviceTools({ session: 's', platform: 'ios' });
101+
102+
const schema = rawSchemaOf(tools, 'open');
103+
assert.equal('platform' in schema.properties, false);
104+
105+
await executeOf(tools, 'open')({ app: 'com.example.app' });
106+
assert.deepEqual(executorCalls, [
107+
{ name: 'open', input: { app: 'com.example.app', session: 's', platform: 'ios' } },
108+
]);
109+
});
110+
111+
test('a command error is re-thrown as the normalized AppError, not swallowed', async () => {
112+
executeResult = async () => ({
113+
isError: true,
114+
structuredContent: {
115+
code: 'DEVICE_NOT_FOUND',
116+
message: 'No device matched the selector',
117+
details: { hint: 'try a broader selector' },
118+
},
119+
content: [{ type: 'text', text: 'Error (DEVICE_NOT_FOUND): No device matched the selector' }],
120+
});
121+
122+
const { tools } = await createAgentDeviceTools({ session: 's' });
123+
124+
await assert.rejects(executeOf(tools, 'snapshot')({}), (error: unknown) => {
125+
assert.ok(error instanceof AppError);
126+
assert.equal(error.code, 'DEVICE_NOT_FOUND');
127+
assert.match(error.message, /No device matched the selector/);
128+
return true;
129+
});
130+
});
131+
132+
test('approval is returned as toolApproval, ready for ToolLoopAgent', async () => {
133+
const { toolApproval } = await createAgentDeviceTools({
134+
session: 's',
135+
approval: { close: 'user-approval' },
136+
});
137+
assert.deepEqual(toolApproval, { close: 'user-approval' });
138+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import assert from 'node:assert/strict';
2+
import { test, vi } from 'vitest';
3+
import { AppError } from '@agent-device/kernel/errors';
4+
5+
// `ai` is an optional peer dependency; this simulates the real "not installed"
6+
// resolution failure a consumer would hit, isolated to its own file so the
7+
// other ai-sdk tests can still exercise the real, installed `ai` package.
8+
vi.mock('ai', () => {
9+
throw new Error("Cannot find package 'ai' imported from agent-device/ai-sdk");
10+
});
11+
12+
const { createAgentDeviceTools } = await import('../index.ts');
13+
14+
test('createAgentDeviceTools() reports a clear error when the optional peer dependency is missing', async () => {
15+
await assert.rejects(createAgentDeviceTools({ session: 's' }), (error: unknown) => {
16+
assert.ok(error instanceof AppError);
17+
assert.equal(error.code, 'MISSING_PEER_DEPENDENCY');
18+
assert.match(error.message, /requires the optional peer dependency 'ai'/);
19+
assert.match(error.message, /pnpm add ai/);
20+
return true;
21+
});
22+
});

‎test/integration/installed-package-metro.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ test('installed package exposes Node APIs and packaged companion tunnel entrypoi
288288
).sort();
289289
const subpathSmoke = {
290290
'.': (mod) => mod.centerOfRect({ x: 0, y: 0, width: 10, height: 4 }),
291+
'./ai-sdk': (mod) => typeof mod.createAgentDeviceTools,
291292
'./android-adb': (mod) => {
292293
const manager = mod.createAndroidPortReverseManager(async () => ({
293294
stdout: '',
@@ -387,6 +388,7 @@ test('installed package exposes Node APIs and packaged companion tunnel entrypoi
387388
assert.deepEqual(imports.smokedSubpaths, imports.exportSubpaths);
388389
assert.deepEqual(imports.subpathSmokeResults, {
389390
'.': { x: 5, y: 2 },
391+
'./ai-sdk': 'function',
390392
'./android-adb': 'function',
391393
'./artifacts': 'undefined',
392394
'./batch': 'INVALID_ARGS',

0 commit comments

Comments
 (0)