// ── tests/test_api_define_process.el ─────────────────────────────────────────
//
// Test the handle_api_define_process read-back fix (neuron-api.el).
//
// Bug: handle_api_define_process was the only write handler that did NOT call
// api_persisted() after the write, returning {"id":"...","ok":true} even when
// the engram write failed (hallucinated save).
//
// Fix: added `if !api_persisted(id) { return api_not_persisted(id) }` before
// the return, consistent with all sibling handlers (remember, capture_knowledge,
// evolve_knowledge, promote_knowledge, node_create).
//
// Tests:
//   1. define_process returns ok==true and id resolves via engram_get_node_json.
//   2. Missing content returns the standard error.
//   3. Unnamed process uses default label and still persists.
// ──────────────────────────────────────────────────────────────────────────────

import "../neuron-api.el"

let pass_count: Int = 0
let fail_count: Int = 0

fn assert_eq(label: String, got: String, expected: String) -> Void {
    if str_eq(got, expected) {
        let pass_count = pass_count + 1
        println("  PASS: " + label)
    } else {
        let fail_count = fail_count + 1
        println("  FAIL: " + label)
        println("    got:      " + got)
        println("    expected: " + expected)
    }
}

fn assert_not_eq(label: String, got: String, not_want: String) -> Void {
    if str_eq(got, not_want) {
        let fail_count = fail_count + 1
        println("  FAIL: " + label + " (got: " + got + ", should differ)")
    } else {
        let pass_count = pass_count + 1
        println("  PASS: " + label)
    }
}

fn assert_contains(label: String, haystack: String, needle: String) -> Void {
    if str_contains(haystack, needle) {
        let pass_count = pass_count + 1
        println("  PASS: " + label)
    } else {
        let fail_count = fail_count + 1
        println("  FAIL: " + label)
        println("    missing '" + needle + "' in: " + haystack)
    }
}

// ── Section 1: define_process — happy path with read-back ────────────────────

println("")
println("1. handle_api_define_process — write then verify id resolves")

let proc_body: String = "{\"content\":\"Test process: run step A, then step B, then step C.\",\"name\":\"test-process-guard\"}"
let proc_result: String = handle_api_define_process(proc_body)

let proc_ok: String = json_get(proc_result, "ok")
let proc_id: String = json_get(proc_result, "id")

assert_eq("define_process -> ok==true", proc_ok, "true")
assert_not_eq("define_process -> id is non-empty", proc_id, "")

let node_json: String = engram_get_node_json(proc_id)
let node_status: String = if str_eq(node_json, "") { "empty" } else {
    if str_eq(node_json, "null") { "null" } else { "ok" }
}
assert_eq("define_process -> node read-back resolves (not empty/null)", node_status, "ok")
assert_contains("define_process -> node content contains process text", node_json, "Test process")

// ── Section 2: define_process — missing content returns error ────────────────

println("")
println("2. handle_api_define_process — missing content returns error")

let no_content_body: String = "{\"name\":\"nameless\"}"
let no_content_result: String = handle_api_define_process(no_content_body)
let no_content_error: String = json_get(no_content_result, "error")
assert_eq("missing content -> error is 'content is required'", no_content_error, "content is required")

// ── Section 3: define_process — unnamed process gets default label ────────────

println("")
println("3. handle_api_define_process — unnamed process writes and read-back succeeds")

let unnamed_body: String = "{\"content\":\"Unnamed test process for coverage.\"}"
let unnamed_result: String = handle_api_define_process(unnamed_body)
let unnamed_ok: String = json_get(unnamed_result, "ok")
let unnamed_id: String = json_get(unnamed_result, "id")
assert_eq("unnamed process -> ok==true", unnamed_ok, "true")
assert_not_eq("unnamed process -> id non-empty", unnamed_id, "")

let unnamed_node: String = engram_get_node_json(unnamed_id)
let unnamed_status: String = if str_eq(unnamed_node, "") { "empty" } else {
    if str_eq(unnamed_node, "null") { "null" } else { "ok" }
}
assert_eq("unnamed process -> node read-back ok", unnamed_status, "ok")

// ── Summary ───────────────────────────────────────────────────────────────────

println("")
println("api_define_process tests: " + int_to_str(pass_count) + " passed, " + int_to_str(fail_count) + " failed")
