mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-20 15:13:47 +01:00
fix(browse): screenshot accepts --clip=x,y,w,h and --selector=<css>
Agent-generated commands often use the GNU `--flag=value` form, which threw "Unknown screenshot flag". Both value flags now accept it, through the same validation and conflict checks as the space-separated form. Adopts #1563. Co-authored-by: techcenter68 <75930234+techcenter68@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
c56a642f3b
commit
3e84751884
2 files changed
+33
-6
No files matched your search
@@ -447,15 +447,18 @@ export async function handleMetaCommand(
|
||||
const remaining: string[] = [];
|
||||
let flagSelector: string | undefined;
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--viewport') {
|
||||
const inline = /^(--clip|--selector)=(.*)$/s.exec(args[i]);
|
||||
const flag = inline ? inline[1] : args[i];
|
||||
const flagValue = () => (inline ? inline[2] : args[++i]);
|
||||
if (flag === '--viewport') {
|
||||
viewportOnly = true;
|
||||
} else if (args[i] === '--base64') {
|
||||
} else if (flag === '--base64') {
|
||||
base64Mode = true;
|
||||
} else if (args[i] === '--selector') {
|
||||
flagSelector = args[++i];
|
||||
} else if (flag === '--selector') {
|
||||
flagSelector = flagValue();
|
||||
if (!flagSelector) throw new Error('Usage: screenshot --selector <css> [path]');
|
||||
} else if (args[i] === '--clip') {
|
||||
const coords = args[++i];
|
||||
} else if (flag === '--clip') {
|
||||
const coords = flagValue();
|
||||
if (!coords) throw new Error('Usage: screenshot --clip x,y,w,h [path]');
|
||||
const parts = coords.split(',').map(Number);
|
||||
if (parts.length !== 4 || parts.some(isNaN))
|
||||
|
||||
@@ -790,6 +790,30 @@ describe('Visual', () => {
|
||||
}
|
||||
});
|
||||
|
||||
test('screenshot accepts --clip=x,y,w,h and --selector=<css>', async () => {
|
||||
await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
|
||||
const p = tmpp(`browse-test-clip-eq-${Date.now()}.png`);
|
||||
try {
|
||||
expect(await handleMetaCommand('screenshot', ['--clip=0,0,10,10', p], bm, async () => {})).toContain('Screenshot saved (clip 0,0,10,10)');
|
||||
const png = fs.readFileSync(p);
|
||||
expect([png.readUInt32BE(16), png.readUInt32BE(20)]).toEqual([10, 10]);
|
||||
expect(await handleMetaCommand('screenshot', ['--selector=#title', p], bm, async () => {})).toContain('Screenshot saved (element)');
|
||||
} finally {
|
||||
fs.rmSync(p, { force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('screenshot --clip= keeps the usage errors and the selector conflict', async () => {
|
||||
await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
|
||||
const run = (args: string[]) => handleMetaCommand('screenshot', args, bm, async () => {});
|
||||
await expect(run(['--clip='])).rejects.toThrow('Usage: screenshot --clip x,y,w,h');
|
||||
await expect(run(['--clip=1,2,3'])).rejects.toThrow('all must be numbers');
|
||||
await expect(run(['--selector='])).rejects.toThrow('Usage: screenshot --selector');
|
||||
await expect(run(['--clip=0,0,10,10', '#title'])).rejects.toThrow('Cannot use --clip with a selector/ref');
|
||||
await expect(run(['--selector=#title', '--clip=0,0,10,10'])).rejects.toThrow('Cannot use --clip with a selector/ref');
|
||||
await expect(run(['--viewport=1'])).rejects.toThrow('Unknown screenshot flag');
|
||||
});
|
||||
|
||||
test('screenshot unknown flag throws', async () => {
|
||||
await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user