mirror of
https://github.com/JuLi0n21/goTsClient.git
synced 2026-06-05 03:06:28 +00:00
add broadcasts to api clie
nt
This commit is contained in:
+51
-4
@@ -1,6 +1,7 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -29,6 +30,12 @@ func (s *MockAPI) GetUser(id int) (User, error) { return User{}, nil }
|
||||
func (s *MockAPI) CreateOrder(o Order) error { return nil }
|
||||
func (s *MockAPI) NoArgs() error { return nil }
|
||||
|
||||
type MockBroadcasts struct {
|
||||
UserUpdated User `json:"user_updated"`
|
||||
SystemAlert string `json:"system_alert"`
|
||||
Tick float64 `json:"tick"`
|
||||
}
|
||||
|
||||
func TestGoTypeToTS(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -75,7 +82,7 @@ func TestCollectStructs(t *testing.T) {
|
||||
|
||||
func TestGenerateTS_Success(t *testing.T) {
|
||||
api := &MockAPI{}
|
||||
output, err := GenerateTS(reflect.TypeOf(api), "TestClient")
|
||||
output, err := GenerateTS(reflect.TypeOf(api), nil, "TestClient")
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateTS failed: %v", err)
|
||||
}
|
||||
@@ -89,6 +96,30 @@ func TestGenerateTS_Success(t *testing.T) {
|
||||
"CreateOrder(order: Order): Promise<RPCResult<any>>;",
|
||||
"useBackend(url: string = '/ws')",
|
||||
}
|
||||
fmt.Println(output)
|
||||
|
||||
for _, s := range expectedStrings {
|
||||
if !strings.Contains(output, s) {
|
||||
t.Errorf("Generated TS missing expected string: %s", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateTS_WithBroadcasts(t *testing.T) {
|
||||
api := &MockAPI{}
|
||||
broadcasts := &MockBroadcasts{}
|
||||
output, err := GenerateTS(reflect.TypeOf(api), reflect.TypeOf(broadcasts), "TestClient")
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateTS failed: %v", err)
|
||||
}
|
||||
|
||||
expectedStrings := []string{
|
||||
"export interface BroadcastEvents {",
|
||||
"'user_updated': User;",
|
||||
"'system_alert': string;",
|
||||
"'tick': number;",
|
||||
"export type BroadcastTopic = keyof BroadcastEvents;",
|
||||
}
|
||||
|
||||
for _, s := range expectedStrings {
|
||||
if !strings.Contains(output, s) {
|
||||
@@ -110,7 +141,7 @@ func TestGenerateTS_Validation(t *testing.T) {
|
||||
|
||||
t.Run("InvalidReturnCount", func(t *testing.T) {
|
||||
api := &APIWithTooManyReturns{}
|
||||
_, err := GenerateTS(reflect.TypeOf(api), "Client")
|
||||
_, err := GenerateTS(reflect.TypeOf(api), nil, "Client")
|
||||
if err == nil {
|
||||
t.Error("Expected error for method with 3 return values")
|
||||
}
|
||||
@@ -118,7 +149,7 @@ func TestGenerateTS_Validation(t *testing.T) {
|
||||
|
||||
t.Run("NoResidentError", func(t *testing.T) {
|
||||
api := &APIWithNoErrorHandler{}
|
||||
_, err := GenerateTS(reflect.TypeOf(api), "Client")
|
||||
_, err := GenerateTS(reflect.TypeOf(api), nil, "Client")
|
||||
if err == nil {
|
||||
t.Error("Expected error for method missing 'error' return type")
|
||||
}
|
||||
@@ -130,7 +161,23 @@ func TestGenClient(t *testing.T) {
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
api := &MockAPI{}
|
||||
err := GenClient(api, tmpFile)
|
||||
err := GenClient(api, nil, tmpFile)
|
||||
if err != nil {
|
||||
t.Fatalf("GenClient failed: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(tmpFile); os.IsNotExist(err) {
|
||||
t.Error("GenClient did not create the file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenClient_WithBroadcasts(t *testing.T) {
|
||||
tmpFile := "test_client_broadcast.ts"
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
api := &MockAPI{}
|
||||
broadcasts := &MockBroadcasts{}
|
||||
err := GenClient(api, broadcasts, tmpFile)
|
||||
if err != nil {
|
||||
t.Fatalf("GenClient failed: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user