package pkg_test

import (
	"bytes"
	"compress/gzip"
	"crypto/sha512"
	"io/fs"
	"net/http"
	"testing"
	"testing/fstest"

	"hakurei.app/check"
	"hakurei.app/internal/pkg"
)

func TestDecompress(t *testing.T) {
	t.Parallel()

	var buf bytes.Buffer
	gw := gzip.NewWriter(&buf)
	if _, err := gw.Write([]byte{0}); err != nil {
		t.Fatal(err)
	} else if err = gw.Close(); err != nil {
		t.Fatal(err)
	}
	testdata := buf.String()

	var transport http.Transport
	client := http.Client{Transport: &transport}
	transport.RegisterProtocol("file", http.NewFileTransportFS(fstest.MapFS{
		"testdata": {Data: []byte(testdata), Mode: 0400},
	}))
	testdataChecksum := func() pkg.Checksum {
		h := sha512.New384()
		h.Write([]byte(testdata))
		return (pkg.Checksum)(h.Sum(nil))
	}()

	checkWithCache(t, []cacheTestCase{
		{"decompress", 0, nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
			cureMany(t, c, []cureStep{
				{"close", pkg.NewDecompress(pkg.NewHTTPGet(
					&client,
					"file:///testdata",
					pkg.Checksum{0xfd},
				), pkg.Gzip), nil, nil, pkg.WNew, &pkg.ChecksumMismatchError{
					Got:  testdataChecksum,
					Want: pkg.Checksum{0xfd},
				}},

				{"gzip", pkg.NewDecompress(pkg.NewHTTPGet(
					&client,
					"file:///testdata",
					testdataChecksum,
				), pkg.Gzip), ignorePathname, expectsChecksum(sha512.Sum384([]byte{0})), pkg.WNew, nil},
			})
		}, expectsFS{
			".": {Mode: fs.ModeDir | 0700},

			"checksum": {Mode: fs.ModeDir | 0700},
			"checksum/" + pkg.Encode(sha512.Sum384([]byte{0})): {Mode: 0400, Data: []byte{0}},

			"identifier": {Mode: fs.ModeDir | 0700},
			"identifier/QpjkahDrz7pz-tv0eAGNXR6x9NAtTjWCK5Hr7G1cIZj9rT7bLYJWUQeLD4wamAlF": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/vsAhtPNo4waRNOASwrQwcIPTqb3SBuJOXw2G4T1mNmVZM-wrQTRllmgXqcIIoRcX")},

			"substitute": {Mode: fs.ModeDir | 0700},
			"work":       {Mode: fs.ModeDir | 0700},
		}},
	})
}
