tercul-backend/cmd/cli/internal/bootstrap/bootstrap_test.go
Damir Mukimov be97b587b2
feat: Implement Bleve migration script and unify CLI (#26) (#64)
* docs: Update TASKS.md and PRODUCTION-TASKS.md to reflect current codebase state (December 2024 audit)

* refactor: Unify all commands into a single Cobra CLI

- Refactor cmd/api/main.go into 'tercul serve' command
- Refactor cmd/worker/main.go into 'tercul worker' command
- Refactor cmd/tools/enrich/main.go into 'tercul enrich' command
- Add 'tercul bleve-migrate' command for Bleve index migration
- Extract common initialization logic into cmd/cli/internal/bootstrap
- Update Dockerfile to build unified CLI
- Update README with new CLI usage

This consolidates all entry points into a single, maintainable CLI structure.

* fix: Fix CodeQL workflow and add comprehensive test coverage

- Fix Go version mismatch by setting up Go before CodeQL init
- Add Go version verification step
- Improve error handling for code scanning upload
- Add comprehensive test suite for CLI commands:
  - Bleve migration tests with in-memory indexes
  - Edge case tests (empty data, large batches, errors)
  - Command-level integration tests
  - Bootstrap initialization tests
- Optimize tests to use in-memory Bleve indexes for speed
- Add test tags for skipping slow tests in short mode
- Update workflow documentation

Test coverage: 18.1% with 806 lines of test code
All tests passing in short mode

* fix: Fix test workflow and Bleve test double-close panic

- Add POSTGRES_USER to PostgreSQL service configuration in test workflow
- Fix TestInitBleveIndex double-close panic by removing defer before explicit close
- Test now passes successfully

Fixes failing Unit Tests workflow in PR #64
2025-11-30 21:54:18 +01:00

113 lines
2.6 KiB
Go

package bootstrap
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tercul/internal/platform/config"
"github.com/weaviate/weaviate-go-client/v5/weaviate"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestNewWeaviateClient(t *testing.T) {
cfg := &config.Config{
WeaviateHost: "localhost:8080",
WeaviateScheme: "http",
}
client, err := NewWeaviateClient(cfg)
require.NoError(t, err)
assert.NotNil(t, client)
}
func TestBootstrap(t *testing.T) {
// Skip if integration tests are not enabled
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup test database using SQLite
dbPath := filepath.Join(t.TempDir(), "test.db")
testDB, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
require.NoError(t, err)
defer func() {
sqlDB, _ := testDB.DB()
if sqlDB != nil {
sqlDB.Close()
}
os.Remove(dbPath)
}()
// Setup test config
cfg := &config.Config{
Environment: "test",
WeaviateHost: "localhost:8080",
WeaviateScheme: "http",
}
// Create a mock Weaviate client (in real tests, you'd use a test container)
weaviateClient, err := weaviate.NewClient(weaviate.Config{
Host: cfg.WeaviateHost,
Scheme: cfg.WeaviateScheme,
})
require.NoError(t, err)
// Test bootstrap
deps, err := Bootstrap(cfg, testDB, weaviateClient)
require.NoError(t, err)
assert.NotNil(t, deps)
assert.NotNil(t, deps.Config)
assert.NotNil(t, deps.Database)
assert.NotNil(t, deps.WeaviateClient)
assert.NotNil(t, deps.Repos)
assert.NotNil(t, deps.Application)
assert.NotNil(t, deps.JWTManager)
assert.NotNil(t, deps.AnalysisRepo)
assert.NotNil(t, deps.SentimentProvider)
}
func TestBootstrapWithMetrics(t *testing.T) {
// Skip if integration tests are not enabled
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup test database using SQLite
dbPath := filepath.Join(t.TempDir(), "test.db")
testDB, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
require.NoError(t, err)
defer func() {
sqlDB, _ := testDB.DB()
if sqlDB != nil {
sqlDB.Close()
}
os.Remove(dbPath)
}()
// Setup test config
cfg := &config.Config{
Environment: "test",
WeaviateHost: "localhost:8080",
WeaviateScheme: "http",
}
// Create a mock Weaviate client
weaviateClient, err := weaviate.NewClient(weaviate.Config{
Host: cfg.WeaviateHost,
Scheme: cfg.WeaviateScheme,
})
require.NoError(t, err)
// Test bootstrap with metrics
deps, err := BootstrapWithMetrics(cfg, testDB, weaviateClient)
require.NoError(t, err)
assert.NotNil(t, deps)
assert.NotNil(t, deps.Application)
}