client_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "strings"
  10. "testing"
  11. )
  12. func TestClientFromEnvironment(t *testing.T) {
  13. type testCase struct {
  14. value string
  15. expect string
  16. err error
  17. }
  18. testCases := map[string]*testCase{
  19. "empty": {value: "", expect: "http://127.0.0.1:11434"},
  20. "only address": {value: "1.2.3.4", expect: "http://1.2.3.4:11434"},
  21. "only port": {value: ":1234", expect: "http://:1234"},
  22. "address and port": {value: "1.2.3.4:1234", expect: "http://1.2.3.4:1234"},
  23. "scheme http and address": {value: "http://1.2.3.4", expect: "http://1.2.3.4:80"},
  24. "scheme https and address": {value: "https://1.2.3.4", expect: "https://1.2.3.4:443"},
  25. "scheme, address, and port": {value: "https://1.2.3.4:1234", expect: "https://1.2.3.4:1234"},
  26. "hostname": {value: "example.com", expect: "http://example.com:11434"},
  27. "hostname and port": {value: "example.com:1234", expect: "http://example.com:1234"},
  28. "scheme http and hostname": {value: "http://example.com", expect: "http://example.com:80"},
  29. "scheme https and hostname": {value: "https://example.com", expect: "https://example.com:443"},
  30. "scheme, hostname, and port": {value: "https://example.com:1234", expect: "https://example.com:1234"},
  31. "trailing slash": {value: "example.com/", expect: "http://example.com:11434"},
  32. "trailing slash port": {value: "example.com:1234/", expect: "http://example.com:1234"},
  33. }
  34. for k, v := range testCases {
  35. t.Run(k, func(t *testing.T) {
  36. t.Setenv("OLLAMA_HOST", v.value)
  37. client, err := ClientFromEnvironment()
  38. if err != v.err {
  39. t.Fatalf("expected %s, got %s", v.err, err)
  40. }
  41. if client.base.String() != v.expect {
  42. t.Fatalf("expected %s, got %s", v.expect, client.base.String())
  43. }
  44. })
  45. }
  46. }
  47. // testError represents an internal error type with status code and message
  48. // this is used since the error response from the server is not a standard error struct
  49. type testError struct {
  50. message string
  51. statusCode int
  52. }
  53. func (e testError) Error() string {
  54. return e.message
  55. }
  56. func TestClientStream(t *testing.T) {
  57. testCases := []struct {
  58. name string
  59. responses []any
  60. wantErr string
  61. }{
  62. {
  63. name: "immediate error response",
  64. responses: []any{
  65. testError{
  66. message: "test error message",
  67. statusCode: http.StatusBadRequest,
  68. },
  69. },
  70. wantErr: "test error message",
  71. },
  72. {
  73. name: "error after successful chunks, ok response",
  74. responses: []any{
  75. ChatResponse{Message: Message{Content: "partial response 1"}},
  76. ChatResponse{Message: Message{Content: "partial response 2"}},
  77. testError{
  78. message: "mid-stream error",
  79. statusCode: http.StatusOK,
  80. },
  81. },
  82. wantErr: "mid-stream error",
  83. },
  84. {
  85. name: "successful stream completion",
  86. responses: []any{
  87. ChatResponse{Message: Message{Content: "chunk 1"}},
  88. ChatResponse{Message: Message{Content: "chunk 2"}},
  89. ChatResponse{
  90. Message: Message{Content: "final chunk"},
  91. Done: true,
  92. DoneReason: "stop",
  93. },
  94. },
  95. },
  96. }
  97. for _, tc := range testCases {
  98. t.Run(tc.name, func(t *testing.T) {
  99. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  100. flusher, ok := w.(http.Flusher)
  101. if !ok {
  102. t.Fatal("expected http.Flusher")
  103. }
  104. w.Header().Set("Content-Type", "application/x-ndjson")
  105. for _, resp := range tc.responses {
  106. if errResp, ok := resp.(testError); ok {
  107. w.WriteHeader(errResp.statusCode)
  108. err := json.NewEncoder(w).Encode(map[string]string{
  109. "error": errResp.message,
  110. })
  111. if err != nil {
  112. t.Fatal("failed to encode error response:", err)
  113. }
  114. return
  115. }
  116. if err := json.NewEncoder(w).Encode(resp); err != nil {
  117. t.Fatalf("failed to encode response: %v", err)
  118. }
  119. flusher.Flush()
  120. }
  121. }))
  122. defer ts.Close()
  123. client := NewClient(&url.URL{Scheme: "http", Host: ts.Listener.Addr().String()}, http.DefaultClient)
  124. var receivedChunks []ChatResponse
  125. err := client.stream(context.Background(), http.MethodPost, "/v1/chat", nil, func(chunk []byte) error {
  126. var resp ChatResponse
  127. if err := json.Unmarshal(chunk, &resp); err != nil {
  128. return fmt.Errorf("failed to unmarshal chunk: %w", err)
  129. }
  130. receivedChunks = append(receivedChunks, resp)
  131. return nil
  132. })
  133. if tc.wantErr != "" {
  134. if err == nil {
  135. t.Fatal("expected error but got nil")
  136. }
  137. if !strings.Contains(err.Error(), tc.wantErr) {
  138. t.Errorf("expected error containing %q, got %v", tc.wantErr, err)
  139. }
  140. return
  141. }
  142. if err != nil {
  143. t.Errorf("unexpected error: %v", err)
  144. }
  145. })
  146. }
  147. }
  148. func TestClientDo(t *testing.T) {
  149. testCases := []struct {
  150. name string
  151. response any
  152. wantErr string
  153. }{
  154. {
  155. name: "immediate error response",
  156. response: testError{
  157. message: "test error message",
  158. statusCode: http.StatusBadRequest,
  159. },
  160. wantErr: "test error message",
  161. },
  162. {
  163. name: "server error response",
  164. response: testError{
  165. message: "internal error",
  166. statusCode: http.StatusInternalServerError,
  167. },
  168. wantErr: "internal error",
  169. },
  170. {
  171. name: "successful response",
  172. response: struct {
  173. ID string `json:"id"`
  174. Success bool `json:"success"`
  175. }{
  176. ID: "msg_123",
  177. Success: true,
  178. },
  179. },
  180. }
  181. for _, tc := range testCases {
  182. t.Run(tc.name, func(t *testing.T) {
  183. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  184. if errResp, ok := tc.response.(testError); ok {
  185. w.WriteHeader(errResp.statusCode)
  186. err := json.NewEncoder(w).Encode(map[string]string{
  187. "error": errResp.message,
  188. })
  189. if err != nil {
  190. t.Fatal("failed to encode error response:", err)
  191. }
  192. return
  193. }
  194. w.Header().Set("Content-Type", "application/json")
  195. if err := json.NewEncoder(w).Encode(tc.response); err != nil {
  196. t.Fatalf("failed to encode response: %v", err)
  197. }
  198. }))
  199. defer ts.Close()
  200. client := NewClient(&url.URL{Scheme: "http", Host: ts.Listener.Addr().String()}, http.DefaultClient)
  201. var resp struct {
  202. ID string `json:"id"`
  203. Success bool `json:"success"`
  204. }
  205. err := client.do(context.Background(), http.MethodPost, "/v1/messages", nil, &resp)
  206. if tc.wantErr != "" {
  207. if err == nil {
  208. t.Fatalf("got nil, want error %q", tc.wantErr)
  209. }
  210. if err.Error() != tc.wantErr {
  211. t.Errorf("error message mismatch: got %q, want %q", err.Error(), tc.wantErr)
  212. }
  213. return
  214. }
  215. if err != nil {
  216. t.Fatalf("got error %q, want nil", err)
  217. }
  218. if expectedResp, ok := tc.response.(struct {
  219. ID string `json:"id"`
  220. Success bool `json:"success"`
  221. }); ok {
  222. if resp.ID != expectedResp.ID {
  223. t.Errorf("response ID mismatch: got %q, want %q", resp.ID, expectedResp.ID)
  224. }
  225. if resp.Success != expectedResp.Success {
  226. t.Errorf("response Success mismatch: got %v, want %v", resp.Success, expectedResp.Success)
  227. }
  228. }
  229. })
  230. }
  231. }