code wiki / (root) / nx_lib_serve_test.nx

nx_lib_serve_test.nx source

↩ module page · 80 lines · 3522 B

1// nx_lib_serve_test.nx -- gate for the sovereign query/serve layer. 2// Proves the program's READ PATH works from the sovereign store (not SQLite): 3// put a work -> serve it as the exact API JSON, not-found -> -1, list, and 4// JSON-escaping. Exit = failed assertion #. 5import "nx_syscalls.nx" 6import "nx_lib_serve.nx" 7 8func lsvt_eq(a: *u8, alen: i64, b: *u8, blen: i64) -> i64 { 9 if alen != blen { return 0 } 10 var i: i64 = 0 11 while i < alen { if a[i] != b[i] { return 0 } i = i + 1 } 12 return 1 13} 14func lsvt_has(hay: *u8, hayn: i64, needle: *u8) -> i64 { 15 let nn: i64 = ls_strlen(needle) 16 if nn == 0 { return 1 } 17 if hayn < nn { return 0 } 18 let last: i64 = hayn - nn 19 var i: i64 = 0 20 while i <= last { 21 var j: i64 = 0 22 var st: i64 = 0 23 while st == 0 { 24 if j >= nn { st = 2 } 25 if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } } 26 } 27 if st == 2 { return 1 } 28 i = i + 1 29 } 30 return 0 31} 32func lsvt_has_byte(buf: *u8, n: i64, b: i64) -> i64 { 33 var i: i64 = 0 34 while i < n { if (buf[i] as i64) == b { return 1 } i = i + 1 } 35 return 0 36} 37 38func main() -> i64 { 39 let rec: *u8 = "Sovereign Reader\t10.9/z\t2026\tcc-by" as *u8 40 nx_lib_store_put_work("SRVT1" as *u8, rec, ls_strlen(rec)) 41 42 let LSVT_OUTCAP: i64 = 65536 43 let out: *u8 = sys_mmap(LSVT_OUTCAP) 44 45 // serve one work == exact API JSON, straight from the sovereign store 46 let jl: i64 = nx_lib_serve_work("SRVT1" as *u8, out) 47 if jl < 0 { return 1 } 48 let want: *u8 = "{\"work_hk\":\"SRVT1\",\"title\":\"Sovereign Reader\",\"doi\":\"10.9/z\",\"published\":\"2026\",\"license\":\"cc-by\",\"abstract\":\"\",\"authors\":\"\"}" as *u8 49 if lsvt_eq(out, jl, want, ls_strlen(want)) != 1 { return 2 } 50 51 // not found -> -1 (no false hit) 52 if nx_lib_serve_work("SRVT_MISSING" as *u8, out) >= 0 { return 3 } 53 54 // the work-list serves the hk 55 let ll: i64 = nx_lib_serve_list(out, LSVT_OUTCAP) 56 if ll <= 0 { return 4 } 57 if lsvt_has(out, ll, "\"SRVT1\"" as *u8) != 1 { return 5 } 58 59 // neg-control-capacity-refusal: the writer must REFUSE rather than overrun a too-small buffer. 60 // Without this the capacity argument is decoration -- a parameter nothing ever exercises. 61 let tiny: *u8 = sys_mmap(LSVT_OUTCAP) 62 if nx_lib_serve_list(tiny, 4) >= 0 { return 14 } 63 64 // JSON escaping: a quote in the title comes out backslash-escaped 65 let recq: *u8 = "A \"q\" title\t1\t2\t3" as *u8 66 nx_lib_store_put_work("SRVT2" as *u8, recq, ls_strlen(recq)) 67 let jl2: i64 = nx_lib_serve_work("SRVT2" as *u8, out) 68 if jl2 < 0 { return 6 } 69 if lsvt_has_byte(out, jl2, 92) != 1 { return 7 } 70 71 // ⛔THE COUNT IS GONE ON PURPOSE. It read "7/7" while EIGHT assertions ran -- I added one and the 72 // hand-rolled literal silently lied, which is the exact drift gv_ctr exists to make impossible. 73 // ★★★★★A SUMMARY THAT RECITES A NUMBER THE CODE DOES NOT DERIVE GOES STALE ON THE NEXT EDIT, AND IT 74 // DRIFTS TOWARD UNDERSTATEMENT -- THE DIRECTION NOBODY AUDITS. Bumping it to 8/8 would re-arm the 75 // same trap; every assertion already states itself by returning its own index. OWED: migrate this 76 // test onto gv_check/gv_ctr so declared == executed BY CONSTRUCTION rather than by my memory. 77 let msg: *u8 = "nx_lib_serve: sovereign serve-from-store assertions PASS (JSON read path, capacity refusal, no SQLite/python)\n" as *u8 78 sys_write(1, msg, ls_strlen(msg)) 79 return 0 80}