Files
xTeVe/src/toolchain_test.go
T
nathan 3bbea8e952 Phase 2b: server/client timeouts, atomic state writes, log mutex, panic and shutdown fixes
- http.Server with ReadHeaderTimeout/IdleTimeout (no read/write timeouts: /stream/ is long-lived)
- shared outbound clients: providerHTTPClient (5m), apiHTTPClient (30s), imgcache client (30s)
- imgcache: download outside the lock, per-item helper, cache URL uses the file name not the fs path
- writeFileAtomic (temp + fsync + rename) for settings/xepg/pms/urls/authentication JSON
- one package-level logMu for WebScreenLog and notifications; ring buffer keeps the newest lines
- notifications evict the oldest instead of random map entries
- xepg XMLTV file removal rebuilt after the loop; data.go range-mutation removed
- API handler returns after error body; WS request/response fresh per command
- checked type assertions in data/backup/provider/screen
- SIGINT/SIGTERM handled in main via src.Shutdown(); fatal paths exit 1
2026-09-26 13:17:07 +10:00

87 lines
1.8 KiB
Go

package src
import (
"os"
"path/filepath"
"reflect"
"testing"
)
func TestWriteFileAtomic(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "settings.json")
if err := writeFileAtomic(file, []byte("first"), 0644); err != nil {
t.Fatal(err)
}
if err := writeFileAtomic(file, []byte("second"), 0600); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
if string(got) != "second" {
t.Errorf("content = %q, want %q", got, "second")
}
fi, err := os.Stat(file)
if err != nil {
t.Fatal(err)
}
if fi.Mode().Perm() != 0600 {
t.Errorf("mode = %o, want 0600", fi.Mode().Perm())
}
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
if e.Name() != "settings.json" {
t.Errorf("unexpected leftover file %q", e.Name())
}
}
}
func TestWriteFileAtomicMissingDir(t *testing.T) {
file := filepath.Join(t.TempDir(), "missing", "settings.json")
if err := writeFileAtomic(file, []byte("x"), 0644); err == nil {
t.Fatal("expected an error for a missing directory")
}
}
func TestRemoveStrings(t *testing.T) {
list := []string{"a.xml", "b.xml", "c.xml", "d.xml"}
got := removeStrings(list, map[string]bool{"b.xml": true, "d.xml": true})
want := []string{"a.xml", "c.xml"}
if !reflect.DeepEqual(got, want) {
t.Errorf("removeStrings = %v, want %v", got, want)
}
// Nothing to remove: same content, original untouched.
got = removeStrings(list, nil)
if !reflect.DeepEqual(got, list) {
t.Errorf("removeStrings(nil) = %v, want %v", got, list)
}
}
func TestToFloat64(t *testing.T) {
cases := []struct {
in any
want float64
}{
{float64(3), 3},
{int(2), 2},
{int64(1), 1},
{"x", 0},
{nil, 0},
}
for _, c := range cases {
if got := toFloat64(c.in); got != c.want {
t.Errorf("toFloat64(%v) = %v, want %v", c.in, got, c.want)
}
}
}