nx_lib_write_test.nx source
↩ module page · 57 lines · 2351 B
1// nx_lib_write_test.nx -- gate for the sovereign WRITE endpoint (POST /api/ingest).
2// Ingest a TSV body via lhd_ingest_response -> store + reindex -> the new work
3// is immediately GETtable and SEARCHable. Proves write->read over the router,
4// no socket needed. No python/sqlite. Exit = failed assertion #.
5import "nx_syscalls.nx"
6import "nx_lib_http.nx"
7
8func lwt_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(262144)
29
30 // POST /api/ingest a single new work
31 let body: *u8 = "W_NEWMR\tThe Mind Reader Protocol\t10.5/mr\t2026\tcc-by\n" as *u8
32 let n1: i64 = lhd_ingest_response(body, ls_strlen(body), out)
33 if lwt_has(out, n1, "HTTP/1.1 200 OK" as *u8) != 1 { return 1 }
34 if lwt_has(out, n1, "\"ingested\":1" as *u8) != 1 { return 2 }
35
36 // the new work is now GETtable
37 let p2: *u8 = "/api/work/W_NEWMR" as *u8
38 let n2: i64 = lhd_response(p2, ls_strlen(p2), out)
39 if lwt_has(out, n2, "\"title\":\"The Mind Reader Protocol\"" as *u8) != 1 { return 3 }
40
41 // and SEARCHable (index was rebuilt inside the ingest)
42 let p3: *u8 = "/api/search?q=reader" as *u8
43 let n3: i64 = lhd_response(p3, ls_strlen(p3), out)
44 if lwt_has(out, n3, "\"W_NEWMR\"" as *u8) != 1 { return 4 }
45
46 // multi-line ingest
47 let body2: *u8 = "W_A1\tAlpha One Study\t10.1/a\t2020\tcc0\nW_A2\tBeta Two Trial\t10.2/b\t2021\tcc0\n" as *u8
48 let n4: i64 = lhd_ingest_response(body2, ls_strlen(body2), out)
49 if lwt_has(out, n4, "\"ingested\":2" as *u8) != 1 { return 5 }
50 let p5: *u8 = "/api/work/W_A2" as *u8
51 let n5: i64 = lhd_response(p5, ls_strlen(p5), out)
52 if lwt_has(out, n5, "Beta Two Trial" as *u8) != 1 { return 6 }
53
54 let msg: *u8 = "nx_lib_write: 6/6 sovereign WRITE-endpoint PASS (POST /api/ingest -> store + reindex -> gettable + searchable; no python/sqlite)\n" as *u8
55 sys_write(1, msg, ls_strlen(msg))
56 return 0
57}