code wiki / _hdl_build / nx_epub_book_toc_gate.nx

nx_epub_book_toc_gate.nx source

↩ module page · 88 lines · 5474 B

1// nx_epub_book_toc_gate.nx -- END-TO-END liar-kill gate for R-TOC: proves nx_epub_book locates the EPUB nav 2// document and emits the correct HIERARCHICAL toc[] in book.json (not the flat spine). Self-contained + sovereign 3// (fork/exec, NO shell): rebuilds the fixture + nx_epub_book via the runner, runs nx_epub_book on the fixture, 4// reads the emitted book.json, asserts the nested TOC + href->chapter-idx resolution. Negative control: NO toc 5// entry is unresolved (idx:-1 absent) and a bogus marker is absent (discrimination teeth). expect_exit: 0 6import "nx_syscalls.nx" 7 8func gt_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func gt_n(v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(1,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48 as u8;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 } 10func gt_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 11 12// spawn path with up to 2 args, child stdout+stderr -> redir (truncate). returns child exit code. 13func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 14 let pid: i64 = sys_fork() 15 if pid == 0 { 16 let fd: i64 = sys_openat_wr(redir, 420) 17 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 18 let argv: *i64 = sys_mmap(64) as *i64 19 var n: i64 = 0 20 argv[0] = path as i64; n = 1 21 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 22 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 23 argv[n] = 0 24 let envp: *i64 = sys_mmap(16) as *i64 25 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 26 sys_execve(path, argv, envp) 27 sys_exit(127) 28 } 29 let st: *i64 = sys_mmap(16) as *i64 30 sys_wait4(pid, st, 0) 31 return (st[0] >> 8) & 0xff 32} 33 34func gt_find(hay: *u8, hl: i64, needle: *u8) -> i64 { 35 let nl: i64 = gt_slen(needle) 36 if nl == 0 { return 0-1 } 37 var i: i64 = 0 38 while i + nl <= hl { 39 var k: i64 = 0; var hit: i64 = 1 40 while k < nl { if hay[i+k] != needle[k] { hit = 0; k = nl } else { k = k + 1 } } 41 if hit == 1 { return i } 42 i = i + 1 43 } 44 return 0-1 45} 46 47func main() -> i64 { 48 gt_p("=== nx_epub_book_toc_gate: end-to-end hierarchical TOC from nav-doc (R-TOC) ===\n" as *u8) 49 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 50 let SCRATCH: *u8 = "knowledge/status/epub_toc_gate_scratch.log\x00" as *u8 51 52 // 1. rebuild fixture + nx_epub_book (the runner's own nx_epub_book run fails on the NAS default; we only need the fresh elf) 53 spawn(RUNNER, "nx_epub_fixture_build\x00" as *u8, 0 as *u8, SCRATCH) 54 spawn(RUNNER, "nx_epub_book\x00" as *u8, 0 as *u8, SCRATCH) 55 56 // 2. emit book.json from the fixture (argv-driven) 57 let rc: i64 = spawn("_offc/nx_epub_book.elf\x00" as *u8, "knowledge/fixtures/nishi_fixture.epub\x00" as *u8, "nishi_fixture_gate\x00" as *u8, SCRATCH) 58 gt_p(" nx_epub_book on fixture exit=" as *u8); gt_n(rc); gt_p("\n" as *u8) 59 60 // 3. read the emitted book.json 61 let lp: *i64 = sys_mmap(8) as *i64; lp[0] = 0 62 let buf: *u8 = sys_read_file("knowledge/staging/media/reader/nishi_fixture_gate/book.json\x00" as *u8, lp) 63 if (buf as i64) == 0 { gt_p("EPUB-BOOK-TOC-GATE verdict=RED reason=book.json-missing\n" as *u8); sys_exit(1); return 1 } 64 let n: i64 = lp[0] 65 gt_p(" book.json bytes=" as *u8); gt_n(n); gt_p("\n" as *u8) 66 67 var pass: i64 = 0 68 var fail: i64 = 0 69 // nav doc located + parsed 70 if gt_find(buf, n, "\"toc_source\":\"nav-xhtml\"" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL toc_source\n" as *u8) } 71 if gt_find(buf, n, "\"ntoc\":4" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL ntoc=4\n" as *u8) } 72 // hierarchy: nested entry at depth 1 resolved to the right chapter (fragment stripped for mapping) 73 if gt_find(buf, n, "\"depth\":1,\"title\":\"2.1 The Pool of Tears\",\"idx\":1" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL nested-2.1->ch idx1\n" as *u8) } 74 if gt_find(buf, n, "\"depth\":1,\"title\":\"Chapter Three\",\"idx\":2" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL nested-ChapterThree->idx2\n" as *u8) } 75 if gt_find(buf, n, "\"depth\":0,\"title\":\"Part Two\",\"idx\":1" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL parent-PartTwo->idx1\n" as *u8) } 76 // spine chapters[] still present (reading order intact) 77 if gt_find(buf, n, "\"chapters\":[" as *u8) >= 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL chapters-present\n" as *u8) } 78 // NEG control: NO toc entry left unresolved (every nav href mapped to a real chapter) 79 if gt_find(buf, n, "\"idx\":-1" as *u8) < 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL unresolved-idx(-1)-present\n" as *u8) } 80 // teeth: a bogus marker must be ABSENT (proves the substring checks discriminate) 81 if gt_find(buf, n, "\"toc_source\":\"WRONG\"" as *u8) < 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL teeth\n" as *u8) } 82 // the book elf must have succeeded 83 if rc == 0 { pass=pass+1 } else { fail=fail+1; gt_p(" FAIL book-exit\n" as *u8) } 84 85 gt_p("EPUB-BOOK-TOC-GATE pass=" as *u8); gt_n(pass); gt_p(" fail=" as *u8); gt_n(fail) 86 if fail == 0 { gt_p(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 87 gt_p(" verdict=RED\n" as *u8); sys_exit(1); return 1 88}