// Copyright 2016 by Sandro Santilli <strk@kbt.io>
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package libravatar

import "testing"

func TestFromEmail(t *testing.T) {

	avt := New()

	// Email tests

	cases := []struct{ in, want string }{
		{"strk@kbt.io", "http://avatars.kbt.io/avatar/5ae72c1d61f690d66f62227997cbc311e948230af98faff72e2bda393a682537"},
		{"strk@keybit.net", "http://cdn.libravatar.org/avatar/9a6c11d66829bf429efe5ef4c066d50272394481cc3f8ae8116a006c81dc6cf9"},
		{"strk@nonexistent.domain", "http://cdn.libravatar.org/avatar/7da75f65717c9cabbbb66f383c4a928189ea95596c6f8e89103f14bbf12af44b"},
		{"invalid", "mail: missing '@' or angle-addr"},
		{"invalid@", "mail: missing '@' or angle-addr"},
		{"@invalid", "mail: missing word in phrase: mail: invalid string"},
	}

	for _, c := range cases {
		got, err := avt.FromEmail(c.in)
		if err != nil {
			got = err.Error()
		}
		if got != c.want {
			t.Errorf("fromEmail(%q) == %q, expected %q", c.in, got, c.want)
		}
	}

	// TODO: test https with email

	// OpenID tests

	cases = []struct{ in, want string }{
		{"https://strk.kbt.io/openid/", "http://cdn.libravatar.org/avatar/1eaf3174c95d0df02f177f7f6a1df5125ad3d6603fbd062defecd30810a0463c"},
		{"invalid", "Is not an absolute URL"},
		{"ssh://user@nothttp/", "Invalid protocol: ssh"},
	}

	for _, c := range cases {
		got, err := avt.FromURL(c.in)
		if err != nil {
			got = err.Error()
		}
		if got != c.want {
			t.Errorf("fromURL(%q) == %q, expected %q", c.in, got, c.want)
		}
	}

	// TODO: test parameters

}
