package infrastructure

import (
	"code.osinet.fr/fgm/kurz/domain"
	"testing"
	)

func TestMemoryEmptyRepo(test *testing.T) {

	domain.RegisterRepositories(
		shortRepo,
		targetRepo,
	)

	_, err := domain.GetTargetURL("whatever")
	if err == nil {
		test.Error("Empty repository should not find a target for any URL")
	}

	s1, isNew, err := domain.GetShortURL(exampleValidHTTPURL)
	if err != nil {
		test.Error("Creating a short URL for a valid URL should not fail")
	}
	if !isNew {
		test.Error("The first short URL in an empty repository should be new")
	}
	s2, isNew, err := domain.GetShortURL(exampleValidHTTPURL)
	if err != nil {
		test.Error("Creating a short URL for a valid URL should not fail")
	}
	if isNew {
		test.Error("The second short URL for an already shortened URL should not be new")
	}
	if s1 != s2 {
		test.Error("The second short URL for an already shortened URL should be the same as the first one")
	}

	t, err := domain.GetTargetURL(s1)
	if err != nil {
		test.Error("Repository should find a target for an existing short URL")
	}
	if t != exampleValidHTTPURL {
		test.Error("Target URL for a short URL should match the target it was created for")
	}
}

func TestMemorySad(test *testing.T) {

	domain.RegisterRepositories(
		MemoryShortURLRepository{},
		MemoryTargetURLRepository{},
	)

	_, _, err := domain.GetShortURL("")
	if err == nil {
		test.Error("Empty target URL has no valid short URL")
	}
}