omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / apps / translate / test.om
| 1 | note apps/translate/test.om: comprehensive pure-Omega test suite for aien-translate core. |
| 2 | note Compose with: si/directives/om2.om om/tls/sha2.om apps/translate/lib.om apps/translate/test.om |
| 3 | |
| 4 | text tt_pass_all "PASS apps/translate: all 10 test suites passed (positive + negative controls)\n" |
| 5 | text tt_fail_pre "FAIL apps/translate: check " |
| 6 | text tt_nl "\n" |
| 7 | |
| 8 | text tt_l_en "en" |
| 9 | text tt_l_eng "English" |
| 10 | text tt_l_zhcn "zh-CN" |
| 11 | text tt_l_zhtw "chinese_traditional" |
| 12 | text tt_l_ja "ja" |
| 13 | text tt_l_de "DEUTSCH" |
| 14 | text tt_l_ms " ms " |
| 15 | text tt_l_auto "auto" |
| 16 | text tt_l_bad "klingon" |
| 17 | text tt_l_bad2 "zz" |
| 18 | |
| 19 | text tt_src_keep "Aien runs on Aienux with Omega at https://aienos.com/docs. Run `aien start` for {user} and <g1>docs</g1> %s." |
| 20 | text tt_trn_ok "Aien fonctionne sur Aienux avec Omega sur https://aienos.com/docs. Lancez `aien start` pour {user} et <g1>doku</g1> %s." |
| 21 | text tt_trn_lost_keep "艾恩 fonctionne sur Aienux avec Omega sur https://aienos.com/docs. Lancez `aien start` pour {user} et <g1>doku</g1> %s." |
| 22 | text tt_trn_lost_url "Aien fonctionne sur Aienux avec Omega. Lancez `aien start` pour {user} et <g1>doku</g1> %s." |
| 23 | text tt_trn_lost_tag "Aien fonctionne sur Aienux avec Omega sur https://aienos.com/docs. Lancez `aien start` pour {user} et doku %s." |
| 24 | text tt_src_subword "AIENOS boots cleanly." |
| 25 | text tt_trn_subword "AIENOS demarre proprement." |
| 26 | text tt_tidy_src "Run aien start" |
| 27 | text tt_tidy_trn " Fuhren Sie `aien start` aus " |
| 28 | text tt_tidy_exp "Fuhren Sie aien start aus" |
| 29 | text tt_sw_link " Read the docs at https: " |
| 30 | |
| 31 | text tt_lc_ok "sure: [\"a\", \"b ] c\"] trailing" |
| 32 | text tt_lc_open "[\"a\", \"b ] still inside\"" |
| 33 | |
| 34 | text tt_req_ok "{\"from\":\"en\",\"to\":\"ja\",\"text\":\"Hello world. Second sentence!\",\"max_chunk\":16}" |
| 35 | text tt_req_bad_lang "{\"from\":\"en\",\"to\":\"xx\",\"text\":\"Hello\"}" |
| 36 | text tt_req_empty "{\"from\":\"en\",\"to\":\"ja\",\"text\":\" \"}" |
| 37 | text tt_req_bad_json "{\"from\":\"en\",\"to\":\"ja\"" |
| 38 | |
| 39 | text tt_c_a "alpha" |
| 40 | text tt_c_b "beta" |
| 41 | text tt_c_c "gamma" |
| 42 | text tt_c_d "delta" |
| 43 | text tt_c_va "ALPHA_JA" |
| 44 | text tt_c_vb "BETA_JA" |
| 45 | text tt_c_vc "GAMMA_JA" |
| 46 | text tt_c_vd "DELTA_JA" |
| 47 | |
| 48 | fn tt_write_text s view uchar * to void |
| 49 | n = len s |
| 50 | w = write 1 s 0 n |
| 51 | if lt w 0 |
| 52 | exit 99 |
| 53 | end |
| 54 | end |
| 55 | |
| 56 | fn tt_fail code ulong to int |
| 57 | tt_write_text tt_fail_pre |
| 58 | buf = new uchar 24 0 |
| 59 | x = code |
| 60 | k = ulong 0 |
| 61 | if eq x 0 |
| 62 | put buf 0 48 |
| 63 | k = 1 |
| 64 | else |
| 65 | while gt x 0 bound 24 |
| 66 | put buf k uchar add 48 rem x 10 |
| 67 | x = div x 10 |
| 68 | k = add k 1 |
| 69 | end |
| 70 | end |
| 71 | outb = new uchar 24 0 |
| 72 | j = ulong 0 |
| 73 | while lt j k bound 24 |
| 74 | put outb j get buf sub sub k 1 j |
| 75 | j = add j 1 |
| 76 | end |
| 77 | w = write 1 outb 0 k |
| 78 | if lt w 0 |
| 79 | exit 99 |
| 80 | end |
| 81 | tt_write_text tt_nl |
| 82 | ret int code |
| 83 | end |
| 84 | |
| 85 | fn tt_put_tmp s view uchar * to void |
| 86 | n = len s |
| 87 | i = ulong 0 |
| 88 | while lt i n bound 512 |
| 89 | put tr_g_tmp_out i get s i |
| 90 | i = add i 1 |
| 91 | end |
| 92 | put tr_g_tmp_out_len 0 n |
| 93 | end |
| 94 | |
| 95 | fn tt_tmp_eq s view uchar * to bool |
| 96 | n = len s |
| 97 | if ne get tr_g_tmp_out_len 0 n |
| 98 | ret false |
| 99 | end |
| 100 | i = ulong 0 |
| 101 | while lt i n bound 512 |
| 102 | if ne get tr_g_tmp_out i get s i |
| 103 | ret false |
| 104 | end |
| 105 | i = add i 1 |
| 106 | end |
| 107 | ret true |
| 108 | end |
| 109 | |
| 110 | note Check 1: ISO-639 and translate.js language allowlist validation + negative controls |
| 111 | fn test_langs to bool |
| 112 | if ne tr_lang_find tt_l_en 0 len tt_l_en 0 |
| 113 | ret false |
| 114 | end |
| 115 | if ne tr_lang_find tt_l_eng 0 len tt_l_eng 0 |
| 116 | ret false |
| 117 | end |
| 118 | if ne tr_lang_find tt_l_zhcn 0 len tt_l_zhcn 1 |
| 119 | ret false |
| 120 | end |
| 121 | if ne tr_lang_find tt_l_zhtw 0 len tt_l_zhtw 2 |
| 122 | ret false |
| 123 | end |
| 124 | if ne tr_lang_find tt_l_ja 0 len tt_l_ja 3 |
| 125 | ret false |
| 126 | end |
| 127 | if ne tr_lang_find tt_l_de 0 len tt_l_de 7 |
| 128 | ret false |
| 129 | end |
| 130 | if ne tr_lang_find tt_l_ms 0 len tt_l_ms 30 |
| 131 | ret false |
| 132 | end |
| 133 | if ne tr_lang_find_src tt_l_auto 0 len tt_l_auto TR_LANG_AUTO |
| 134 | ret false |
| 135 | end |
| 136 | note Negative controls: unsupported language codes refused |
| 137 | if ne tr_lang_find tt_l_bad 0 len tt_l_bad TR_LANG_INVALID |
| 138 | ret false |
| 139 | end |
| 140 | if ne tr_lang_find tt_l_bad2 0 len tt_l_bad2 TR_LANG_INVALID |
| 141 | ret false |
| 142 | end |
| 143 | if ne tr_lang_find tt_l_auto 0 len tt_l_auto TR_LANG_INVALID |
| 144 | ret false |
| 145 | end |
| 146 | ret true |
| 147 | end |
| 148 | |
| 149 | note Check 2: Multi-byte UTF-8 (1/2/3/4-byte code points) validation & codepoint counting |
| 150 | fn test_utf8_valid_multi to bool |
| 151 | note Build: "A" (1B) + e-acute (c3 a9, 2B) + CJK "日" (e6 97 a5, 3B) + emoji U+1F600 (f0 9f 98 80, 4B) = 10 bytes, 4 chars |
| 152 | b = new uchar 16 0 |
| 153 | put b 0 65 |
| 154 | put b 1 0xc3 |
| 155 | put b 2 0xa9 |
| 156 | put b 3 0xe6 |
| 157 | put b 4 0x97 |
| 158 | put b 5 0xa5 |
| 159 | put b 6 0xf0 |
| 160 | put b 7 0x9f |
| 161 | put b 8 0x98 |
| 162 | put b 9 0x80 |
| 163 | if ne tr_validate_text b 10 TR_OK |
| 164 | ret false |
| 165 | end |
| 166 | if ne tr_utf8_chars b 10 4 |
| 167 | ret false |
| 168 | end |
| 169 | ret true |
| 170 | end |
| 171 | |
| 172 | note Check 3: Negative controls for UTF-8, NUL, overlong, surrogate, >U+10FFFF, truncated |
| 173 | fn test_utf8_refusals to bool |
| 174 | b = new uchar 16 0 |
| 175 | note 3a: NUL byte embedded |
| 176 | put b 0 65 |
| 177 | put b 1 0 |
| 178 | put b 2 66 |
| 179 | if ne tr_validate_text b 3 TR_ERR_NUL |
| 180 | ret false |
| 181 | end |
| 182 | note 3b: Overlong 2-byte sequence (0xc0 0xaf) |
| 183 | put b 0 0xc0 |
| 184 | put b 1 0xaf |
| 185 | if ne tr_validate_text b 2 TR_ERR_UTF8 |
| 186 | ret false |
| 187 | end |
| 188 | note 3c: UTF-16 surrogate U+D800 (0xed 0xa0 0x80) |
| 189 | put b 0 0xed |
| 190 | put b 1 0xa0 |
| 191 | put b 2 0x80 |
| 192 | if ne tr_validate_text b 3 TR_ERR_UTF8 |
| 193 | ret false |
| 194 | end |
| 195 | note 3d: Codepoint > U+10FFFF (0xf4 0x90 0x80 0x80) |
| 196 | put b 0 0xf4 |
| 197 | put b 1 0x90 |
| 198 | put b 2 0x80 |
| 199 | put b 3 0x80 |
| 200 | if ne tr_validate_text b 4 TR_ERR_UTF8 |
| 201 | ret false |
| 202 | end |
| 203 | note 3e: Truncated 4-byte emoji (3 of 4 bytes) |
| 204 | put b 0 0xf0 |
| 205 | put b 1 0x9f |
| 206 | put b 2 0x98 |
| 207 | if ne tr_validate_text b 3 TR_ERR_UTF8 |
| 208 | ret false |
| 209 | end |
| 210 | note 3f: Stray continuation byte |
| 211 | put b 0 0x80 |
| 212 | if ne tr_validate_text b 1 TR_ERR_UTF8 |
| 213 | ret false |
| 214 | end |
| 215 | ret true |
| 216 | end |
| 217 | |
| 218 | note Check 4: UTF-8 chunker NEVER splits 2/3/4-byte code points even under tight max_chunk=5 |
| 219 | fn test_utf8_chunker_boundaries to bool |
| 220 | note 14 bytes: 2B(c3 a9) + 3B(e6 97 a5) + 4B(f0 9f 98 80) + 3B(e3 80 82 "。") + 2B(c3 bc) |
| 221 | b = new uchar 32 0 |
| 222 | put b 0 0xc3 |
| 223 | put b 1 0xa9 |
| 224 | put b 2 0xe6 |
| 225 | put b 3 0x97 |
| 226 | put b 4 0xa5 |
| 227 | put b 5 0xf0 |
| 228 | put b 6 0x9f |
| 229 | put b 7 0x98 |
| 230 | put b 8 0x80 |
| 231 | put b 9 0xe3 |
| 232 | put b 10 0x80 |
| 233 | put b 11 0x82 |
| 234 | put b 12 0xc3 |
| 235 | put b 13 0xbc |
| 236 | if ne tr_chunk_utf8 b 14 5 TR_OK |
| 237 | ret false |
| 238 | end |
| 239 | cnt = get tr_g_chk_cnt 0 |
| 240 | if lt cnt 3 |
| 241 | ret false |
| 242 | end |
| 243 | note Verify every chunk is <= 5 bytes, contiguous, covers all 14 bytes, and is strictly valid UTF-8! |
| 244 | tot = ulong 0 |
| 245 | k = ulong 0 |
| 246 | while lt k cnt bound 64 |
| 247 | off = get tr_g_chk_off k |
| 248 | clen = get tr_g_chk_len k |
| 249 | if ne off tot |
| 250 | ret false |
| 251 | end |
| 252 | if or eq clen 0 gt clen 5 |
| 253 | ret false |
| 254 | end |
| 255 | if not tr_utf8_valid part b off clen clen |
| 256 | ret false |
| 257 | end |
| 258 | tot = add tot clen |
| 259 | k = add k 1 |
| 260 | end |
| 261 | if ne tot 14 |
| 262 | ret false |
| 263 | end |
| 264 | note Negative control: max_chunk < 4 must be refused |
| 265 | if ne tr_chunk_utf8 b 14 3 TR_ERR_CHUNK |
| 266 | ret false |
| 267 | end |
| 268 | ret true |
| 269 | end |
| 270 | |
| 271 | note Check 5: Paragraph & sentence boundary preference in chunker |
| 272 | fn test_chunker_para_sent to bool |
| 273 | note "First para.\n\nSecond para!" (25 bytes), max_chunk=16 -> splits cleanly after \n\n at byte 13 |
| 274 | b = new uchar 32 0 |
| 275 | put b 0 70 |
| 276 | put b 1 105 |
| 277 | put b 2 114 |
| 278 | put b 3 115 |
| 279 | put b 4 116 |
| 280 | put b 5 32 |
| 281 | put b 6 112 |
| 282 | put b 7 97 |
| 283 | put b 8 114 |
| 284 | put b 9 97 |
| 285 | put b 10 46 |
| 286 | put b 11 10 |
| 287 | put b 12 10 |
| 288 | put b 13 83 |
| 289 | put b 14 101 |
| 290 | put b 15 99 |
| 291 | put b 16 111 |
| 292 | put b 17 110 |
| 293 | put b 18 100 |
| 294 | put b 19 32 |
| 295 | put b 20 112 |
| 296 | put b 21 97 |
| 297 | put b 22 114 |
| 298 | put b 23 97 |
| 299 | put b 24 33 |
| 300 | if ne tr_chunk_utf8 b 25 16 TR_OK |
| 301 | ret false |
| 302 | end |
| 303 | if ne get tr_g_chk_cnt 0 2 |
| 304 | ret false |
| 305 | end |
| 306 | if ne get tr_g_chk_off 0 0 |
| 307 | ret false |
| 308 | end |
| 309 | if ne get tr_g_chk_len 0 13 |
| 310 | ret false |
| 311 | end |
| 312 | if ne get tr_g_chk_off 1 13 |
| 313 | ret false |
| 314 | end |
| 315 | if ne get tr_g_chk_len 1 12 |
| 316 | ret false |
| 317 | end |
| 318 | ret true |
| 319 | end |
| 320 | |
| 321 | note Check 6: Oversize payload (> 16384 bytes and > 3000 chars) strictly refused |
| 322 | fn test_oversize_refusal to bool |
| 323 | bigb = new uchar 16385 65 |
| 324 | if ne tr_validate_text bigb 16385 TR_ERR_OVERSIZE |
| 325 | ret false |
| 326 | end |
| 327 | if ne tr_validate_text bigb 3001 TR_ERR_OVERSIZE |
| 328 | ret false |
| 329 | end |
| 330 | if ne tr_validate_text bigb 3000 TR_OK |
| 331 | ret false |
| 332 | end |
| 333 | ret true |
| 334 | end |
| 335 | |
| 336 | note Check 7: Deterministic SHA-256 LRU cache hit, miss, and bounded eviction |
| 337 | fn test_cache_lru to bool |
| 338 | tr_cache_init 3 |
| 339 | note Put A, B, C (fills cap=3, 0 evictions) |
| 340 | tt_put_tmp tt_c_va |
| 341 | tr_cache_put 0 3 tt_c_a len tt_c_a |
| 342 | tt_put_tmp tt_c_vb |
| 343 | tr_cache_put 0 3 tt_c_b len tt_c_b |
| 344 | tt_put_tmp tt_c_vc |
| 345 | tr_cache_put 0 3 tt_c_c len tt_c_c |
| 346 | if ne get tr_g_cache_evicts 0 0 |
| 347 | ret false |
| 348 | end |
| 349 | note Touch A so B becomes least-recently-used! |
| 350 | if ne tr_cache_get 0 3 tt_c_a len tt_c_a 1 |
| 351 | ret false |
| 352 | end |
| 353 | if not tt_tmp_eq tt_c_va |
| 354 | ret false |
| 355 | end |
| 356 | note Put D -> must evict B (LRU), keeping A, C, D! |
| 357 | tt_put_tmp tt_c_vd |
| 358 | tr_cache_put 0 3 tt_c_d len tt_c_d |
| 359 | if ne get tr_g_cache_evicts 0 1 |
| 360 | ret false |
| 361 | end |
| 362 | note B must now miss; A, C, D must hit! |
| 363 | if ne tr_cache_get 0 3 tt_c_b len tt_c_b 0 |
| 364 | ret false |
| 365 | end |
| 366 | if ne tr_cache_get 0 3 tt_c_a len tt_c_a 1 |
| 367 | ret false |
| 368 | end |
| 369 | if ne tr_cache_get 0 3 tt_c_c len tt_c_c 1 |
| 370 | ret false |
| 371 | end |
| 372 | if not tt_tmp_eq tt_c_vc |
| 373 | ret false |
| 374 | end |
| 375 | if ne tr_cache_get 0 3 tt_c_d len tt_c_d 1 |
| 376 | ret false |
| 377 | end |
| 378 | if not tt_tmp_eq tt_c_vd |
| 379 | ret false |
| 380 | end |
| 381 | note Different target lang (dst=6 French instead of dst=3 Japanese) must miss for A! |
| 382 | if ne tr_cache_get 0 6 tt_c_a len tt_c_a 0 |
| 383 | ret false |
| 384 | end |
| 385 | ret true |
| 386 | end |
| 387 | |
| 388 | note Check 8: Guard checks (keep-list, URL, backticks, placeholders, inline tags, runaway) |
| 389 | fn test_guard to bool |
| 390 | if ne tr_guard_check tt_src_keep len tt_src_keep tt_trn_ok len tt_trn_ok TR_OK |
| 391 | ret false |
| 392 | end |
| 393 | note Negative control: dropped keep word "Aien" must be refused |
| 394 | if ne tr_guard_check tt_src_keep len tt_src_keep tt_trn_lost_keep len tt_trn_lost_keep TR_ERR_GUARD |
| 395 | ret false |
| 396 | end |
| 397 | note Negative control: dropped URL must be refused |
| 398 | if ne tr_guard_check tt_src_keep len tt_src_keep tt_trn_lost_url len tt_trn_lost_url TR_ERR_GUARD |
| 399 | ret false |
| 400 | end |
| 401 | note Negative control: dropped <g1> inline tag must be refused |
| 402 | if ne tr_guard_check tt_src_keep len tt_src_keep tt_trn_lost_tag len tt_trn_lost_tag TR_ERR_GUARD |
| 403 | ret false |
| 404 | end |
| 405 | note Subword check: AIEN inside AIENOS is NOT falsely protected as separate word |
| 406 | if ne tr_guard_check tt_src_subword len tt_src_subword tt_trn_subword len tt_trn_subword TR_OK |
| 407 | ret false |
| 408 | end |
| 409 | note Runaway length check: 100 chars for 2-char source ("hi") > 2*4 + 80 = 88 -> refused |
| 410 | hi = new uchar 2 104 |
| 411 | runaway = new uchar 100 97 |
| 412 | if ne tr_guard_check hi 2 runaway 100 TR_ERR_GUARD |
| 413 | ret false |
| 414 | end |
| 415 | ret true |
| 416 | end |
| 417 | |
| 418 | note Check 9: Tidy & split_ws & list_closed |
| 419 | fn test_helpers to bool |
| 420 | w = tr_tidy tt_tidy_src len tt_tidy_src tt_tidy_trn len tt_tidy_trn |
| 421 | if ne w len tt_tidy_exp |
| 422 | ret false |
| 423 | end |
| 424 | if not tt_tmp_eq tt_tidy_exp |
| 425 | ret false |
| 426 | end |
| 427 | tr_split_ws tt_sw_link len tt_sw_link |
| 428 | note " Read the docs at https: " -> lead=2, core_end=18 ("Read the docs at") |
| 429 | if ne get tr_g_sw_lead 0 2 |
| 430 | ret false |
| 431 | end |
| 432 | if ne get tr_g_sw_core_end 0 18 |
| 433 | ret false |
| 434 | end |
| 435 | if not tr_list_closed tt_lc_ok len tt_lc_ok |
| 436 | ret false |
| 437 | end |
| 438 | if tr_list_closed tt_lc_open len tt_lc_open |
| 439 | ret false |
| 440 | end |
| 441 | ret true |
| 442 | end |
| 443 | |
| 444 | note Check 10: End-to-end JSONL request handling (cache miss then cache hit + negative refusals) |
| 445 | fn test_jsonl_pipeline to bool |
| 446 | tr_cache_init 16 |
| 447 | note First request: 3 chunks ("Hello world.", " Second ", "sentence!"), all 3 miss cache |
| 448 | rc1 = tr_handle_request tt_req_ok len tt_req_ok |
| 449 | if ne rc1 TR_OK |
| 450 | ret false |
| 451 | end |
| 452 | if ne get tr_g_chk_cnt 0 3 |
| 453 | ret false |
| 454 | end |
| 455 | if ne get tr_g_cache_misses 0 3 |
| 456 | ret false |
| 457 | end |
| 458 | if ne get tr_g_cache_hits 0 0 |
| 459 | ret false |
| 460 | end |
| 461 | note Second identical request: all 3 chunks HIT cache! |
| 462 | rc2 = tr_handle_request tt_req_ok len tt_req_ok |
| 463 | if ne rc2 TR_OK |
| 464 | ret false |
| 465 | end |
| 466 | if ne get tr_g_cache_hits 0 3 |
| 467 | ret false |
| 468 | end |
| 469 | note Negative control: unsupported language code |
| 470 | if ne tr_handle_request tt_req_bad_lang len tt_req_bad_lang TR_ERR_LANG |
| 471 | ret false |
| 472 | end |
| 473 | note Negative control: empty/whitespace text |
| 474 | if ne tr_handle_request tt_req_empty len tt_req_empty TR_ERR_EMPTY |
| 475 | ret false |
| 476 | end |
| 477 | note Negative control: malformed JSON |
| 478 | if ne tr_handle_request tt_req_bad_json len tt_req_bad_json TR_ERR_JSON |
| 479 | ret false |
| 480 | end |
| 481 | ret true |
| 482 | end |
| 483 | |
| 484 | fn main to int |
| 485 | if not test_langs |
| 486 | ret tt_fail 1 |
| 487 | end |
| 488 | if not test_utf8_valid_multi |
| 489 | ret tt_fail 2 |
| 490 | end |
| 491 | if not test_utf8_refusals |
| 492 | ret tt_fail 3 |
| 493 | end |
| 494 | if not test_utf8_chunker_boundaries |
| 495 | ret tt_fail 4 |
| 496 | end |
| 497 | if not test_chunker_para_sent |
| 498 | ret tt_fail 5 |
| 499 | end |
| 500 | if not test_oversize_refusal |
| 501 | ret tt_fail 6 |
| 502 | end |
| 503 | if not test_cache_lru |
| 504 | ret tt_fail 7 |
| 505 | end |
| 506 | if not test_guard |
| 507 | ret tt_fail 8 |
| 508 | end |
| 509 | if not test_helpers |
| 510 | ret tt_fail 9 |
| 511 | end |
| 512 | if not test_jsonl_pipeline |
| 513 | ret tt_fail 10 |
| 514 | end |
| 515 | tt_write_text tt_pass_all |
| 516 | ret 0 |
| 517 | end |