nx_lib_serve_test.nx source
↩ module page · 68 lines · 2540 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 out: *u8 = sys_mmap(65536)
43
44 // serve one work == exact API JSON, straight from the sovereign store
45 let jl: i64 = nx_lib_serve_work("SRVT1" as *u8, out)
46 if jl < 0 { return 1 }
47 let want: *u8 = "{\"work_hk\":\"SRVT1\",\"title\":\"Sovereign Reader\",\"doi\":\"10.9/z\",\"published\":\"2026\",\"license\":\"cc-by\",\"abstract\":\"\",\"authors\":\"\"}" as *u8
48 if lsvt_eq(out, jl, want, ls_strlen(want)) != 1 { return 2 }
49
50 // not found -> -1 (no false hit)
51 if nx_lib_serve_work("SRVT_MISSING" as *u8, out) >= 0 { return 3 }
52
53 // the work-list serves the hk
54 let ll: i64 = nx_lib_serve_list(out)
55 if ll <= 0 { return 4 }
56 if lsvt_has(out, ll, "\"SRVT1\"" as *u8) != 1 { return 5 }
57
58 // JSON escaping: a quote in the title comes out backslash-escaped
59 let recq: *u8 = "A \"q\" title\t1\t2\t3" as *u8
60 nx_lib_store_put_work("SRVT2" as *u8, recq, ls_strlen(recq))
61 let jl2: i64 = nx_lib_serve_work("SRVT2" as *u8, out)
62 if jl2 < 0 { return 6 }
63 if lsvt_has_byte(out, jl2, 92) != 1 { return 7 }
64
65 let msg: *u8 = "nx_lib_serve: 7/7 sovereign serve-from-store assertions PASS (JSON read path, no SQLite/python)\n" as *u8
66 sys_write(1, msg, ls_strlen(msg))
67 return 0
68}