nx_gtm_svc_test.nx source
↩ module page · 220 lines · 11615 B
1// nx_gtm_svc_test.nx -- gate for the GTM service facade. Drives the dispatcher
2// exactly as an agent or a workflow stage would -- build an argv, call, read
3// the JSON -- and checks the envelope, the routed values, structured errors,
4// and the exit-code contract. It also proves the facade adds NO domain logic:
5// the numbers it emits equal the numbers the libraries compute directly.
6// expect_exit: 0 license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_service.nx"
9import "nx_supp_ingredient.nx"
10import "nx_supp_channel.nx"
11import "nx_trial_ethics.nx"
12import "nx_gtm_svc.nx"
13
14func t_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
15func t_putn(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } let t: *u8 = sys_mmap(28); var k: i64 = 0; 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 } var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 }
16
17// Does j's buffer contain the needle substring? (structured-reply spot check)
18func j_has(j: *NxJson, needle: *u8) -> i64 {
19 var nn: i64 = 0
20 while needle[nn] != (0 as u8) { nn = nn + 1 }
21 if nn == 0 { return 0 }
22 let b: *u8 = j.buf
23 let last: i64 = j.off - nn
24 var i: i64 = 0
25 var found: i64 = 0
26 while i <= last {
27 if found == 0 {
28 var m: i64 = 0
29 var ok: i64 = 1
30 while m < nn {
31 if b[i + m] != needle[m] { ok = 0 }
32 m = m + 1
33 }
34 if ok == 1 { found = 1 }
35 }
36 i = i + 1
37 }
38 return found
39}
40
41// Build an argv of up to 6 string tokens for the dispatcher.
42func mk_argv(a0: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8) -> *i64 {
43 let av: *i64 = sys_mmap(64)
44 av[0] = a0 as i64
45 av[1] = a1 as i64
46 av[2] = a2 as i64
47 av[3] = a3 as i64
48 av[4] = a4 as i64
49 av[5] = a5 as i64
50 return av
51}
52
53func main() -> i64 {
54 var pass: i64 = 0
55 var total: i64 = 0
56 let NUL: *u8 = "\x00" as *u8
57
58 // --- T1 describe emits the verb catalog (agentic discovery) ---
59 total = total + 1
60 let j1: *NxJson = nx_json_new(8192)
61 let av1: *i64 = mk_argv("nx_gtm_svc" as *u8, "describe" as *u8, NUL, NUL, NUL, NUL)
62 let rc1: i64 = gtm_dispatch(2, av1, j1)
63 let d1: i64 = j_has(j1, "\"verb_count\":8" as *u8)
64 let d2: i64 = j_has(j1, "ingredient.info" as *u8)
65 let d3: i64 = j_has(j1, "supply.effective" as *u8)
66 t_puts("T1 describe: rc=" as *u8); t_putn(rc1); t_puts(" verb_count+catalog present=" as *u8); t_putn(d1); t_putn(d2); t_putn(d3); t_puts(": " as *u8)
67 var ok1: i64 = 1
68 if rc1 != 0 { ok1 = 0 }
69 if d1 != 1 { ok1 = 0 }
70 if d2 != 1 { ok1 = 0 }
71 if d3 != 1 { ok1 = 0 }
72 if ok1 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
73
74 // --- T2 ingredient.info routes and matches the library directly ---
75 total = total + 1
76 let j2: *NxJson = nx_json_new(4096)
77 let av2: *i64 = mk_argv("nx_gtm_svc" as *u8, "ingredient.info" as *u8, "3" as *u8, NUL, NUL, NUL)
78 gtm_dispatch(3, av2, j2)
79 let env_ok: i64 = j_has(j2, "\"ok\":true" as *u8)
80 let name_ok: i64 = j_has(j2, "Ashwagandha" as *u8)
81 let lawful_ok: i64 = j_has(j2, "\"lawful_dietary\":true" as *u8)
82 t_puts("T2 ingredient.info id=3 (ashwagandha): env=" as *u8); t_putn(env_ok); t_puts(" name=" as *u8); t_putn(name_ok); t_puts(" lawful=" as *u8); t_putn(lawful_ok); t_puts(": " as *u8)
83 var ok2: i64 = 1
84 if env_ok != 1 { ok2 = 0 }
85 if name_ok != 1 { ok2 = 0 }
86 if lawful_ok != 1 { ok2 = 0 }
87 if ok2 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
88
89 // --- T3 a peptide routes to a NOT_DIETARY class via the facade ---
90 total = total + 1
91 let j3: *NxJson = nx_json_new(4096)
92 let av3: *i64 = mk_argv("nx_gtm_svc" as *u8, "ingredient.info" as *u8, "9" as *u8, NUL, NUL, NUL)
93 gtm_dispatch(3, av3, j3)
94 let kp_class: i64 = j_has(j3, "\"class\":4" as *u8)
95 let kp_unlawful: i64 = j_has(j3, "\"lawful_dietary\":false" as *u8)
96 t_puts("T3 ingredient.info id=9 (kisspeptin): class4=" as *u8); t_putn(kp_class); t_puts(" unlawful=" as *u8); t_putn(kp_unlawful); t_puts(": " as *u8)
97 if kp_class == 1 { if kp_unlawful == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } } else { t_puts("FAIL\n" as *u8) }
98
99 // --- T4 claims.classify joins a multi-word sentence and classifies it ---
100 total = total + 1
101 let j4: *NxJson = nx_json_new(4096)
102 let av4: *i64 = mk_argv("nx_gtm_svc" as *u8, "claims.classify" as *u8, "supports" as *u8, "healthy" as *u8, "libido" as *u8, NUL)
103 gtm_dispatch(5, av4, j4)
104 let sent_ok: i64 = j_has(j4, "\"sentence\":\"supports healthy libido\"" as *u8)
105 let law_ok: i64 = j_has(j4, "\"lawful\":true" as *u8)
106 let notify_ok: i64 = j_has(j4, "\"fda_notification_days\":30" as *u8)
107 t_puts("T4 claims.classify joined 3 words: sentence=" as *u8); t_putn(sent_ok); t_puts(" lawful=" as *u8); t_putn(law_ok); t_puts(" notify30=" as *u8); t_putn(notify_ok); t_puts(": " as *u8)
108 var ok4: i64 = 1
109 if sent_ok != 1 { ok4 = 0 }
110 if law_ok != 1 { ok4 = 0 }
111 if notify_ok != 1 { ok4 = 0 }
112 if ok4 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
113
114 // --- T5 a disease claim classifies as unlawful through the facade ---
115 total = total + 1
116 let j5: *NxJson = nx_json_new(4096)
117 let av5: *i64 = mk_argv("nx_gtm_svc" as *u8, "claims.classify" as *u8, "treats" as *u8, "erectile" as *u8, "dysfunction" as *u8, NUL)
118 gtm_dispatch(5, av5, j5)
119 let unlaw: i64 = j_has(j5, "\"lawful\":false" as *u8)
120 let cls2: i64 = j_has(j5, "\"classification\":2" as *u8)
121 t_puts("T5 'treats erectile dysfunction' unlawful=" as *u8); t_putn(unlaw); t_puts(" class2=" as *u8); t_putn(cls2); t_puts(": " as *u8)
122 if unlaw == 1 { if cls2 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } } else { t_puts("FAIL\n" as *u8) }
123
124 // --- T6 ethics.rank matches the library value exactly (no drift) ---
125 total = total + 1
126 let j6: *NxJson = nx_json_new(4096)
127 let av6: *i64 = mk_argv("nx_gtm_svc" as *u8, "ethics.rank" as *u8, "0" as *u8, NUL, NUL, NUL)
128 gtm_dispatch(3, av6, j6)
129 let dk_score: i64 = te_integrity_score(TE_DENMARK)
130 let has_denmark: i64 = j_has(j6, "Denmark" as *u8)
131 let has_score: i64 = j_has(j6, "\"integrity_score\":130" as *u8)
132 t_puts("T6 ethics.rank Denmark: library score=" as *u8); t_putn(dk_score); t_puts(" facade emits 130=" as *u8); t_putn(has_score); t_puts(" name=" as *u8); t_putn(has_denmark); t_puts(": " as *u8)
133 var ok6: i64 = 1
134 if dk_score != 130 { ok6 = 0 }
135 if has_score != 1 { ok6 = 0 }
136 if has_denmark != 1 { ok6 = 0 }
137 if ok6 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
138
139 // --- T7 supply.effective routes numeric args and computes ---
140 total = total + 1
141 let j7: *NxJson = nx_json_new(4096)
142 let av7: *i64 = mk_argv("nx_gtm_svc" as *u8, "supply.effective" as *u8, "4" as *u8, "10000" as *u8, NUL, NUL)
143 gtm_dispatch(4, av7, j7)
144 let has_poland: i64 = j_has(j7, "Poland" as *u8)
145 let has_eff: i64 = j_has(j7, "\"effective_cost_k\":7720" as *u8)
146 t_puts("T7 supply.effective base=4 spend=10000: Poland=" as *u8); t_putn(has_poland); t_puts(" eff=7720=" as *u8); t_putn(has_eff); t_puts(": " as *u8)
147 if has_poland == 1 { if has_eff == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } } else { t_puts("FAIL\n" as *u8) }
148
149 // --- T8 UNKNOWN VERB is a structured 404 and a non-zero exit code ---
150 total = total + 1
151 let j8: *NxJson = nx_json_new(4096)
152 let av8: *i64 = mk_argv("nx_gtm_svc" as *u8, "does.not.exist" as *u8, NUL, NUL, NUL, NUL)
153 let rc8: i64 = gtm_dispatch(2, av8, j8)
154 let is_err: i64 = j_has(j8, "\"ok\":false" as *u8)
155 let is_404: i64 = j_has(j8, "\"status\":404" as *u8)
156 let slug: i64 = j_has(j8, "\"code\":\"UNKNOWN_VERB\"" as *u8)
157 t_puts("T8 unknown verb: rc=" as *u8); t_putn(rc8); t_puts(" ok:false=" as *u8); t_putn(is_err); t_puts(" 404=" as *u8); t_putn(is_404); t_puts(" slug=" as *u8); t_putn(slug); t_puts(": " as *u8)
158 var ok8: i64 = 1
159 if rc8 == 0 { ok8 = 0 }
160 if is_err != 1 { ok8 = 0 }
161 if is_404 != 1 { ok8 = 0 }
162 if slug != 1 { ok8 = 0 }
163 if ok8 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
164
165 // --- T9 bad args are a structured 400 (not a crash, not a wrong number) ---
166 total = total + 1
167 let j9: *NxJson = nx_json_new(4096)
168 let av9: *i64 = mk_argv("nx_gtm_svc" as *u8, "supply.effective" as *u8, "99" as *u8, "10000" as *u8, NUL, NUL)
169 gtm_dispatch(4, av9, j9)
170 let nf: i64 = j_has(j9, "\"status\":404" as *u8)
171 let j9b: *NxJson = nx_json_new(4096)
172 let av9b: *i64 = mk_argv("nx_gtm_svc" as *u8, "formula.assess" as *u8, "0" as *u8, "700" as *u8, "30" as *u8, "2" as *u8)
173 gtm_dispatch(6, av9b, j9b)
174 let ba: i64 = j_has(j9b, "\"status\":400" as *u8)
175 t_puts("T9 unknown base=404(" as *u8); t_putn(nf); t_puts(") zero-retail=400(" as *u8); t_putn(ba); t_puts("): " as *u8)
176 if nf == 1 { if ba == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } } else { t_puts("FAIL\n" as *u8) }
177
178 // --- T10 no-arg invocation defaults to describe (friendly entrypoint) ---
179 total = total + 1
180 let j10: *NxJson = nx_json_new(8192)
181 let av10: *i64 = mk_argv("nx_gtm_svc" as *u8, NUL, NUL, NUL, NUL, NUL)
182 let rc10: i64 = gtm_dispatch(1, av10, j10)
183 let desc: i64 = j_has(j10, "\"verb\":\"describe\"" as *u8)
184 t_puts("T10 no verb -> describe: rc=" as *u8); t_putn(rc10); t_puts(" describe=" as *u8); t_putn(desc); t_puts(": " as *u8)
185 if rc10 == 0 { if desc == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } } else { t_puts("FAIL\n" as *u8) }
186
187 // --- T11 WORKFLOW SHAPE: fan a list through one verb, feed the next.
188 // Screen 3 ingredients for lawfulness; a pipeline would keep the
189 // survivors. Proves the verbs compose without shared state. ---
190 total = total + 1
191 let ids: *i64 = sys_mmap(32)
192 ids[0] = SI_L_CITRULLINE
193 ids[1] = SI_OXYTOCIN
194 ids[2] = SI_ASHWAGANDHA
195 var lawful_survivors: i64 = 0
196 var i11: i64 = 0
197 while i11 < 3 {
198 let jx: *NxJson = nx_json_new(2048)
199 let avx: *i64 = sys_mmap(64)
200 avx[0] = "nx_gtm_svc" as i64
201 avx[1] = "channel.best" as i64
202 let idbuf: *u8 = sys_mmap(8)
203 let idv: i64 = ids[i11]
204 idbuf[0] = (48 + idv / 10) as u8
205 idbuf[1] = (48 + idv % 10) as u8
206 idbuf[2] = 0 as u8
207 avx[2] = idbuf as i64
208 gtm_dispatch(3, avx, jx)
209 if j_has(jx, "\"has_lawful_route\":true" as *u8) == 1 {
210 if j_has(jx, "\"requires_prescriber\":false" as *u8) == 1 { lawful_survivors = lawful_survivors + 1 }
211 }
212 i11 = i11 + 1
213 }
214 t_puts("T11 workflow fan-out: of citrulline/oxytocin/ashwagandha, OTC-lawful survivors=" as *u8); t_putn(lawful_survivors); t_puts(" want 2: " as *u8)
215 if lawful_survivors == 2 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
216
217 t_puts("GTM-SVC-GATE passed " as *u8); t_putn(pass); t_puts("/" as *u8); t_putn(total)
218 if pass == total { t_puts(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
219 t_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1
220}