package gmi

import (
	"bytes"
	"net/url"
	"strconv"
	"testing"

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

func TestGemtextToMarkdown(t *testing.T) {

	gmiGateway, _ := url.Parse("https://gateway.com/x/")

	tests := []struct {
		in, exp string
	}{
		{
			in:  "",
			exp: "",
		},
		{
			in:  "=> foo",
			exp: "[Link](foo)\n",
		},
		{
			in:  "what\n=> foo\n=> bar",
			exp: "what\n[Link](foo)\n[Link](bar)\n",
		},
		{
			in:  "=> foo description is here   ",
			exp: "[description is here](foo)\n",
		},
		{
			in:  "=> img.png",
			exp: "![Image](img.png)\n",
		},
		{
			in:  "=> img.png description is here   ",
			exp: "![description is here](img.png)\n",
		},
		{
			in:  "=> gemini://somewhere.com/foo Somewhere",
			exp: "[Somewhere](https://gateway.com/x/somewhere.com/foo)\n",
		},
		{
			in:  "=> gemini://somewhere.com:420/foo Somewhere",
			exp: "[Somewhere](https://gateway.com/x/somewhere.com:420/foo)\n",
		},
		{
			in:  "=> gemini://somewhere.com:420/foo?bar=baz Somewhere",
			exp: "[Somewhere](https://gateway.com/x/somewhere.com:420/foo?bar=baz)\n",
		},
	}

	for i, test := range tests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {

			got := new(bytes.Buffer)
			err := GemtextToMarkdown(got, bytes.NewBufferString(test.in), gmiGateway)
			assert.NoError(t, err)
			assert.Equal(t, test.exp, got.String())
		})
	}
}
