Files
neuron/tests/test_api_define_process.el
will.anderson 8b692e4666
Neuron Soul CI / build (pull_request) Failing after 13m22s
fix/test: PR #21 review — guard, safety Bell, api write-back, temp paths
fix(soul): add HTTP-engram guard to safe_to_seed — when ENGRAM_URL is set
the HTTP Engram owns persistence; genesis must never save to local snapshot
regardless of node counts (was: guard_disk forced to empty string, making
the ratio check vacuously true and allowing init_soul_edges+engram_save).

fix(soul): use multiplication form for ratio guard — node_count * 16000 <
disk_len avoids floor-division truncation that underestimated boundary files
(250KB / 16000 = 15.6, floors to 15; a 15-node graph wrongly passed old guard).

fix(chat): add safety_augment_system to handle_chat_as_soul,
handle_dharma_room_turn, and handle_dharma_room_turn_agentic — all three
called the LLM without Hard Bell evaluation, leaving users in dharma rooms
without crisis resource routing.

fix(neuron-api): add api_persisted read-back to handle_api_define_process —
was the only write handler that returned ok:true without verifying the node
was actually written to engram.

fix(routes): unique temp file path in connectd_post — replaces fixed
/tmp/neuron-connectors-req.json with a timestamped path to prevent
collision if concurrency is added or two soul instances share a machine.

test: add tests/test_bell_safety.el — covers safety_detect_bell_level
(none/soft/hard), safety_classify_hard_bell (abuse/self_harm routing),
safety_normalize (smart-quote), safety_augment_system, and
handle_safety_contact_post (validation + read-back).

test: add tests/test_soul_guard.el — pure-function logic tests for the
safe_to_seed predicate: 200KB boundary, 47MB/63-node clobber scenario,
HTTP-engram mode, multiplication vs division truncation at 250KB.

test: add tests/test_api_define_process.el — verifies the define_process
write is read-back verified after the fix.
2026-06-17 13:19:15 -05:00

110 lines
4.8 KiB
EmacsLisp

// 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")