Files
bzzz/pkg/hmmm_adapter/smoke_test.go
anthonyrawlins 92779523c0 🚀 Complete BZZZ Issue Resolution - All 17 Issues Solved
Comprehensive multi-agent implementation addressing all issues from INDEX.md:

## Core Architecture & Validation
-  Issue 001: UCXL address validation at all system boundaries
-  Issue 002: Fixed search parsing bug in encrypted storage
-  Issue 003: Wired UCXI P2P announce and discover functionality
-  Issue 011: Aligned temporal grammar and documentation
-  Issue 012: SLURP idempotency, backpressure, and DLQ implementation
-  Issue 013: Linked SLURP events to UCXL decisions and DHT

## API Standardization & Configuration
-  Issue 004: Standardized UCXI payloads to UCXL codes
-  Issue 010: Status endpoints and configuration surface

## Infrastructure & Operations
-  Issue 005: Election heartbeat on admin transition
-  Issue 006: Active health checks for PubSub and DHT
-  Issue 007: DHT replication and provider records
-  Issue 014: SLURP leadership lifecycle and health probes
-  Issue 015: Comprehensive monitoring, SLOs, and alerts

## Security & Access Control
-  Issue 008: Key rotation and role-based access policies

## Testing & Quality Assurance
-  Issue 009: Integration tests for UCXI + DHT encryption + search
-  Issue 016: E2E tests for HMMM → SLURP → UCXL workflow

## HMMM Integration
-  Issue 017: HMMM adapter wiring and comprehensive testing

## Key Features Delivered:
- Enterprise-grade security with automated key rotation
- Comprehensive monitoring with Prometheus/Grafana stack
- Role-based collaboration with HMMM integration
- Complete API standardization with UCXL response formats
- Full test coverage with integration and E2E testing
- Production-ready infrastructure monitoring and alerting

All solutions include comprehensive testing, documentation, and
production-ready implementations.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-29 12:39:38 +10:00

301 lines
9.4 KiB
Go

