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

import { resolveScope } from '../scope';

afterEach(() => {
  document.body.innerHTML = '';
});

/**
 * jsdom does not lay out elements, so geometry-based tiers (3 dialog
 * sizing, heuristic scoring) can only be smoke-tested. These tests
 * cover the structural tier selection — which is the part that matters
 * for ladder ordering.
 */
describe('resolveScope — tier ladder', () => {
  it('honors an explicit target selector first', () => {
    document.body.innerHTML = `<main id="m">content</main><div id="t">x</div>`;
    const scope = resolveScope({ strategy: 'container', targetSelector: '#t' });
    expect(scope.tier).toBe('explicit-selector');
    expect(scope.roots[0].id).toBe('t');
  });

  it("'full' strategy forces body", () => {
    document.body.innerHTML = `<main>content</main>`;
    const scope = resolveScope({ strategy: 'full' });
    expect(scope.tier).toBe('tier4-body');
    expect(scope.roots[0]).toBe(document.body);
  });

  it('Tier 1 — picks [data-ai-context] over <main>', () => {
    document.body.innerHTML = `
      <main>main content</main>
      <div data-ai-context>annotated region</div>
    `;
    const scope = resolveScope({ strategy: 'container' });
    expect(scope.tier).toBe('tier1-annotation');
    expect(scope.roots[0].dataset.aiContext).toBe('');
  });

  it('Tier 1 — ignores data-ai-context="exclude"', () => {
    document.body.innerHTML = `
      <main>main content</main>
      <div data-ai-context="exclude">excluded</div>
    `;
    const scope = resolveScope({ strategy: 'container' });
    // Annotation tier yields nothing usable → falls to <main>.
    expect(scope.tier).toBe('tier2-main');
  });

  it('Tier 2 — picks <main> when no annotation', () => {
    document.body.innerHTML = `
      <nav>navigation</nav>
      <main id="m">main content here</main>
    `;
    const scope = resolveScope({ strategy: 'container' });
    expect(scope.tier).toBe('tier2-main');
    expect(scope.roots[0].id).toBe('m');
  });

  it('Tier 2 — role="main" also counts', () => {
    document.body.innerHTML = `<div role="main" id="m">content</div>`;
    const scope = resolveScope({ strategy: 'container' });
    expect(scope.tier).toBe('tier2-main');
    expect(scope.roots[0].id).toBe('m');
  });

  it('Tier 4 — falls back to body when nothing else matches', () => {
    // No main, no annotation, no sized blocks (jsdom = zero geometry).
    document.body.innerHTML = `<span>just text</span>`;
    const scope = resolveScope({ strategy: 'container' });
    expect(scope.tier).toBe('tier4-body');
  });
});
