code wiki / (root) / nx_lib_page_test.nx

nx_lib_page_test.nx source

↩ module page · 74 lines · 3376 B

1// nx_lib_page_test.nx -- gate for the sovereign zero-JS HTML frontend. 2// Ingest works (one with an '&' title) -> render search page + work detail -> 3// correct HTML, matching links, and & < > ESCAPED (the &amp; bug fixed in .nx). 4import "nx_syscalls.nx" 5import "nx_lib_ingest.nx" 6import "nx_lib_ingest_tsv.nx" // nx_lib_ingest_buf 7import "nx_lib_page.nx" 8 9func lpt_has(hay: *u8, hayn: i64, needle: *u8) -> i64 { 10 let nn: i64 = ls_strlen(needle) 11 if nn == 0 { return 1 } 12 if hayn < nn { return 0 } 13 let last: i64 = hayn - nn 14 var i: i64 = 0 15 while i <= last { 16 var j: i64 = 0 17 var st: i64 = 0 18 while st == 0 { 19 if j >= nn { st = 2 } 20 if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } } 21 } 22 if st == 2 { return 1 } 23 i = i + 1 24 } 25 return 0 26} 27 28func main() -> i64 { 29 // a title with '&' to prove HTML escaping is correct (renders &amp;, not raw &) 30 let tsv: *u8 = "W_AMP\tCats & Dogs Study\t10.9/amp\t2026\tcc-by\nW_QM\tQuantum Metrology Advances\t10.3/qm\t2024\tcc0\n" as *u8 31 nx_lib_ingest_buf(tsv, ls_strlen(tsv)) 32 33 let out: *u8 = sys_mmap(1200000) 34 35 // home with a query -> HTML page, matching link, ESCAPED title 36 let q: *u8 = "dogs" as *u8 37 let n1: i64 = lhd_page_home(q, ls_strlen(q), out) 38 if lpt_has(out, n1, "Content-Type: text/html" as *u8) != 1 { return 1 } 39 if lpt_has(out, n1, "<a href=\"/work/W_AMP\">" as *u8) != 1 { return 2 } 40 if lpt_has(out, n1, "Cats &amp; Dogs Study" as *u8) != 1 { return 3 } 41 // the raw unescaped ampersand+space must NOT appear in the rendered title 42 if lpt_has(out, n1, "Cats & Dogs" as *u8) == 1 { return 4 } 43 44 // a query that matches nothing -> "No matches" 45 let q2: *u8 = "zzznope" as *u8 46 let n2: i64 = lhd_page_home(q2, ls_strlen(q2), out) 47 if lpt_has(out, n2, "No matches" as *u8) != 1 { return 5 } 48 49 // empty query -> browse the whole catalog (lists works as links) 50 let n3: i64 = lhd_page_home(("" as *u8), 0, out) 51 if lpt_has(out, n3, "Browse the catalog" as *u8) != 1 { return 6 } 52 if lpt_has(out, n3, "<li><a href=\"/work/" as *u8) != 1 { return 6 } 53 54 // work detail -> escaped title + doi 55 let n4: i64 = lhd_page_work("W_AMP" as *u8, 5, out) 56 if lpt_has(out, n4, "<h1>Cats &amp; Dogs Study</h1>" as *u8) != 1 { return 7 } 57 if lpt_has(out, n4, "10.9/amp" as *u8) != 1 { return 8 } 58 if lpt_has(out, n4, "back to search" as *u8) != 1 { return 9 } 59 60 // missing work -> Not found page 61 let n5: i64 = lhd_page_work("NOPEXX" as *u8, 6, out) 62 if lpt_has(out, n5, "Not found" as *u8) != 1 { return 10 } 63 64 // a work WITH an abstract -> the detail page renders it (the reader body) 65 let tsv2: *u8 = "W_ABS\tGraph Theory Primer\t10.1/g\t2020\tcc-by\tA study of connected components in sparse graphs.\tPaul Erdos, Alfred Renyi\n" as *u8 66 nx_lib_ingest_buf(tsv2, ls_strlen(tsv2)) 67 let n6b: i64 = lhd_page_work("W_ABS" as *u8, 5, out) 68 if lpt_has(out, n6b, "connected components in sparse graphs" as *u8) != 1 { return 11 } 69 if lpt_has(out, n6b, "Paul Erdos, Alfred Renyi" as *u8) != 1 { return 12 } 70 71 let msg: *u8 = "nx_lib_page: 12/12 sovereign zero-JS frontend PASS (browse + search + reader-with-abstract+authors, HTML-escaped; no app.js/leaflet/python)\n" as *u8 72 sys_write(1, msg, ls_strlen(msg)) 73 return 0 74}