package hmmm_adapter
import (
"context"
"encoding/json"
"fmt"
"sync"
"testing"
"time"
)
// TestPerIssueTopicSmokeTest tests the per-issue topic functionality without full BZZZ integration
func TestPerIssueTopicSmokeTest(t *testing.T) {
// Mock pubsub functions that track calls
joinedTopics := make(map[string]int)
publishedMessages := make(map[string][]byte)
var mu sync.Mutex
joiner := func(topic string) error {
mu.Lock()
defer mu.Unlock()
joinedTopics[topic]++
return nil
}
publisher := func(topic string, payload []byte) error {
mu.Lock()
defer mu.Unlock()
publishedMessages[topic] = payload
return nil
}
adapter := NewAdapter(joiner, publisher)
// Test per-issue topic publishing
issueID := int64(42)
topic := fmt.Sprintf("bzzz/meta/issue/%d", issueID)
testMessage := map[string]interface{}{
"version": 1,
"type": "meta_msg",
"issue_id": issueID,
"thread_id": "test-thread-42",
"msg_id": "smoke-test-msg-1",
"node_id": "test-node-id",
"hop_count": 0,
"timestamp": time.Now().UTC(),
"message": "Smoke test: HMMM per-issue room initialized.",
}
payload, err := json.Marshal(testMessage)
if err != nil {
t.Fatalf("Failed to marshal test message: %v", err)
}
// Publish the message
err = adapter.Publish(context.Background(), topic, payload)
if err != nil {
t.Fatalf("Failed to publish message: %v", err)
}
// Verify join was called once
mu.Lock()
if joinedTopics[topic] != 1 {
t.Errorf("Expected topic %s to be joined once, got %d times", topic, joinedTopics[topic])
}
// Verify message was published
if _, exists := publishedMessages[topic]; !exists {
t.Errorf("Expected message to be published to topic %s", topic)
}
mu.Unlock()
// Verify metrics
metrics := adapter.GetMetrics()
if metrics.PublishCount != 1 {
t.Errorf("Expected publish count 1, got %d", metrics.PublishCount)
}
if metrics.JoinCount != 1 {
t.Errorf("Expected join count 1, got %d", metrics.JoinCount)
}
if metrics.ErrorCount != 0 {
t.Errorf("Expected error count 0, got %d", metrics.ErrorCount)
}
// Test publishing another message to the same topic (should not join again)
testMessage2 := map[string]interface{}{
"version": 1,
"type": "meta_msg",
"issue_id": issueID,
"thread_id": "test-thread-42",
"msg_id": "smoke-test-msg-2",
"node_id": "test-node-id",
"hop_count": 0,
"timestamp": time.Now().UTC(),
"message": "Second message in same issue room.",
}
payload2, err := json.Marshal(testMessage2)
if err != nil {
t.Fatalf("Failed to marshal second test message: %v", err)
}
err = adapter.Publish(context.Background(), topic, payload2)
if err != nil {
t.Fatalf("Failed to publish second message: %v", err)
}
// Verify join was still called only once (topic cached)
mu.Lock()
if joinedTopics[topic] != 1 {
t.Errorf("Expected topic %s to still be joined only once (cached), got %d times", topic, joinedTopics[topic])
}
mu.Unlock()
// Verify updated metrics
metrics = adapter.GetMetrics()
if metrics.PublishCount != 2 {
t.Errorf("Expected publish count 2, got %d", metrics.PublishCount)
}
if metrics.JoinCount != 1 {
t.Errorf("Expected join count to remain 1 (cached), got %d", metrics.JoinCount)
}
t.Logf("✅ Per-issue topic smoke test passed: topic=%s, publishes=%d, joins=%d",
topic, metrics.PublishCount, metrics.JoinCount)
}
// TestMultiplePerIssueTopics tests publishing to multiple different per-issue topics
func TestMultiplePerIssueTopics(t *testing.T) {
joinedTopics := make(map[string]int)
publishedMessages := make(map[string][]byte)
var mu sync.Mutex
joiner := func(topic string) error {
mu.Lock()
defer mu.Unlock()
joinedTopics[topic]++
return nil
}
publisher := func(topic string, payload []byte) error {
mu.Lock()
defer mu.Unlock()
publishedMessages[topic] = payload
return nil
}
adapter := NewAdapter(joiner, publisher)
// Test multiple issues
issueIDs := []int64{100, 200, 300}
for _, issueID := range issueIDs {
topic := fmt.Sprintf("bzzz/meta/issue/%d", issueID)
testMessage := map[string]interface{}{
"version": 1,
"type": "meta_msg",
"issue_id": issueID,
"thread_id": fmt.Sprintf("issue-%d", issueID),
"msg_id": fmt.Sprintf("msg-%d-1", issueID),
"node_id": "test-node-id",
"hop_count": 0,
"timestamp": time.Now().UTC(),
"message": fmt.Sprintf("Message for issue %d", issueID),
}
payload, err := json.Marshal(testMessage)
if err != nil {
t.Fatalf("Failed to marshal message for issue %d: %v", issueID, err)
}
err = adapter.Publish(context.Background(), topic, payload)
if err != nil {
t.Fatalf("Failed to publish message for issue %d: %v", issueID, err)
}
}
// Verify all topics were joined once
mu.Lock()
for _, issueID := range issueIDs {
topic := fmt.Sprintf("bzzz/meta/issue/%d", issueID)
if joinedTopics[topic] != 1 {
t.Errorf("Expected topic %s to be joined once, got %d times", topic, joinedTopics[topic])
}
if _, exists := publishedMessages[topic]; !exists {
t.Errorf("Expected message to be published to topic %s", topic)
}
}
mu.Unlock()
// Verify metrics
metrics := adapter.GetMetrics()
expectedJoinCount := int64(len(issueIDs))
expectedPublishCount := int64(len(issueIDs))
if metrics.PublishCount != expectedPublishCount {
t.Errorf("Expected publish count %d, got %d", expectedPublishCount, metrics.PublishCount)
}
if metrics.JoinCount != expectedJoinCount {
t.Errorf("Expected join count %d, got %d", expectedJoinCount, metrics.JoinCount)
}
if metrics.ErrorCount != 0 {
t.Errorf("Expected error count 0, got %d", metrics.ErrorCount)
}
// Verify all topics are cached
cachedTopics := adapter.GetJoinedTopics()
if len(cachedTopics) != len(issueIDs) {
t.Errorf("Expected %d cached topics, got %d", len(issueIDs), len(cachedTopics))
}
t.Logf("✅ Multiple per-issue topics test passed: issues=%v, publishes=%d, joins=%d",
issueIDs, metrics.PublishCount, metrics.JoinCount)
}
// TestHMMMMessageFormat tests that the adapter can handle HMMM-formatted messages
func TestHMMMMessageFormat(t *testing.T) {
joinedTopics := make(map[string]bool)
var publishedPayload []byte
var mu sync.Mutex
joiner := func(topic string) error {
mu.Lock()
defer mu.Unlock()
joinedTopics[topic] = true
return nil
}
publisher := func(topic string, payload []byte) error {
mu.Lock()
defer mu.Unlock()
publishedPayload = make([]byte, len(payload))
copy(publishedPayload, payload)
return nil
}
adapter := NewAdapter(joiner, publisher)
// Create HMMM-compliant message (following HMMM message schema)
hmmmMessage := map[string]interface{}{
"version": 1,
"type": "meta_msg",
"issue_id": 42,
"thread_id": "issue-42",
"msg_id": "seed-" + fmt.Sprintf("%d", time.Now().UnixNano()),
"parent_id": nil,
"node_id": "test-node-12D3KooW",
"author": "test-author",
"hop_count": 0,
"timestamp": time.Now().UTC(),
"message": "Seed: HMMM per-issue room initialized.",
}
payload, err := json.Marshal(hmmmMessage)
if err != nil {
t.Fatalf("Failed to marshal HMMM message: %v", err)
}
topic := "bzzz/meta/issue/42"
err = adapter.Publish(context.Background(), topic, payload)
if err != nil {
t.Fatalf("Failed to publish HMMM message: %v", err)
}
// Verify the message was published correctly
mu.Lock()
if !joinedTopics[topic] {
t.Errorf("Expected topic %s to be joined", topic)
}
if len(publishedPayload) == 0 {
t.Fatalf("Expected payload to be published")
}
// Unmarshal and verify the published payload matches the original
var publishedMessage map[string]interface{}
err = json.Unmarshal(publishedPayload, &publishedMessage)
mu.Unlock()
if err != nil {
t.Fatalf("Failed to unmarshal published payload: %v", err)
}
// Verify key fields
if publishedMessage["version"].(float64) != 1 {
t.Errorf("Expected version 1, got %v", publishedMessage["version"])
}
if publishedMessage["type"].(string) != "meta_msg" {
t.Errorf("Expected type 'meta_msg', got %v", publishedMessage["type"])
}
if publishedMessage["issue_id"].(float64) != 42 {
t.Errorf("Expected issue_id 42, got %v", publishedMessage["issue_id"])
}
if publishedMessage["message"].(string) != "Seed: HMMM per-issue room initialized." {
t.Errorf("Expected specific message, got %v", publishedMessage["message"])
}
t.Logf("✅ HMMM message format test passed: successfully published and parsed HMMM-compliant message")
}