Testing Infrastructure
This package provides testing utilities for ModFetch unit and integration tests.
Overview
The testutil package provides:
- Mock HTTP servers with canned responses
- In-memory SQLite database for testing
- Test fixture loading helpers
- Mock HTTP round trippers
- Temporary file/directory helpers
Usage
Creating a Test Database
func TestSomething(t *testing.T) {
db := testutil.TestDB(t)
// Use db for testing
// Automatically cleaned up when test completes
}
Mock HTTP Server
func TestHTTPFetcher(t *testing.T) {
server := testutil.NewMockHTTPServer()
defer server.Close()
// Add canned responses
server.AddJSONResponse("/api/models/123", 200, `{"id": 123, "name": "test"}`)
// Use server.URL in your tests
client := &http.Client{}
resp, err := client.Get(server.URL + "/api/models/123")
// ... assertions
}
Loading Fixtures
func TestWithFixture(t *testing.T) {
json := testutil.LoadFixture(t, "huggingface_model.json")
// Use fixture data in tests
}
Mock Round Tripper
func TestHTTPClient(t *testing.T) {
mock := testutil.NewMockRoundTripper()
mock.AddStringResponse("https://api.example.com/model", 200, `{"result": "ok"}`)
client := &http.Client{Transport: mock}
// Use client
// Verify requests
mock.AssertRequestMade(t, "https://api.example.com/model")
}
Test Fixtures
Fixtures are stored in testdata/fixtures/ and include:
huggingface_model.json - Sample HuggingFace API response
civitai_model.json - Sample CivitAI API response
Add new fixtures as needed for additional test cases.
Running Tests
Most tests run against temporary files, in-memory databases, local HTTP servers,
or fixtures. Tests that need gated provider access read tokens from the
environment and skip when those tokens are not set.
# Run all tests
go test ./...
# Run tests for specific package
go test ./internal/metadata/...
# Run with verbose output
go test -v ./internal/state/...
# Run specific test
go test -run TestDB_UpsertMetadata ./internal/state/...
Best Practices
- Always use testutil.TestDB() for database tests - it creates an in-memory database that's automatically cleaned up
- Use fixtures for API responses - keeps tests maintainable and realistic
- Mock HTTP transports - don't make real network calls in tests
- Use t.Helper() in utility functions - improves error reporting
- Clean up resources - use t.Cleanup() for deferred cleanup
Coverage
Current test coverage includes:
- ✅ HuggingFace fetcher with mocked API responses
- ✅ CivitAI fetcher with mocked API responses
- ✅ Model type inference from filenames and tags
- ✅ Quantization extraction from filenames
- ✅ URL pattern matching
- ✅ Error handling for API failures
State Package
- ✅ Metadata CRUD operations (create, read, update, delete)
- ✅ Filtering by source, type, rating, tags, favorites
- ✅ Full-text search across metadata fields
- ✅ Usage tracking (times_used, last_used)
- ✅ Tag JSON serialization/deserialization
- ✅ Timestamp handling
Current Coverage
Coverage now spans batch parsing, downloader behavior, resolver behavior,
configuration validation, metrics, archive extraction, catalog import/export,
scanner behavior, and TUI state transitions. Keep new helpers small and prefer
real temporary filesystem, SQLite, archive, and HTTP execution where practical.