package smtp2shoutrrr import ( "os" "path/filepath" "testing" "github.com/stretchr/testify/require" ) func writeConfig(t *testing.T, contents string) string { t.Helper() path := filepath.Join(t.TempDir(), "config.toml") require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) return path } func TestLoadConfig(t *testing.T) { path := writeConfig(t, ` Port = 2525 Username = "user" Password = "secret" [[Recipients]] Addresses = ["user@example.com"] Targets = ["ntfy://ntfy.sh/topic"] [[Recipients]] Addresses = ["legacy@example.com"] Target = "ntfy://ntfy.sh/legacy" [CatchAll] Targets = ["ntfy://ntfy.sh/catch-all"] `) config, err := LoadConfig(path) require.NoError(t, err) require.Equal(t, 2525, config.Port) require.Equal(t, "user", config.Username) require.Equal(t, "secret", config.Password) require.Len(t, config.Recipients, 2) require.Equal(t, []string{"user@example.com"}, config.Recipients[0].Addresses) require.Equal(t, []string{"ntfy://ntfy.sh/topic"}, config.Recipients[0].Targets) require.Equal(t, "ntfy://ntfy.sh/legacy", config.Recipients[1].Target) require.NotNil(t, config.CatchAll) require.Equal(t, []string{"ntfy://ntfy.sh/catch-all"}, config.CatchAll.Targets) } func TestLoadConfigAppliesDefaults(t *testing.T) { path := writeConfig(t, ` [[Recipients]] Addresses = ["user@example.com"] Targets = ["ntfy://ntfy.sh/topic"] `) config, err := LoadConfig(path) require.NoError(t, err) require.Equal(t, 11125, config.Port) require.Equal(t, "username", config.Username) require.Equal(t, "password", config.Password) require.Nil(t, config.CatchAll) } func TestLoadConfigMissingFile(t *testing.T) { _, err := LoadConfig(filepath.Join(t.TempDir(), "does-not-exist.toml")) require.Error(t, err) } func TestLoadConfigInvalidTOML(t *testing.T) { _, err := LoadConfig(writeConfig(t, "Port = ")) require.Error(t, err) }