//! Regression test: CSS internal `url()` must resolve against the CSS
//! file's own directory, not the HTML document's directory.
//!
//! Prior to the `FulgurNetProvider` migration, CSS was inlined into the
//! DOM so url() tokens resolved against the HTML. Now blitz/stylo
//! resolves them against each stylesheet's `source_url` (UrlExtraData).
//! This test pins that behaviour so a future rewrite does not regress.

use std::fs;

use fulgur::{Engine, PageSize};
use tempfile::tempdir;

#[test]
fn css_internal_url_resolves_against_stylesheet_directory() {
    let dir = tempdir().unwrap();
    let root = dir.path();

    fs::create_dir(root.join("css")).unwrap();

    // Minimal 1x1 transparent PNG.
    let png: [u8; 67] = [
        0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44,
        0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F,
        0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00,
        0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49,
        0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
    ];
    // Only-reachable-from-CSS-dir target:
    fs::write(root.join("css/only-reachable-from-css-dir.png"), png).unwrap();
    fs::write(
        root.join("css/style.css"),
        "body { background-image: url(./only-reachable-from-css-dir.png); }\n",
    )
    .unwrap();

    let html = r#"
        <!DOCTYPE html>
        <html><head>
            <link rel="stylesheet" href="css/style.css">
        </head><body><p>x</p></body></html>
    "#;

    let engine = Engine::builder()
        .page_size(PageSize::A4)
        .base_path(root)
        .build();
    let pdf = engine.render(html).expect("render must succeed");
    // Note: fulgur does not fail when an image resource is unreachable,
    // so this is a soft pin: it catches "renderer panics on CSS-relative
    // url()" regressions but NOT "url resolved against the wrong base"
    // ones. A stronger visual assertion is deferred to a later task.
    assert!(!pdf.is_empty(), "PDF bytes should be produced");
    assert!(pdf.starts_with(b"%PDF"), "output should be a PDF");
}
