package pkg_test

import (
	"io/fs"
	"testing"

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

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

	want := expectsFile{0}
	const ident = "N7M6W8-e55Wky_1q1a6qRKFVDw1S5XtPdB4HE2YZObG8OFum8HittyJgZUG5Ru2u"
	wantShebang := expectsFile("#!/bin/sh\n")
	const identShebang = "oKTGZb_TRtJf5G1iGGj4SOx1WUbI6YhmsUeEsCNvmK9tZYRwdwQf-DLFUxRPx5qm"
	wantELF := expectsFile("\x7fELF\xde\xad\xbe\xef")
	const identELF = "LjeHmIJgD1SiXIH8ewpGRed2_LU84NLapstnOAa_0hxLd8YY7x4KM0aApsj-RwCh"
	checkWithCache(t, []cacheTestCase{
		{"file", pkg.CValidateKnown, nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
			cureMany(t, c, []cureStep{
				{"short", pkg.NewFile("null", want), base.Append(
					"identifier",
					ident,
				), want, pkg.WNew, nil},

				{"shebang", pkg.NewFile("shebang", wantShebang), base.Append(
					"identifier",
					identShebang,
				), wantShebang, pkg.WNew, nil},

				{"elf", pkg.NewFile("ELF", wantELF), base.Append(
					"identifier",
					identELF,
				), wantELF, pkg.WNew, nil},
			})
		}, expectsFS{
			".": {Mode: fs.ModeDir | 0700},

			"checksum":                                   {Mode: fs.ModeDir | 0700},
			"checksum/" + pkg.Encode(want.hash()):        {Mode: 0400, Data: want},
			"checksum/" + pkg.Encode(wantShebang.hash()): {Mode: 0500, Data: wantShebang},
			"checksum/" + pkg.Encode(wantELF.hash()):     {Mode: 0500, Data: wantELF},

			"identifier":                 {Mode: fs.ModeDir | 0700},
			"identifier/" + ident:        {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/" + pkg.Encode(want.hash()))},
			"identifier/" + identShebang: {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/" + pkg.Encode(wantShebang.hash()))},
			"identifier/" + identELF:     {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/" + pkg.Encode(wantELF.hash()))},

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

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