Skip to content

Commit b7a7848

Browse files
committed
fix: reject unquoted trailing text after interaction selectors
press/click/longpress positionals like 'press text=Gesture lab' used to silently drop the leftover tokens and act on the truncated selector (text=Gesture), potentially hitting the wrong element. Reject non-empty splitSelectorFromArgs rest with INVALID_ARGS guidance that suggests the merged quoted form (text="Gesture lab"). Fill keeps consuming rest as its text payload; wait/is/replay-heal already handle rest explicitly.
1 parent 252e70b commit b7a7848

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎src/__tests__/cli-grammar.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,44 @@ test('interaction and fill grammar share ref, selector, and point parsing', () =
5454
});
5555
});
5656

57+
test('interaction selectors reject unquoted trailing text instead of dropping it', () => {
58+
assert.throws(
59+
() => readInputFromCli('press', ['text=Gesture', 'lab'], BASE_FLAGS),
60+
(err: any) => {
61+
assert.equal(err.code, 'INVALID_ARGS');
62+
assert.match(err.message, /unexpected extra arguments: "lab"/);
63+
assert.match(err.message, /text="Gesture lab"/);
64+
return true;
65+
},
66+
);
67+
assert.throws(
68+
() => readInputFromCli('click', ['role=button', 'text=Sign', 'in'], BASE_FLAGS),
69+
(err: any) => {
70+
assert.equal(err.code, 'INVALID_ARGS');
71+
assert.match(err.message, /unexpected extra arguments: "in"/);
72+
assert.match(err.message, /role=button text="Sign in"/);
73+
return true;
74+
},
75+
);
76+
assert.throws(
77+
() => readInputFromCli('longpress', ['text=Gesture', 'lab'], BASE_FLAGS),
78+
(err: any) => {
79+
assert.equal(err.code, 'INVALID_ARGS');
80+
assert.match(err.message, /unexpected extra arguments: "lab"/);
81+
return true;
82+
},
83+
);
84+
85+
// Quoted selector values and fill's trailing text payload keep working.
86+
assert.deepEqual(readInputFromCli('press', ['text="Gesture lab"'], BASE_FLAGS).target, {
87+
kind: 'selector',
88+
selector: 'text="Gesture lab"',
89+
});
90+
const fillAfterSelector = readInputFromCli('fill', ['id=email', '[email protected]'], BASE_FLAGS);
91+
assert.deepEqual(fillAfterSelector.target, { kind: 'selector', selector: 'id=email' });
92+
assert.equal(fillAfterSelector.text, '[email protected]');
93+
});
94+
5795
test('bare snapshot refs without @ prefix throw helpful error', () => {
5896
assert.throws(
5997
() => readInputFromCli('click', ['e3'], BASE_FLAGS),

‎src/core/interaction-positionals.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ export function readInteractionTargetFromPositionals(
2727
);
2828
}
2929
const selectorArgs = splitSelectorFromArgs(positionals);
30-
if (selectorArgs) return { selector: selectorArgs.selectorExpression };
30+
if (selectorArgs) {
31+
if (selectorArgs.rest.length > 0) {
32+
throw new AppError(
33+
'INVALID_ARGS',
34+
formatSelectorTrailingArgsFailure(positionals, selectorArgs),
35+
);
36+
}
37+
return { selector: selectorArgs.selectorExpression };
38+
}
3139
return readPointTarget(positionals);
3240
}
3341

@@ -85,6 +93,29 @@ function formatTargetParseFailure(positionals: string[]): string {
8593
return `${base} To match by visible text, use text=${quoteSelectorValue(raw)} (or label=/id=/role=), or pass an @ref from snapshot.`;
8694
}
8795

96+
function formatSelectorTrailingArgsFailure(
97+
positionals: string[],
98+
selectorArgs: { selectorExpression: string; rest: string[] },
99+
): string {
100+
const base = `Selector ${selectorArgs.selectorExpression} is followed by unexpected extra arguments: "${selectorArgs.rest.join(' ')}".`;
101+
const suggestion = mergeRestIntoSelectorValue(positionals, selectorArgs) ?? 'text="Sign in"';
102+
return `${base} Selector values with spaces need quotes, e.g. ${suggestion}.`;
103+
}
104+
105+
function mergeRestIntoSelectorValue(
106+
positionals: string[],
107+
selectorArgs: { selectorExpression: string; rest: string[] },
108+
): string | undefined {
109+
const boundary = positionals.length - selectorArgs.rest.length;
110+
const lastSelectorToken = positionals[boundary - 1];
111+
if (lastSelectorToken === undefined) return undefined;
112+
const equalsIdx = lastSelectorToken.indexOf('=');
113+
if (equalsIdx <= 0) return undefined;
114+
const mergedValue = [lastSelectorToken.slice(equalsIdx + 1), ...selectorArgs.rest].join(' ');
115+
const mergedToken = `${lastSelectorToken.slice(0, equalsIdx + 1)}${quoteSelectorValue(mergedValue)}`;
116+
return [...positionals.slice(0, boundary - 1), mergedToken].join(' ');
117+
}
118+
88119
function quoteSelectorValue(value: string): string {
89120
return `"${value.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`;
90121
}

0 commit comments

Comments
 (0)