internal refactor of notifications

This commit is contained in:
leonardotrapani
2026-01-02 23:29:33 +01:00
parent 3a8695edb4
commit 6a3781567b
7 changed files with 261 additions and 393 deletions
+28 -42
View File
@@ -5,10 +5,12 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"time"
"github.com/BurntSushi/toml"
"github.com/leonardotrapani/hyprvoice/internal/injection"
"github.com/leonardotrapani/hyprvoice/internal/notify"
"github.com/leonardotrapani/hyprvoice/internal/recording"
"github.com/leonardotrapani/hyprvoice/internal/transcriber"
)
@@ -64,52 +66,36 @@ type MessagesConfig struct {
InjectionAborted MessageConfig `toml:"injection_aborted"`
}
func (c *Config) GetRecordingStarted() (title, body string) {
m := c.Notifications.Messages.RecordingStarted
if m.Title == "" && m.Body == "" {
return "Hyprvoice", "Recording Started"
}
return m.Title, m.Body
}
// Resolve merges user config with defaults from MessageDefs
func (m *MessagesConfig) Resolve() map[notify.MessageType]notify.Message {
result := make(map[notify.MessageType]notify.Message)
func (c *Config) GetTranscribing() (title, body string) {
m := c.Notifications.Messages.Transcribing
if m.Title == "" && m.Body == "" {
return "Hyprvoice", "Recording Ended... Transcribing"
// Build toml tag → field index map
v := reflect.ValueOf(m).Elem()
t := v.Type()
tagToField := make(map[string]int)
for i := 0; i < t.NumField(); i++ {
tagToField[t.Field(i).Tag.Get("toml")] = i
}
return m.Title, m.Body
}
func (c *Config) GetConfigReloaded() (title, body string) {
m := c.Notifications.Messages.ConfigReloaded
if m.Title == "" && m.Body == "" {
return "Hyprvoice", "Config Reloaded"
for _, def := range notify.MessageDefs {
msg := notify.Message{
Title: def.DefaultTitle,
Body: def.DefaultBody,
IsError: def.IsError,
}
if idx, ok := tagToField[def.ConfigKey]; ok {
userMsg := v.Field(idx).Interface().(MessageConfig)
if userMsg.Title != "" {
msg.Title = userMsg.Title
}
if userMsg.Body != "" {
msg.Body = userMsg.Body
}
}
result[def.Type] = msg
}
return m.Title, m.Body
}
func (c *Config) GetOperationCancelled() (title, body string) {
m := c.Notifications.Messages.OperationCancelled
if m.Title == "" && m.Body == "" {
return "Hyprvoice", "Operation Cancelled"
}
return m.Title, m.Body
}
func (c *Config) GetRecordingAborted() string {
m := c.Notifications.Messages.RecordingAborted
if m.Body == "" {
return "Recording Aborted"
}
return m.Body
}
func (c *Config) GetInjectionAborted() string {
m := c.Notifications.Messages.InjectionAborted
if m.Body == "" {
return "Injection Aborted"
}
return m.Body
return result
}
func (c *Config) ToRecordingConfig() recording.Config {
+39 -127
View File
@@ -5,6 +5,8 @@ import (
"path/filepath"
"testing"
"time"
"github.com/leonardotrapani/hyprvoice/internal/notify"
)
// createTestConfig returns a valid configuration for testing
@@ -1229,142 +1231,52 @@ func TestConfig_Validate_GroqTranslation_RejectsTurbo(t *testing.T) {
}
}
func TestConfig_MessageGetters_Defaults(t *testing.T) {
config := createTestConfig()
func TestMessagesConfig_Resolve_Defaults(t *testing.T) {
cfg := createTestConfig()
msgs := cfg.Notifications.Messages.Resolve()
t.Run("GetRecordingStarted returns defaults", func(t *testing.T) {
title, body := config.GetRecordingStarted()
if title != "Hyprvoice" {
t.Errorf("GetRecordingStarted() title = %q, want %q", title, "Hyprvoice")
}
if body != "Recording Started" {
t.Errorf("GetRecordingStarted() body = %q, want %q", body, "Recording Started")
}
})
t.Run("GetTranscribing returns defaults", func(t *testing.T) {
title, body := config.GetTranscribing()
if title != "Hyprvoice" {
t.Errorf("GetTranscribing() title = %q, want %q", title, "Hyprvoice")
}
if body != "Recording Ended... Transcribing" {
t.Errorf("GetTranscribing() body = %q, want %q", body, "Recording Ended... Transcribing")
}
})
t.Run("GetConfigReloaded returns defaults", func(t *testing.T) {
title, body := config.GetConfigReloaded()
if title != "Hyprvoice" {
t.Errorf("GetConfigReloaded() title = %q, want %q", title, "Hyprvoice")
}
if body != "Config Reloaded" {
t.Errorf("GetConfigReloaded() body = %q, want %q", body, "Config Reloaded")
}
})
t.Run("GetOperationCancelled returns defaults", func(t *testing.T) {
title, body := config.GetOperationCancelled()
if title != "Hyprvoice" {
t.Errorf("GetOperationCancelled() title = %q, want %q", title, "Hyprvoice")
}
if body != "Operation Cancelled" {
t.Errorf("GetOperationCancelled() body = %q, want %q", body, "Operation Cancelled")
}
})
t.Run("GetRecordingAborted returns default", func(t *testing.T) {
body := config.GetRecordingAborted()
if body != "Recording Aborted" {
t.Errorf("GetRecordingAborted() = %q, want %q", body, "Recording Aborted")
}
})
t.Run("GetInjectionAborted returns default", func(t *testing.T) {
body := config.GetInjectionAborted()
if body != "Injection Aborted" {
t.Errorf("GetInjectionAborted() = %q, want %q", body, "Injection Aborted")
}
})
// Check defaults are applied
if msgs[notify.MsgRecordingStarted].Title != "Hyprvoice" {
t.Errorf("MsgRecordingStarted title = %q, want %q", msgs[notify.MsgRecordingStarted].Title, "Hyprvoice")
}
if msgs[notify.MsgRecordingStarted].Body != "Recording Started" {
t.Errorf("MsgRecordingStarted body = %q, want %q", msgs[notify.MsgRecordingStarted].Body, "Recording Started")
}
if msgs[notify.MsgTranscribing].Body != "Recording Ended... Transcribing" {
t.Errorf("MsgTranscribing body = %q, want %q", msgs[notify.MsgTranscribing].Body, "Recording Ended... Transcribing")
}
if msgs[notify.MsgRecordingAborted].IsError != true {
t.Errorf("MsgRecordingAborted IsError = %v, want true", msgs[notify.MsgRecordingAborted].IsError)
}
}
func TestConfig_MessageGetters_Custom(t *testing.T) {
config := createTestConfig()
config.Notifications.Messages = MessagesConfig{
func TestMessagesConfig_Resolve_CustomOverrides(t *testing.T) {
cfg := createTestConfig()
cfg.Notifications.Messages = MessagesConfig{
RecordingStarted: MessageConfig{
Title: "",
Body: "🎤",
},
Transcribing: MessageConfig{
Title: "",
Body: "⏳",
},
ConfigReloaded: MessageConfig{
Title: "",
Body: "🔧",
},
OperationCancelled: MessageConfig{
Title: "Custom",
Body: "Cancelled!",
Title: "Custom Title",
Body: "Custom Body",
},
RecordingAborted: MessageConfig{
Body: "Recording stopped",
},
InjectionAborted: MessageConfig{
Body: "Inject failed",
Body: "Custom Abort",
},
}
t.Run("GetRecordingStarted returns custom emoji", func(t *testing.T) {
title, body := config.GetRecordingStarted()
if title != "" {
t.Errorf("GetRecordingStarted() title = %q, want %q", title, "")
}
if body != "🎤" {
t.Errorf("GetRecordingStarted() body = %q, want %q", body, "🎤")
}
})
msgs := cfg.Notifications.Messages.Resolve()
t.Run("GetTranscribing returns custom emoji", func(t *testing.T) {
title, body := config.GetTranscribing()
if title != "" {
t.Errorf("GetTranscribing() title = %q, want %q", title, "")
}
if body != "⏳" {
t.Errorf("GetTranscribing() body = %q, want %q", body, "⏳")
}
})
// Custom values should override defaults
if msgs[notify.MsgRecordingStarted].Title != "Custom Title" {
t.Errorf("MsgRecordingStarted title = %q, want %q", msgs[notify.MsgRecordingStarted].Title, "Custom Title")
}
if msgs[notify.MsgRecordingStarted].Body != "Custom Body" {
t.Errorf("MsgRecordingStarted body = %q, want %q", msgs[notify.MsgRecordingStarted].Body, "Custom Body")
}
if msgs[notify.MsgRecordingAborted].Body != "Custom Abort" {
t.Errorf("MsgRecordingAborted body = %q, want %q", msgs[notify.MsgRecordingAborted].Body, "Custom Abort")
}
t.Run("GetConfigReloaded returns custom emoji", func(t *testing.T) {
title, body := config.GetConfigReloaded()
if title != "" {
t.Errorf("GetConfigReloaded() title = %q, want %q", title, "")
}
if body != "🔧" {
t.Errorf("GetConfigReloaded() body = %q, want %q", body, "🔧")
}
})
t.Run("GetOperationCancelled returns custom values", func(t *testing.T) {
title, body := config.GetOperationCancelled()
if title != "Custom" {
t.Errorf("GetOperationCancelled() title = %q, want %q", title, "Custom")
}
if body != "Cancelled!" {
t.Errorf("GetOperationCancelled() body = %q, want %q", body, "Cancelled!")
}
})
t.Run("GetRecordingAborted returns custom value", func(t *testing.T) {
body := config.GetRecordingAborted()
if body != "Recording stopped" {
t.Errorf("GetRecordingAborted() = %q, want %q", body, "Recording stopped")
}
})
t.Run("GetInjectionAborted returns custom value", func(t *testing.T) {
body := config.GetInjectionAborted()
if body != "Inject failed" {
t.Errorf("GetInjectionAborted() = %q, want %q", body, "Inject failed")
}
})
// Non-customized messages should still have defaults
if msgs[notify.MsgTranscribing].Title != "Hyprvoice" {
t.Errorf("MsgTranscribing title = %q, want %q", msgs[notify.MsgTranscribing].Title, "Hyprvoice")
}
}