|
| 1 | +package capabilityclient_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/delegateas/ctx-sdk/capabilityclient" |
| 14 | +) |
| 15 | + |
| 16 | +func TestClient_New(t *testing.T) { |
| 17 | + if capabilityclient.New() == nil { |
| 18 | + t.Fatal("New() returned nil") |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestClient_Run_NoURL_Error(t *testing.T) { |
| 23 | + t.Setenv("CTX_CAPABILITY_URL", "") |
| 24 | + c := capabilityclient.New() |
| 25 | + _, err := c.Run(context.Background(), "ai.generate", map[string]string{"prompt": "hi"}) |
| 26 | + if err == nil { |
| 27 | + t.Fatal("expected error when CTX_CAPABILITY_URL is not set") |
| 28 | + } |
| 29 | + if !strings.Contains(err.Error(), "CTX_CAPABILITY_URL") { |
| 30 | + t.Errorf("error should mention CTX_CAPABILITY_URL, got: %v", err) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestClient_Run_Success(t *testing.T) { |
| 35 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 36 | + w.Header().Set("Content-Type", "application/json") |
| 37 | + w.Write([]byte(`{"output":"hello"}`)) //nolint:errcheck |
| 38 | + })) |
| 39 | + defer srv.Close() |
| 40 | + t.Setenv("CTX_CAPABILITY_URL", srv.URL) |
| 41 | + |
| 42 | + c := capabilityclient.New() |
| 43 | + out, err := c.Run(context.Background(), "ai.generate", map[string]string{"prompt": "hello"}) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("unexpected error: %v", err) |
| 46 | + } |
| 47 | + var result map[string]string |
| 48 | + if err := json.Unmarshal(out, &result); err != nil { |
| 49 | + t.Fatalf("unmarshal output: %v", err) |
| 50 | + } |
| 51 | + if result["output"] != "hello" { |
| 52 | + t.Errorf("output = %q; want %q", result["output"], "hello") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestClient_Run_NonOK(t *testing.T) { |
| 57 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 58 | + http.Error(w, "something went wrong", http.StatusInternalServerError) |
| 59 | + })) |
| 60 | + defer srv.Close() |
| 61 | + t.Setenv("CTX_CAPABILITY_URL", srv.URL) |
| 62 | + |
| 63 | + c := capabilityclient.New() |
| 64 | + _, err := c.Run(context.Background(), "ai.generate", map[string]string{"prompt": "hello"}) |
| 65 | + if err == nil { |
| 66 | + t.Fatal("expected error, got nil") |
| 67 | + } |
| 68 | + var runErr *capabilityclient.RunError |
| 69 | + if !errors.As(err, &runErr) { |
| 70 | + t.Fatalf("error type = %T; want *RunError", err) |
| 71 | + } |
| 72 | + if runErr.Capability != "ai.generate" { |
| 73 | + t.Errorf("Capability = %q; want %q", runErr.Capability, "ai.generate") |
| 74 | + } |
| 75 | + if !strings.Contains(runErr.Stderr, "something went wrong") { |
| 76 | + t.Errorf("Stderr = %q; want to contain 'something went wrong'", runErr.Stderr) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestClient_Run_SendsToken(t *testing.T) { |
| 81 | + const token = "secret-token" |
| 82 | + var gotAuth string |
| 83 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 84 | + gotAuth = r.Header.Get("Authorization") |
| 85 | + w.Header().Set("Content-Type", "application/json") |
| 86 | + w.Write([]byte(`{}`)) //nolint:errcheck |
| 87 | + })) |
| 88 | + defer srv.Close() |
| 89 | + t.Setenv("CTX_CAPABILITY_URL", srv.URL) |
| 90 | + t.Setenv("CTX_CAPABILITY_TOKEN", token) |
| 91 | + |
| 92 | + capabilityclient.New().Run(context.Background(), "ai.generate", nil) //nolint:errcheck |
| 93 | + if gotAuth != "Bearer "+token { |
| 94 | + t.Errorf("Authorization = %q; want %q", gotAuth, "Bearer "+token) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestClient_Run_MarshalInput(t *testing.T) { |
| 99 | + var gotBody []byte |
| 100 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 101 | + gotBody, _ = io.ReadAll(r.Body) |
| 102 | + w.Header().Set("Content-Type", "application/json") |
| 103 | + w.Write([]byte(`{"output":"ok"}`)) //nolint:errcheck |
| 104 | + })) |
| 105 | + defer srv.Close() |
| 106 | + t.Setenv("CTX_CAPABILITY_URL", srv.URL) |
| 107 | + |
| 108 | + type myInput struct { |
| 109 | + Prompt string `json:"prompt"` |
| 110 | + Count int `json:"count"` |
| 111 | + } |
| 112 | + capabilityclient.New().Run(context.Background(), "test.cap", myInput{Prompt: "test", Count: 42}) //nolint:errcheck |
| 113 | + |
| 114 | + var envelope struct { |
| 115 | + Input struct { |
| 116 | + Prompt string `json:"prompt"` |
| 117 | + Count int `json:"count"` |
| 118 | + } `json:"input"` |
| 119 | + } |
| 120 | + if err := json.Unmarshal(gotBody, &envelope); err != nil { |
| 121 | + t.Fatalf("unmarshal request body: %v", err) |
| 122 | + } |
| 123 | + if envelope.Input.Prompt != "test" || envelope.Input.Count != 42 { |
| 124 | + t.Errorf("input = %+v; want {Prompt:test Count:42}", envelope.Input) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestRunError_Error_WithStderr(t *testing.T) { |
| 129 | + err := &capabilityclient.RunError{ |
| 130 | + Capability: "ai.generate", |
| 131 | + Cause: errors.New("exit status 1"), |
| 132 | + Stderr: "something failed", |
| 133 | + } |
| 134 | + msg := err.Error() |
| 135 | + if !strings.Contains(msg, "ai.generate") { |
| 136 | + t.Errorf("Error() missing capability name: %q", msg) |
| 137 | + } |
| 138 | + if !strings.Contains(msg, "something failed") { |
| 139 | + t.Errorf("Error() missing stderr: %q", msg) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestRunError_Error_WithoutStderr(t *testing.T) { |
| 144 | + err := &capabilityclient.RunError{ |
| 145 | + Capability: "ai.generate", |
| 146 | + Cause: errors.New("exit status 1"), |
| 147 | + } |
| 148 | + if !strings.Contains(err.Error(), "ai.generate") { |
| 149 | + t.Errorf("Error() missing capability name: %q", err.Error()) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestRunError_Unwrap(t *testing.T) { |
| 154 | + cause := errors.New("root cause") |
| 155 | + runErr := &capabilityclient.RunError{Capability: "ai.generate", Cause: cause} |
| 156 | + if !errors.Is(runErr, cause) { |
| 157 | + t.Error("errors.Is failed — Unwrap not working") |
| 158 | + } |
| 159 | +} |
0 commit comments