agent-device 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -4
- package/dist/src/bin.js +26 -21
- package/dist/src/daemon.js +9 -8
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunner.xcodeproj/project.pbxproj +2 -0
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +15 -0
- package/package.json +3 -2
- package/skills/agent-device/SKILL.md +22 -6
- package/skills/agent-device/references/session-management.md +9 -0
- package/skills/agent-device/references/snapshot-refs.md +18 -5
- package/skills/agent-device/references/video-recording.md +2 -2
- package/src/cli.ts +6 -0
- package/src/core/__tests__/capabilities.test.ts +67 -0
- package/src/core/capabilities.ts +49 -0
- package/src/core/dispatch.ts +29 -118
- package/src/daemon/__tests__/is-predicates.test.ts +68 -0
- package/src/daemon/__tests__/selectors.test.ts +128 -0
- package/src/daemon/__tests__/session-routing.test.ts +108 -0
- package/src/daemon/__tests__/session-selector.test.ts +64 -0
- package/src/daemon/__tests__/session-store.test.ts +95 -0
- package/src/daemon/__tests__/snapshot-processing.test.ts +47 -0
- package/src/daemon/action-utils.ts +29 -0
- package/src/daemon/app-state.ts +66 -0
- package/src/daemon/context.ts +36 -0
- package/src/daemon/device-ready.ts +13 -0
- package/src/daemon/handlers/__tests__/find.test.ts +99 -0
- package/src/daemon/handlers/__tests__/replay-heal.test.ts +364 -0
- package/src/daemon/handlers/__tests__/snapshot.test.ts +128 -0
- package/src/daemon/handlers/find.ts +304 -0
- package/src/daemon/handlers/interaction.ts +510 -0
- package/src/daemon/handlers/parse-utils.ts +8 -0
- package/src/daemon/handlers/record-trace.ts +154 -0
- package/src/daemon/handlers/session.ts +732 -0
- package/src/daemon/handlers/snapshot.ts +396 -0
- package/src/daemon/is-predicates.ts +46 -0
- package/src/daemon/selectors.ts +423 -0
- package/src/daemon/session-routing.ts +22 -0
- package/src/daemon/session-selector.ts +39 -0
- package/src/daemon/session-store.ts +275 -0
- package/src/daemon/snapshot-processing.ts +127 -0
- package/src/daemon/types.ts +55 -0
- package/src/daemon.ts +66 -1592
- package/src/platforms/ios/index.ts +0 -62
- package/src/platforms/ios/runner-client.ts +2 -0
- package/src/utils/args.ts +19 -10
- package/src/utils/interactors.ts +102 -16
- package/src/utils/snapshot.ts +1 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { AppError } from '../utils/errors.ts';
|
|
2
|
+
import type { SnapshotNode, SnapshotState } from '../utils/snapshot.ts';
|
|
3
|
+
import { extractNodeText, isFillableType, normalizeType } from './snapshot-processing.ts';
|
|
4
|
+
import { uniqueStrings } from './action-utils.ts';
|
|
5
|
+
|
|
6
|
+
type SelectorKey =
|
|
7
|
+
| 'id'
|
|
8
|
+
| 'role'
|
|
9
|
+
| 'text'
|
|
10
|
+
| 'label'
|
|
11
|
+
| 'value'
|
|
12
|
+
| 'visible'
|
|
13
|
+
| 'hidden'
|
|
14
|
+
| 'editable'
|
|
15
|
+
| 'selected'
|
|
16
|
+
| 'enabled'
|
|
17
|
+
| 'hittable';
|
|
18
|
+
|
|
19
|
+
type SelectorTerm = {
|
|
20
|
+
key: SelectorKey;
|
|
21
|
+
value: string | boolean;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type Selector = {
|
|
25
|
+
raw: string;
|
|
26
|
+
terms: SelectorTerm[];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type SelectorChain = {
|
|
30
|
+
raw: string;
|
|
31
|
+
selectors: Selector[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type SelectorDiagnostics = {
|
|
35
|
+
selector: string;
|
|
36
|
+
matches: number;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type SelectorResolution = {
|
|
40
|
+
node: SnapshotNode;
|
|
41
|
+
selector: Selector;
|
|
42
|
+
selectorIndex: number;
|
|
43
|
+
matches: number;
|
|
44
|
+
diagnostics: SelectorDiagnostics[];
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const TEXT_KEYS = new Set<SelectorKey>(['id', 'role', 'text', 'label', 'value']);
|
|
48
|
+
const BOOLEAN_KEYS = new Set<SelectorKey>([
|
|
49
|
+
'visible',
|
|
50
|
+
'hidden',
|
|
51
|
+
'editable',
|
|
52
|
+
'selected',
|
|
53
|
+
'enabled',
|
|
54
|
+
'hittable',
|
|
55
|
+
]);
|
|
56
|
+
const ALL_KEYS = new Set<SelectorKey>([...TEXT_KEYS, ...BOOLEAN_KEYS]);
|
|
57
|
+
|
|
58
|
+
export function parseSelectorChain(expression: string): SelectorChain {
|
|
59
|
+
const raw = expression.trim();
|
|
60
|
+
if (!raw) {
|
|
61
|
+
throw new AppError('INVALID_ARGS', 'Selector expression cannot be empty');
|
|
62
|
+
}
|
|
63
|
+
const segments = splitByFallback(raw);
|
|
64
|
+
if (segments.length === 0) {
|
|
65
|
+
throw new AppError('INVALID_ARGS', 'Selector expression cannot be empty');
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
raw,
|
|
69
|
+
selectors: segments.map((segment) => parseSelector(segment)),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function tryParseSelectorChain(expression: string): SelectorChain | null {
|
|
74
|
+
try {
|
|
75
|
+
return parseSelectorChain(expression);
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function resolveSelectorChain(
|
|
82
|
+
nodes: SnapshotState['nodes'],
|
|
83
|
+
chain: SelectorChain,
|
|
84
|
+
options: {
|
|
85
|
+
platform: 'ios' | 'android';
|
|
86
|
+
requireRect?: boolean;
|
|
87
|
+
requireUnique?: boolean;
|
|
88
|
+
},
|
|
89
|
+
): SelectorResolution | null {
|
|
90
|
+
const requireRect = options.requireRect ?? false;
|
|
91
|
+
const requireUnique = options.requireUnique ?? true;
|
|
92
|
+
const diagnostics: SelectorDiagnostics[] = [];
|
|
93
|
+
for (let i = 0; i < chain.selectors.length; i += 1) {
|
|
94
|
+
const selector = chain.selectors[i];
|
|
95
|
+
const matches = nodes.filter((node) => {
|
|
96
|
+
if (requireRect && !node.rect) return false;
|
|
97
|
+
return matchesSelector(node, selector, options.platform);
|
|
98
|
+
});
|
|
99
|
+
diagnostics.push({ selector: selector.raw, matches: matches.length });
|
|
100
|
+
if (matches.length === 0) continue;
|
|
101
|
+
if (requireUnique && matches.length !== 1) continue;
|
|
102
|
+
return {
|
|
103
|
+
node: matches[0],
|
|
104
|
+
selector,
|
|
105
|
+
selectorIndex: i,
|
|
106
|
+
matches: matches.length,
|
|
107
|
+
diagnostics,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function findSelectorChainMatch(
|
|
114
|
+
nodes: SnapshotState['nodes'],
|
|
115
|
+
chain: SelectorChain,
|
|
116
|
+
options: {
|
|
117
|
+
platform: 'ios' | 'android';
|
|
118
|
+
requireRect?: boolean;
|
|
119
|
+
},
|
|
120
|
+
): { selectorIndex: number; selector: Selector; matches: number; diagnostics: SelectorDiagnostics[] } | null {
|
|
121
|
+
const requireRect = options.requireRect ?? false;
|
|
122
|
+
const diagnostics: SelectorDiagnostics[] = [];
|
|
123
|
+
for (let i = 0; i < chain.selectors.length; i += 1) {
|
|
124
|
+
const selector = chain.selectors[i];
|
|
125
|
+
const matches = nodes.filter((node) => {
|
|
126
|
+
if (requireRect && !node.rect) return false;
|
|
127
|
+
return matchesSelector(node, selector, options.platform);
|
|
128
|
+
});
|
|
129
|
+
diagnostics.push({ selector: selector.raw, matches: matches.length });
|
|
130
|
+
if (matches.length > 0) {
|
|
131
|
+
return { selectorIndex: i, selector, matches: matches.length, diagnostics };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function formatSelectorFailure(
|
|
138
|
+
chain: SelectorChain,
|
|
139
|
+
diagnostics: SelectorDiagnostics[],
|
|
140
|
+
options: { unique?: boolean },
|
|
141
|
+
): string {
|
|
142
|
+
const unique = options.unique ?? true;
|
|
143
|
+
if (diagnostics.length === 0) {
|
|
144
|
+
return `Selector did not match: ${chain.raw}`;
|
|
145
|
+
}
|
|
146
|
+
const summary = diagnostics.map((entry) => `${entry.selector} -> ${entry.matches}`).join(', ');
|
|
147
|
+
if (unique) {
|
|
148
|
+
return `Selector did not resolve uniquely (${summary})`;
|
|
149
|
+
}
|
|
150
|
+
return `Selector did not match (${summary})`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function isSelectorToken(token: string): boolean {
|
|
154
|
+
const trimmed = token.trim();
|
|
155
|
+
if (!trimmed) return false;
|
|
156
|
+
if (trimmed === '||') return true;
|
|
157
|
+
const equalsIdx = trimmed.indexOf('=');
|
|
158
|
+
if (equalsIdx !== -1) {
|
|
159
|
+
const key = trimmed.slice(0, equalsIdx).trim().toLowerCase() as SelectorKey;
|
|
160
|
+
return ALL_KEYS.has(key);
|
|
161
|
+
}
|
|
162
|
+
return ALL_KEYS.has(trimmed.toLowerCase() as SelectorKey);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function splitSelectorFromArgs(args: string[]): { selectorExpression: string; rest: string[] } | null {
|
|
166
|
+
if (args.length === 0) return null;
|
|
167
|
+
let i = 0;
|
|
168
|
+
while (i < args.length && isSelectorToken(args[i])) {
|
|
169
|
+
i += 1;
|
|
170
|
+
}
|
|
171
|
+
if (i === 0) return null;
|
|
172
|
+
const selectorExpression = args.slice(0, i).join(' ').trim();
|
|
173
|
+
if (!selectorExpression) return null;
|
|
174
|
+
return {
|
|
175
|
+
selectorExpression,
|
|
176
|
+
rest: args.slice(i),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function isNodeVisible(node: SnapshotNode): boolean {
|
|
181
|
+
if (node.hittable === true) return true;
|
|
182
|
+
if (!node.rect) return false;
|
|
183
|
+
return node.rect.width > 0 && node.rect.height > 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function isNodeEditable(node: SnapshotNode, platform: 'ios' | 'android'): boolean {
|
|
187
|
+
const type = node.type ?? '';
|
|
188
|
+
return isFillableType(type, platform) && node.enabled !== false;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function buildSelectorChainForNode(
|
|
192
|
+
node: SnapshotNode,
|
|
193
|
+
platform: 'ios' | 'android',
|
|
194
|
+
options: { action?: 'click' | 'fill' | 'get' } = {},
|
|
195
|
+
): string[] {
|
|
196
|
+
const chain: string[] = [];
|
|
197
|
+
const role = normalizeType(node.type ?? '');
|
|
198
|
+
const id = normalizeSelectorText(node.identifier);
|
|
199
|
+
const label = normalizeSelectorText(node.label);
|
|
200
|
+
const value = normalizeSelectorText(node.value);
|
|
201
|
+
const text = normalizeSelectorText(extractNodeText(node));
|
|
202
|
+
const requireEditable = options.action === 'fill';
|
|
203
|
+
|
|
204
|
+
if (id) {
|
|
205
|
+
chain.push(`id=${quoteSelectorValue(id)}`);
|
|
206
|
+
}
|
|
207
|
+
if (role && label) {
|
|
208
|
+
chain.push(
|
|
209
|
+
requireEditable
|
|
210
|
+
? `role=${quoteSelectorValue(role)} label=${quoteSelectorValue(label)} editable=true`
|
|
211
|
+
: `role=${quoteSelectorValue(role)} label=${quoteSelectorValue(label)}`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
if (label) {
|
|
215
|
+
chain.push(requireEditable ? `label=${quoteSelectorValue(label)} editable=true` : `label=${quoteSelectorValue(label)}`);
|
|
216
|
+
}
|
|
217
|
+
if (value) {
|
|
218
|
+
chain.push(requireEditable ? `value=${quoteSelectorValue(value)} editable=true` : `value=${quoteSelectorValue(value)}`);
|
|
219
|
+
}
|
|
220
|
+
if (text && text !== label && text !== value) {
|
|
221
|
+
chain.push(requireEditable ? `text=${quoteSelectorValue(text)} editable=true` : `text=${quoteSelectorValue(text)}`);
|
|
222
|
+
}
|
|
223
|
+
if (role && requireEditable && !chain.some((entry) => entry.includes('editable=true'))) {
|
|
224
|
+
chain.push(`role=${quoteSelectorValue(role)} editable=true`);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const deduped = uniqueStrings(chain);
|
|
228
|
+
if (deduped.length === 0 && role) {
|
|
229
|
+
deduped.push(requireEditable ? `role=${quoteSelectorValue(role)} editable=true` : `role=${quoteSelectorValue(role)}`);
|
|
230
|
+
}
|
|
231
|
+
if (deduped.length === 0) {
|
|
232
|
+
const visible = isNodeVisible(node);
|
|
233
|
+
if (visible) deduped.push('visible=true');
|
|
234
|
+
}
|
|
235
|
+
return deduped;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function parseSelector(segment: string): Selector {
|
|
239
|
+
const raw = segment.trim();
|
|
240
|
+
if (!raw) throw new AppError('INVALID_ARGS', 'Selector segment cannot be empty');
|
|
241
|
+
const tokens = tokenize(raw);
|
|
242
|
+
if (tokens.length === 0) {
|
|
243
|
+
throw new AppError('INVALID_ARGS', `Invalid selector segment: ${segment}`);
|
|
244
|
+
}
|
|
245
|
+
const terms = tokens.map(parseTerm);
|
|
246
|
+
return { raw, terms };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function parseTerm(token: string): SelectorTerm {
|
|
250
|
+
const normalized = token.trim();
|
|
251
|
+
if (!normalized) {
|
|
252
|
+
throw new AppError('INVALID_ARGS', 'Empty selector term');
|
|
253
|
+
}
|
|
254
|
+
const equalsIdx = normalized.indexOf('=');
|
|
255
|
+
if (equalsIdx === -1) {
|
|
256
|
+
const key = normalized.toLowerCase() as SelectorKey;
|
|
257
|
+
if (!BOOLEAN_KEYS.has(key)) {
|
|
258
|
+
throw new AppError('INVALID_ARGS', `Invalid selector term "${token}", expected key=value`);
|
|
259
|
+
}
|
|
260
|
+
return { key, value: true };
|
|
261
|
+
}
|
|
262
|
+
const keyRaw = normalized.slice(0, equalsIdx).trim().toLowerCase() as SelectorKey;
|
|
263
|
+
const valueRaw = normalized.slice(equalsIdx + 1).trim();
|
|
264
|
+
if (!ALL_KEYS.has(keyRaw)) {
|
|
265
|
+
throw new AppError('INVALID_ARGS', `Unknown selector key: ${keyRaw}`);
|
|
266
|
+
}
|
|
267
|
+
if (!valueRaw) {
|
|
268
|
+
throw new AppError('INVALID_ARGS', `Missing selector value for key: ${keyRaw}`);
|
|
269
|
+
}
|
|
270
|
+
if (BOOLEAN_KEYS.has(keyRaw)) {
|
|
271
|
+
const parsedBoolean = parseBoolean(valueRaw);
|
|
272
|
+
if (parsedBoolean === null) {
|
|
273
|
+
throw new AppError('INVALID_ARGS', `Invalid boolean value for ${keyRaw}: ${valueRaw}`);
|
|
274
|
+
}
|
|
275
|
+
return { key: keyRaw, value: parsedBoolean };
|
|
276
|
+
}
|
|
277
|
+
return { key: keyRaw, value: unquote(valueRaw) };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function matchesSelector(node: SnapshotNode, selector: Selector, platform: 'ios' | 'android'): boolean {
|
|
281
|
+
return selector.terms.every((term) => matchesTerm(node, term, platform));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function matchesTerm(node: SnapshotNode, term: SelectorTerm, platform: 'ios' | 'android'): boolean {
|
|
285
|
+
switch (term.key) {
|
|
286
|
+
case 'id':
|
|
287
|
+
return textEquals(node.identifier, String(term.value));
|
|
288
|
+
case 'role':
|
|
289
|
+
return roleEquals(node.type, String(term.value));
|
|
290
|
+
case 'label':
|
|
291
|
+
return textEquals(node.label, String(term.value));
|
|
292
|
+
case 'value':
|
|
293
|
+
return textEquals(node.value, String(term.value));
|
|
294
|
+
case 'text': {
|
|
295
|
+
const query = normalizeText(String(term.value));
|
|
296
|
+
return normalizeText(extractNodeText(node)) === query;
|
|
297
|
+
}
|
|
298
|
+
case 'visible':
|
|
299
|
+
return isNodeVisible(node) === Boolean(term.value);
|
|
300
|
+
case 'hidden':
|
|
301
|
+
return (!isNodeVisible(node)) === Boolean(term.value);
|
|
302
|
+
case 'editable':
|
|
303
|
+
return isNodeEditable(node, platform) === Boolean(term.value);
|
|
304
|
+
case 'selected':
|
|
305
|
+
return Boolean(node.selected === true) === Boolean(term.value);
|
|
306
|
+
case 'enabled':
|
|
307
|
+
return Boolean(node.enabled !== false) === Boolean(term.value);
|
|
308
|
+
case 'hittable':
|
|
309
|
+
return Boolean(node.hittable === true) === Boolean(term.value);
|
|
310
|
+
default:
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function splitByFallback(expression: string): string[] {
|
|
316
|
+
const segments: string[] = [];
|
|
317
|
+
let current = '';
|
|
318
|
+
let quote: '"' | "'" | null = null;
|
|
319
|
+
for (let i = 0; i < expression.length; i += 1) {
|
|
320
|
+
const ch = expression[i];
|
|
321
|
+
if ((ch === '"' || ch === "'") && expression[i - 1] !== '\\') {
|
|
322
|
+
if (!quote) {
|
|
323
|
+
quote = ch;
|
|
324
|
+
} else if (quote === ch) {
|
|
325
|
+
quote = null;
|
|
326
|
+
}
|
|
327
|
+
current += ch;
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (!quote && ch === '|' && expression[i + 1] === '|') {
|
|
331
|
+
const segment = current.trim();
|
|
332
|
+
if (!segment) {
|
|
333
|
+
throw new AppError('INVALID_ARGS', `Invalid selector fallback expression: ${expression}`);
|
|
334
|
+
}
|
|
335
|
+
segments.push(segment);
|
|
336
|
+
current = '';
|
|
337
|
+
i += 1;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
current += ch;
|
|
341
|
+
}
|
|
342
|
+
const finalSegment = current.trim();
|
|
343
|
+
if (!finalSegment) {
|
|
344
|
+
throw new AppError('INVALID_ARGS', `Invalid selector fallback expression: ${expression}`);
|
|
345
|
+
}
|
|
346
|
+
segments.push(finalSegment);
|
|
347
|
+
return segments;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function tokenize(segment: string): string[] {
|
|
351
|
+
const tokens: string[] = [];
|
|
352
|
+
let current = '';
|
|
353
|
+
let quote: '"' | "'" | null = null;
|
|
354
|
+
for (let i = 0; i < segment.length; i += 1) {
|
|
355
|
+
const ch = segment[i];
|
|
356
|
+
if ((ch === '"' || ch === "'") && segment[i - 1] !== '\\') {
|
|
357
|
+
if (!quote) {
|
|
358
|
+
quote = ch;
|
|
359
|
+
} else if (quote === ch) {
|
|
360
|
+
quote = null;
|
|
361
|
+
}
|
|
362
|
+
current += ch;
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (!quote && /\s/.test(ch)) {
|
|
366
|
+
if (current.trim().length > 0) {
|
|
367
|
+
tokens.push(current.trim());
|
|
368
|
+
}
|
|
369
|
+
current = '';
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
current += ch;
|
|
373
|
+
}
|
|
374
|
+
if (quote) {
|
|
375
|
+
throw new AppError('INVALID_ARGS', `Unclosed quote in selector: ${segment}`);
|
|
376
|
+
}
|
|
377
|
+
if (current.trim().length > 0) {
|
|
378
|
+
tokens.push(current.trim());
|
|
379
|
+
}
|
|
380
|
+
return tokens;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function unquote(value: string): string {
|
|
384
|
+
const trimmed = value.trim();
|
|
385
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
386
|
+
return trimmed.slice(1, -1).replace(/\\(["'])/g, '$1');
|
|
387
|
+
}
|
|
388
|
+
return trimmed;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function parseBoolean(value: string): boolean | null {
|
|
392
|
+
const normalized = unquote(value).toLowerCase();
|
|
393
|
+
if (normalized === 'true') return true;
|
|
394
|
+
if (normalized === 'false') return false;
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function textEquals(value: string | undefined, query: string): boolean {
|
|
399
|
+
return normalizeText(value ?? '') === normalizeText(query);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function roleEquals(value: string | undefined, query: string): boolean {
|
|
403
|
+
return normalizeRole(value ?? '') === normalizeRole(query);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function normalizeText(value: string): string {
|
|
407
|
+
return value.trim().toLowerCase().replace(/\s+/g, ' ');
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function normalizeRole(value: string): string {
|
|
411
|
+
return normalizeType(value);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function quoteSelectorValue(value: string): string {
|
|
415
|
+
return JSON.stringify(value);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function normalizeSelectorText(value: string | undefined): string | null {
|
|
419
|
+
if (!value) return null;
|
|
420
|
+
const trimmed = value.trim();
|
|
421
|
+
if (!trimmed) return null;
|
|
422
|
+
return trimmed;
|
|
423
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DaemonRequest } from './types.ts';
|
|
2
|
+
import { SessionStore } from './session-store.ts';
|
|
3
|
+
import type { CommandFlags } from '../core/dispatch.ts';
|
|
4
|
+
|
|
5
|
+
export function resolveEffectiveSessionName(
|
|
6
|
+
req: DaemonRequest,
|
|
7
|
+
sessionStore: SessionStore,
|
|
8
|
+
): string {
|
|
9
|
+
const requested = req.session || 'default';
|
|
10
|
+
if (hasExplicitSessionFlag(req)) return requested;
|
|
11
|
+
if (requested !== 'default') return requested;
|
|
12
|
+
if (sessionStore.has(requested)) return requested;
|
|
13
|
+
|
|
14
|
+
const sessions = sessionStore.toArray();
|
|
15
|
+
if (sessions.length === 1) return sessions[0].name;
|
|
16
|
+
return requested;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function hasExplicitSessionFlag(req: DaemonRequest): boolean {
|
|
20
|
+
const value = (req.flags as CommandFlags | undefined)?.session;
|
|
21
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
22
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AppError } from '../utils/errors.ts';
|
|
2
|
+
import type { CommandFlags } from '../core/dispatch.ts';
|
|
3
|
+
import type { SessionState } from './types.ts';
|
|
4
|
+
|
|
5
|
+
export function assertSessionSelectorMatches(
|
|
6
|
+
session: SessionState,
|
|
7
|
+
flags?: CommandFlags,
|
|
8
|
+
): void {
|
|
9
|
+
if (!flags) return;
|
|
10
|
+
|
|
11
|
+
const mismatches: string[] = [];
|
|
12
|
+
const device = session.device;
|
|
13
|
+
|
|
14
|
+
if (flags.platform && flags.platform !== device.platform) {
|
|
15
|
+
mismatches.push(`--platform=${flags.platform}`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (flags.udid && (device.platform !== 'ios' || flags.udid !== device.id)) {
|
|
19
|
+
mismatches.push(`--udid=${flags.udid}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (flags.serial && (device.platform !== 'android' || flags.serial !== device.id)) {
|
|
23
|
+
mismatches.push(`--serial=${flags.serial}`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (mismatches.length === 0) return;
|
|
27
|
+
|
|
28
|
+
throw new AppError(
|
|
29
|
+
'INVALID_ARGS',
|
|
30
|
+
`Session "${session.name}" is bound to ${describeDevice(session)} and cannot be used with ${mismatches.join(', ')}. Use a different --session name or close this session first.`,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function describeDevice(session: SessionState): string {
|
|
35
|
+
const platform = session.device.platform;
|
|
36
|
+
const name = session.device.name.trim();
|
|
37
|
+
const id = session.device.id;
|
|
38
|
+
return `${platform} device "${name}" (${id})`;
|
|
39
|
+
}
|