// tests/test_imprint.el
// Comprehensive test suite for imprint.el (Layer 3 boundary).
//
// El has no native test framework. Tests are plain El programs that
// call functions, compare results, and print PASS/FAIL via println.
// Each test is a fn returning Int: 0 = pass, 1 = fail.
// run_all() drives them and returns a final summary line.
//
// Syntax rules observed:
//   - No Bool type annotation — inference only
//   - No && / || — nested if/else used instead
//   - No unary ! — inverted with if/else
//   - No closures or lambdas

import "imprint.elh"

// ---------------------------------------------------------------------------
// helpers
// ---------------------------------------------------------------------------

fn assert_eq(label: String, got: String, want: String) -> Int {
    if str_eq(got, want) {
        println("PASS  " + label)
        return 0
    }
    println("FAIL  " + label + "  got=" + got + "  want=" + want)
    return 1
}

fn assert_not_eq(label: String, got: String, not_want: String) -> Int {
    if str_eq(got, not_want) {
        println("FAIL  " + label + "  got=" + got + "  (should differ)")
        return 1
    }
    println("PASS  " + label)
    return 0
}

fn assert_contains(label: String, haystack: String, needle: String) -> Int {
    if str_contains(haystack, needle) {
        println("PASS  " + label)
        return 0
    }
    println("FAIL  " + label + "  value=" + haystack + "  missing=" + needle)
    return 1
}

fn assert_not_contains(label: String, haystack: String, needle: String) -> Int {
    if str_contains(haystack, needle) {
        println("FAIL  " + label + "  value=" + haystack + "  unexpected=" + needle)
        return 1
    }
    println("PASS  " + label)
    return 0
}

fn assert_not_empty(label: String, got: String) -> Int {
    if str_eq(got, "") {
        println("FAIL  " + label + "  got empty string")
        return 1
    }
    println("PASS  " + label)
    return 0
}

// ---------------------------------------------------------------------------
// TEST 1
// imprint_current() with no prior state should return "base".
// We cannot guarantee a clean state across runs so we call imprint_unload()
// first to normalise, then check.
// ---------------------------------------------------------------------------
fn test_01_current_after_unload_is_base() -> Int {
    imprint_unload()
    let id: String = imprint_current()
    return assert_eq("01 imprint_current after unload == base", id, "base")
}

// ---------------------------------------------------------------------------
// TEST 2
// imprint_unload() then imprint_current() always returns "base".
// Calling unload twice must be idempotent.
// ---------------------------------------------------------------------------
fn test_02_unload_idempotent() -> Int {
    imprint_unload()
    imprint_unload()
    let id: String = imprint_current()
    return assert_eq("02 double-unload still base", id, "base")
}

// ---------------------------------------------------------------------------
// TEST 3
// imprint_load() with a nonexistent ID must return ok==false and an error
// message that mentions the requested ID.
// We use a UUID-like name that will never exist in the engram.
// ---------------------------------------------------------------------------
fn test_03_load_nonexistent_returns_ok_false() -> Int {
    let result: String = imprint_load("__test_ghost_imprint_xyz__")
    let ok_field: String = json_get(result, "ok")
    let fails: Int = 0
    let fails = fails + assert_eq("03a load nonexistent ok==false", ok_field, "false")
    let fails = fails + assert_contains("03b load nonexistent error mentions id", result, "__test_ghost_imprint_xyz__")
    return if fails > 0 { 1 } else { 0 }
}

// ---------------------------------------------------------------------------
// TEST 4
// json_get on imprint_load result should always return the "ok" field.
// Both ok=true and ok=false payloads must carry the field.
// We test the miss case (guaranteed) for the field's presence.
// ---------------------------------------------------------------------------
fn test_04_load_result_has_ok_field() -> Int {
    let result: String = imprint_load("__test_field_check__")
    let ok_field: String = json_get(result, "ok")
    return assert_not_empty("04 load result contains ok field", ok_field)
}

// ---------------------------------------------------------------------------
// TEST 5
// imprint_respond() with imprint_id == "base" must return input unchanged.
// The base path is the identity function — no annotation is added.
// ---------------------------------------------------------------------------
fn test_05_respond_base_passthrough() -> Int {
    let input: String = "Hello from the base layer."
    let output: String = imprint_respond(input, "base")
    return assert_eq("05 respond with base id == passthrough", output, input)
}

// ---------------------------------------------------------------------------
// TEST 6
// imprint_respond() with imprint_id == "" (empty string) must also return
// input unchanged — empty string is treated as base.
// ---------------------------------------------------------------------------
fn test_06_respond_empty_id_passthrough() -> Int {
    let input: String = "Test input for empty imprint_id."
    let output: String = imprint_respond(input, "")
    return assert_eq("06 respond with empty id == passthrough", output, input)
}

// ---------------------------------------------------------------------------
// TEST 7
// imprint_respond() with an unknown imprint_id (node not in engram) must
// fall back gracefully and return input unchanged.
// The spec says: never hard-fail at L3 — graceful fallback to base.
// ---------------------------------------------------------------------------
fn test_07_respond_unknown_id_graceful_fallback() -> Int {
    let input: String = "Graceful fallback test payload."
    let output: String = imprint_respond(input, "__no_such_imprint_ever__")
    return assert_eq("07 respond unknown id graceful fallback == passthrough", output, input)
}

