StreamRecorder/pkg/nas/location_test.go
Aaron e2566fdefa
Some checks failed
Build Go Binaries / build (push) Has been cancelled
Claude bugfixes
2026-08-07 03:49:37 +00:00

124 lines
3.6 KiB
Go

package nas
import (
"strings"
"testing"
)
func TestParseLocationRemoteForms(t *testing.T) {
// Every spelling of the same destination must parse identically, so that
// an operator can configure it the way their platform's documentation
// describes without changing behaviour.
equivalent := []string{
`\\HomeLabNAS\dci\streams`,
`//HomeLabNAS/dci/streams`,
`smb://HomeLabNAS/dci/streams`,
`SMB://HomeLabNAS/dci/streams`,
`cifs://HomeLabNAS/dci/streams`,
`\\HomeLabNAS\dci\streams\`,
`\\HomeLabNAS\\dci\\streams`,
}
for _, raw := range equivalent {
loc, err := ParseLocation(raw)
if err != nil {
t.Errorf("ParseLocation(%q) failed: %v", raw, err)
continue
}
if !loc.IsRemote() {
t.Errorf("ParseLocation(%q) should be remote", raw)
}
if loc.Server != "HomeLabNAS" || loc.Share != "dci" {
t.Errorf("ParseLocation(%q) = server %q share %q, want HomeLabNAS/dci", raw, loc.Server, loc.Share)
}
if len(loc.Sub) != 1 || loc.Sub[0] != "streams" {
t.Errorf("ParseLocation(%q) sub = %v, want [streams]", raw, loc.Sub)
}
}
}
func TestParseLocationRendering(t *testing.T) {
loc, err := ParseLocation(`//HomeLabNAS/dci/streams/2026`)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := loc.UNC(), `\\HomeLabNAS\dci\streams\2026`; got != want {
t.Errorf("UNC() = %q, want %q", got, want)
}
if got, want := loc.ShareUNC(), `\\HomeLabNAS\dci`; got != want {
t.Errorf("ShareUNC() = %q, want %q", got, want)
}
if got, want := loc.SMBSource(), "//HomeLabNAS/dci"; got != want {
t.Errorf("SMBSource() = %q, want %q", got, want)
}
}
func TestParseLocationShareRoot(t *testing.T) {
loc, err := ParseLocation(`\\nas\media`)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(loc.Sub) != 0 {
t.Errorf("expected no subpath, got %v", loc.Sub)
}
if got, want := loc.UNC(), `\\nas\media`; got != want {
t.Errorf("UNC() = %q, want %q", got, want)
}
}
func TestParseLocationLocalPaths(t *testing.T) {
for _, raw := range []string{"/mnt/nas/streams", "data/streams", `C:\data\streams`, "/mnt"} {
loc, err := ParseLocation(raw)
if err != nil {
t.Errorf("ParseLocation(%q) failed: %v", raw, err)
continue
}
if loc.IsRemote() {
t.Errorf("ParseLocation(%q) should not be treated as a network share", raw)
}
if loc.Raw != raw {
t.Errorf("ParseLocation(%q).Raw = %q", raw, loc.Raw)
}
}
}
func TestParseLocationRejectsIncomplete(t *testing.T) {
for _, raw := range []string{"", " ", `\\`, `\\server`, "//server", "smb://server"} {
if _, err := ParseLocation(raw); err == nil {
t.Errorf("ParseLocation(%q) should have failed", raw)
}
}
}
func TestParseLocationTrimsWhitespace(t *testing.T) {
loc, err := ParseLocation(" \\\\nas\\dci ")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if loc.Server != "nas" || loc.Share != "dci" {
t.Errorf("whitespace not trimmed: server %q share %q", loc.Server, loc.Share)
}
}
func TestJoinBaseKeepsUNCSeparators(t *testing.T) {
// filepath.Join would produce \\nas\dci/event on Linux, which is not a
// path Windows would accept if the value were ever written to state.
got := joinBase(`\\nas\dci`, "event", "1080p")
if want := `\\nas\dci\event\1080p`; got != want {
t.Errorf("joinBase UNC = %q, want %q", got, want)
}
if got, want := joinBase(`\\nas\dci\`, "event"), `\\nas\dci\event`; got != want {
t.Errorf("joinBase trailing separator = %q, want %q", got, want)
}
if got := joinBase("/mnt/dci"); got != "/mnt/dci" {
t.Errorf("joinBase with no elements = %q", got)
}
if got, want := joinBase("/mnt/dci", "event"), "/mnt/dci/event"; !strings.HasSuffix(got, want) {
t.Errorf("joinBase local = %q, want suffix %q", got, want)
}
}