-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathintegration-progress-model.ts
More file actions
571 lines (533 loc) · 20.6 KB
/
Copy pathintegration-progress-model.ts
File metadata and controls
571 lines (533 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import fs from 'node:fs';
import path from 'node:path';
import { PUBLIC_COMMANDS } from '../src/command-catalog.ts';
import { listCommandMetadata } from '../src/commands/command-metadata.ts';
import { getFlagDefinitions } from '../src/utils/cli-flags.ts';
const EMPTY_COVERAGE_METRIC = { pct: 0 };
const EMPTY_STATEMENT_COVERAGE = { covered: 0, pct: 0, total: 0 };
export function buildIntegrationProgressModel({ root = process.cwd() } = {}) {
const coverageSummary = path.join(root, 'coverage/coverage-summary.json');
const handlerTestDir = path.join(root, 'src/daemon/handlers/__tests__');
const providerScenarioDir = path.join(root, 'test/integration/provider-scenarios');
const commandContractFiles = listFiles(path.join(root, 'src/commands'), (file) =>
file.endsWith(`${path.sep}index.ts`),
);
const clientCommandMethods = readClientCommandMethods(commandContractFiles);
const handlerTests = listFiles(handlerTestDir, (file) => file.endsWith('.test.ts'));
const providerScenarioTests = listFiles(providerScenarioDir, (file) => file.endsWith('.test.ts'));
const providerScenarioSources = listFiles(providerScenarioDir, (file) => file.endsWith('.ts'));
const providerScenarioSupportSources = providerScenarioSources.filter((file) => !file.endsWith('.test.ts'));
const handlerStats = summarizeFiles(handlerTests);
const providerScenarioStats = summarizeFiles(providerScenarioTests);
const providerScenarioSupportStats = summarizeFiles(providerScenarioSupportSources);
const mockHeavyHandlerFiles = handlerTests.filter((file) =>
fs.readFileSync(file, 'utf8').includes('vi.mock('),
);
const mockHeavyHandlerRows = summarizeMockHeavyHandlerFiles(root, mockHeavyHandlerFiles);
const providerPressureRows = summarizeProviderPressure(providerScenarioSources);
const publicCommandRows = summarizePublicCommandCoverage(providerScenarioTests, clientCommandMethods);
const missingPublicCommands = publicCommandRows.filter((command) => command.references === 0);
const flagCoverageRows = summarizeProviderScenarioFlagCoverage(providerScenarioTests);
const missingFlagRows = flagCoverageRows.filter((flag) => flag.references === 0);
const excludedFlagRows = summarizeProviderScenarioFlagExclusions();
const publicCliFlagKeys = readPublicCliFlagKeys();
const classifiedFlagKeys = new Set([
...flagCoverageRows.map((flag) => flag.key),
...excludedFlagRows.flatMap((group) => group.keys),
]);
const unclassifiedFlagKeys = [...publicCliFlagKeys].filter((key) => !classifiedFlagKeys.has(key));
const coverage = readCoverageSummary(coverageSummary);
const lowCoverageFiles = readLowCoverageFiles(root, coverageSummary);
const summaryRows = [
['Handler unit test files', String(handlerStats.files)],
['Handler unit test LOC', String(handlerStats.lines)],
['Handler unit tests', String(handlerStats.tests)],
['Handler files with vi.mock', String(mockHeavyHandlerFiles.length)],
['Provider scenario files', String(providerScenarioStats.files)],
['Provider scenario LOC', String(providerScenarioStats.lines)],
['Provider scenario tests', String(providerScenarioStats.tests)],
['Provider scenario support files', String(providerScenarioSupportStats.files)],
['Provider scenario support LOC', String(providerScenarioSupportStats.lines)],
['Provider scenario / handler LOC', ratio(providerScenarioStats.lines, handlerStats.lines)],
[
'Public commands covered by provider-backed integration',
`${publicCommandRows.length - missingPublicCommands.length}/${publicCommandRows.length}`,
],
['Public commands missing provider-backed integration coverage', String(missingPublicCommands.length)],
[
'Device-observable workflow flags covered by provider-backed integration',
`${flagCoverageRows.length - missingFlagRows.length}/${flagCoverageRows.length}`,
],
['Device-observable workflow flags missing provider-backed integration coverage', String(missingFlagRows.length)],
[
'Public CLI flags intentionally outside provider-backed integration',
String(excludedFlagRows.reduce((sum, group) => sum + group.keys.length, 0)),
],
['Public CLI flags unclassified by progress script', String(unclassifiedFlagKeys.length)],
];
if (coverage) {
summaryRows.push(
['Coverage statements', formatPercent(coverage.statements)],
['Coverage branches', formatPercent(coverage.branches)],
['Coverage functions', formatPercent(coverage.functions)],
['Coverage lines', formatPercent(coverage.lines)],
);
} else {
summaryRows.push(['Coverage summary', 'not available; run pnpm test:coverage first']);
}
return {
coverage,
excludedFlagRows,
flagCoverageRows,
lowCoverageFiles,
missingFlagRows,
missingPublicCommands,
mockHeavyHandlerRows,
providerPressureRows,
publicCommandRows,
summaryRows,
unclassifiedFlagKeys,
};
}
export function buildIntegrationProgressFailures(progress) {
const failures = [];
if (progress.missingPublicCommands.length > 0) {
failures.push(
`missing Provider-backed integration command coverage: ${progress.missingPublicCommands.map((row) => row.command).join(', ')}`,
);
}
if (progress.missingFlagRows.length > 0) {
failures.push(
`missing Provider-backed integration workflow flag coverage: ${progress.missingFlagRows.map((row) => row.key).join(', ')}`,
);
}
if (progress.unclassifiedFlagKeys.length > 0) {
failures.push(`unclassified public CLI flags: ${progress.unclassifiedFlagKeys.join(', ')}`);
}
return failures;
}
function summarizeProviderScenarioFlagCoverage(files) {
const flagTargets = [
['platform', 'selection across platform-specific provider-backed integration flows'],
['target', 'target-class routing such as tv/mobile/desktop'],
['device', 'human-readable device selection'],
['udid', 'Apple device selection'],
['serial', 'Android device selection'],
['iosSimulatorDeviceSet', 'iOS simulator-set scoping reaches inventory resolution'],
['androidDeviceAllowlist', 'Android serial allowlist reaches inventory resolution'],
['session', 'named session routing'],
['surface', 'macOS app/frontmost/desktop/menubar surfaces'],
['activity', 'Android explicit launch activity'],
['launchConsole', 'iOS simulator launch console capture'],
['saveScript', 'open/close replay recording output'],
['relaunch', 'open terminates before launch'],
['shutdown', 'close/disconnect shutdown behavior'],
['appsFilter', 'apps --all vs default filtering'],
['header', 'install-from-source URL headers', ['headers']],
['retainPaths', 'retained install-source materialization'],
['retentionMs', 'install-source materialization TTL'],
['count', 'repeated press/click/swipe input'],
['fps', 'recording frame-rate request'],
['quality', 'recording quality scaling'],
['hideTouches', 'recording without touch overlays'],
['intervalMs', 'repeated press interval'],
['delayMs', 'typing/fill delay'],
['holdMs', 'press hold duration'],
['jitterPx', 'press jitter'],
['pixels', 'scroll distance'],
['doubleTap', 'double tap gesture'],
['clickButton', 'desktop mouse button selection', ['button']],
['backMode', 'explicit app/system back behavior', ['mode']],
['pauseMs', 'swipe repeat pause'],
['pattern', 'swipe repeat pattern'],
['snapshotInteractiveOnly', 'interactive snapshot/ref refresh', ['interactiveOnly']],
['snapshotDepth', 'scoped snapshot depth', ['depth']],
['snapshotScope', 'scoped snapshot capture', ['scope']],
['snapshotRaw', 'raw snapshot node output', ['raw']],
['out', 'artifact output path plumbing'],
['overlayRefs', 'screenshot ref overlay annotation'],
['screenshotFullscreen', 'screenshot full-screen capture mode'],
['screenshotMaxSize', 'screenshot max-size post-processing'],
['screenshotNoStabilize', 'screenshot stabilization opt-out', ['stabilize']],
['restart', 'logs clear --restart workflow'],
['networkInclude', 'network dump include modes', ['include']],
['noRecord', 'action recording suppression'],
['replayUpdate', 'selector-healing replay update', ['update']],
['replayEnv', 'replay/test variable injection', ['env']],
['failFast', 'test suite stops after first failure'],
['timeoutMs', 'wait/test timeout flags'],
['retries', 'test suite retry budget flows through request path'],
['artifactsDir', 'test artifact root'],
['steps', 'batch inline steps'],
['batchOnError', 'batch stop-on-error policy', ['onError']],
['batchMaxSteps', 'batch max-step guard', ['maxSteps']],
['findFirst', 'find first disambiguation'],
['findLast', 'find last disambiguation'],
];
const sources = files.map((file) => fs.readFileSync(file, 'utf8')).join('\n');
return flagTargets.map(([key, reason, aliases = []]) => {
const references = [key, ...aliases].reduce(
(count, candidate) => count + countFlagReferences(sources, candidate),
0,
);
return { key, reason, references };
});
}
function countFlagReferences(text, key) {
const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return text.match(new RegExp(`\\b${escaped}\\s*:`, 'g'))?.length ?? 0;
}
function summarizeProviderScenarioFlagExclusions() {
return [
{
name: 'config, output, diagnostics, and transport',
owner: 'args/CLI transport/auth tests',
keys: [
'config',
'remoteConfig',
'stateDir',
'daemonBaseUrl',
'daemonAuthToken',
'daemonTransport',
'daemonServerMode',
'tenant',
'sessionIsolation',
'runId',
'leaseId',
'leaseBackend',
'json',
'help',
'version',
'verbose',
],
},
{
name: 'remote connection and session-lock policy',
owner: 'connection/runtime/request policy tests',
keys: ['force', 'noLogin', 'sessionLock', 'sessionLocked', 'sessionLockConflicts'],
},
{
name: 'Metro and React Native runtime preparation',
owner: 'Metro companion integration and parser tests',
keys: [
'metroHost',
'metroPort',
'metroProjectRoot',
'metroKind',
'metroPublicBaseUrl',
'metroProxyBaseUrl',
'metroBearerToken',
'metroPreparePort',
'metroListenHost',
'metroStatusHost',
'metroStartupTimeoutMs',
'metroProbeTimeoutMs',
'metroRuntimeFile',
'metroNoReuseExisting',
'metroNoInstallDeps',
'bundleUrl',
'launchUrl',
],
},
{
name: 'Apple launch and perf artifact options',
owner: 'iOS platform, observability command, and parser tests',
keys: [
'deviceHub',
'kind',
'launchArgs',
'perfTemplate',
'iosXctestrunFile',
'iosXctestDerivedDataPath',
'iosXctestEnvDir',
],
},
{
name: 'parser/client-only command flags',
owner: 'args, CLI, debug-symbols, screenshot-diff, and batch tests',
keys: [
'artifact',
'dsym',
'githubActionsArtifact',
'snapshotDiff',
'snapshotForceFull',
'baseline',
'threshold',
'reportJunit',
'replayMaestro',
'replayExportFormat',
'recordVideo',
'shardAll',
'shardSplit',
'searchPath',
'stepsFile',
],
},
{
name: 'platform boot fallback without provider seam',
owner: 'handler and Android platform unit tests',
keys: ['headless'],
},
];
}
function readPublicCliFlagKeys() {
return new Set(
getFlagDefinitions()
.filter((definition) => definition.names.some((name) => name.startsWith('-')))
.map((definition) => definition.key),
);
}
function listFiles(dir, predicate) {
if (!fs.existsSync(dir)) return [];
const entries = fs.readdirSync(dir, { withFileTypes: true });
return entries.flatMap((entry) => {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) return listFiles(fullPath, predicate);
return predicate(fullPath) ? [fullPath] : [];
});
}
function summarizeFiles(files) {
let lines = 0;
let tests = 0;
for (const file of files) {
const text = fs.readFileSync(file, 'utf8');
lines += text.split('\n').length;
tests += countTestDeclarations(text);
}
return { files: files.length, lines, tests };
}
function summarizeMockHeavyHandlerFiles(root, files) {
return files
.map((file) => {
const text = fs.readFileSync(file, 'utf8');
return {
file: path.relative(root, file),
lines: text.split('\n').length,
tests: countTestDeclarations(text),
};
})
.sort((a, b) => b.lines - a.lines)
.slice(0, 12);
}
function summarizeProviderPressure(files) {
const surfaces = [
{
name: 'Android ADB provider',
pattern: /\bAndroidAdbProvider\b|\bandroidAdbProvider\b|\badbProvider\b|\badb\.(?:exec|installer|puller|portReverse)\b/g,
},
{
name: 'Apple runner provider',
pattern: /\bAppleRunnerProvider\b|\bappleRunnerProvider\b|\b(?:ios|macos|tvos)\.runner\b/g,
},
{
name: 'Apple simctl/devicectl provider',
pattern:
/\bsimctl\b|\bdevicectl\b|\brunXcrun\b|\bsimctl\s*:|\bdevicectl\s*:/g,
},
{
name: 'Apple macOS helper provider',
pattern: /\bmacos-helper\b|\bagent-device-macos-helper\b|\bmacosHelper\s*:/g,
},
{
name: 'Apple macOS host provider',
pattern:
/\bmacos-host\b|\bmacosHost\s*:|\bAppleMacOsHostProvider\b|\bopenBundle\b|\bopenTarget\b|\breadClipboard\b|\bwriteClipboard\b|\breadDarkMode\b|\bsetDarkMode\b|\blistApps\b/g,
},
{
name: 'Apple generic host-tool provider',
pattern:
/\bxcrun\b|['"](?:open|pbcopy|pbpaste|plutil|osascript|swift|codesign|mdfind|ps|pkill)['"]/g,
},
{
name: 'Linux semantic desktop provider',
pattern: /\bdesktop\b|\bopenTarget\b|\bcloseApp\b/g,
},
{
name: 'Linux semantic accessibility/clipboard/screenshot provider',
pattern:
/\baccessibility\b|\bcaptureTree\b|\bclipboard\b|\breadText\b|\bwriteText\b|\bscreenshot\b|\bcapture\s*:/g,
},
{
name: 'Linux semantic input provider',
pattern: /\bLinuxInputProvider\b|\bprovider\.input\b|\binput\s*:|\['input'/g,
},
{
name: 'Linux generic tool provider',
pattern:
/\bLinuxToolProvider\b|\blinuxToolProvider\b|\brunCommand\b|\bwhichCommand\b|\bxdotool\b|\bydotool\b|\bxclip\b|\bscrot\b|\bgrim\b|\bwmctrl\b|\bpkill\b/g,
},
{
name: 'Recording provider',
pattern: /\bRecordingProvider\b|\brecordingProvider\b|\bstartRecording\b/g,
},
];
return surfaces
.map((surface) => ({ name: surface.name, ...countSurfaceReferences(files, surface.pattern) }))
.filter((surface) => surface.references > 0);
}
function countSurfaceReferences(files, pattern) {
let references = 0;
let filesWithReferences = 0;
for (const file of files) {
const matches = countPatternReferences(fs.readFileSync(file, 'utf8'), pattern);
references += matches;
filesWithReferences += matches > 0 ? 1 : 0;
}
return { references, files: filesWithReferences };
}
function countPatternReferences(text, pattern) {
return text.match(pattern)?.length ?? 0;
}
function summarizePublicCommandCoverage(files, clientCommandMethods) {
const publicCommands = readPublicCommands();
const commandRefsByFile = files.map((file) => ({
file,
commands: extractProviderScenarioCommandReferences(
fs.readFileSync(file, 'utf8'),
clientCommandMethods,
),
}));
return publicCommands.map((command) => {
let references = 0;
let filesWithReferences = 0;
for (const file of commandRefsByFile) {
const count = file.commands.filter((candidate) => candidate === command).length;
references += count;
if (count > 0) filesWithReferences += 1;
}
return { command, references, files: filesWithReferences };
});
}
function readPublicCommands() {
const metadataNames = new Set(listCommandMetadata().map((metadata) => metadata.name));
return Object.values(PUBLIC_COMMANDS)
.map((name) => {
if (!metadataNames.has(name)) {
throw new Error(`Missing command metadata for public command: ${name}`);
}
return name;
})
.sort();
}
function readClientCommandMethods(commandContractFiles) {
const commands = new Map();
for (const file of commandContractFiles) {
const text = fs.readFileSync(file, 'utf8');
for (const block of readCommandContractBlocks(text)) {
for (const method of block.source.matchAll(/\bclient\.([A-Za-z0-9_]+)\.([A-Za-z0-9_]+)\s*\(/g)) {
commands.set(`${method[1]}.${method[2]}`, block.name);
}
}
}
return commands;
}
function readCommandContractBlocks(text) {
const constants = new Map();
for (const match of text.matchAll(/\bconst\s+([A-Z0-9_]+)\s*=\s*['"]([^'"]+)['"]/g)) {
constants.set(match[1], match[2]);
}
const metadataNames = new Map();
for (const match of text.matchAll(
/\bconst\s+([A-Za-z0-9_]+CommandMetadata)\s*=\s*defineFieldCommandMetadata\(\s*([^,\s)]+)/g,
)) {
metadataNames.set(match[1], readMetadataName(match[2], constants));
}
const starts = [
...text.matchAll(/defineExecutableCommand\(\s*metadata\(\s*['"]([^'"]+)['"]\s*\)/g),
...[...text.matchAll(/defineExecutableCommand\(\s*([A-Za-z0-9_]+CommandMetadata)\b/g)].flatMap(
(match) => {
const name = metadataNames.get(match[1]);
return name ? [{ ...match, 1: name }] : [];
},
),
...text.matchAll(/defineFieldCommand\(\s*['"]([^'"]+)['"]/g),
...text.matchAll(/defineCommand\(\s*\{[\s\S]*?\bname:\s*['"]([^'"]+)['"]/g),
]
.map((match) => ({
index: match.index ?? 0,
name: match[1],
}))
.sort((a, b) => a.index - b.index);
return starts.map((start, index) => {
const end = starts[index + 1]?.index ?? text.length;
return {
name: start.name,
source: text.slice(start.index, end),
};
});
}
function readMetadataName(token, constants) {
const literal = token.match(/^['"]([^'"]+)['"]$/);
if (literal) return literal[1];
return constants.get(token);
}
function extractProviderScenarioCommandReferences(text, clientCommandMethods) {
return [
...extractLiteralCommandReferences(text),
...extractClientCommandReferences(text, clientCommandMethods),
];
}
function extractLiteralCommandReferences(text) {
const commands = [];
for (const match of text.matchAll(/\bcommand:\s*['"]([^'"]+)['"]|\.callCommand\(\s*['"]([^'"]+)['"]/g)) {
commands.push(match[1] ?? match[2]);
}
return commands;
}
function extractClientCommandReferences(text, clientCommandMethods) {
const commands = [];
for (const [method, command] of clientCommandMethods) {
const escapedMethod = method.replace('.', '\\.');
const matches = countPatternReferences(text, new RegExp(`\\.${escapedMethod}\\s*\\(`, 'g'));
for (let index = 0; index < matches; index += 1) commands.push(command);
}
return commands;
}
function countTestDeclarations(text) {
return [...text.matchAll(/(?:^|[^\w.])test\(/g)].length;
}
function readCoverageSummary(coverageSummary) {
const total = readCoverageSummaryJson(coverageSummary)?.total;
if (!total) return null;
return {
statements: readCoveragePercent(total, 'statements'),
branches: readCoveragePercent(total, 'branches'),
functions: readCoveragePercent(total, 'functions'),
lines: readCoveragePercent(total, 'lines'),
};
}
function readLowCoverageFiles(root, coverageSummary) {
const summary = readCoverageSummaryJson(coverageSummary);
if (!summary) return [];
return Object.entries(summary)
.filter(([file]) => file !== 'total')
.map(([file, value]) => readLowCoverageFile(root, file, value))
.filter((file) => file.statementTotal >= 10 && file.statementPercent < 60)
.sort((a, b) => b.missingStatements - a.missingStatements)
.slice(0, 10);
}
function readCoverageSummaryJson(coverageSummary) {
if (!fs.existsSync(coverageSummary)) return null;
return JSON.parse(fs.readFileSync(coverageSummary, 'utf8'));
}
function readCoveragePercent(total, key) {
return Number((total[key] ?? EMPTY_COVERAGE_METRIC).pct);
}
function readLowCoverageFile(root, file, value) {
const statements = value.statements ?? EMPTY_STATEMENT_COVERAGE;
const statementTotal = Number(statements.total);
const statementCovered = Number(statements.covered);
return {
file: path.relative(root, file),
statementPercent: Number(statements.pct),
statementTotal,
missingStatements: statementTotal - statementCovered,
};
}
function ratio(numerator, denominator) {
if (denominator === 0) return 'n/a';
return `${((numerator / denominator) * 100).toFixed(1)}%`;
}
export function formatPercent(value) {
return `${value.toFixed(2)}%`;
}