// ---------------------------------------------------------------------------
// TEST 8
// After imprint_unload(), imprint_respond should produce base behaviour.
// We call respond with the just-cleared state ID ("base") to confirm
// the unload/respond pipeline produces the identity transform.
// ---------------------------------------------------------------------------
fn test_08_respond_after_unload_is_passthrough() -> Int {
    imprint_unload()
    let current: String = imprint_current()
    let input: String = "Post-unload response passthrough check."
    let output: String = imprint_respond(input, current)
    return assert_eq("08 respond after unload == passthrough", output, input)
}

// ---------------------------------------------------------------------------
// TEST 9
// imprint_surface_knowledge() must return a String (not crash, not empty
// in a way that signals an error code). We test both base and named paths.
// For "base" the query is passed directly; for a named imprint the query
// is scoped but the return must still be a String.
// ---------------------------------------------------------------------------
fn test_09_surface_knowledge_returns_string() -> Int {
    let result_base: String = imprint_surface_knowledge("test query", "base")
    // Must be a String — "" or "[]" is valid (no matching nodes), but the
    // call must not return an error token. We check it is not the literal
    // string "error" to catch any error-signalling convention.
    let fails: Int = 0
    let fails = fails + assert_not_eq("09a surface_knowledge base != error", result_base, "error")
    let result_named: String = imprint_surface_knowledge("test query", "demo-imprint")
    let fails = fails + assert_not_eq("09b surface_knowledge named != error", result_named, "error")
    // Scoped query must embed the domain scope string
    // (test indirectly: the scoped call does not crash and returns a String)
    let fails = fails + assert_not_eq("09c surface_knowledge named != crash sentinel", result_named, "CRASH")
    return if fails > 0 { 1 } else { 0 }
}

// ---------------------------------------------------------------------------
// TEST 10
// imprint_surface_memory_read() must return a String for any query.
// This is a read-only engram search — it must never write.
// We check the return is not an error sentinel and is a valid String.
// ---------------------------------------------------------------------------
fn test_10_surface_memory_read_returns_string() -> Int {
    let result: String = imprint_surface_memory_read("soul memory test")
    let fails: Int = 0
    let fails = fails + assert_not_eq("10a surface_memory_read != error", result, "error")
    let fails = fails + assert_not_eq("10b surface_memory_read != crash", result, "CRASH")
    return if fails > 0 { 1 } else { 0 }
}

// ---------------------------------------------------------------------------
// TEST 11
// imprint_surface_knowledge() with empty imprint_id uses the base path
// (no domain scoping) — must behave identically to base.
// ---------------------------------------------------------------------------
fn test_11_surface_knowledge_empty_id_equals_base() -> Int {
    let base_result: String = imprint_surface_knowledge("neuron layer test", "base")
    let empty_result: String = imprint_surface_knowledge("neuron layer test", "")
    return assert_eq("11 surface_knowledge empty id == base id", empty_result, base_result)
}

// ---------------------------------------------------------------------------
// TEST 12
// imprint_respond() must NOT annotate when imprint_id is "base" — the
// "[imprint:" marker must be absent in the output.
// ---------------------------------------------------------------------------
fn test_12_respond_base_no_annotation() -> Int {
    let input: String = "No annotation expected."
    let output: String = imprint_respond(input, "base")
    return assert_not_contains("12 respond base has no imprint annotation", output, "[imprint:")
}

// ---------------------------------------------------------------------------
// TEST 13
// imprint_load() with empty-string ID must return ok==false.
// An empty ID is not a valid imprint identifier.
// ---------------------------------------------------------------------------
fn test_13_load_empty_id_returns_ok_false() -> Int {
    let result: String = imprint_load("")
    let ok_field: String = json_get(result, "ok")
    return assert_eq("13 load empty id ok==false", ok_field, "false")
}

// ---------------------------------------------------------------------------
// TEST 14
// After a failed imprint_load(), imprint_current() must still return "base"
// — a failed load must leave state untouched.
// ---------------------------------------------------------------------------
fn test_14_failed_load_does_not_mutate_state() -> Int {
    imprint_unload()
    let discard: String = imprint_load("__nonexistent_for_state_test__")
    let id: String = imprint_current()
    return assert_eq("14 failed load leaves state as base", id, "base")
}

// ---------------------------------------------------------------------------
// run_all — executes every test and prints a summary.
// Returns total failure count as Int.
// ---------------------------------------------------------------------------
fn run_all() -> Int {
    println("=== imprint.el test suite ===")
    let total: Int = 0
    let failed: Int = 0

    let failed = failed + test_01_current_after_unload_is_base()
    let failed = failed + test_02_unload_idempotent()
    let failed = failed + test_03_load_nonexistent_returns_ok_false()
    let failed = failed + test_04_load_result_has_ok_field()
    let failed = failed + test_05_respond_base_passthrough()
    let failed = failed + test_06_respond_empty_id_passthrough()
    let failed = failed + test_07_respond_unknown_id_graceful_fallback()
    let failed = failed + test_08_respond_after_unload_is_passthrough()
    let failed = failed + test_09_surface_knowledge_returns_string()
    let failed = failed + test_10_surface_memory_read_returns_string()
    let failed = failed + test_11_surface_knowledge_empty_id_equals_base()
    let failed = failed + test_12_respond_base_no_annotation()
    let failed = failed + test_13_load_empty_id_returns_ok_false()
    let failed = failed + test_14_failed_load_does_not_mutate_state()

    let total = 14
    let passed: Int = total - failed
    println("=== " + int_to_str(passed) + "/" + int_to_str(total) + " passed ===")
    return failed
}
