nx_doc_census_test.nx source
↩ module page · 202 lines · 8873 B
1// nx_doc_census_test.nx -- KAT for D1 docs census classifier.
2
3import "nx_syscalls.nx"
4import "nx_string_ops.nx"
5import "nx_grep.nx"
6import "nx_grep_rt.nx" // nx_grep_any lives here, not in nx_grep.nx
7import "nx_etg.nx"
8import "nx_doc_census.nx"
9
10func main() -> i64 {
11 let cap: nx_int = 1024
12 let content: *u8 = sys_mmap(cap)
13
14 // ----- T1 Quadrant sealed enum -----
15 var i: i64 = 0
16 while i < NX_DOC_Q_N {
17 if nx_doc_q_is_valid(i) != 1 { return 1 + i }
18 i = i + 1
19 }
20 if nx_doc_q_is_valid(NX_DOC_Q_N) != 0 { return 10 }
21
22 // ----- T2 Topic type sealed enum -----
23 i = 0
24 while i < NX_DOC_T_N {
25 if nx_doc_t_is_valid(i) != 1 { return 20 + i }
26 i = i + 1
27 }
28 if nx_doc_t_is_valid(NX_DOC_T_N) != 0 { return 30 }
29
30 // ----- T3 Status sealed enum -----
31 i = 0
32 while i < NX_DOC_S_N {
33 if nx_doc_s_is_valid(i) != 1 { return 40 + i }
34 i = i + 1
35 }
36 if nx_doc_s_is_valid(NX_DOC_S_N) != 0 { return 50 }
37
38 // ----- T4 Quadrant classifier: TUTORIAL marker -----
39 let bs1: *u8 = "header **Quadrant:** TUTORIAL more"
40 nx_str_slice_copy(bs1, 0, 33, content, cap)
41 if nx_doc_classify_quadrant(content, 33) != NX_DOC_Q_TUTORIAL { return 100 }
42
43 // ----- T5 Quadrant classifier: REFERENCE -----
44 let bs2: *u8 = "**Quadrant:** REFERENCE end"
45 nx_str_slice_copy(bs2, 0, 27, content, cap)
46 if nx_doc_classify_quadrant(content, 27) != NX_DOC_Q_REFERENCE { return 101 }
47
48 // ----- T6 Quadrant classifier: EXPLANATION NOT shadowed by REFERENCE -----
49 let bs3: *u8 = "**Quadrant:** EXPLANATION text"
50 nx_str_slice_copy(bs3, 0, 30, content, cap)
51 if nx_doc_classify_quadrant(content, 30) != NX_DOC_Q_EXPLANATION { return 102 }
52
53 // ----- T7 Quadrant classifier: MIXED (compound REFERENCE + EXPLANATION) -----
54 let bs4: *u8 = "**Quadrant:** REFERENCE + EXPLANATION (Diátaxis)"
55 nx_str_slice_copy(bs4, 0, 48, content, cap)
56 if nx_doc_classify_quadrant(content, 48) != NX_DOC_Q_MIXED { return 103 }
57
58 // ----- T8 Quadrant: explicit MIXED -----
59 let bs5: *u8 = "**Quadrant:** MIXED right"
60 nx_str_slice_copy(bs5, 0, 25, content, cap)
61 if nx_doc_classify_quadrant(content, 25) != NX_DOC_Q_MIXED { return 104 }
62
63 // ----- T9 Quadrant: HOW_TO -----
64 let bs6: *u8 = "**Quadrant:** HOW_TO done"
65 nx_str_slice_copy(bs6, 0, 25, content, cap)
66 if nx_doc_classify_quadrant(content, 25) != NX_DOC_Q_HOW_TO { return 105 }
67
68 // ----- T10 Quadrant: NONE on plain content -----
69 let bs7: *u8 = "no quadrant marker here"
70 nx_str_slice_copy(bs7, 0, 23, content, cap)
71 if nx_doc_classify_quadrant(content, 23) != NX_DOC_Q_NONE { return 106 }
72
73 // ----- T11 Topic type classifier all 5 markers -----
74 let bs_c: *u8 = "**Topic Type:** CONCEPT"
75 nx_str_slice_copy(bs_c, 0, 23, content, cap)
76 if nx_doc_classify_topic_type(content, 23) != NX_DOC_T_CONCEPT { return 110 }
77 let bs_t: *u8 = "**Topic Type:** TASK"
78 nx_str_slice_copy(bs_t, 0, 20, content, cap)
79 if nx_doc_classify_topic_type(content, 20) != NX_DOC_T_TASK { return 111 }
80 let bs_r: *u8 = "**Topic Type:** REFERENCE"
81 nx_str_slice_copy(bs_r, 0, 25, content, cap)
82 if nx_doc_classify_topic_type(content, 25) != NX_DOC_T_REFERENCE { return 112 }
83 let bs_p: *u8 = "**Topic Type:** PATTERN"
84 nx_str_slice_copy(bs_p, 0, 23, content, cap)
85 if nx_doc_classify_topic_type(content, 23) != NX_DOC_T_PATTERN { return 113 }
86 let bs_e: *u8 = "**Topic Type:** EXPLANATION"
87 nx_str_slice_copy(bs_e, 0, 27, content, cap)
88 if nx_doc_classify_topic_type(content, 27) != NX_DOC_T_EXPLANATION { return 114 }
89
90 // ----- T12 Status classifier all 4 markers -----
91 let bs_s1: *u8 = "**Status:** CANONICAL"
92 nx_str_slice_copy(bs_s1, 0, 21, content, cap)
93 if nx_doc_classify_status(content, 21) != NX_DOC_S_CANONICAL { return 120 }
94 let bs_s2: *u8 = "**Status:** DRAFT"
95 nx_str_slice_copy(bs_s2, 0, 17, content, cap)
96 if nx_doc_classify_status(content, 17) != NX_DOC_S_DRAFT { return 121 }
97 let bs_s3: *u8 = "**Status:** SUPERSEDED by other"
98 nx_str_slice_copy(bs_s3, 0, 31, content, cap)
99 if nx_doc_classify_status(content, 31) != NX_DOC_S_SUPERSEDED { return 122 }
100 let bs_s4: *u8 = "**Status:** DEPRECATED text"
101 nx_str_slice_copy(bs_s4, 0, 27, content, cap)
102 if nx_doc_classify_status(content, 27) != NX_DOC_S_DEPRECATED { return 123 }
103
104 // ----- T13 Trust + Competitive + WebSearch + bidirectional presence -----
105 // Note: nx_grep_any is case-sensitive (substrate-honest); we test
106 // the EXACT marker strings the doctrine specifies.
107 let bs_ts: *u8 = "**Trust State:** CROSS_ARCH_TESTED"
108 nx_str_slice_copy(bs_ts, 0, 34, content, cap)
109 if nx_doc_has_trust_state(content, 34) != 1 { return 130 }
110
111 let bs_provenance: *u8 = "WebSearch-verified citations"
112 nx_str_slice_copy(bs_provenance, 0, 28, content, cap)
113 if nx_doc_has_websearch_provenance(content, 28) != 1 { return 132 }
114
115 let bs_cs: *u8 = "**Competitive State:** UNCLASSIFIED"
116 nx_str_slice_copy(bs_cs, 0, 35, content, cap)
117 if nx_doc_has_competitive_state(content, 35) != 1 { return 140 }
118
119 let bs_link: *u8 = "see [[other-doc]] for more"
120 nx_str_slice_copy(bs_link, 0, 26, content, cap)
121 if nx_doc_has_bidirectional_links(content, 26) != 1 { return 150 }
122
123 // ----- T14 NxDocV2Record classify on full V2-compliant doc -----
124 let full: *u8 = "**Quadrant:** REFERENCE (Diátaxis)\n**Topic Type:** REFERENCE\n**Status:** CANONICAL\n**Trust State:** CROSS_ARCH_TESTED\n**Competitive State:** UNCLASSIFIED\nWebSearch-verified citations include [[other-doc]] below"
125 let full_len: i64 = 210
126 nx_str_slice_copy(full, 0, full_len, content, cap)
127 let r_buf: *u8 = sys_mmap(128)
128 let r: *NxDocV2Record = r_buf as *NxDocV2Record
129 nx_doc_classify(content, full_len, r)
130 if r.quadrant != NX_DOC_Q_REFERENCE { return 160 }
131 if r.topic_type != NX_DOC_T_REFERENCE { return 161 }
132 if r.status != NX_DOC_S_CANONICAL { return 162 }
133 if r.has_trust_state != 1 { return 163 }
134 if r.has_competitive_state != 1 { return 164 }
135 if r.has_websearch_provenance != 1 { return 165 }
136 if r.has_bidirectional_links != 1 { return 166 }
137 if r.compliance_score != 7 { return 167 }
138
139 // ----- T15 Empty content -> zero compliance -----
140 let r2_buf: *u8 = sys_mmap(128)
141 let r2: *NxDocV2Record = r2_buf as *NxDocV2Record
142 let bs_empty: *u8 = "no markers here"
143 nx_str_slice_copy(bs_empty, 0, 15, content, cap)
144 nx_doc_classify(content, 15, r2)
145 if r2.quadrant != NX_DOC_Q_NONE { return 170 }
146 if r2.compliance_score != 0 { return 171 }
147
148 // ----- T16 Aggregator: init + accumulate -----
149 let c_buf: *u8 = sys_mmap(128)
150 let c: *NxDocCensusCounts = c_buf as *NxDocCensusCounts
151 nx_doc_census_init(c)
152 if c.n_total != 0 { return 180 }
153
154 // Accumulate full-compliance record
155 nx_doc_census_accumulate(c, r)
156 if c.n_total != 1 { return 181 }
157 if c.n_quadrant_declared != 1 { return 182 }
158 if c.n_full_compliance != 1 { return 183 }
159
160 // Accumulate zero-compliance record
161 nx_doc_census_accumulate(c, r2)
162 if c.n_total != 2 { return 184 }
163 if c.n_full_compliance != 1 { return 185 }
164 if c.n_zero_compliance != 1 { return 186 }
165
166 // pct compliance: full (7) + zero (0) = 7 / (2*7=14) = 50%
167 if nx_doc_census_pct_compliance(c) != 50 { return 187 }
168
169 // ----- T17 pct_compliance = 0 on no docs (no div-by-zero) -----
170 let c2_buf: *u8 = sys_mmap(128)
171 let c2: *NxDocCensusCounts = c2_buf as *NxDocCensusCounts
172 nx_doc_census_init(c2)
173 if nx_doc_census_pct_compliance(c2) != 0 { return 190 }
174
175 // ----- T18 Outcome mapping -----
176 // n_total=0 -> INCONCLUSIVE
177 if nx_doc_census_to_etg_outcome(c2) != NX_ETG_OUTCOME_INCONCLUSIVE { return 200 }
178 // 50% -> INCONCLUSIVE (mid-band)
179 if nx_doc_census_to_etg_outcome(c) != NX_ETG_OUTCOME_INCONCLUSIVE { return 201 }
180 // Full compliance only -> 100% -> CONFIRMED
181 nx_doc_census_init(c)
182 nx_doc_census_accumulate(c, r)
183 if nx_doc_census_to_etg_outcome(c) != NX_ETG_OUTCOME_CONFIRMED { return 202 }
184 // Zero compliance only -> 0% -> FALSIFIED
185 nx_doc_census_init(c)
186 nx_doc_census_accumulate(c, r2)
187 if nx_doc_census_to_etg_outcome(c) != NX_ETG_OUTCOME_FALSIFIED { return 203 }
188
189 // ----- T19 ETG attestation -----
190 let e_buf: *u8 = sys_mmap(128)
191 let e: *NxEtgEntry = e_buf as *NxEtgEntry
192 nx_doc_census_init(c)
193 nx_doc_census_accumulate(c, r) // 100% compliance
194 let rc_a: i64 = nx_doc_census_attest(c, e, 0xC0DEC0DE, 1, 20260520)
195 if rc_a != 0 { return 210 }
196 if e.outcome != NX_ETG_OUTCOME_CONFIRMED { return 211 }
197 if e.claim_value != 1 { return 212 } // n_total
198 if e.measurement_value != 100 { return 213 } // pct compliance
199 if e.attestation_hash == 0 { return 214 }
200
201 return 0
202}