// runner.el — acceptance tests for el_html_sanitize.
//
// Reads cases.json from the cwd, runs every case through
// el_html_sanitize, prints PASS/FAIL per case and a summary,
// and exits non-zero if any case failed.
//
// cases.json shape:
//   {
//     "allowlist": "<json string passed to el_html_sanitize>",
//     "cases": [
//       { "name": "...", "input": "...", "expected": "..." },
//       ...
//     ]
//   }

fn run_case(name: String, input: String, expected: String, allowlist: String, idx: Int) -> Int {
    let got: String = el_html_sanitize(input, allowlist)
    if str_eq(got, expected) {
        println("PASS  " + name)
        return 1
    } else {
        println("FAIL  " + name)
        println("      input:    " + input)
        println("      expected: " + expected)
        println("      got:      " + got)
        return 0
    }
}

fn main() -> Int {
    let raw: String = fs_read("cases.json")
    if str_eq(raw, "") {
        println("FATAL  could not read cases.json from cwd")
        return 2
    }
    let allowlist: String = json_get(raw, "allowlist")
    let cases_raw: String = json_get_raw(raw, "cases")
    let n: Int = json_array_len(cases_raw)
    let i: Int = 0
    let pass: Int = 0
    let fail: Int = 0
    while i < n {
        let case_obj: String = json_array_get(cases_raw, i)
        let cname: String = json_get(case_obj, "name")
        let cinp: String = json_get(case_obj, "input")
        let cexp: String = json_get(case_obj, "expected")
        let r: Int = run_case(cname, cinp, cexp, allowlist, i)
        if r == 1 {
            let pass = pass + 1
        } else {
            let fail = fail + 1
        }
        let i = i + 1
    }
    println("")
    println(int_to_str(pass) + " passed, " + int_to_str(fail) + " failed")
    if fail > 0 { return 1 }
    return 0
}
