package testing import ( "context" "os" "testing" "time" "git.linuxforward.com/byom/byom-core/config" "git.linuxforward.com/byom/byom-core/store" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" "gorm.io/driver/sqlite" "gorm.io/gorm" ) // TestDB creates a temporary test database func TestDB(t *testing.T) *store.DataStore { t.Helper() // Create temp directory for test database tmpDir, err := os.MkdirTemp("", "byom-test-*") require.NoError(t, err) // Clean up after test t.Cleanup(func() { os.RemoveAll(tmpDir) }) // Create test database //dbPath := filepath.Join(tmpDir, "test.db") dbPath := "../.data/app.db" db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) require.NoError(t, err) return store.NewDataStore(db) } // TestConfig creates a test configuration func TestConfig() *config.Config { return &config.Config{ Server: &config.Server{ ListeningPort: 8080, TlsConfig: &config.TlsConfig{ Enabled: false, }, CorsOrigins: []string{"http://localhost:3000"}, }, Database: &config.Database{ Path: "./data/app.db", MaxOpenConns: 1, MaxIdleConns: 1, ConnMaxLifetime: 300, }, Jwt: &config.Jwt{ JwtSecret: "test-secret", }, Smtp: &config.Smtp{ Host: "localhost", Port: 1025, User: "test", Pass: "test", }, Hook: &config.Hook{ BaseURL: "http://localhost:8081", Domain: "test", SecretKey: "test-secret", }, } } // TestLogger creates a test logger func TestLogger() *logrus.Logger { logger := logrus.New() logger.SetOutput(os.Stdout) logger.SetLevel(logrus.DebugLevel) return logger } // TestContext creates a context for testing func TestContext() (context.Context, context.CancelFunc) { return context.WithTimeout(context.Background(), 5*time.Second) } // MockEmailService is a mock implementation of the email service type MockEmailService struct { SendInviteEmailFunc func(email, token, workspaceID string) error } func (m *MockEmailService) SendInviteEmail(email, token, workspaceID string) error { if m.SendInviteEmailFunc != nil { return m.SendInviteEmailFunc(email, token, workspaceID) } return nil } func (m *MockEmailService) Close() error { return nil } // MockHookClient is a mock implementation of the hook client type MockHookClient struct { SendHookFunc func(event string, payload interface{}) error } func (m *MockHookClient) SendHook(event string, payload interface{}) error { if m.SendHookFunc != nil { return m.SendHookFunc(event, payload) } return nil } func (m *MockHookClient) Close() error { return nil }