package protocol

import (
	"encoding/base64"
	"testing"
)

func TestLicenseDerivation_Ts3jVectors(t *testing.T) {
	// Vectors from ts3j LicenseDerivationTest.java
	tests := []struct {
		name     string
		license  string
		expected string
	}{
		{
			name: "Vector 1",
			license: "AQA1hUFJiiSs0wFXkYuPUJVcDa6XCrZTcsvkB0Ffzz4CmwIITRXgCqeTYA" +
				"cAAAAgQW5vbnltb3VzAADSN9wlGHZEHZvX7ImHoqYezibj5byDh0f4oMsG3afDxyAKePI" +
				"VCnma1Q==",
			expected: "z/bYm6TmHmuAil/osx8eGi6Oits2vIO4i6Bm13RuiGg=",
		},
		{
			name: "Vector 2",
			license: "AQA1hUFJiiSs0wFXkYuPUJVcDa6XCrZTcsvkB0Ffzz4C" +
				"mwIITRXgCqeTYAcAAAAgQW5vbnltb3VzAABx1YQfzCiB8b" +
				"ZZAdGwXNTLmdhiOpjaH3OOlISy5vrM3iAKePBVCnmZFQ==",
			expected: "lrukIi392D7ltdKFp5mURT3Ydk+oWYNjMt3kptbQl6I=",
		},
		{
			name: "Vector 3",
			license: "AQA1hUFJiiSs0wFXkYuPUJVcDa6XCrZTcsvkB0Ffzz4CmwIITR" +
				"XgCqeTYAcAAAAgQW5vbnltb3VzAAAK5C0l+xtOTAZGEA/GHHOySAUEBmq7fN5" +
				"PG7uSGPEADiAKePGHCnmaRw==",
			expected: "H+UcEreBUkCWN18nTYZp0QQkQqGA8IqzqvJ5qB225Z8=",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			licenseData, err := base64.StdEncoding.DecodeString(tt.license)
			if err != nil {
				t.Fatalf("Failed to decode license base64: %v", err)
			}

			derivedKey, err := ParseLicenseAndDeriveKey(licenseData)
			if err != nil {
				t.Fatalf("ParseLicenseAndDeriveKey failed: %v", err)
			}

			derivedKeyBase64 := base64.StdEncoding.EncodeToString(derivedKey)
			if derivedKeyBase64 != tt.expected {
				t.Errorf("Mismatch.\nGot:  %s\nWant: %s", derivedKeyBase64, tt.expected)
			}
		})
	}
}
