Files
talk/cmd/talk/testcmd.go
T
m3ta-chironandClaude Fable 5.1 2d9d2a86e9
release / release (push) Canceled after 0s
test / test (push) Canceled after 0s
feat(notify): Windows toasts under talk's own identity, icon on Linux
The daemon (and talk test --toast) now register the AppUserModelID
m3ta.talk under HKCU with display name and the installed icon, so
toasts appear as "talk" with the icon instead of Windows PowerShell;
the PowerShell identity stays as fallback until the registration has
run once. The redundant "talk" title line is dropped under the own
identity. notify.Setup bundles icon install, identity and the talk:
protocol handler for both callers. Linux notifications carry the
installed icon as app_icon. doctor names the active identity.

Verified on the Windows 11 VM: registry key, icon file, toast with
working Cancel button, exe icon; Linux via quickshell.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-25 12:43:14 +01:00

148 lines
5.5 KiB
Go

package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"time"
"code.m3ta.dev/m3tam3re/talk/internal/config"
"code.m3ta.dev/m3tam3re/talk/internal/notify"
"code.m3ta.dev/m3tam3re/talk/internal/queue"
"code.m3ta.dev/m3tam3re/talk/internal/speak"
)
// testPhrase is the fixed test sentence per language.
var testPhrase = map[string]string{
"de": "Dies ist eine Testansage von talk. Wenn du das hörst, funktioniert alles.",
"en": "This is a test announcement from talk. If you can hear this, everything works.",
}
// toastPhrase is the text of the test notification per language.
var toastPhrase = map[string]string{
"de": "Dies ist eine Testbenachrichtigung von talk. So sehen Ansagen in der Ruhezeit aus.",
"en": "This is a test notification from talk. This is what announcements look like during quiet hours.",
}
// runTest implements `talk test`: speaks the test sentence DIRECTLY in this
// process (not via the daemon), so the command sees which tier really spoke
// and how long synthesis and playback took. Mute and quiet hours
// deliberately do not apply (the user is testing right now); an active mute
// is mentioned. With --toast it sends a test desktop notification instead,
// through the same path quiet_mode = toast uses, so the notification setup
// can be checked without a quiet window and a daemon restart.
func runTest(args []string, opts *sayOptions, _ io.Reader, stdout, stderr io.Writer) int {
if len(args) > 0 {
fmt.Fprintln(stderr, "talk test: no arguments expected (language: --lang de|en)")
return exitUsage
}
if code, bad := validateSayOptions(opts, stderr); bad {
return code
}
cfg, err := config.Load(config.Options{Getenv: getenv})
if err != nil {
fmt.Fprintf(stderr, "talk: %v\n", err)
return exitFailure
}
lang := opts.lang
if lang == "" {
lang = cfg.Lang
}
if opts.toast {
return runToastTest(lang, getenv, stdout, stderr)
}
if dir, err := config.Dir(getenv); err == nil {
if (queue.MuteStore{Path: filepath.Join(dir, queue.MuteFileName)}).Muted() {
fmt.Fprintln(stderr, "talk test: mute is on; the test plays anyway, normal announcements stay silent (talk unmute)")
}
}
if cfg.Audio.Sink == config.SinkNull {
fmt.Fprintln(stderr, "talk test: audio.sink = null; no sound will be produced")
}
engine, err := speak.New(cfg, getenv, stderr)
if err != nil {
fmt.Fprintf(stderr, "talk: %v\n", err)
return exitFailure
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
out, err := engine.Speak(ctx, speak.Request{Text: testPhrase[lang], Lang: lang, Provider: opts.provider, Voice: opts.voice})
if err != nil {
fmt.Fprintf(stderr, "talk test: %v\n", err)
fmt.Fprintln(stderr, "hint: `talk doctor` shows which tier is missing and why.")
return exitFailure
}
fmt.Fprintf(stdout, "provider: %s\n", out.Provider)
fmt.Fprintf(stdout, "language: %s\n", lang)
switch {
case out.Discarded:
fmt.Fprintln(stdout, "playback: discarded (audio.sink = null)")
case out.Direct:
fmt.Fprintf(stdout, "duration: %s (direct speech via the OS)\n", out.Synthesis.Round(time.Millisecond))
default:
fmt.Fprintf(stdout, "synthesis: %s (%d bytes)\n", out.Synthesis.Round(time.Millisecond), out.Bytes)
fmt.Fprintf(stdout, "playback: %s\n", out.Playback.Round(time.Millisecond))
}
return exitOK
}
// runToastTest shows the test notification the way a playing announcement
// gets one (with a Cancel button where supported), waits a few seconds for a
// click and reports what happened. Exit 1 when no tool exists or the call
// fails.
func runToastTest(lang string, getenv func(string) string, stdout, stderr io.Writer) int {
// Same desktop setup as the daemon, so the test shows what announcements
// will show (icon, identity, Cancel button).
if dir, err := config.Dir(getenv); err == nil {
exe, _ := os.Executable()
if err := notify.Setup(dir, exe); err != nil {
fmt.Fprintf(stderr, "talk test: %v\n", err)
}
}
fmt.Fprintf(stdout, "toast: %s\n", notify.Describe())
if !notify.Available() {
fmt.Fprintln(stderr, "talk test: no notification path found; on Linux install libnotify (notify-send) or run a notification daemon, macOS and Windows have one built in")
return exitFailure
}
label := "Cancel"
if lang == "de" {
label = "Abbrechen"
}
clicked := make(chan struct{}, 1)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
start := time.Now()
closeFn, err := notify.Announce(ctx, notify.Announcement{Text: toastPhrase[lang], CancelLabel: label, CancelURI: "talk:test", Tag: "test"}, func() {
select {
case clicked <- struct{}{}:
default:
}
})
if err != nil {
fmt.Fprintf(stderr, "talk test: %v\n", err)
fmt.Fprintln(stderr, "hint: on Linux a notification daemon (mako, dunst, GNOME Shell, KDE) must be running.")
return exitFailure
}
fmt.Fprintf(stdout, "sent: %s\n", time.Since(start).Round(time.Millisecond))
if !notify.SupportsCancel() {
fmt.Fprintln(stdout, "cancel: not available on this platform (notification without button)")
fmt.Fprintln(stdout, "If no notification appeared, your notification daemon did not show it (talk only hands it over).")
return exitOK
}
fmt.Fprintf(stdout, "cancel: button %q shown; waiting up to %s for a click …\n", label, toastWait)
select {
case <-clicked:
fmt.Fprintln(stdout, "cancel: clicked, works")
case <-time.After(toastWait):
fmt.Fprintln(stdout, "cancel: not clicked (that is fine); closing the notification")
}
closeFn()
return exitOK
}
// toastWait is how long `talk test --toast` waits for a click on Cancel.
const toastWait = 8 * time.Second