fix: keep snapshot refs contiguous

This commit is contained in:
morluto 2026-03-12 19:06:42 +08:00
parent a4f6e3f67b
commit 7a693dc052
2 changed files with 18 additions and 2 deletions

View File

@ -168,8 +168,6 @@ export async function handleSnapshot(
// Compact filter: skip elements with no name and no inline content that aren't interactive
if (opts.compact && !isInteractive && !node.name && !node.children) continue;
// Assign ref
const ref = `e${refCounter++}`;
const indent = ' '.repeat(depth);
// Build Playwright locator
@ -207,6 +205,7 @@ export async function handleSnapshot(
}
if (!handle) continue;
const ref = `e${refCounter++}`;
refMap.set(ref, handle);
// Format output line

View File

@ -25,6 +25,14 @@ function extractRef(snapshot: string, predicate: (line: string) => boolean): str
return `@${refMatch![1]}`;
}
function extractRefNumbers(snapshot: string): number[] {
return snapshot
.split('\n')
.map((line) => line.match(/@e(\d+)/))
.filter((match): match is RegExpMatchArray => Boolean(match))
.map((match) => Number(match[1]));
}
beforeAll(async () => {
testServer = startTestServer(0);
baseUrl = testServer.url;
@ -113,6 +121,15 @@ describe('Snapshot', () => {
expect(result).toContain('[button]');
expect(result).toContain('Say "Hello"');
});
test('snapshot emits contiguous refs when skipped nodes are not materialized', async () => {
await handleWriteCommand('goto', [baseUrl + '/snapshot.html'], bm);
const result = await handleMetaCommand('snapshot', [], bm, shutdown);
const refs = extractRefNumbers(result);
expect(refs.length).toBeGreaterThan(0);
expect(refs).toEqual(Array.from({ length: refs.length }, (_, index) => index + 1));
});
});
// ─── Ref-Based Interaction ──────────────────────────────────────