// @vitest-environment jsdom
import { describe, expect, it } from 'vitest';

import { PageSnapshotEngine } from '../engine';
import { CST_SCHEMA_VERSION } from '../cst/payload';

describe('PageSnapshotEngine (P0 skeleton)', () => {
  it('returns a schema-valid empty snapshot', () => {
    const { payload, telemetry } = new PageSnapshotEngine({
      scope: 'full',
    }).capture();

    expect(payload.snapshot.type).toBe('root');
    expect(payload.snapshot.children).toEqual([]);
    expect(payload.title).toBe(document.title);
    expect(payload.url).toBe(window.location.href);
    expect(payload.route).toBe(window.location.pathname);

    expect(payload.metadata.schemaVersion).toBe(CST_SCHEMA_VERSION);
    expect(payload.metadata.representation).toBe('CST');
    expect(payload.metadata.contentHash).toMatch(/^[0-9a-f]{8}$/);
    expect(payload.metadata.tokenEstimate).toBeGreaterThan(0);
    expect(payload.metadata.redactedCount).toBe(0);
    expect(payload.metadata.foldedCount).toBe(0);

    expect(telemetry.nodesVisited).toBe(0);
    expect(telemetry.executionTimeMs).toBeGreaterThanOrEqual(0);
  });

  it('produces a stable content hash across repeated captures', () => {
    const engine = new PageSnapshotEngine();
    const a = engine.capture().payload.metadata.contentHash;
    const b = engine.capture().payload.metadata.contentHash;
    expect(a).toBe(b);
  });
});
