first commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
# Testing
|
||||
|
||||
**Özet:** Fiber v3 test framework'ü ile mock upstream sunucuları kullanarak proxy mantığının test edilmesi. Transform katmanı unit testleri `internal/transform`, entegrasyon testleri `tests/` paketinde.
|
||||
|
||||
**Kütüphaneler:** Go `testing`, `net/http/httptest`, Fiber v3 test (`app.Test()`), `encoding/json`
|
||||
|
||||
**Bağlantılar:** [[OpenAIHandler]], [[AnthropicHandler]], [[BifrostTransform]], [[ProxyEngine]], [[Index]]
|
||||
|
||||
## Test Stratejisi
|
||||
|
||||
- `mockUpstream(t, status, body)` — `httptest.Server` ile sabit yanıt dönen test sunucusu
|
||||
- `newTestApp(cfg)` — OpenAI handler bağlı Fiber test uygulaması (DB'siz)
|
||||
- `newAnthropicTestApp(cfg)` — Anthropic handler bağlı Fiber test uygulaması (DB'siz)
|
||||
- Gerçek upstream'e ihtiyaç yok, mock sunucu kullanılır
|
||||
- `app.Test(req, fiber.TestConfig{Timeout: -1})` ile Fiber native HTTP test
|
||||
|
||||
## Transform Unit Testleri (`internal/transform`)
|
||||
|
||||
### Anthropic → OpenAI (`AnthropicToBifrost`)
|
||||
|
||||
| Test | Açıklama |
|
||||
|---|---|
|
||||
| `TestAnthropicToBifrost_Basic` | Temel dönüşüm, model prefix + max_tokens + messages |
|
||||
| `TestAnthropicToBifrost_WithSystem` | `system` alanının `messages[0]`'a eklenmesi |
|
||||
| `TestAnthropicToBifrost_ModelPrefix` | 6 model için prefix tahmini (claude→Anthropic/, deepseek→DeepSeek/, vb.) |
|
||||
| `TestAnthropicToBifrost_StreamPassthrough` | `stream: true` korunur |
|
||||
| `TestAnthropicToBifrost_InvalidJSON` | Geçersiz JSON → hata döner |
|
||||
|
||||
### OpenAI → Anthropic (`BifrostToAnthropic`)
|
||||
|
||||
| Test | Açıklama |
|
||||
|---|---|
|
||||
| `TestBifrostToAnthropic_Basic` | Temel dönüşüm, content/text/stop_reason kontrolü |
|
||||
| `TestBifrostToAnthropic_EmptyFinishReason` | Boş `finish_reason` → `end_turn` |
|
||||
| `TestBifrostToAnthropic_MultipleChoices` | 2 choice'ın 2 content block'a dönüşümü |
|
||||
| `TestBifrostToAnthropic_InvalidJSON` | Geçersiz JSON → hata döner |
|
||||
|
||||
## Entegrasyon Testleri (`tests/anthropic_test.go`, `tests/openai_test.go`)
|
||||
|
||||
| Test | Açıklama |
|
||||
|---|---|
|
||||
| `TestAnthropicProxy_Success` | Full Bifrost döngüsü: Anthropic→OpenAI→forward→OpenAI→Anthropic |
|
||||
| `TestAnthropicProxy_InvalidFormatPassthrough` | Geçersiz Anthropic formatı → raw passthrough |
|
||||
| `TestAnthropicProxy_UpstreamError` | Upstream kapalı → 502 Bad Gateway |
|
||||
| `TestAnthropicProxy_ModelsList` | `GET /anthropic/v1/models` → DeepSeek model listesi |
|
||||
| `TestAnthropicProxy_EmptyBody` | Boş body → varsayılan "ready" yanıtı |
|
||||
| `TestAnthropicProxy_HEAD` | `HEAD` → 404 |
|
||||
| `TestAnthropicProxy_xApiKeyConversion` | `x-api-key` header'ının `Authorization: Bearer`'a dönüşümü |
|
||||
| `TestAnthropicProxy_DefaultModelInjection` | İstekte model yoksa `OPENAI_MODEL` enjekte edilir (zaten "/" var, prefix eklenmez) |
|
||||
| `TestAnthropicProxy_DefaultModelInjection_NopWhenModelExists` | İstekte model varsa `OPENAI_MODEL` override etmez |
|
||||
| `TestAnthropicProxy_TransformErrorFallsbackToPassthrough` | Upstream yanıtı bozuksa raw passthrough |
|
||||
| `TestOpenAIProxy_Success` | OpenAI handler başarılı proxy |
|
||||
| `TestOpenAIProxy_DefaultModelInjection` | OpenAI handler'da model yoksa enjekte |
|
||||
| `TestOpenAIProxy_DefaultModelInjection_ExistingModel` | OpenAI handler'da var olan model korunur |
|
||||
| `TestOpenAIProxy_UpstreamError` | OpenAI handler upstream hatasında 502 |
|
||||
|
||||
## Çalıştırma
|
||||
|
||||
```bash
|
||||
# Tüm testler
|
||||
go test ./tests/ -v
|
||||
|
||||
# Sadece transform unit testleri
|
||||
go test ./internal/transform/ -v
|
||||
```
|
||||
|
||||
## Notlar
|
||||
|
||||
- Tüm testler DB'siz çalışır (`db = nil`) — loglama katmanı test edilmez
|
||||
- Fiber v3'ün `app.Test()` metodu ile entegre test
|
||||
- Mock upstream her test için ayrı `httptest.Server` oluşturur (izolasyon)
|
||||
Reference in New Issue
Block a user