package filter

import (
	"os"
	"testing"

	"gitbase.de/apairon/mark2web/pkg/jobm"
	"gitbase.de/apairon/mark2web/pkg/mark2web"

	"github.com/flosch/pongo2"

	. "github.com/smartystreets/goconvey/convey"
)

func TestImageProcessFilter(t *testing.T) {
	Convey("set context", t, func() {
		ctx := pongo2.Context{
			"testlocal": "/img/test.jpg",
			"testurl":   "http://url",
		}

		// we want to check files after function calls, so no multithreading
		jobm.SetNumCPU(1)

		mark2web.Config.Directories.Input = "../../test/in"
		mark2web.Config.Directories.Output = "../../test/out"

		mark2web.CurrentTreeNode = &mark2web.TreeNode{
			InputPath:  "../../test/in/content",
			OutputPath: "../../test/out",
			Config: &mark2web.PathConfig{
				Imaging: &mark2web.ImagingConfig{
					Quality: 60,
					Height:  300,
					Width:   300,
					Process: "fit",
				},
			},
		}

		os.Remove("../../test/out/fit_300x300_q060_test.jpg")

		Convey("local image with defaults", func() {
			output, err := pongo2.RenderTemplateString("{{ testlocal|image_process }}", ctx)
			So(err, ShouldBeNil)
			So(output, ShouldEqual, "fit_300x300_q060_test.jpg")

			Convey("local image with fit", func() {
				output, err := pongo2.RenderTemplateString(`{{ testlocal|image_process:"p=fit,w=300,h=300,q=60,t=/" }}`, ctx)
				So(err, ShouldBeNil)
				So(output, ShouldEqual, "/fit_300x300_q060_test.jpg")
			})
		})
	})
}
