change from recording boolean to status enum

This commit is contained in:
LeonardoTrapani
2025-08-08 00:30:21 +02:00
parent 903274a249
commit 62c213456e
3 changed files with 36 additions and 90 deletions
-9
View File
@@ -1,9 +0,0 @@
package bus
import "testing"
func TestSockPath(t *testing.T) {
if _, err := SockPath(); err != nil {
t.Fatalf("SockPath: %v", err)
}
}
+32 -11
View File
@@ -15,9 +15,19 @@ import (
"github.com/leonardotrapani/hyprvoice/internal/notify" "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 { type Daemon struct {
mu sync.Mutex mu sync.Mutex
recording bool status Status
notifier notify.Notifier notifier notify.Notifier
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
@@ -32,13 +42,14 @@ func New(n notify.Notifier) *Daemon {
notifier: n, notifier: n,
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
status: Idle,
} }
} }
func (d *Daemon) Rec() bool { func (d *Daemon) Status() Status {
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock() defer d.mu.Unlock()
return d.recording return d.status
} }
func (d *Daemon) Run() error { func (d *Daemon) Run() error {
@@ -120,20 +131,30 @@ func (d *Daemon) handle(c net.Conn) {
switch cmd { switch cmd {
case 't': // toggle case 't': // toggle
d.mu.Lock() d.mu.Lock()
d.recording = !d.recording defer d.mu.Unlock()
isRecording := d.recording
d.mu.Unlock()
d.notifier.RecordingChanged(isRecording) switch d.status {
log.Printf("Recording toggled: %t", isRecording) 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 case 's': // status
d.mu.Lock() d.mu.Lock()
isRecording := d.recording status := d.status
d.mu.Unlock() d.mu.Unlock()
fmt.Fprintf(c, "STATUS recording=%t\n", isRecording) fmt.Fprintf(c, "STATUS recording=%s\n", status)
case 'v': // protocol version case 'v': // protocol version
fmt.Fprintf(c, "STATUS proto=%s\n", bus.ProtoVer) fmt.Fprintf(c, "STATUS proto=%s\n", bus.ProtoVer)
case 'q': // quit daemon case 'q': // quit daemon
-66
View File
@@ -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")
}
}