package main

import (
	"os"
	"path"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

const mirrorlist = `################################################################################
	################# Arch Linux mirrorlist generated by Reflector #################
	################################################################################

	# With:       reflector --country Germany --verbose --sort rate --save /etc/pacman.d/mirrorlist
	# When:       2020-01-10 11:51:21 GMT+7
	# From:       https://archlinux.org/mirrors/status/json/
	# Retrieved:  2020-01-10 11:51:21 GMT+7
	# Last Check: 2020-01-10 11:51:21 GMT+7

	Server = http://mirror.sample.ca/archlinux/$repo/os/$arch
	Server = https://a.ab.net/archlinux/$arch/os/$repo
	Server = http://example.tld/repos/$repo/os/$arch#something # Comment
	Server =https://whatever.mirror.server.com/$repo/os/$arch
	Server= http://localhost/mirror/packages/Blackarch/$repo/os/$arch
	Server = https://tooManyTests.com/archlinux/$whatever/os/$arch #Comment
	Server = http://another.test.co.uk/archlinux/$repo/o\$s/$arch
	Server = http://ThisOneShouldNotBeIncluded.test.co.uk/archlinux/
	`

var expectedURLs = []string{
	`http://mirror.sample.ca/archlinux/`,
	`https://a.ab.net/archlinux/`,
	`http://example.tld/repos/`,
	`https://whatever.mirror.server.com/`,
	`http://localhost/mirror/packages/Blackarch/`,
	`https://tooManyTests.com/archlinux/`,
	`http://another.test.co.uk/archlinux/`,
}

func TestParseMirrorlist(t *testing.T) {
	temp := t.TempDir()
	tmpMirrorfile := path.Join(temp, "tmpMirrorFile")

	f, err := os.Create(tmpMirrorfile)
	require.NoError(t, err)
	f.Write([]byte(mirrorlist))
	f.Close()
	f, err = os.Open(tmpMirrorfile)

	actualURLs, err := parseMirrorlistURLs(f)
	require.NoError(t, err)
	require.Equal(t, expectedURLs, actualURLs)
}

func TestGetCurrentURLs(t *testing.T) {
	temp := t.TempDir()
	tmpMirrorfile := path.Join(temp, "tmpMirrorFile")

	require.NoError(t, os.WriteFile(tmpMirrorfile, []byte(mirrorlist), 0o644))

	config := parseConfig([]byte(`
cache_dir: ` + temp + `
purge_files_after: 2592000 # 3600 * 24 * 30days
download_timeout: 200
port: 9139
repos:
  archTest:
    mirrorlist: ` + tmpMirrorfile + `

`))
	archTest := config.Repos["archTest"]
	require.Equal(t, expectedURLs, archTest.getUrls())

	fileInfo, _ := os.Stat(tmpMirrorfile)
	require.Equal(t, fileInfo.ModTime(), archTest.LastModificationTime)

	gotCheckTime := archTest.LastMirrorlistCheck
	require.LessOrEqual(t, time.Since(gotCheckTime), 3*time.Second)
}

func TestEmptyMirrorlist(t *testing.T) {
	temp := t.TempDir()
	tmpMirrorfile := path.Join(temp, "tmpMirrorFile")

	require.NoError(t, os.WriteFile(tmpMirrorfile, []byte(""), 0o644))

	config := parseConfig([]byte(`
cache_dir: ` + temp + `
repos:
  archTest:
    mirrorlist: ` + tmpMirrorfile + `
`))
	archTest := config.Repos["archTest"]
	urls, err := archTest.getMirrorlistURLs()
	require.Error(t, err)
	require.Nil(t, urls)
}
