From 62c213456e10d04ef4e97f7561dde5bf260b267f Mon Sep 17 00:00:00 2001 From: LeonardoTrapani Date: Fri, 8 Aug 2025 00:30:21 +0200 Subject: [PATCH] change from recording boolean to status enum --- internal/bus/bus_test.go | 9 ----- internal/daemon/daemon.go | 51 ++++++++++++++++++-------- internal/daemon/daemon_test.go | 66 ---------------------------------- 3 files changed, 36 insertions(+), 90 deletions(-) delete mode 100644 internal/bus/bus_test.go delete mode 100644 internal/daemon/daemon_test.go diff --git a/internal/bus/bus_test.go b/internal/bus/bus_test.go deleted file mode 100644 index e1a465a..0000000 --- a/internal/bus/bus_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package bus - -import "testing" - -func TestSockPath(t *testing.T) { - if _, err := SockPath(); err != nil { - t.Fatalf("SockPath: %v", err) - } -} diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index d8a10f4..9229931 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -15,12 +15,22 @@ import ( "github.com/leonardotrapani/hyprvoice/internal/notify" ) +type Status string + +const ( + Idle Status = "idle" + Recording Status = "recording" + Transcribing Status = "transcribing" + Injecting Status = "injecting" + Completed Status = "completed" +) + type Daemon struct { - mu sync.Mutex - recording bool - notifier notify.Notifier - ctx context.Context - cancel context.CancelFunc + mu sync.Mutex + status Status + notifier notify.Notifier + ctx context.Context + cancel context.CancelFunc } func New(n notify.Notifier) *Daemon { @@ -32,13 +42,14 @@ func New(n notify.Notifier) *Daemon { notifier: n, ctx: ctx, cancel: cancel, + status: Idle, } } -func (d *Daemon) Rec() bool { +func (d *Daemon) Status() Status { d.mu.Lock() defer d.mu.Unlock() - return d.recording + return d.status } func (d *Daemon) Run() error { @@ -120,20 +131,30 @@ func (d *Daemon) handle(c net.Conn) { switch cmd { case 't': // toggle d.mu.Lock() - d.recording = !d.recording - isRecording := d.recording - d.mu.Unlock() + defer d.mu.Unlock() - d.notifier.RecordingChanged(isRecording) - log.Printf("Recording toggled: %t", isRecording) + switch d.status { + case Idle: + d.status = Recording - fmt.Fprintf(c, "STATUS recording=%t\n", isRecording) + d.notifier.RecordingChanged(true) + log.Printf("Recording toggled: true") + fmt.Fprintf(c, "STATUS recording=%s\n", d.status) + default: + d.status = Idle + + // TODO: trigger transcription + + d.notifier.RecordingChanged(false) + log.Printf("Recording toggled: false") + fmt.Fprintf(c, "STATUS recording=%s\n", d.status) + } case 's': // status d.mu.Lock() - isRecording := d.recording + status := d.status d.mu.Unlock() - fmt.Fprintf(c, "STATUS recording=%t\n", isRecording) + fmt.Fprintf(c, "STATUS recording=%s\n", status) case 'v': // protocol version fmt.Fprintf(c, "STATUS proto=%s\n", bus.ProtoVer) case 'q': // quit daemon diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go deleted file mode 100644 index 5d654f0..0000000 --- a/internal/daemon/daemon_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package daemon - -import ( - "testing" - "time" - - "github.com/leonardotrapani/hyprvoice/internal/bus" - "github.com/leonardotrapani/hyprvoice/internal/notify" -) - -func TestToggle(t *testing.T) { - // Clean up any existing daemon - bus.RemovePidFile() - - d := New(notify.Nop{}) - - // Start daemon in goroutine - errCh := make(chan error, 1) - go func() { - errCh <- d.Run() - }() - - // Wait for daemon to be ready by trying to connect - maxAttempts := 50 - for i := range maxAttempts { - if _, err := bus.SendCommand('s'); err == nil { - break // daemon is ready - } - if i == maxAttempts-1 { - t.Fatal("daemon failed to start within timeout") - } - time.Sleep(10 * time.Millisecond) - } - - defer func() { - bus.SendCommand('q') - // Wait for daemon to exit - select { - case <-errCh: - case <-time.After(3 * time.Second): - t.Error("daemon did not exit within timeout") - } - }() - - // Test first toggle - if out, err := bus.SendCommand('t'); err != nil { - t.Fatalf("first toggle failed: %v", err) - } else if out != "STATUS recording=true\n" { - t.Fatalf("unexpected first toggle response: %s", out) - } - - if !d.Rec() { - t.Fatalf("state should be true after first toggle") - } - - // Test second toggle - if out, err := bus.SendCommand('t'); err != nil { - t.Fatalf("second toggle failed: %v", err) - } else if out != "STATUS recording=false\n" { - t.Fatalf("unexpected second toggle response: %s", out) - } - - if d.Rec() { - t.Fatalf("state should be false after second toggle") - } -}