package cookie

import "testing"

const (
	testCookieKey = "def000008bf3d70e7012b7493c382d561e193218d0c74ab162fb0ea8029ce20e926531b4bcf0aaec9381152e6c161f198e06918b2d1aad67cc7cf40819a51ee328c63830"
	testCookie    = "def5020099dce5cd9ecf197adb5532a74e3db2ed9cba3d59b98f365353099b710bd562efa48b6bad1ad0a12b2ee54de0fbfcc6baa0545a8234141b03bfc1fbbbb9061af5011764b9c4dfd9c0ddcad767a453e0cc24d6b4a7c524e6c49aabd66ecc390e1a964b6e81a051b171051c829542facbb36cf64fcfebf069906dcc95476578be3fe59aaae466cf70bd9c877d301d908ec3aa4f55366567f460dfefac1684ce381293e8d4138382a42716d6aaecdcc7"
)

func TestNativeCodecDecodeFixture(t *testing.T) {
	codec, err := NewCodec(Config{
		CookieName: "PrestaShop-test",
		CookieKey:  testCookieKey,
	})
	if err != nil {
		t.Fatalf("NewCodec() error = %v", err)
	}

	session, err := codec.Decode(testCookie)
	if err != nil {
		t.Fatalf("Decode() error = %v", err)
	}

	if session.Values["id_lang"] != "1" {
		t.Fatalf("id_lang = %q, want 1", session.Values["id_lang"])
	}
	if session.Values["id_currency"] != "1" {
		t.Fatalf("id_currency = %q, want 1", session.Values["id_currency"])
	}
	if session.Values["checksum"] != "2076001436" {
		t.Fatalf("checksum = %q, want 2076001436", session.Values["checksum"])
	}
	if session.Values["detect_language"] != "1" {
		t.Fatalf("detect_language = %q, want 1", session.Values["detect_language"])
	}
	if session.GuestID != nil {
		t.Fatalf("guest_id = %v, want nil", session.GuestID)
	}
}

func TestNativeCodecRoundTrip(t *testing.T) {
	codec, err := NewCodec(Config{
		CookieName: "PrestaShop-test",
		CookieKey:  testCookieKey,
	})
	if err != nil {
		t.Fatalf("NewCodec() error = %v", err)
	}

	decoded, err := codec.Decode(testCookie)
	if err != nil {
		t.Fatalf("Decode() error = %v", err)
	}

	encoded, err := codec.Encode(decoded)
	if err != nil {
		t.Fatalf("Encode() error = %v", err)
	}

	redecoded, err := codec.Decode(encoded)
	if err != nil {
		t.Fatalf("Decode(encoded) error = %v", err)
	}

	if redecoded.Plaintext != decoded.Plaintext {
		t.Fatalf("plaintext mismatch after roundtrip\n got: %s\nwant: %s", redecoded.Plaintext, decoded.Plaintext)
	}
}
