File size: 13,290 Bytes
2e6b65c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | # Testing Infrastructure Improvement Plan
## Current State Analysis
- **Total Go files**: 377
- **Test files**: 50 (~13% coverage)
- **Issues**:
- Low test coverage
- Heavy use of `init()` functions making tests hard to isolate
- External dependencies not mocked
- No contract tests between API formats
- Integration tests missing for storage backends
## Target Metrics
- **Unit test coverage**: 70%+
- **Integration test coverage**: Key paths covered
- **Test execution time**: < 2 minutes for full suite
- **Flaky tests**: 0%
## Phase 1: Testing Framework Setup (Week 1)
### 1.1 Test Utilities Package
**File**: `internal/testutil/helpers.go`
```go
package testutil
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Must panics if error is not nil (use in test setup)
func Must(t *testing.T, err error) {
t.Helper()
require.NoError(t, err)
}
// Fixture loads test fixture from file
func Fixture(t *testing.T, name string) []byte {
t.Helper()
data, err := os.ReadFile(filepath.Join("testdata", name))
require.NoError(t, err)
return data
}
// TempDir creates a temp directory that auto-cleans
func TempDir(t *testing.T) string {
t.Helper()
dir, err := os.MkdirTemp("", "cliproxy-test-*")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(dir) })
return dir
}
// Context returns a context with timeout for tests
func Context(t *testing.T) context.Context {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
return ctx
}
```
### 1.2 Mock Generation
**File**: `Makefile` additions
```makefile
.PHONY: mocks
mocks:
go install github.com/vektra/mockery/v2@latest
mockery --config=.mockery.yml
```
**File**: `.mockery.yml`
```yaml
with-expecter: true
keeptree: false
dir: internal/domain/ports
outpkg: mocks
output: internal/testutil/mocks
packages:
github.com/echyai/cliproxyapi/internal/domain/ports:
interfaces:
AuthRepository:
ConfigRepository:
TokenStore:
Translator:
```
### 1.3 Test Containers for Integration Tests
**File**: `internal/testutil/containers.go`
```go
package testutil
import (
"context"
"database/sql"
"testing"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/modules/minio"
)
// PostgresContainer wraps test postgres container
type PostgresContainer struct {
*postgres.PostgresContainer
ConnStr string
}
func NewPostgres(t *testing.T) *PostgresContainer {
t.Helper()
ctx := context.Background()
container, err := postgres.Run(ctx,
"postgres:16-alpine",
postgres.WithDatabase("test"),
postgres.WithUsername("test"),
postgres.WithPassword("test"),
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, container.Terminate(ctx))
})
connStr, err := container.ConnectionString(ctx)
require.NoError(t, err)
return &PostgresContainer{
PostgresContainer: container,
ConnStr: connStr,
}
}
func (p *PostgresContainer) DB(t *testing.T) *sql.DB {
t.Helper()
db, err := sql.Open("postgres", p.ConnStr)
require.NoError(t, err)
t.Cleanup(func() { db.Close() })
return db
}
// MinIOContainer wraps test minio container
type MinIOContainer struct {
*minio.MinioContainer
Endpoint string
}
func NewMinIO(t *testing.T) *MinIOContainer {
t.Helper()
ctx := context.Background()
container, err := minio.Run(ctx, "minio/minio:latest")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, container.Terminate(ctx))
})
endpoint, err := container.Endpoint(ctx, "http")
require.NoError(t, err)
return &MinIOContainer{
MinioContainer: container,
Endpoint: endpoint,
}
}
```
## Phase 2: Unit Testing Patterns (Week 2-3)
### 2.1 Table-Driven Tests
**Example**: `internal/translator/gemini/translator_test.go`
```go
package gemini
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTranslator_ConvertRequest(t *testing.T) {
tests := []struct {
name string
input openai.ChatCompletionRequest
want *gemini.GenerateContentRequest
wantErr bool
errMsg string
}{
{
name: "simple request with single message",
input: openai.ChatCompletionRequest{
Model: "gemini-pro",
Messages: []openai.ChatMessage{
{Role: "user", Content: "Hello"},
},
},
want: &gemini.GenerateContentRequest{
Model: "gemini-pro",
Contents: []gemini.Content{
{Role: "user", Parts: []gemini.Part{{Text: "Hello"}}},
},
},
},
{
name: "system message conversion",
input: openai.ChatCompletionRequest{
Model: "gemini-pro",
Messages: []openai.ChatMessage{
{Role: "system", Content: "You are helpful"},
{Role: "user", Content: "Hi"},
},
},
want: &gemini.GenerateContentRequest{
Model: "gemini-pro",
SystemInstruction: &gemini.Content{
Parts: []gemini.Part{{Text: "You are helpful"}},
},
Contents: []gemini.Content{
{Role: "user", Parts: []gemini.Part{{Text: "Hi"}}},
},
},
},
{
name: "unsupported model",
input: openai.ChatCompletionRequest{
Model: "unknown-model",
},
wantErr: true,
errMsg: "unsupported model",
},
{
name: "max tokens exceeded",
input: openai.ChatCompletionRequest{
Model: "gemini-pro",
MaxTokens: 1000000,
Messages: []openai.ChatMessage{{Role: "user", Content: "Hi"}},
},
wantErr: true,
errMsg: "max tokens exceeds limit",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := NewTranslator()
got, err := tr.ConvertRequest(tt.input)
if tt.wantErr {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.errMsg)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
```
### 2.2 Mock-Based Tests
**Example**: `internal/application/usecase/auth_test.go`
```go
package usecase
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/echyai/cliproxyapi/internal/domain/models"
"github.com/echyai/cliproxyapi/internal/testutil/mocks"
)
func TestAuthUseCase_GetAuth(t *testing.T) {
tests := []struct {
name string
id string
mock func(*mocks.AuthRepository)
want *models.Auth
wantErr error
}{
{
name: "existing auth",
id: "auth-123",
mock: func(r *mocks.AuthRepository) {
r.On("FindByID", mock.Anything, "auth-123").
Return(&models.Auth{
ID: "auth-123",
Provider: "gemini",
Status: models.AuthStatusActive,
}, nil)
},
want: &models.Auth{
ID: "auth-123",
Provider: "gemini",
Status: models.AuthStatusActive,
},
},
{
name: "not found",
id: "auth-missing",
mock: func(r *mocks.AuthRepository) {
r.On("FindByID", mock.Anything, "auth-missing").
Return(nil, nil)
},
wantErr: domainerrors.NewConfigNotFound("auth-missing"),
},
{
name: "repository error",
id: "auth-err",
mock: func(r *mocks.AuthRepository) {
r.On("FindByID", mock.Anything, "auth-err").
Return(nil, errors.New("db error"))
},
wantErr: domainerrors.NewInternalError(errors.New("db error")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockRepo := mocks.NewAuthRepository(t)
tt.mock(mockRepo)
uc := NewAuthUseCase(mockRepo)
got, err := uc.GetAuth(testutil.Context(t), tt.id)
if tt.wantErr != nil {
assert.ErrorIs(t, err, tt.wantErr)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
mockRepo.AssertExpectations(t)
})
}
}
```
## Phase 3: Integration Tests (Week 3-4)
### 3.1 API Integration Tests
**File**: `test/integration/api_test.go`
```go
package integration
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/echyai/cliproxyapi/internal/api"
"github.com/echyai/cliproxyapi/internal/testutil"
)
func TestAPI_ChatCompletions(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
// Setup test server
gin.SetMode(gin.TestMode)
container := testutil.NewPostgres(t)
server, err := api.NewTestServer(container.ConnStr)
require.NoError(t, err)
// Test cases
tests := []struct {
name string
request map[string]interface{}
wantStatus int
wantFields []string
}{
{
name: "valid request",
request: map[string]interface{}{
"model": "gemini-pro",
"messages": []map[string]string{
{"role": "user", "content": "Hello"},
},
},
wantStatus: 200,
wantFields: []string{"id", "choices", "usage"},
},
{
name: "missing model",
request: map[string]interface{}{
"messages": []map[string]string{
{"role": "user", "content": "Hello"},
},
},
wantStatus: 400,
wantFields: []string{"error"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body, _ := json.Marshal(tt.request)
req := httptest.NewRequest("POST", "/v1/chat/completions", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer test-key")
w := httptest.NewRecorder()
server.Handler.ServeHTTP(w, req)
assert.Equal(t, tt.wantStatus, w.Code)
var resp map[string]interface{}
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
for _, field := range tt.wantFields {
assert.Contains(t, resp, field)
}
})
}
}
```
### 3.2 Contract Tests
**File**: `test/contract/translator_contract_test.go`
```go
package contract
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/echyai/cliproxyapi/internal/translator/gemini"
"github.com/echyai/cliproxyapi/internal/translator/openai"
)
// TestOpenAIGeminiRoundTrip verifies that translating OpenAI -> Gemini -> OpenAI
// preserves essential fields
func TestOpenAIGeminiRoundTrip(t *testing.T) {
original := openai.ChatCompletionRequest{
Model: "gemini-pro",
Messages: []openai.ChatMessage{
{Role: "system", Content: "Be helpful"},
{Role: "user", Content: "Hello"},
{Role: "assistant", Content: "Hi there!"},
{Role: "user", Content: "How are you?"},
},
Temperature: 0.7,
MaxTokens: 100,
TopP: 0.9,
}
geminiTranslator := gemini.NewTranslator()
// Convert to Gemini
geminiReq, err := geminiTranslator.ConvertRequest(original)
require.NoError(t, err)
// Convert back to OpenAI
openaiReq, err := geminiTranslator.ConvertResponse(geminiReq)
require.NoError(t, err)
// Verify essential fields preserved
assert.Equal(t, original.Model, openaiReq.Model)
assert.Len(t, openaiReq.Messages, len(original.Messages))
assert.InDelta(t, original.Temperature, openaiReq.Temperature, 0.01)
}
```
## Phase 4: CI/CD Integration (Week 4)
### 4.1 GitHub Actions Workflow
**File**: `.github/workflows/test.yml`
```yaml
name: Test
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
jobs:
unit-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.24'
- name: Run unit tests
run: go test -v -race -coverprofile=coverage.out ./internal/... ./sdk/...
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage.out
fail_ci_if_error: true
integration-test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: test
POSTGRES_DB: test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
minio:
image: minio/minio
env:
MINIO_ROOT_USER: test
MINIO_ROOT_PASSWORD: testpassword
ports:
- 9000:9000
options: >-
server /data
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.24'
- name: Run integration tests
run: go test -v ./test/integration/... -tags=integration
env:
TEST_POSTGRES_URL: postgres://postgres:test@localhost:5432/test?sslmode=disable
TEST_MINIO_ENDPOINT: localhost:9000
contract-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.24'
- name: Run contract tests
run: go test -v ./test/contract/...
```
## Success Metrics
- [ ] 70%+ code coverage
- [ ] All critical paths have integration tests
- [ ] Contract tests for all translators
- [ ] CI pipeline runs tests in < 5 minutes
- [ ] Zero flaky tests
## Testing Best Practices
1. **Use subtests** for table-driven tests with `t.Run()`
2. **Cleanup resources** with `t.Cleanup()`
3. **Parallel tests** where possible with `t.Parallel()`
4. **Golden files** for complex JSON responses
5. **Property-based testing** for parsers using `rapid`
|