- Add Server struct with store Store and webhookSecret fields - Add NewServer constructor - Convert all 6 handler functions to methods on *Server - Replace all inline SQL with s.store.X() calls - Remove package-level globals db, mu, webhookSecret - Remove InitDB, SetWebhookSecret, UpdateEvent, GetUpdates functions - Update export_test.go: replace old helpers with NewTestServer, NewTestServerWithSecret, TestUpsertEvent, TestGetUpdatesMap - Update main.go: sql.Open -> RunMigrations -> NewSQLiteStore -> NewServer -> routes
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package diunwebhook
|
|
|
|
import "database/sql"
|
|
|
|
// NewTestServer constructs a Server with a fresh in-memory SQLite database.
|
|
// Each call returns an isolated server -- tests do not share state.
|
|
func NewTestServer() (*Server, error) {
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := RunMigrations(db); err != nil {
|
|
return nil, err
|
|
}
|
|
store := NewSQLiteStore(db)
|
|
return NewServer(store, ""), nil
|
|
}
|
|
|
|
// NewTestServerWithSecret constructs a Server with webhook authentication enabled.
|
|
func NewTestServerWithSecret(secret string) (*Server, error) {
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := RunMigrations(db); err != nil {
|
|
return nil, err
|
|
}
|
|
store := NewSQLiteStore(db)
|
|
return NewServer(store, secret), nil
|
|
}
|
|
|
|
// TestUpsertEvent calls UpsertEvent on the server's store (for test setup).
|
|
func (s *Server) TestUpsertEvent(event DiunEvent) error {
|
|
return s.store.UpsertEvent(event)
|
|
}
|
|
|
|
// TestGetUpdates calls GetUpdates on the server's store (for test assertions).
|
|
func (s *Server) TestGetUpdates() (map[string]UpdateEntry, error) {
|
|
return s.store.GetUpdates()
|
|
}
|
|
|
|
// TestGetUpdatesMap is a convenience wrapper that returns the map without error.
|
|
func (s *Server) TestGetUpdatesMap() map[string]UpdateEntry {
|
|
m, _ := s.store.GetUpdates()
|
|
return m
|
|
}
|