// Copyright (c) 2017-2026 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// SPDX-License-Identifier: EUPL-1.2

package p2p

import "testing"

func TestPeerBuildVersion_Good(t *testing.T) {
	tests := []struct {
		name   string
		input  string
		want   uint64
		wantOK bool
	}{
		{"release", "6.0.1.2[go-blockchain]", 601, true},
		{"two_digits", "12.3.4.5", 1234, true},
		{"suffix", "6.0.1-beta.2", 601, true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, ok := PeerBuildVersion(tt.input)
			if ok != tt.wantOK {
				t.Fatalf("PeerBuildVersion(%q) ok = %v, want %v", tt.input, ok, tt.wantOK)
			}
			if got != tt.want {
				t.Fatalf("PeerBuildVersion(%q) = %d, want %d", tt.input, got, tt.want)
			}
		})
	}
}

func TestPeerBuildVersion_Bad(t *testing.T) {
	tests := []string{
		"",
		"6",
		"6.0",
		"abc.def.ghi",
	}

	for _, input := range tests {
		t.Run(input, func(t *testing.T) {
			if got, ok := PeerBuildVersion(input); ok || got != 0 {
				t.Fatalf("PeerBuildVersion(%q) = (%d, %v), want (0, false)", input, got, ok)
			}
		})
	}
}

func TestMeetsMinimumBuildVersion_Good(t *testing.T) {
	if !MeetsMinimumBuildVersion("6.0.1.2[go-blockchain]", false) {
		t.Fatal("expected mainnet build version to satisfy minimum")
	}
	if !MeetsMinimumBuildVersion("6.0.1.2[go-blockchain]", true) {
		t.Fatal("expected testnet build version to satisfy minimum")
	}
}

func TestMeetsMinimumBuildVersion_Bad(t *testing.T) {
	if MeetsMinimumBuildVersion("0.0.1.0", false) {
		t.Fatal("expected low mainnet build version to fail")
	}
	if MeetsMinimumBuildVersion("0.0.1.0", true) {
		t.Fatal("expected low testnet build version to fail")
	}
	if MeetsMinimumBuildVersion("bogus", false) {
		t.Fatal("expected malformed version to fail")
	}
}
