package sshd

import (
	"strings"
	"testing"
)

func TestKeyExchangePreference(t *testing.T) {
	got := keyExchanges()
	if kexSupported(kexMLKEM) && (len(got) == 0 || got[0] != kexMLKEM) {
		t.Fatalf("mlkem hoort vooraan: %v", got)
	}
	curve := indexOf(got, kexCurve)
	libssh := indexOf(got, kexLibSSH)
	if curve < 0 || libssh < 0 || libssh < curve {
		t.Fatalf("curve25519 hoort als fallback: %v", got)
	}
	for _, name := range []string{kexMLKEM, "sntrup761x25519-sha512", "sntrup761x25519-sha512@openssh.com"} {
		has := indexOf(got, name) >= 0
		if kexSupported(name) != has {
			t.Fatalf("%s ondersteund=%v aangeboden=%v in %v", name, kexSupported(name), has, got)
		}
		if has && indexOf(got, name) > curve {
			t.Fatalf("%s hoort voor curve25519: %v", name, got)
		}
	}
	if strings.Join(got, ",") != strings.Join(unique(got), ",") {
		t.Fatalf("dubbele namen: %v", got)
	}
}

func indexOf(list []string, want string) int {
	for i, name := range list {
		if name == want {
			return i
		}
	}
	return -1
}

func unique(list []string) []string {
	seen := map[string]bool{}
	var out []string
	for _, name := range list {
		if seen[name] {
			continue
		}
		seen[name] = true
		out = append(out, name)
	}
	return out
}
