package worker

import "testing"

func TestTranslatePath(t *testing.T) {
	cases := []struct {
		name string
		path string
		m    map[string]string
		want string
	}{
		{
			name: "empty map returns path as is",
			path: "/srv/media/downloads",
			m:    nil,
			want: "/srv/media/downloads",
		},
		{
			name: "no matching prefix returns path as is",
			path: "/other/downloads",
			m:    map[string]string{"/data": "/srv/media"},
			want: "/other/downloads",
		},
		{
			name: "prefix translated on segment boundary",
			path: "/data/downloads/Show/e1.mkv",
			m:    map[string]string{"/data": "/srv/media"},
			want: "/srv/media/downloads/Show/e1.mkv",
		},
		{
			name: "exact match of key",
			path: "/data",
			m:    map[string]string{"/data": "/srv/media"},
			want: "/srv/media",
		},
		{
			name: "does not match partial segment",
			path: "/data2/downloads",
			m:    map[string]string{"/data": "/srv/media"},
			want: "/data2/downloads",
		},
		{
			name: "longest matching prefix wins",
			path: "/data/dl/x.mkv",
			m: map[string]string{
				"/data":    "/srv/A",
				"/data/dl": "/srv/B",
			},
			want: "/srv/B/x.mkv",
		},
		{
			name: "trailing slashes in keys/values normalized",
			path: "/data/downloads/x.mkv",
			m:    map[string]string{"/data/": "/srv/media/"},
			want: "/srv/media/downloads/x.mkv",
		},
		{
			name: "empty path returns empty",
			path: "",
			m:    map[string]string{"/data": "/srv/media"},
			want: "",
		},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if got := translatePath(tc.path, tc.m); got != tc.want {
				t.Errorf("translatePath(%q) = %q, want %q", tc.path, got, tc.want)
			}
		})
	}
}
