code wiki / (root) / nx_browser_buyguard_gate.nx

nx_browser_buyguard_gate.nx source

↩ module page · 112 lines · 6608 B

1// nx_browser_buyguard_gate.nx -- proves the browser "before you buy" pass works on REAL page HTML via the 2// browser's own nx_html_to_text. A realistic product page (nav, the $249.99 product price, $12.99 shipping, 3// a $25.00 gift-card footer) -> strip to text -> detect the product price (largest, not shipping) -> emit 4// the overlay (what you have vs what it truly costs + opportunity cost) -> PAUSE. A page with NO price -> 5// ALLOW + empty overlay (does nothing). Exits 0 iff ALL pass. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_money.nx" 8import "nx_html_to_text.nx" 9import "nx_price_extract.nx" 10import "nx_adhd.nx" 11import "nx_browser_buyguard.nx" 12 13func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14func g_putn(v: i64) -> i64 { 15 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 16 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 17 let d: *u8 = sys_mmap(24); var k: i64 = 0 18 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 19 var j: i64 = k - 1 20 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 21 return 0 22} 23func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 24 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") } 25 else { st[1] = st[1] + 1; g_puts(" FAIL "); g_puts(name); g_puts(" got="); g_putn(got); g_puts(" want="); g_putn(want); g_puts("\n") } 26 return 0 27} 28func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 29func gfind(hay: *u8, n: i64, needle: *u8) -> i64 { 30 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 } 31 if nl == 0 { return 0 } 32 var i: i64 = 0 33 while i + nl <= n { 34 var j: i64 = 0; var ok: i64 = 1 35 while j < nl { if hay[i + j] != needle[j] { ok = 0; j = nl } else { j = j + 1 } } 36 if ok == 1 { return i } 37 i = i + 1 38 } 39 return 0 - 1 40} 41func has(hay: *u8, n: i64, needle: *u8) -> i64 { if gfind(hay, n, needle) >= 0 { return 1 } return 0 } 42func seti(a: *i64, i: i64, v: i64) -> i64 { a[i] = v; return 0 } 43 44func main() -> i64 { 45 let st: *i64 = sys_mmap(16) as *i64 46 st[0] = 0; st[1] = 0 47 48 // the person's real numbers (the intake survey fills these) 49 let prof: *i64 = sys_mmap(8 * 8) as *i64 50 seti(prof, 0, 50000) // available $500 51 seti(prof, 1, 2000000) // debt $20,000 52 seti(prof, 2, mny_rate_from_pct(24, 1)) // 24% APR 53 seti(prof, 3, 60000) // $600/mo 54 seti(prof, 4, 2500) // $25/hr 55 seti(prof, 5, 100000) // $1,000 buffer goal 56 seti(prof, 6, 10000) // $100 pause threshold 57 58 // a realistic product page (tags, nav, multiple $-amounts) 59 let page: *u8 = "<!doctype html><html><head><title>SoundMax Pro</title></head><body><nav><a href=\"/\">Home</a> <a href=\"/deals\">Deals</a></nav><h1>SoundMax Pro Wireless Headphones</h1><div class=\"price\">$249.99</div><p>Premium noise-cancelling. Free 30-day returns.</p><div class=\"ship\">Shipping: $12.99</div><button>Add to cart</button><footer>Gift cards from $25.00</footer></body></html>\x00" as *u8 60 let pn: i64 = slen(page) 61 62 // the REAL browser path: html_to_text then detect the product price (largest, not shipping/gift-card) 63 let textbuf: *u8 = sys_mmap(8192) 64 let tn: i64 = nx_html_to_text(page, pn, textbuf, 8192) 65 chk("browser html_to_text + price-detect -> $249.99 (24999c)", px_find_price(textbuf, tn), 24999, st) 66 67 // run the before-you-buy pass on the page 68 let overlay: *u8 = sys_mmap(8192) 69 let decision: i64 = bgw_scan(page, pn, prof, overlay, 8192) 70 g_puts(" [info] overlay: "); g_puts(overlay); g_puts("\n") 71 chk("decision = PAUSE (cooling-off)", decision, ADHD_PAUSE, st) 72 chk("overlay says 'Before you buy'", has(overlay, slen(overlay), "Before you buy\x00" as *u8), 1, st) 73 chk("overlay shows the page price $249.99", has(overlay, slen(overlay), "$249.99\x00" as *u8), 1, st) 74 chk("overlay shows 'It truly costs'", has(overlay, slen(overlay), "It truly costs\x00" as *u8), 1, st) 75 chk("overlay shows 'hours of your life'", has(overlay, slen(overlay), "hours of your life\x00" as *u8), 1, st) 76 chk("overlay advises 'Sleep on it 24h?'", has(overlay, slen(overlay), "Sleep on it 24h?\x00" as *u8), 1, st) 77 chk("overlay shows what you HAVE ($500.00)", has(overlay, slen(overlay), "$500.00\x00" as *u8), 1, st) 78 79 // a page with NO price -> the guard stays silent 80 let blog: *u8 = "<!doctype html><html><body><h1>How I learned to focus</h1><p>Some thoughts on ADHD and routines.</p></body></html>\x00" as *u8 81 let overlay2: *u8 = sys_mmap(8192) 82 chk("no-price page -> ALLOW (guard stays silent)", bgw_scan(blog, slen(blog), prof, overlay2, 8192), ADHD_ALLOW, st) 83 var empty: i64 = 0 84 if overlay2[0] == (0 as u8) { empty = 1 } 85 chk("no-price page -> empty overlay", empty, 1, st) 86 87 // --- bgw_inject: splice the overlay into the page body so the browser renders it as a box --- 88 let injected: *u8 = sys_mmap(16384) 89 let il: i64 = bgw_inject(page, pn, prof, injected, 16384) 90 chk("inject: overlay present in the page HTML", has(injected, il, "Before you buy\x00" as *u8), 1, st) 91 chk("inject: original page content preserved", has(injected, il, "SoundMax Pro Wireless Headphones\x00" as *u8), 1, st) 92 let body_at: i64 = gfind(injected, il, "<body>\x00" as *u8) 93 let guard_at: i64 = gfind(injected, il, "Before you buy\x00" as *u8) 94 let h1_at: i64 = gfind(injected, il, "SoundMax Pro Wireless\x00" as *u8) 95 var ordered: i64 = 0 96 if body_at >= 0 { if guard_at > body_at { if h1_at > guard_at { ordered = 1 } } } 97 chk("inject: overlay sits inside <body>, ABOVE the page content", ordered, 1, st) 98 var grew: i64 = 0 99 if il > pn { grew = 1 } 100 chk("inject: result longer than original (overlay added)", grew, 1, st) 101 102 // no-price page -> inject returns the page UNTOUCHED (no overlay, same length) 103 let injected2: *u8 = sys_mmap(16384) 104 let il2: i64 = bgw_inject(blog, slen(blog), prof, injected2, 16384) 105 chk("inject: no-price page -> NO overlay added", has(injected2, il2, "Before you buy\x00" as *u8), 0, st) 106 chk("inject: no-price page -> length unchanged (untouched copy)", il2, slen(blog), st) 107 108 g_puts("nx_browser_buyguard_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n") 109 if st[1] == 0 { g_puts("BROWSER BUYGUARD: GREEN\n"); return 0 } 110 g_puts("BROWSER BUYGUARD: RED\n") 111 return 1 112}