#!/usr/bin/env nu # Integration tests for nu_plugin_nats. # Requires: NATS server running on $NATS_SERVER (default nats://127.0.0.1:4222). # Run: nu tests/integration.nu def pass [label: string] { print $"✓ ($label)" } def fail [label: string, detail: string] { print $"✗ ($label): ($detail)" exit 1 } def assert_eq [label: string, got: any, expected: any] { if $got == $expected { pass $label } else { fail $label $"expected ($expected | to nuon), got ($got | to nuon)" } } def assert_gte [label: string, got: int, min: int] { if $got >= $min { pass $label } else { fail $label $"expected >= ($min), got ($got)" } } def assert_not_empty [label: string, val: list] { if ($val | length) > 0 { pass $label } else { fail $label "expected non-empty list, got empty" } } print "── nats stream setup ──────────────────────────────" # Plugin commands propagate errors as Nushell errors — if this fails the test aborts. nats stream setup pass "nats stream setup completed without error" print "── nats consumer setup ────────────────────────────" nats consumer setup pass "nats consumer setup completed without error" print "── nats status ────────────────────────────────────" let status = nats status assert_eq "status returns 6 streams" ($status | length) 6 let stream_names = $status | get stream for name in ["TASKS", "VAULT", "AUTH", "WORKSPACE", "AUDIT", "HEALTH"] { if not ($stream_names | any { |s| $s == $name }) { fail $"status contains ($name)" "stream missing from output" } pass $"stream ($name) present" } let workspace_row = $status | where stream == "WORKSPACE" | first assert_gte "WORKSPACE has cli-notifications consumer" $workspace_row.consumers 1 let audit_row = $status | where stream == "AUDIT" | first assert_gte "AUDIT has cli-notifications consumer" $audit_row.consumers 1 print "── nats pub ───────────────────────────────────────" let pub_ws = ( {test_id: "integration-1", status: "ok", ts: (date now | format date "%+")} | nats pub "workspace.test.event" ) assert_eq "pub WORKSPACE stream name" $pub_ws.stream "WORKSPACE" assert_gte "pub WORKSPACE sequence > 0" $pub_ws.sequence 1 let pub_audit = ( {test_id: "integration-1", action: "test_run"} | nats pub "audit.test.event" ) assert_eq "pub AUDIT stream name" $pub_audit.stream "AUDIT" assert_gte "pub AUDIT sequence > 0" $pub_audit.sequence 1 # provisioning. prefix applied automatically assert_eq "subject has provisioning. prefix" ($pub_ws.subject | str starts-with "provisioning.") true # Full subject passed in does not get double-prefixed let pub_full = ({v: 1} | nats pub "provisioning.workspace.test.explicit") assert_eq "full subject not double-prefixed" $pub_full.subject "provisioning.workspace.test.explicit" print "── nats notify ────────────────────────────────────" let msgs = nats notify --timeout 3 assert_not_empty "notify drains published messages" $msgs let ws_msg = $msgs | where stream == "WORKSPACE" | first assert_eq "WORKSPACE msg subject contains workspace.test" ($ws_msg.subject | str contains "workspace.test") true assert_eq "WORKSPACE payload is record" ($ws_msg.payload | describe | str starts-with "record") true assert_eq "WORKSPACE payload.test_id" $ws_msg.payload.test_id "integration-1" let audit_msg = $msgs | where stream == "AUDIT" | first assert_eq "AUDIT msg stream field" $audit_msg.stream "AUDIT" assert_eq "sequence type is int" ($ws_msg.sequence | describe) "int" assert_eq "timestamp is a string" ($ws_msg.timestamp | describe) "string" assert_eq "timestamp is not unknown" ($ws_msg.timestamp != "unknown") true print "" print "── all tests passed ───────────────────────────────"