package cli

import (
	"errors"
	"testing"
)

func TestPathPrefixValueSet(t *testing.T) {
	cases := map[string]string{
		"":           "",
		"/":          "",
		"foo":        "foo",
		"/foo":       "foo",
		"foo/":       "foo",
		"/foo/":      "foo",
		"foo//bar":   "foo/bar",
		"foo/./bar":  "foo/bar",
		"foo/bar/..": "foo",
		"a/b/../c":   "a/c",
	}
	for in, want := range cases {
		var p string
		v := &pathPrefixValue{p: &p}
		if err := v.Set(in); err != nil {
			t.Errorf("Set(%q): unexpected error %v", in, err)
			continue
		}
		if p != want {
			t.Errorf("Set(%q) = %q, want %q", in, p, want)
		}
	}
}

func TestPathPrefixValueSetEscapes(t *testing.T) {
	for _, in := range []string{"..", "../x", "/../x", "foo/../../x"} {
		var p string
		v := &pathPrefixValue{p: &p}
		var ue usageError
		if err := v.Set(in); !errors.As(err, &ue) {
			t.Errorf("Set(%q): expected usageError, got %v", in, err)
		}
	}
}

func TestPrefixedPath(t *testing.T) {
	if got := prefixedPath("", "assets/x.js"); got != "assets/x.js" {
		t.Errorf("empty prefix: got %q", got)
	}
	if got := prefixedPath("foo", "assets/x.js"); got != "foo/assets/x.js" {
		t.Errorf("prefixed: got %q", got)
	}
}
