code wiki / _hdl_build / nx_brand_packs_gate.nx
nx_brand_packs_gate.nx source
↩ module page · 114 lines · 5832 B
1// nx_brand_packs_gate.nx -- R3 gate: multi-brand theme packs taught to the universal builder. Composes the whole
2// stack: TEACH (nb_register catalog) -> LOAD (distinct brands) -> RENDER (wb_doc_open_branded) -> GUARD (each pack
3// proven on-brand, AA contrast via R2) -> SOVEREIGNTY -> DETERMINISM. Hermetic test registry (/tmp); a final LIVE
4// idempotent teach grows the real catalog. 100% sovereign. license_tier: ORIGINAL expect_exit: 0
5import "nx_syscalls.nx"
6import "nx_sitegate_emit_lib.nx"
7import "nx_brand_packs.nx"
8import "nx_brand_guard.nx"
9import "nx_web_builder.nx"
10import "nx_brand_tokens.nx"
11
12
13func g_has(s: *u8, n: i64, lit: *u8) -> i64 { if bt_find(s, n, lit, bt_len(lit)) >= 0 { return 1 } return 0 }
14func g_primary(path: *u8, id: *u8, out: *u8) -> i64 { // load a pack -> its color/primary value into out
15 let b: *u8 = sys_mmap(8192)
16 let bl: i64 = bp_load(path, id, b, 8192)
17 if bl < 0 { return 0 - 1 }
18 return bt_lookup(b, bl, "color" as *u8, "primary" as *u8, out, 64)
19}
20func g_pack_aa(path: *u8, id: *u8) -> i64 { // load a pack -> its ink/bg contrast x100
21 let b: *u8 = sys_mmap(8192)
22 let bl: i64 = bp_load(path, id, b, 8192)
23 if bl < 0 { return 0 - 1 }
24 return bg_pair_contrast(b, bl, "color" as *u8, "ink" as *u8, "color" as *u8, "bg" as *u8)
25}
26
27func main() -> i64 {
28 let tot: *i64 = sys_mmap(32) as *i64
29 tot[0]=0; tot[1]=0
30 let tp: *u8 = "/tmp/nx_brand_packs_test.tsv" as *u8
31 gw("=== nx_brand_packs_gate -- R3 multi-brand theme packs taught to the universal builder ===\n" as *u8)
32
33 // clean hermetic registry
34 let cfd: i64 = sys_openat_wr(tp, 0x1a4)
35 if cfd >= 0 { sys_close(cfd) }
36
37 // T1 TEACH: 3 packs newly taught + counted
38 let c1: i64 = bp_register_all(tp)
39 let cnt1: i64 = nb_count(tp, "brand" as *u8, "pack" as *u8)
40 var t1: i64 = 0
41 if c1 == 3 { if cnt1 == 3 { t1 = 1 } }
42 gw(" teach taught=" as *u8); gn(c1); gw(" catalog-count=" as *u8); gn(cnt1); gw("\n" as *u8)
43 t_row("T1 teach: 3 brand packs registered into the builder catalog" as *u8, t1, tot)
44
45 // T2 IDEMPOTENT: re-teach adds nothing
46 let c2: i64 = bp_register_all(tp)
47 let cnt2: i64 = nb_count(tp, "brand" as *u8, "pack" as *u8)
48 var t2: i64 = 0
49 if c2 == 0 { if cnt2 == 3 { t2 = 1 } }
50 t_row("T2 idempotent: re-teach taught=0, count still 3" as *u8, t2, tot)
51
52 // T3 DISTINCT: each pack resolves a different primary brand color
53 let pn: *u8 = sys_mmap(64)
54 let po: *u8 = sys_mmap(64)
55 let pf: *u8 = sys_mmap(64)
56 g_primary(tp, "nishi" as *u8, pn)
57 g_primary(tp, "ocean" as *u8, po)
58 g_primary(tp, "forest" as *u8, pf)
59 gw(" primaries nishi=" as *u8); gw(pn); gw(" ocean=" as *u8); gw(po); gw(" forest=" as *u8); gw(pf); gw("\n" as *u8)
60 var t3: i64 = 0
61 if nb_streq(pn, po) == 0 { if nb_streq(po, pf) == 0 { if nb_streq(pn, pf) == 0 { t3 = 1 } } }
62 t_row("T3 distinct: 3 packs -> 3 different primary colors (real multi-brand)" as *u8, t3, tot)
63
64 // T4 RENDER: a loaded pack drives the builder's :root (swap a whole brand by DATA)
65 let ob: *u8 = sys_mmap(8192)
66 let oln: i64 = bp_load(tp, "ocean" as *u8, ob, 8192)
67 let wfd: i64 = sys_openat_wr("/tmp/nx_r3_ocean.html" as *u8, 0x1a4)
68 wb_doc_open_branded(wfd, "Ocean" as *u8, ob, oln)
69 wb_w(wfd, "<p>x</p>\n" as *u8)
70 wb_doc_close(wfd)
71 sys_close(wfd)
72 let lp: *i64 = sys_mmap(16) as *i64
73 let pg: *u8 = sys_read_file("/tmp/nx_r3_ocean.html" as *u8, lp)
74 let pgn: i64 = lp[0]
75 var t4: i64 = 0
76 if pgn > 0 { if g_has(pg, pgn, "--nx-color-primary: #0a3d62" as *u8) == 1 { if g_has(pg, pgn, "var(--nx-color-primary)" as *u8) == 1 { t4 = 1 } } }
77 t_row("T4 render: ocean pack drives the builder :root (#0a3d62) via var(--nx-*)" as *u8, t4, tot)
78
79 // T5 GUARD: every pack is on-brand by construction (ink/bg meets WCAG AA) -- composes R2
80 let aN: i64 = g_pack_aa(tp, "nishi" as *u8)
81 let aO: i64 = g_pack_aa(tp, "ocean" as *u8)
82 let aF: i64 = g_pack_aa(tp, "forest" as *u8)
83 gw(" pack AA (ink/bg x100) nishi=" as *u8); gn(aN); gw(" ocean=" as *u8); gn(aO); gw(" forest=" as *u8); gn(aF); gw("\n" as *u8)
84 var t5: i64 = 0
85 if aN >= 450 { if aO >= 450 { if aF >= 450 { t5 = 1 } } }
86 t_row("T5 guard: all 3 packs pass WCAG AA on body text (on-brand by construction)" as *u8, t5, tot)
87
88 // T6 SOVEREIGNTY: a malicious template is refused; the real packs are safe
89 var t6: i64 = 0
90 if nb_template_safe("token|color|x|<script>alert(1)</script>" as *u8) == 0 {
91 if nb_template_safe(bp_pack("ocean" as *u8)) == 1 { t6 = 1 }
92 }
93 t_row("T6 sovereignty: <script> template refused; real packs pass nb_template_safe" as *u8, t6, tot)
94
95 // T7 DETERMINISM: same pack loads byte-identical
96 let d1: *u8 = sys_mmap(8192)
97 let d2: *u8 = sys_mmap(8192)
98 let l1: i64 = bp_load(tp, "forest" as *u8, d1, 8192)
99 let l2: i64 = bp_load(tp, "forest" as *u8, d2, 8192)
100 var t7: i64 = 0
101 if l1 == l2 { if l1 > 0 { var eq: i64 = 1; var i: i64 = 0; while i < l1 { if d1[i] != d2[i] { eq = 0 } i = i + 1 } t7 = eq } }
102 t_row("T7 determinism: a pack loads byte-identical (portable/reproducible)" as *u8, t7, tot)
103
104 gw("\nrows pass=" as *u8); gn(tot[0]); gw(" fail=" as *u8); gn(tot[1]); gw("\n" as *u8)
105
106 // LIVE teach (idempotent, additive): grow the REAL builder catalog so the system actually learned the brands
107 let lc: i64 = bp_register_all(NB_PATH)
108 let live: i64 = nb_count(NB_PATH, "brand" as *u8, "pack" as *u8)
109 gw("LIVE catalog: newly-taught=" as *u8); gn(lc); gw(" brand packs now in knowledge/registry/nishi_builder.tsv = " as *u8); gn(live); gw("\n" as *u8)
110
111 if tot[1] == 0 { gw("VERDICT=GREEN -- multi-brand packs taught, loaded, rendered, guard-passing, sovereign\n" as *u8); return 0 }
112 gw("VERDICT=RED\n" as *u8)
113 return 1
114}