package zhub import ( "testing" "time" ) func TestConfigLoading(t *testing.T) { // 测试配置加载 opts := &ConfigOptions{ ConfigPath: "example-config.yml", ConfigKey: "zhub", } config, err := LoadConfigWithOptions(opts) if err != nil { t.Skipf("Config file not found, skipping test: %v", err) } if config.Appname == "" { t.Error("Appname should not be empty") } if config.Addr == "" { t.Error("Addr should not be empty") } } func TestJSONConversion(t *testing.T) { // 测试 JSON 转换 testCases := []struct { input interface{} expected string }{ {"hello", "hello"}, {123, "123"}, {true, "true"}, {map[string]interface{}{"key": "value"}, `{"key":"value"}`}, } for _, tc := range testCases { result, err := ToJSON(tc.input) if err != nil { t.Errorf("ToJSON failed for %v: %v", tc.input, err) continue } if result != tc.expected { t.Errorf("ToJSON(%v) = %s, expected %s", tc.input, result, tc.expected) } } } func TestClientCreation(t *testing.T) { // 注意:这个测试需要实际的 zhub 服务器运行 t.Skip("Skipping integration test - requires zhub server") config := &Config{ Appname: "test-app", Addr: "127.0.0.1:1216", Groupid: "test-group", Auth: "test-token", } client, err := NewClient(config) if err != nil { t.Fatalf("Failed to create client: %v", err) } defer client.Close() // 测试发布消息 err = client.Publish("test-topic", "test message") if err != nil { t.Errorf("Failed to publish message: %v", err) } // 测试订阅 messageReceived := make(chan string, 1) client.Subscribe("test-topic", func(message string) { messageReceived <- message }) // 等待消息 select { case msg := <-messageReceived: if msg != "test message" { t.Errorf("Expected 'test message', got '%s'", msg) } case <-time.After(time.Second * 5): t.Error("Timeout waiting for message") } }