package app import ( "log/slog" "testing" "github.com/stretchr/testify/assert" ) func TestNewModule(t *testing.T) { assert := assert.New(t) // Test creating a module with no options mod := NewModule("testModule", ModuleOpts{}) assert.NotNil(mod, "expected module to be created") assert.Equal("testModule", mod.name, "expected module name to match") assert.Nil(mod.setup, "expected setup function to be nil") assert.Nil(mod.teardown, "expected teardown function to be nil") assert.Empty(mod.depends, "expected dependencies to be empty") assert.False(mod.loaded, "expected module to not be loaded") // Test creating a module with setup and teardown functions and dependencies setupFn := func(m *Module) error { return nil } teardownFn := func(m *Module) error { return nil } modWithOpts := NewModule("testModuleWithOpts", ModuleOpts{ Setup: setupFn, Teardown: teardownFn, Depends: []string{"dependency1", "dependency2"}, }) assert.NotNil(modWithOpts, "expected module with options to be created") assert.NotNil(modWithOpts.setup, "expected setup function to be set") assert.NotNil(modWithOpts.teardown, "expected teardown function to be set") assert.Equal([]string{"dependency1", "dependency2"}, modWithOpts.depends, "expected dependencies to match") } func TestModule(t *testing.T) { assert := assert.New(t) // Create a module and set its logger mod := NewModule("testModule", ModuleOpts{}) assert.Panics(func() { mod.Logger() }, "expected Logger to panic when logger is not set") logger := slog.Default() mod.logger = logger // Test Logger method assert.Equal(logger, mod.Logger(), "expected Logger to return the correct logger") // Test Name method assert.Equal("testModule", mod.Name(), "expected Name to return the module's name") // Test Loaded method assert.False(mod.Loaded(), "expected Loaded to return false initially") // Test RequireLoaded method var mod2 *Module assert.PanicsWithValue("module is nil", func() { mod2.RequireLoaded() }, "expected RequireLoaded to panic when module is nil") assert.Panics(func() { mod.RequireLoaded() }, "expected RequireLoaded to panic when module is not loaded") assert.PanicsWithValue("module testModule not loaded: message", func() { mod.RequireLoaded("message") }, "expected RequireLoaded to panic with custom message when module is not loaded") // Test setting the module as loaded mod.loaded = true assert.NotPanics(func() { mod.RequireLoaded() }, "expected RequireLoaded to not panic when module is loaded") }