- Replace the Python implementation with a Go module compiling to a single static binary (no Python/pip runtime dependency) - internal/tonieapi: minimal, dependency-light HTTP client for the TonieCloud REST API (login, households, creative tonies, file upload via presigned S3 request, chapter add/sort/clear) - internal/config: YAML-based mapping of local playlist folders to Kreativ-Tonies, credentials never stored - internal/syncer: diff/apply logic (upload new tracks, prune removed chapters, reorder to match local file order), built against a TonieClient interface for testability - cmd/toni-sync: Cobra CLI with `tonies list`, `config add/list/remove`, `sync [NAME|--all] [--dry-run]` - Unit tests for config persistence and syncer plan/apply logic Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestUpsertAddsNewMapping(t *testing.T) {
|
|
cfg := &Config{}
|
|
m := Mapping{Name: "a", HouseholdID: "h1", TonieID: "t1", Folder: "/tmp/a"}
|
|
cfg.Upsert(m)
|
|
got := cfg.Get("a")
|
|
if got == nil || got.TonieID != "t1" {
|
|
t.Fatalf("expected mapping to be added, got %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestUpsertReplacesExistingMapping(t *testing.T) {
|
|
cfg := &Config{}
|
|
cfg.Upsert(Mapping{Name: "a", HouseholdID: "h1", TonieID: "t1", Folder: "/tmp/a"})
|
|
cfg.Upsert(Mapping{Name: "a", HouseholdID: "h1", TonieID: "t2", Folder: "/tmp/a2"})
|
|
|
|
if len(cfg.Mappings) != 1 {
|
|
t.Fatalf("expected 1 mapping, got %d", len(cfg.Mappings))
|
|
}
|
|
if cfg.Get("a").TonieID != "t2" {
|
|
t.Fatalf("expected updated tonie id t2, got %s", cfg.Get("a").TonieID)
|
|
}
|
|
}
|
|
|
|
func TestRemoveReturnsFalseWhenMissing(t *testing.T) {
|
|
cfg := &Config{}
|
|
if cfg.Remove("nope") {
|
|
t.Fatal("expected Remove to return false for missing mapping")
|
|
}
|
|
}
|
|
|
|
func TestSaveAndLoadRoundtrip(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
|
|
cfg := &Config{}
|
|
cfg.Upsert(Mapping{
|
|
Name: "peppa",
|
|
HouseholdID: "h1",
|
|
TonieID: "t1",
|
|
Folder: "/tmp/peppa",
|
|
PlaylistRef: "https://example.com/playlist/1",
|
|
Prune: true,
|
|
})
|
|
|
|
if err := Save(cfg, path); err != nil {
|
|
t.Fatalf("Save failed: %v", err)
|
|
}
|
|
|
|
loaded, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load failed: %v", err)
|
|
}
|
|
m := loaded.Get("peppa")
|
|
if m == nil || m.TonieID != "t1" || m.PlaylistRef != "https://example.com/playlist/1" {
|
|
t.Fatalf("unexpected loaded mapping: %+v", m)
|
|
}
|
|
}
|
|
|
|
func TestLoadMissingFileReturnsEmptyConfig(t *testing.T) {
|
|
cfg, err := Load(filepath.Join(t.TempDir(), "does-not-exist.yaml"))
|
|
if err != nil {
|
|
t.Fatalf("Load failed: %v", err)
|
|
}
|
|
if len(cfg.Mappings) != 0 {
|
|
t.Fatalf("expected empty mappings, got %+v", cfg.Mappings)
|
|
}
|
|
}
|