package microvm

import (
	"slices"
	"testing"
)

func TestWorkflowSubstituters(t *testing.T) {
	urls, keys, err := workflowSubstituters(map[string]string{
		"https://hydra.nixos.org/":  "hydra.nixos.org-1:CNHJZBh9K4tP3EKF6FkkgeVYsS3ohTl+oS0Qa8bezVs=",
		"https://cache.garnix.io/":  "cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=",
		"https://unsigned.example/": "",
	})
	if err != nil {
		t.Fatal(err)
	}

	wantURLs := []string{
		"https://cache.garnix.io/",
		"https://hydra.nixos.org/",
		"https://unsigned.example/",
	}
	if !slices.Equal(urls, wantURLs) {
		t.Fatalf("urls: got %v, want %v", urls, wantURLs)
	}
	wantKeys := []string{
		"cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=",
		"hydra.nixos.org-1:CNHJZBh9K4tP3EKF6FkkgeVYsS3ohTl+oS0Qa8bezVs=",
	}
	if !slices.Equal(keys, wantKeys) {
		t.Fatalf("keys: got %v, want %v", keys, wantKeys)
	}
}

func TestWorkflowSubstitutersRejectsBadURLs(t *testing.T) {
	for _, bad := range []string{"ftp://cache.example/", "not a url"} {
		if _, _, err := workflowSubstituters(map[string]string{bad: ""}); err == nil {
			t.Errorf("workflowSubstituters(%q): expected error, got nil", bad)
		}
	}
}
