package testing_test

import (
	"crypto/md5"
	"crypto/sha256"
	"encoding/hex"
	"os"
	"path/filepath"
	"testing"

	"metis/pkg/image"
)

func TestDownloadFileURL(t *testing.T) {
	tmp := t.TempDir()
	src := filepath.Join(tmp, "src.bin")
	if err := os.WriteFile(src, []byte("hello"), 0o644); err != nil {
		t.Fatal(err)
	}
	dest := filepath.Join(tmp, "dest.bin")
	if err := image.Download("file://"+src, dest); err != nil {
		t.Fatalf("Download: %v", err)
	}
	data, err := os.ReadFile(dest)
	if err != nil {
		t.Fatal(err)
	}
	if string(data) != "hello" {
		t.Fatalf("downloaded content = %q", data)
	}
}

func TestChecksumHelpers(t *testing.T) {
	tmp := t.TempDir()
	path := filepath.Join(tmp, "file.bin")
	if err := os.WriteFile(path, []byte("abc"), 0o644); err != nil {
		t.Fatal(err)
	}
	sha := sha256.Sum256([]byte("abc"))
	if err := image.VerifyChecksum(path, "sha256:"+hex.EncodeToString(sha[:])); err != nil {
		t.Fatalf("VerifyChecksum sha256: %v", err)
	}
	md5sum := md5.Sum([]byte("abc"))
	if err := image.VerifyChecksum(path, "md5:"+hex.EncodeToString(md5sum[:])); err != nil {
		t.Fatalf("VerifyChecksum md5: %v", err)
	}
}
