// packages/mq_streams/examples/demo.vk // Self-contained verification suite & runnable example for `mq_streams`, `mq_streams/nats`, and `mq_streams/kafka`. use [ mq = mq_streams nats = mq_streams/nats kafka = mq_streams/kafka io ] assert-true = fn [cond bool, label str] enum [ if not cond [ println (str/join ['[FAIL] ' label] '') os/exit 1 ] print (str/join ['[PASS] ' label] '') .ok ] // --- 1. NATS Subject Wildcard Matching (`*` and `>`) --- wc_ok_1 = (nats/subject-match? 'orders.*.eu' 'orders.8841.eu') and (not (nats/subject-match? 'orders.*.eu' 'orders.8841.us')) and (not (nats/subject-match? 'orders.*.eu' 'orders.8841.west.eu')) wc_ok_2 = (nats/subject-match? 'events.>' 'events.auth') and (nats/subject-match? 'events.>' 'events.auth.login.mfa') and (not (nats/subject-match? 'events.>' 'events')) _ = assert-true (wc_ok_1 and wc_ok_2) 'NATS wildcard subject matcher (* single token and > multi-token tail)' // --- 2. NATS `HPUB` / `HMSG` / `CONNECT` / `SUB` Wire Codec & `io/mem-stream` Session --- hpub_wire = nats/encode-hpub/reply 'orders.99.eu' { 'Nats-Msg-Id' = 'msg-0099', 'Trace-Parent' = '00-4bf92f' } (bytes/from-str '{"amount":2500}') '_INBOX.reply.1' hmsg_wire = nats/encode-hmsg/reply/status/reason 'orders.99.eu' '7' { 'Nats-Stream' = 'ORDERS', 'Nats-Sequence' = '42' } (bytes/from-str '{"amount":2500}') '_INBOX.reply.1' 100 'FlowControl' mut mixed_wire = bytes/with-cap 512 _ = bytes/push-all mut mixed_wire hpub_wire _ = bytes/push-all mut mixed_wire hmsg_wire _ = bytes/push-all mut mixed_wire (nats/encode-sub/queue 'orders.*.eu' '7' 'workers') _ = bytes/push-all mut mixed_wire (nats/encode-ok) _ = bytes/push-all mut mixed_wire (nats/encode-err 'Authorization Violation') parsed_frames = nats/decode-frames mixed_wire f_hpub = parsed_frames[0] f_hmsg = parsed_frames[1] f_sub = parsed_frames[2] f_ok = parsed_frames[3] f_err = parsed_frames[4] nats_wire_ok_1 = (len parsed_frames) == 5 and f_hpub.kind == .hpub and f_hpub.subject == 'orders.99.eu' and f_hpub.reply_to == '_INBOX.reply.1' nats_wire_ok_2 = (map/get f_hpub.headers 'Nats-Msg-Id') == 'msg-0099' and (bytes/to-str f_hpub.payload) == '{"amount":2500}' nats_wire_ok_3 = f_hmsg.kind == .hmsg and f_hmsg.sid == '7' and f_hmsg.status_code == 100 and (map/get f_hmsg.headers 'Nats-Stream') == 'ORDERS' nats_wire_ok_4 = f_sub.kind == .sub and f_sub.queue_group == 'workers' and f_ok.kind == .ok_ack and f_err.error_text == 'Authorization Violation' _ = assert-true (nats_wire_ok_1 and nats_wire_ok_2 and nats_wire_ok_3 and nats_wire_ok_4) 'NATS HPUB, HMSG, SUB, +OK, and -ERR wire protocol roundtrip' // Test `connect-stream` over `io/mem-stream` with server INFO + auto-PONG + HMSG delivery mut server_feed = bytes/with-cap 256 _ = bytes/push-all mut server_feed (nats/encode-info { server_id = 'NATS-VEKO-1', max_payload = 1048576, headers = true }) _ = bytes/push-all mut server_feed (nats/encode-ping) _ = bytes/push-all mut server_feed (nats/encode-hmsg 'events.billing.paid' '1' { 'X-Source' = 'stripe' } (bytes/from-str 'inv_771')) mut mem_st = io/mem-stream server_feed mut nc = nats/connect-stream mut mem_st { name = 'demo-worker' } sid1 = nats/client-sub mut nc 'events.>' _ = nats/client-hpub mut nc 'events.billing.paid' { 'X-Source' = 'stripe' } (bytes/from-str 'inv_771') delivered_frm = nats/client-read-frame mut nc _ = assert-true (sid1 == '1' and (map/get nc.server_info 'server_id') == 'NATS-VEKO-1' and delivered_frm.kind == .hmsg and (bytes/to-str delivered_frm.payload) == 'inv_771') 'NATS io/Stream client handshake, auto-PONG, and HMSG stream read' // --- 3. Kafka CRC-32C Castagnoli & Multi-Record KIP-98 RecordBatch v2 --- crc_vec = kafka/crc32c (bytes/from-str '123456789') _ = assert-true (crc_vec == 0xe3069283) 'CRC-32C Castagnoli (0x82F63B78) matches RFC 3720 check vector 0xE3069283' recs_in = [ kafka/KafkaRecord { timestamp_ms = 1730000000100 key = bytes/from-str 'acct-10' value = bytes/from-str '{"ev":"credit","cents":5000}' headers = [ kafka/RecordHeader { key = 'trace-id', value = (bytes/from-str 'tr-aaa') } ] } kafka/KafkaRecord { timestamp_ms = 1730000000250 key = bytes/from-str 'acct-20' value = bytes/from-str '{"ev":"debit","cents":1200}' headers = [ kafka/RecordHeader { key = 'trace-id', value = (bytes/from-str 'tr-bbb') } kafka/RecordHeader { key = 'region', value = (bytes/from-str 'eu-central') } ] } kafka/KafkaRecord { timestamp_ms = 1730000000400 key = bytes/with-cap 0 has_key = false value = bytes/from-str '{"ev":"heartbeat"}' headers = [] } ] batch_wire = kafka/encode-record-batch/zstd/producer-id 500 1730000000100 recs_in true 9001 dec_batch = kafka/decode-record-batch batch_wire kb_ok_1 = dec_batch.magic == 2 and dec_batch.base_offset == 500 and dec_batch.producer_id == 9001 and (dec_batch.attributes & 7) == 4 kb_ok_2 = (len dec_batch.records) == 3 and dec_batch.records[0].offset == 500 and dec_batch.records[1].offset == 501 and dec_batch.records[2].offset == 502 kb_ok_3 = dec_batch.records[1].timestamp_ms == 1730000000250 and (bytes/to-str dec_batch.records[1].key) == 'acct-20' and dec_batch.records[1].headers[1].key == 'region' kb_ok_4 = (not dec_batch.records[2].has_key) and (bytes/to-str dec_batch.records[2].value) == '{"ev":"heartbeat"}' _ = assert-true (kb_ok_1 and kb_ok_2 and kb_ok_3 and kb_ok_4) 'Kafka RecordBatch v2 (magic=2, Zstd, headers, null-key) encode + CRC32C-verify + decode' // Corrupt 1 byte inside the batch payload and confirm CRC-32C catches corruption mut corrupted = bytes/with-cap (len batch_wire) _ = bytes/push-all mut corrupted batch_wire last_b = (len corrupted) - 1 _ = bytes/set mut corrupted last_b (corrupted[last_b] ^ 0xff) tampered_ok = err? (kafka/decode-record-batch/or corrupted [e -> e]) _ = assert-true tampered_ok 'Kafka RecordBatch v2 rejects bit-flipped payload on CRC-32C mismatch' // ProduceRequest / FetchRequest / FetchResponse wire roundtrip prod_pkt = kafka/encode-produce-request 42 'veko-ingestor' 'ledger.events' 0 batch_wire fetch_pkt = kafka/encode-fetch-request 43 'veko-consumer' 'ledger.events' 0 500 prod_unframed = kafka/unframe-packet prod_pkt prod_hdr = kafka/decode-request-header-v2 prod_unframed 0 fetch_resp_pkt = kafka/encode-fetch-response 43 'ledger.events' 0 503 batch_wire fetched_recs = kafka/decode-fetch-records fetch_resp_pkt _ = assert-true (prod_hdr.api_key == (kafka/API-PRODUCE) and prod_hdr.correlation_id == 42 and (len (kafka/unframe-packet fetch_pkt)) > 20 and (len fetched_recs) == 3 and fetched_recs[0].offset == 500) 'Kafka ProduceRequest, FetchRequest, and FetchResponse wire roundtrip' // --- 4. Unified `MemoryBroker` Wildcard Pub/Sub (Queue Groups) & Partition Log --- mut broker = mq/new-broker audit_sid = mq/subscribe mut broker 'orders.>' q_sid_a = mq/subscribe/queue mut broker 'orders.*.eu' 'eu-fulfillment' q_sid_b = mq/subscribe/queue mut broker 'orders.*.eu' 'eu-fulfillment' _ = mq/publish/headers mut broker 'orders.101.eu' (bytes/from-str 'ord-101') { 'Idemp-Key' = 'k1' } _ = mq/publish/headers mut broker 'orders.102.eu' (bytes/from-str 'ord-102') { 'Idemp-Key' = 'k2' } _ = mq/publish mut broker 'orders.103.us' (bytes/from-str 'ord-103') audit_msgs = mq/drain-sub mut broker audit_sid qa_msgs = mq/drain-sub mut broker q_sid_a qb_msgs = mq/drain-sub mut broker q_sid_b hwm = mq/append-partition-batch mut broker 'orders-log' 0 1730000000100 recs_in tail_recs = mq/fetch-partition broker 'orders-log' 0 1 _ = assert-true ((len audit_msgs) == 3 and (len qa_msgs) == 1 and (len qb_msgs) == 1 and hwm == 3 and (len tail_recs) == 2 and tail_recs[0].offset == 1) 'MemoryBroker wildcard fan-out, queue-group round-robin, and partition log offset fetch' print 'mq_streams: all verification checks passed'