| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestUploadUserDataFileOpenAI(t *testing.T) { |
| 13 | var gotPurpose, gotAuth string |
| 14 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 15 | if r.URL.Path != "/files" || r.Method != http.MethodPost { |
| 16 | t.Errorf("request %s %s", r.Method, r.URL.Path) |
| 17 | } |
| 18 | gotAuth = r.Header.Get("Authorization") |
| 19 | if err := r.ParseMultipartForm(1 << 20); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | gotPurpose = r.FormValue("purpose") |
| 23 | file, hdr, err := r.FormFile("file") |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | defer file.Close() |
| 28 | raw, _ := io.ReadAll(file) |
| 29 | if hdr.Filename != "shot.png" || string(raw) != "PNGDATA" { |
| 30 | t.Errorf("file = %q %q", hdr.Filename, raw) |
| 31 | } |
| 32 | w.Header().Set("Content-Type", "application/json") |
| 33 | _, _ = w.Write([]byte(`{"id":"file-api-0a1b2c3d4e5f6071","object":"file"}`)) |
| 34 | })) |
| 35 | defer srv.Close() |
| 36 | |
| 37 | id, err := UploadUserDataFile(context.Background(), FileUpload{ |
| 38 | BaseURL: srv.URL, |
| 39 | APIKey: "sk-test", |
| 40 | Filename: "shot.png", |
| 41 | Data: []byte("PNGDATA"), |
| 42 | Client: srv.Client(), |
| 43 | }) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if id != "file-api-0a1b2c3d4e5f6071" { |
| 48 | t.Fatalf("id = %q", id) |
| 49 | } |
| 50 | if gotPurpose != "user_data" || gotAuth != "Bearer sk-test" { |
| 51 | t.Fatalf("purpose=%q auth=%q", gotPurpose, gotAuth) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestUploadUserDataFileAnthropic(t *testing.T) { |
| 56 | var gotBeta, gotKey string |
| 57 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 58 | if r.URL.Path != "/v1/files" { |
| 59 | t.Errorf("path = %s, want /v1/files", r.URL.Path) |
| 60 | } |
| 61 | gotBeta = r.Header.Get("anthropic-beta") |
| 62 | gotKey = r.Header.Get("x-api-key") |
| 63 | w.Header().Set("Content-Type", "application/json") |
| 64 | _, _ = w.Write([]byte(`{"id":"file-api-anthropicfile01","type":"file"}`)) |
| 65 | })) |
| 66 | defer srv.Close() |
| 67 | |
| 68 | id, err := UploadUserDataFile(context.Background(), FileUpload{ |
| 69 | BaseURL: srv.URL, |
| 70 | APIKey: "sk-ant", |
| 71 | Protocol: "anthropic", |
| 72 | Filename: "a.jpg", |
| 73 | Data: []byte("JPEG"), |
| 74 | Client: srv.Client(), |
| 75 | }) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if id != "file-api-anthropicfile01" { |
| 80 | t.Fatalf("id = %q", id) |
| 81 | } |
| 82 | if gotBeta != "files-api-2025-04-14" || gotKey != "sk-ant" { |
| 83 | t.Fatalf("beta=%q key=%q", gotBeta, gotKey) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestUploadUserDataFileRejectsEmpty(t *testing.T) { |
| 88 | _, err := UploadUserDataFile(context.Background(), FileUpload{APIKey: "k", Data: nil}) |
| 89 | if err == nil || !strings.Contains(err.Error(), "64 MiB") { |
| 90 | t.Fatalf("err = %v", err) |
| 91 | } |
| 92 | } |
| 93 |