Skip to content

Commit ce84f7f

Browse files
committed
fix: address Fallow findings on the new SDK examples
Fallow flagged the four examples/sdk/*.ts files as unused files (not reachable from any entry point) and three functions as high complexity. - Register the examples as manual entry points in .fallowrc.json, matching how other standalone scripts (scripts/patch-xcuitest-runner-icon.ts, scripts/runner-request-count/run.ts) are already declared. - Reduce complexity in client-session.ts and contracts-result.ts by extracting device-resolution/error-reporting and rect-assertion helpers out of main(). - Reduce complexity in the drift guard's parseSubpathManifest by splitting bullet-matching and backtick-name extraction into their own functions. Verified: pnpm check:fallow --base <PR base sha> now reports no issues, and pnpm check:tooling / pnpm test:unit stay green. Refs #1463
1 parent da89b14 commit ce84f7f

4 files changed

Lines changed: 56 additions & 34 deletions

File tree

‎.fallowrc.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
"scripts/patch-xcuitest-runner-icon.ts",
2020
"scripts/runner-request-count/run.ts",
2121
"src/utils/update-check-entry.ts",
22+
"examples/sdk/client-session.ts",
23+
"examples/sdk/metro-runtime.ts",
24+
"examples/sdk/contracts-result.ts",
25+
"examples/sdk/batch-orchestration.ts",
2226
"test/scripts/metro-prepare-packaged-smoke.mjs",
2327
"test/integration/*.test.ts",
2428
"website/docs/404.mdx",

‎examples/sdk/client-session.ts‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ import {
1919
normalizeAgentDeviceError,
2020
} from 'agent-device';
2121

22+
async function resolveSnapshotCapableIosDevice(client: ReturnType<typeof createAgentDeviceClient>) {
23+
const devices = await client.devices.list({ platform: 'ios' });
24+
const device = devices[0];
25+
if (!device) {
26+
throw new AppError('DEVICE_NOT_FOUND', 'No iOS device available');
27+
}
28+
29+
const capabilities = await client.devices.capabilities({ platform: 'ios' });
30+
if (!capabilities.availableCommands.includes('snapshot')) {
31+
throw new AppError('UNSUPPORTED_OPERATION', 'Selected target does not support snapshots');
32+
}
33+
34+
return device;
35+
}
36+
37+
function reportAgentDeviceError(error: unknown): void {
38+
const normalized = normalizeAgentDeviceError(error);
39+
console.error(`agent-device error [${normalized.code}]: ${normalized.message}`);
40+
if (normalized.hint) {
41+
console.error(`hint: ${normalized.hint}`);
42+
}
43+
process.exitCode = 1;
44+
}
45+
2246
async function main(): Promise<void> {
2347
const client = createAgentDeviceClient({
2448
session: 'sdk-example',
@@ -27,16 +51,7 @@ async function main(): Promise<void> {
2751
});
2852

2953
try {
30-
const devices = await client.devices.list({ platform: 'ios' });
31-
const device = devices[0];
32-
if (!device) {
33-
throw new AppError('DEVICE_NOT_FOUND', 'No iOS device available');
34-
}
35-
36-
const capabilities = await client.devices.capabilities({ platform: 'ios' });
37-
if (!capabilities.availableCommands.includes('snapshot')) {
38-
throw new AppError('UNSUPPORTED_OPERATION', 'Selected target does not support snapshots');
39-
}
54+
const device = await resolveSnapshotCapableIosDevice(client);
4055

4156
await client.apps.open({
4257
app: 'com.apple.Preferences',
@@ -50,16 +65,8 @@ async function main(): Promise<void> {
5065
await client.interactions.press({ ref: target.ref });
5166
}
5267
} catch (error) {
53-
if (isAgentDeviceError(error)) {
54-
const normalized = normalizeAgentDeviceError(error);
55-
console.error(`agent-device error [${normalized.code}]: ${normalized.message}`);
56-
if (normalized.hint) {
57-
console.error(`hint: ${normalized.hint}`);
58-
}
59-
process.exitCode = 1;
60-
return;
61-
}
62-
throw error;
68+
if (!isAgentDeviceError(error)) throw error;
69+
reportAgentDeviceError(error);
6370
} finally {
6471
await client.sessions.close();
6572
}

‎examples/sdk/contracts-result.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@
1414
import { createAgentDeviceClient } from 'agent-device';
1515
import { centerOfRect } from 'agent-device/contracts';
1616

17+
function assertHasRect<T extends { rect?: unknown }>(
18+
node: T | undefined,
19+
): asserts node is T & { rect: NonNullable<T['rect']> } {
20+
if (!node?.rect) {
21+
throw new Error('No visible node with a rect in the snapshot');
22+
}
23+
}
24+
1725
async function main(): Promise<void> {
1826
const client = createAgentDeviceClient({ session: 'sdk-example' });
1927

2028
try {
2129
const snapshot = await client.capture.snapshot({ interactiveOnly: true });
2230
const node = snapshot.nodes.find((candidate) => candidate.rect !== undefined);
23-
if (!node?.rect) {
24-
throw new Error('No visible node with a rect in the snapshot');
25-
}
31+
assertHasRect(node);
2632

2733
const center = centerOfRect(node.rect);
2834
console.log(

‎src/__tests__/client-api-examples-drift.test.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,30 @@ const REQUIRED_EXAMPLE_SYMBOLS: readonly { subpath: string; symbol: string }[] =
3838
// bullet starts a subpath section; backtick-quoted identifiers on its nested
3939
// bullet lines (stripping a trailing `(...)` call signature) are that
4040
// subpath's documented symbols, until the next top-level bullet.
41+
function matchTopLevelSubpathBullet(line: string): string | null {
42+
return /^- `(agent-device[a-z0-9/-]*)`$/.exec(line)?.[1] ?? null;
43+
}
44+
45+
function extractBacktickedNames(line: string): string[] {
46+
return [...line.matchAll(/`([A-Za-z0-9_]+)(?:\([^)]*\))?`/g)]
47+
.map((match) => match[1])
48+
.filter((name): name is string => name !== undefined);
49+
}
50+
4151
function parseSubpathManifest(markdown: string): SubpathManifest {
4252
const manifest = new Map<string, Set<string>>();
4353
let currentSubpath: string | null = null;
4454
for (const line of markdown.split('\n')) {
45-
const topLevelMatch = /^- `(agent-device[a-z0-9/-]*)`$/.exec(line);
46-
if (topLevelMatch) {
47-
currentSubpath = topLevelMatch[1] ?? null;
48-
if (currentSubpath && !manifest.has(currentSubpath)) {
49-
manifest.set(currentSubpath, new Set());
50-
}
55+
const subpath = matchTopLevelSubpathBullet(line);
56+
if (subpath) {
57+
currentSubpath = subpath;
58+
if (!manifest.has(subpath)) manifest.set(subpath, new Set());
5159
continue;
5260
}
53-
if (currentSubpath === null || !/^\s+- /.test(line)) continue;
61+
if (!currentSubpath || !/^\s+- /.test(line)) continue;
5462
const symbols = manifest.get(currentSubpath);
5563
if (!symbols) continue;
56-
for (const match of line.matchAll(/`([A-Za-z0-9_]+)(?:\([^)]*\))?`/g)) {
57-
const name = match[1];
58-
if (name) symbols.add(name);
59-
}
64+
for (const name of extractBacktickedNames(line)) symbols.add(name);
6065
}
6166
return manifest;
6267
}

0 commit comments

Comments
 (0)