package nix_test

import (
	"encoding/json"
	"slices"
	"strings"
	"testing"

	"gensokyo.uk/nix"
)

func TestCollectFromDerivations(t *testing.T) {
	testCases := []struct {
		name string
	}{
		{"getchoo atlas"},
		{"getchoo glados"},
		{"pluiedev pappardelle"},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			var derivations nix.DerivationMap
			if err := json.Unmarshal(drvShow[tc.name], &derivations); err != nil {
				t.Fatalf("cannot unmarshal test data: %v", err)
			}
			got := nix.CollectFromDerivations(derivations)
			want := collectWant[tc.name]
			if !slices.Equal(got, want) {
				t.Errorf("CollectFromDerivations:\n%s, want\n%s",
					strings.Join(got, "\n"), strings.Join(want, "\n"))
			}
		})
	}

	t.Run("edge cases", func(t *testing.T) {
		// this exclusively tests edge cases for nil values and buffer growing, so the data doesn't have to make sense
		want := []string{"", "big"}
		got := nix.CollectFromDerivations(nix.DerivationMap{
			"nil": nil,
			"big": &nix.Derivation{InputSources: make([]string, 1<<18)},
		})
		if !slices.Equal(got, want) {
			t.Errorf("CollectFromDerivations: %#v, want %#v", got, want)
		}
	})
}
