nx_lib_ingest_json_test.nx source
↩ module page · 55 lines · 2401 B
1// nx_lib_ingest_json_test.nx -- gate the deterministic ingest tail:
2// HTTP response -> strip headers -> parse work JSON -> store -> gettable.
3// No network, no python. Exit = failed assertion #.
4import "nx_syscalls.nx"
5import "nx_lib_ingest_json.nx"
6import "nx_lib_serve.nx"
7
8func ij_has(hay: *u8, hayn: i64, needle: *u8) -> i64 {
9 let nn: i64 = ls_strlen(needle)
10 if nn == 0 { return 1 }
11 if hayn < nn { return 0 }
12 let last: i64 = hayn - nn
13 var i: i64 = 0
14 while i <= last {
15 var j: i64 = 0
16 var st: i64 = 0
17 while st == 0 {
18 if j >= nn { st = 2 }
19 if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } }
20 }
21 if st == 2 { return 1 }
22 i = i + 1
23 }
24 return 0
25}
26
27func main() -> i64 {
28 let out: *u8 = sys_mmap(65536)
29
30 // a full HTTP response carrying a work JSON body
31 let resp: *u8 = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"title\":\"Ingested Over TLS\",\"doi\":\"10.7/tls\",\"published\":\"2026\",\"license\":\"cc-by\",\"authors\":\"Nishi Team\"}" as *u8
32 let n: i64 = nx_lib_ingest_json_response(resp, ls_strlen(resp), "W_TLSING" as *u8, 8)
33 if n <= 0 { return 1 }
34 let jn: i64 = nx_lib_serve_work("W_TLSING" as *u8, out)
35 if jn < 0 { return 2 }
36 if ij_has(out, jn, "\"title\":\"Ingested Over TLS\"" as *u8) != 1 { return 3 }
37 if ij_has(out, jn, "\"authors\":\"Nishi Team\"" as *u8) != 1 { return 4 }
38 if ij_has(out, jn, "\"published\":\"2026\"" as *u8) != 1 { return 5 }
39
40 // non-JSON body must be REJECTED (negative), never stored as garbage
41 let bad: *u8 = "HTTP/1.1 200 OK\r\n\r\nnot a json object at all" as *u8
42 let bn: i64 = nx_lib_ingest_json_response(bad, ls_strlen(bad), "W_BADIJ" as *u8, 7)
43 if bn >= 0 { return 6 }
44
45 // headerless body (raw JSON, no CRLFCRLF) also ingests
46 let raw: *u8 = "{\"title\":\"Headerless Work\",\"doi\":\"10.0/h\"}" as *u8
47 let rn: i64 = nx_lib_ingest_json_response(raw, ls_strlen(raw), "W_HDRLESS" as *u8, 9)
48 if rn <= 0 { return 7 }
49 let jn2: i64 = nx_lib_serve_work("W_HDRLESS" as *u8, out)
50 if ij_has(out, jn2, "Headerless Work" as *u8) != 1 { return 8 }
51
52 let msg: *u8 = "nx_lib_ingest_json: 8/8 sovereign fetch-tail PASS (HTTP response -> parse -> store -> gettable; bad body rejected; no python)\n" as *u8
53 sys_write(1, msg, ls_strlen(msg))
54 return 0
55}