package inbox_test

import "seekia/internal/messaging/inbox"

import "seekia/internal/identity"
import "seekia/internal/encoding"

import "bytes"
import "testing"

func TestPublicInboxDerivationFunctions(t *testing.T){

	testIdentityHashString := "wgkxplfvju7yhpoadvxju4kmfdm"

	testIdentityHash, _, err := identity.ReadIdentityHashString(testIdentityHashString)
	if (err != nil){
		t.Fatal("Failed to read testIdentityHashString: " + err.Error())
	}

	expectedPublicInboxString := "7sjss2s73pbqnhhe"

	expectedPublicInbox, err := inbox.ReadInboxString(expectedPublicInboxString)
	if (err != nil){
		t.Fatal("Failed to decode expectedPublicInbox: " + err.Error())
	}

	publicInbox, err := inbox.GetPublicInboxFromIdentityHash(testIdentityHash)
	if (err != nil){
		t.Fatal("Failed to get public inbox from identity hash: " + err.Error())
	}

	if (publicInbox != expectedPublicInbox){
		publicInboxString := encoding.EncodeBytesToBase32String(publicInbox[:])
		t.Fatal("Failed to get public inbox from identity hash: Unexpected result: " + publicInboxString)
	}

	publicInboxSealerKey, err := inbox.GetPublicInboxSealerKeyFromIdentityHash(testIdentityHash)
	if (err != nil){
		t.Fatal("GetPublicInboxSealerKeyFromIdentityHash failed: " + err.Error())
	}

	expectedPublicInboxSealerKey := "7d3fadbc59f692dead5ae794ddd0492cfef2990357f09301a7560966dd955d8b"

	expectedPublicInboxSealerKeyBytes, err := encoding.DecodeHexStringToBytes(expectedPublicInboxSealerKey)
	if (err != nil){
		t.Fatal("expectedPublicInboxSealerKey is not hex: " + err.Error())
	}

	areEqual := bytes.Equal(publicInboxSealerKey[:], expectedPublicInboxSealerKeyBytes)
	if (areEqual == false){
		resultHex := encoding.EncodeBytesToHexString(publicInboxSealerKey[:])
		t.Fatal("GetPublicInboxSealerKeyFromIdentityHash failed: Unexpected result: " + resultHex)
	}
}

func TestSecretInboxDerivationFunction(t *testing.T){

	testSecretInboxSeed := "f574640613cb21cbb92e549e6a0412a152cca1a19ad3"

	testSecretInboxSeedBytes, err := encoding.DecodeHexStringToBytes(testSecretInboxSeed)
	if (err != nil){
		t.Fatal("testSecretInboxSeed is not hex: " + err.Error())
	}
	if (len(testSecretInboxSeedBytes) != 22){
		t.Fatal("testSecretInboxSeed has an invalid length.")
	}

	testSecretInboxSeedArray := [22]byte(testSecretInboxSeedBytes)

	secretInbox, secretInboxSealerKey, err := inbox.GetSecretInboxAndSealerKeyFromSecretInboxSeed(testSecretInboxSeedArray)
	if (err != nil){
		t.Fatal("GetSecretInboxAndSealerKeyFromSecretInboxSeed failed: " + err.Error())
	}

	expectedSecretInboxString := "q7it4ziecjxigop6"

	expectedSecretInbox, err := inbox.ReadInboxString(expectedSecretInboxString)
	if (err != nil){
		t.Fatal("Failed to decode expectedSecretInbox: " + err.Error())
	}

	if (secretInbox != expectedSecretInbox){
		t.Fatal("GetSecretInboxAndSealerKeyFromSecretInboxSeed returning unexpected secretInbox.")
	}

	expectedSecretInboxSealerKey := "070a8c4a9c9d8a46951f02fafab3bbe94e958b3d5db21425ef842141c691d231"
	expectedSecretInboxSealerKeyBytes, err := encoding.DecodeHexStringToBytes(expectedSecretInboxSealerKey)
	if (err != nil){
		t.Fatal("expectedSecretInboxSealerKey is not hex: " + err.Error())
	}
	areEqual := bytes.Equal(secretInboxSealerKey[:], expectedSecretInboxSealerKeyBytes)
	if (areEqual == false){
		t.Fatal("GetSecretInboxAndSealerKeyFromSecretInboxSeed returning unexpected secretInboxSealerKey")
	}
}

