package player import ( "fmt" "os" "path/filepath" "strings" "testing" "time" "github.com/lwoodard/rtsp-streamer/internal/config" "github.com/lwoodard/rtsp-streamer/internal/logbuf" ) func TestLastLinePrefersRealMessageOverStatus(t *testing.T) { w := logbuf.New(20) fmt.Fprintln(w, "Failed to open rtsps://host/x: Connection refused") fmt.Fprint(w, "\x1b[KV: 00:00:35 / 00:00:35 (100%)\n") // ANSI-wrapped status if got := lastLine(w); got != "Failed to open rtsps://host/x: Connection refused" { t.Errorf("lastLine = %q, want the error line", got) } } func TestLastLineFallsBackToStatusWhenOnlyStatus(t *testing.T) { w := logbuf.New(20) fmt.Fprint(w, "\x1b[KV: 00:00:20 / 00:00:20 (100%)\n") if got := lastLine(w); got != "V: 00:00:20 / 00:00:20 (100%)" { t.Errorf("lastLine = %q, want the stripped status line", got) } } func TestLastLineNil(t *testing.T) { if got := lastLine(nil); got != "" { t.Errorf("lastLine(nil) = %q, want empty", got) } } func testClock(cfg config.Clock) *Clock { return &Clock{cfg: cfg, textPath: "/run/user/1000/rtsp-streamer/clock-text.txt"} } // A zero-alpha canvas makes the drawn text inherit alpha 0, so the clock // renders as nothing at all. Config clamps the opacity, and args() must clamp // too rather than emitting a fully transparent canvas. func TestClockCanvasIsNeverFullyTransparent(t *testing.T) { for _, opacity := range []float64{0, -1, 0.45, 1} { c := testClock(config.Clock{Width: 300, Height: 72, BackgroundOpacity: opacity}) src := c.args()[len(c.args())-1] if strings.Contains(src, "black@0.000") { t.Errorf("opacity %v produced a fully transparent canvas: %s", opacity, src) } if !strings.Contains(src, "s=300x72") { t.Errorf("opacity %v: size missing from %s", opacity, src) } } } // The configured opacity must reach the filtergraph verbatim. func TestClockCanvasUsesConfiguredOpacity(t *testing.T) { src := testClock(config.Clock{Width: 300, Height: 72, BackgroundOpacity: 0.6}).args() if got := src[len(src)-1]; !strings.Contains(got, "black@0.600") { t.Errorf("got %s, want canvas alpha 0.600", got) } } // The time must be baked into every frame by drawtext, reloading the text file // each frame. An mpv OSD overlay only renders on the frame where its content // changes, which showed up as a flashing clock. func TestClockDrawsTextIntoEveryFrame(t *testing.T) { c := testClock(config.Clock{Width: 300, Height: 72, FontSize: 44}) src := c.args()[len(c.args())-1] for _, want := range []string{ "drawtext=textfile=" + c.textPath, "reload=1", // re-read the file per frame, else the clock freezes "fontsize=44", // from config, not hardcoded "borderw=3", // outline for legibility over bright scenes } { if !strings.Contains(src, want) { t.Errorf("filtergraph missing %q:\n %s", want, src) } } } // drawtext stamps the time when a frame is generated, so buffering ahead makes // the visible clock lag. These flags keep generation just-in-time. func TestClockDisablesReadahead(t *testing.T) { args := testClock(config.Clock{Width: 300, Height: 72, FontSize: 44}).args() joined := strings.Join(args, " ") for _, want := range []string{"--cache=no", "--demuxer-readahead-secs=0"} { if !strings.Contains(joined, want) { t.Errorf("args missing %q", want) } } } // writeText must replace the file atomically so drawtext cannot read a // half-written time, and must honour the configured layout and zone. func TestClockWriteTextIsAtomicAndFormatted(t *testing.T) { dir := t.TempDir() loc, err := time.LoadLocation("America/Denver") if err != nil { t.Fatal(err) } c := &Clock{ cfg: config.Clock{Format: "15:04:05"}, loc: loc, textPath: filepath.Join(dir, "clock-text.txt"), } if err := c.writeText(); err != nil { t.Fatal(err) } b, err := os.ReadFile(c.textPath) if err != nil { t.Fatal(err) } if got := string(b); len(got) != len("15:04:05") { t.Errorf("wrote %q, want an HH:MM:SS-shaped time", got) } // The temp file used for the atomic rename must not be left behind. if _, err := os.Stat(c.textPath + ".tmp"); !os.IsNotExist(err) { t.Errorf("temp file left behind: %v", err) } }