//! Golden semantic snapshot tests for Milestone 38.

use index_extract::{ExtractFormat, extract_document};
use index_renderer::{RenderOptions, render_document};
use index_transformer::{Transformer, state::Empty};

fn transform(html: &str) -> index_core::IndexDocument {
    Transformer::<Empty>::new()
        .fetched(html)
        .parse()
        .extract()
        .transform()
        .into_document()
}

fn assert_snapshot(actual: &str, expected: &str) {
    assert_eq!(actual.trim_end(), expected.trim_end());
}

#[test]
fn representative_article_outputs_match_golden_snapshots() {
    let document = transform(include_str!("fixtures/article.html"));

    assert_snapshot(
        &render_document(&document, RenderOptions::default()),
        include_str!("golden/article.rendered.txt"),
    );
    assert_snapshot(
        &extract_document(&document, ExtractFormat::Markdown),
        include_str!("golden/article.markdown"),
    );
    assert_snapshot(
        &extract_document(&document, ExtractFormat::Json),
        include_str!("golden/article.json"),
    );
}

#[test]
fn failure_document_output_matches_golden_snapshot() {
    let document = transform(include_str!("fixtures/empty.html"));

    assert_snapshot(
        &extract_document(&document, ExtractFormat::Markdown),
        include_str!("golden/failure.markdown"),
    );
}

#[test]
fn golden_outputs_are_deterministic_across_repeated_runs() {
    let first = transform(include_str!("fixtures/article.html"));
    let second = transform(include_str!("fixtures/article.html"));

    assert_eq!(
        extract_document(&first, ExtractFormat::Json),
        extract_document(&second, ExtractFormat::Json)
    );
    assert_eq!(
        render_document(&first, RenderOptions::default()),
        render_document(&second, RenderOptions::default())
    );
}
