tercul-backend/cmd/cli/internal/bootstrap/bootstrap_test.go
google-labs-jules[bot] 53aa4d0344
Security Hardening and GraphQL Caching (#69)
* feat: add security middleware, graphql apq, and improved linting

- Add RateLimit, RequestValidation, and CORS middleware.
- Configure middleware chain in API server.
- Implement Redis cache for GraphQL Automatic Persisted Queries.
- Add .golangci.yml and fix linting issues (shadowing, timeouts).

* feat: security, caching and linting config

- Fix .golangci.yml config for govet shadow check
- (Previous changes: Security middleware, GraphQL APQ, Linting fixes)

* fix: resolve remaining lint errors

- Fix unhandled errors in tests (errcheck)
- Define constants for repeated strings (goconst)
- Suppress high complexity warnings with nolint:gocyclo
- Fix integer overflow warnings (gosec)
- Add package comments
- Split long lines (lll)
- Rename Analyse -> Analyze (misspell)
- Fix naked returns and unused params

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2025-12-01 00:14:22 +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)
}