// @vitest-environment jsdom
import { afterEach, describe, expect, it } from 'vitest';
import { PageSnapshotEngine } from '../engine';
afterEach(() => {
document.body.innerHTML = '';
});
/**
* Integration: capture realistic pages end-to-end through the engine
* (scope → walk → fold → budget). Verifies the P1 success criteria —
* a 100-row data table, with folding, stays within budget.
*/
describe('capture integration', () => {
it('captures a settings form sensibly and well under budget', () => {
document.body.innerHTML = `
Billing Settings
`;
const { payload, telemetry } = new PageSnapshotEngine().capture();
const json = JSON.stringify(payload.snapshot);
// Content is present, chrome is not.
expect(json).toContain('Billing Settings');
expect(json).toContain('Company');
expect(json).not.toContain('global nav');
// Comfortably small.
expect(payload.metadata.tokenEstimate).toBeLessThan(500);
expect(telemetry.degradation).toBe('none');
expect(telemetry.scopeTier).toBe('tier2-main');
});
it('captures a 100-row table within budget thanks to folding', () => {
const rows = Array.from(
{ length: 100 },
(_, i) => `
Client ${i}
client${i}@example.com
`,
).join('');
document.body.innerHTML = `
${rows}
`;
const { payload, telemetry } = new PageSnapshotEngine().capture();
// Folding fired — the 100 identical rows collapsed.
expect(telemetry.foldedCount).toBeGreaterThan(0);
// The whole snapshot stays within the default 8k budget.
expect(payload.metadata.tokenEstimate).toBeLessThan(8_000);
// The collapse marker is present.
expect(JSON.stringify(payload.snapshot)).toContain('collapsed');
});
it('a 100-row table WITHOUT folding would be far larger (sanity)', () => {
// Same table; confirm the folded result is dramatically smaller
// than the raw row count would imply (~100 rows × 3 cells).
const rows = Array.from(
{ length: 100 },
(_, i) => `
Client ${i}
x${i}
`,
).join('');
document.body.innerHTML = `
${rows}
`;
const { payload } = new PageSnapshotEngine().capture();
const interactiveAndText = JSON.stringify(payload.snapshot).length;
// Folded snapshot is small — nowhere near 100 rows of serialized CST.
expect(interactiveAndText).toBeLessThan(4_000);
});
});