code wiki / _hdl_build / nx_cms_ecommerce_gate.nx

nx_cms_ecommerce_gate.nx source

↩ module page · 87 lines · 5386 B

1// nx_cms_ecommerce_gate.nx -- CMS ECOMMERCE gate: land the missing WooCommerce class HONESTLY by proving 2// the sovereign commerce core -- integer-exact cart/tax/discount money (no float drift), oversell-proof 3// inventory (stock never negative, over-quantity refused), and an exact order state machine. Positive AND 4// negative controls throughout. Appends "CMSGATE row=nx_cms_ecommerce ecommerce ... verdict=PASS" to 5// knowledge/status/cms_gate.log ONLY if every assertion holds. Exit 0 iff all pass. license_tier: ORIGINAL 6import "nx_cms_ecommerce.nx" 7import "nx_syscalls.nx" 8 9func cg_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func cg_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 } 11func cg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var k: i64=0; while s[k]!=(0 as u8){dst[o]=s[k];o=o+1;k=k+1} return o } 12func cg_catnum(dst: *u8, off: i64, v: i64) -> i64 { 13 var o: i64=off; let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; var k: i64=0 14 if m==0 {t[0]=48 as u8;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1} 15 var i: i64=0; while i<k {dst[o]=t[k-1-i]; o=o+1; i=i+1} return o 16} 17 18func cg_row(id: i64, ok: i64, what: *u8) -> i64 { 19 cg_w("ECROW " as *u8); cg_num(id); cg_w(" " as *u8) 20 if ok==1 { cg_w("PASS " as *u8) } else { cg_w("FAIL " as *u8) } 21 cg_w(what); cg_w("\n" as *u8) 22 return ok 23} 24 25func main() -> i64 { 26 var pass: i64 = 0 27 var rows: i64 = 0 28 var ok: i64 = 0 29 30 // R0: line totals + cart subtotal integer-exact. lines: 2 x $19.99 + 3 x $5.00 = $39.98 + $15.00 = $54.98 31 let units: *i64 = sys_mmap(64) as *i64 32 let qtys: *i64 = sys_mmap(64) as *i64 33 units[0] = 1999; qtys[0] = 2 34 units[1] = 500; qtys[1] = 3 35 let sub: i64 = ec_cart_subtotal(units, qtys, 2) 36 ok = 0; if ec_line_total(1999, 2) == 3998 { if sub == 5498 { ok = 1 } } 37 rows=rows+1; pass=pass+cg_row(0, ok, "cart subtotal integer-exact: 2x1999 + 3x500 = 5498 cents" as *u8) 38 39 // R1: tax in basis points -- $100.00 @ 8.25% = $108.25 (10000 + 825), exact integer 40 ok = 0; if ec_apply_tax(10000, 825) == 10825 { ok = 1 } 41 rows=rows+1; pass=pass+cg_row(1, ok, "tax 8.25% on 10000c -> 10825c (basis-point, no float)" as *u8) 42 43 // R2: discount in basis points -- $100.00 @ 15% off = $85.00 44 ok = 0; if ec_apply_discount(10000, 1500) == 8500 { ok = 1 } 45 rows=rows+1; pass=pass+cg_row(2, ok, "discount 15% on 10000c -> 8500c (basis-point)" as *u8) 46 47 // R3: no float drift -- 1999c * 3 = 5997c exactly, reproducibly (float dollars would drift) 48 let d1: i64 = ec_line_total(1999, 3) 49 let d2: i64 = ec_line_total(1999, 3) 50 ok = 0; if d1 == 5997 { if d1 == d2 { ok = 1 } } 51 rows=rows+1; pass=pass+cg_row(3, ok, "no float drift: 1999c x3 = 5997c exact + reproducible" as *u8) 52 53 // R4: inventory guard -- in-stock ok, oversell refused, zero-qty refused, decrement correct 54 ok = 0 55 if ec_stock_ok(5, 3) == 1 { if ec_stock_ok(5, 6) == 0 { if ec_stock_ok(5, 0) == 0 { if ec_decrement_stock(5, 3) == 2 { if ec_decrement_stock(5, 6) == 5 { ok = 1 } } } } } 56 rows=rows+1; pass=pass+cg_row(4, ok, "inventory: ok(5,3); oversell(5,6) & zero(5,0) refused; dec(5,3)=2; dec(5,6)=5 unchanged (neg)" as *u8) 57 58 // R5: stock NEVER negative -- exact-zero decrement, and a request against empty stock refused 59 ok = 0; if ec_decrement_stock(2, 2) == 0 { if ec_decrement_stock(0, 1) == 0 { ok = 1 } } 60 rows=rows+1; pass=pass+cg_row(5, ok, "stock never negative: dec(2,2)=0; dec(0,1)=0 refused (neg control)" as *u8) 61 62 // R6: order lifecycle -- cart->pending->paid->shipped all allowed 63 ok = 0 64 if ec_order_can_transition(NX_ORD_CART, NX_ORD_PENDING) == 1 { if ec_order_can_transition(NX_ORD_PENDING, NX_ORD_PAID) == 1 { if ec_order_can_transition(NX_ORD_PAID, NX_ORD_SHIPPED) == 1 { ok = 1 } } } 65 rows=rows+1; pass=pass+cg_row(6, ok, "order FSM: cart->pending->paid->shipped allowed" as *u8) 66 67 // R7 (neg controls): illegal/backwards transitions refused 68 ok = 0 69 if ec_order_can_transition(NX_ORD_PAID, NX_ORD_CART) == 0 { if ec_order_can_transition(NX_ORD_SHIPPED, NX_ORD_PAID) == 0 { if ec_order_can_transition(NX_ORD_CANCELLED, NX_ORD_SHIPPED) == 0 { ok = 1 } } } 70 rows=rows+1; pass=pass+cg_row(7, ok, "order FSM: paid->cart, shipped->paid, cancelled->shipped REFUSED (neg)" as *u8) 71 72 cg_w("CMS-ECOMMERCE-GATE rows=" as *u8); cg_num(rows); cg_w(" pass=" as *u8); cg_num(pass); cg_w("\n" as *u8) 73 74 if pass == rows { 75 let line: *u8 = sys_mmap(256) 76 var off: i64 = cg_cat(line, 0, "CMSGATE row=nx_cms_ecommerce ecommerce rows=" as *u8) 77 off = cg_catnum(line, off, rows) 78 off = cg_cat(line, off, " pass=" as *u8); off = cg_catnum(line, off, pass) 79 off = cg_cat(line, off, " verdict=PASS\n" as *u8) 80 let gf: i64 = sys_openat_append("knowledge/status/cms_gate.log" as *u8, 0x1a4) 81 if gf >= 0 { sys_write(gf, line, off); sys_close(gf) } 82 cg_w("CMS-ECOMMERCE-GATE verdict=PASS -- ecommerce recorded in cms_gate.log\n" as *u8) 83 sys_exit(0); return 0 84 } 85 cg_w("CMS-ECOMMERCE-GATE verdict=FAIL -- NOT recorded (no fake-green)\n" as *u8) 86 sys_exit(1); return 1 87}