package gossip

import (
	"net/netip"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.firedancer.io/radiance/fixtures"
)

func TestMessage(t *testing.T) {
	cases := []struct {
		name    string
		fixture string
		message Message
		err     string
	}{
		{
			name:    "PullRequest",
			fixture: "gossip/pull_request.bin",
			message: &Message__PullRequest{
				Filter: CrdsFilter{
					Filter: Bloom{
						Keys: []uint64{
							0x0e1f75695561c8f4,
							0x7edda0f717d55580,
							0x221a275bb8650ed4,
						},
						Bits: MakeBitVecU64(make([]uint64, 97), 6168),
					},
					Mask:     0x3ffffffffffffff,
					MaskBits: 6,
				},
				Value: CrdsValue{
					Signature: Signature{
						0x50, 0xd2, 0xbd, 0xa4, 0x5b, 0x66, 0xd9, 0xf5, 0xc7, 0x7e, 0xef, 0x3b, 0x25, 0x65, 0xcd, 0xf6,
						0x5a, 0xcb, 0xd6, 0x0f, 0x56, 0xce, 0x77, 0xb7, 0xf4, 0xd8, 0xb0, 0x28, 0x4f, 0x03, 0x12, 0xeb,
						0xf9, 0x78, 0xf8, 0x2f, 0xa6, 0xbe, 0x7e, 0xeb, 0x9f, 0xdd, 0x55, 0x5c, 0x9c, 0x9f, 0x61, 0xc5,
						0xdc, 0x60, 0x57, 0x26, 0xd5, 0xc0, 0xe2, 0x93, 0x4e, 0x16, 0x31, 0xb6, 0x68, 0xa5, 0xde, 0x09,
					},
					Data: &CrdsData__ContactInfo{
						Value: ContactInfo{
							Id: Pubkey{
								0xdd, 0x52, 0xbd, 0x9b, 0x7d, 0xb5, 0xcd, 0x06,
								0x1a, 0xe6, 0xbe, 0x46, 0x98, 0xd9, 0x32, 0x96,
								0xe9, 0x09, 0xa1, 0xb9, 0xc4, 0xec, 0x10, 0xf7,
								0xbc, 0xb1, 0x43, 0x0d, 0xed, 0xf7, 0xb6, 0x30,
							},
							Wallclock: 1660627129489,
						},
					},
				},
			},
		},
		{
			name:    "PullResponse_ContactInfo",
			fixture: "gossip/pull_response_contact_info.bin",
			message: &Message__PullResponse{
				Pubkey: Pubkey{
					0x7a, 0x1f, 0xe3, 0x85, 0x3f, 0x19, 0xa1, 0xcc, 0x52, 0x82, 0x2c, 0x0d, 0x03, 0x2d, 0x19, 0x16,
					0xaf, 0x37, 0x50, 0xbb, 0xd8, 0x25, 0x16, 0x3e, 0x46, 0xe2, 0x87, 0x16, 0xde, 0x8e, 0x35, 0x6f,
				},
				Values: []CrdsValue{
					{
						Signature: Signature{
							0x6b, 0x43, 0x11, 0xd0, 0x90, 0x85, 0x22, 0xe1, 0x28, 0xf9, 0xcf, 0xf9, 0x72, 0xaa, 0xfb, 0xa5,
							0x05, 0x33, 0xdb, 0x3c, 0x85, 0xfa, 0x83, 0x97, 0x22, 0x70, 0xb3, 0xaf, 0x02, 0x7f, 0x1f, 0x3f,
							0x32, 0xb3, 0xb9, 0x7b, 0x42, 0x81, 0xc7, 0x55, 0x61, 0x43, 0x35, 0x48, 0x08, 0x65, 0x26, 0x1a,
							0x7b, 0x66, 0xfa, 0x4a, 0x60, 0xc4, 0xc4, 0xb8, 0x8c, 0x5f, 0xae, 0xcb, 0x40, 0x76, 0x7b, 0x03,
						},
						Data: &CrdsData__ContactInfo{
							Value: ContactInfo{
								Id: Pubkey{
									0x7a, 0x1f, 0xe3, 0x85, 0x3f, 0x19, 0xa1, 0xcc, 0x52, 0x82, 0x2c, 0x0d, 0x03, 0x2d, 0x19, 0x16,
									0xaf, 0x37, 0x50, 0xbb, 0xd8, 0x25, 0x16, 0x3e, 0x46, 0xe2, 0x87, 0x16, 0xde, 0x8e, 0x35, 0x6f,
								},
								Gossip:       SocketAddr{netip.MustParseAddrPort("127.0.0.1:1024")},
								Tvu:          SocketAddr{netip.MustParseAddrPort("127.0.0.1:1025")},
								TvuForwards:  SocketAddr{netip.MustParseAddrPort("127.0.0.1:1026")},
								Repair:       SocketAddr{netip.MustParseAddrPort("127.0.0.1:1031")},
								Tpu:          SocketAddr{netip.MustParseAddrPort("127.0.0.1:1027")},
								TpuForwards:  SocketAddr{netip.MustParseAddrPort("127.0.0.1:1028")},
								TpuVote:      SocketAddr{netip.MustParseAddrPort("127.0.0.1:1029")},
								Rpc:          SocketAddr{netip.MustParseAddrPort("127.0.0.1:8899")},
								RpcPubsub:    SocketAddr{netip.MustParseAddrPort("127.0.0.1:8900")},
								ServeRepair:  SocketAddr{netip.MustParseAddrPort("127.0.0.1:1032")},
								Wallclock:    1660658416429,
								ShredVersion: 25514,
							},
						},
					},
				},
			},
		},
		{
			name:    "PullResponse_SnapshotHashes",
			fixture: "gossip/pull_response_snapshot_hashes.bin",
			message: &Message__PullResponse{
				Pubkey: Pubkey{
					0x7a, 0x1f, 0xe3, 0x85, 0x3f, 0x19, 0xa1, 0xcc, 0x52, 0x82, 0x2c, 0x0d, 0x03, 0x2d, 0x19, 0x16,
					0xaf, 0x37, 0x50, 0xbb, 0xd8, 0x25, 0x16, 0x3e, 0x46, 0xe2, 0x87, 0x16, 0xde, 0x8e, 0x35, 0x6f,
				},
				Values: []CrdsValue{
					{
						Signature: Signature{
							0xbe, 0x85, 0x8b, 0xf3, 0xb2, 0x65, 0x47, 0x7d, 0xac, 0x2b, 0x86, 0xfc, 0x80, 0x8b, 0x9a, 0x78,
							0x99, 0xbc, 0xac, 0x25, 0xbd, 0xa2, 0xd0, 0x66, 0x6f, 0x3b, 0x80, 0x48, 0xea, 0x1f, 0x1f, 0xc2,
							0xfe, 0x11, 0xe1, 0xe3, 0x4d, 0x27, 0xd4, 0xfc, 0xd8, 0xd0, 0x89, 0xcf, 0x04, 0xef, 0x78, 0x45,
							0xb5, 0x12, 0x70, 0x57, 0x1d, 0xff, 0x82, 0xc0, 0x23, 0x9a, 0x6d, 0xf6, 0x75, 0xcd, 0x9a, 0x06,
						},
						Data: &CrdsData__SnapshotHashes{
							Value: SnapshotHashes{
								From: Pubkey{
									0x7a, 0x1f, 0xe3, 0x85, 0x3f, 0x19, 0xa1, 0xcc, 0x52, 0x82, 0x2c, 0x0d, 0x03, 0x2d, 0x19, 0x16,
									0xaf, 0x37, 0x50, 0xbb, 0xd8, 0x25, 0x16, 0x3e, 0x46, 0xe2, 0x87, 0x16, 0xde, 0x8e, 0x35, 0x6f,
								},
								Hashes: []SlotHash{
									{
										Slot: 47411,
										Hash: Hash{
											0xa6, 0xb1, 0x85, 0x23, 0xe7, 0xaa, 0xca, 0x36, 0xe4, 0xda, 0x16, 0xc8, 0x8f, 0x5b, 0xa9, 0xad,
											0xd8, 0x77, 0xf7, 0x62, 0x0b, 0x8f, 0xf2, 0xcc, 0xe4, 0x35, 0x7c, 0x8e, 0xb8, 0xed, 0x3c, 0x8a,
										},
									},
								},
								Wallclock: 1660658416429,
							},
						},
					},
				},
			},
		},
		{
			name:    "PullResponse_Version",
			fixture: "gossip/pull_response_version.bin",
			message: &Message__PullResponse{
				Pubkey: Pubkey{
					0x7a, 0x1f, 0xe3, 0x85, 0x3f, 0x19, 0xa1, 0xcc, 0x52, 0x82, 0x2c, 0x0d, 0x03, 0x2d, 0x19, 0x16,
					0xaf, 0x37, 0x50, 0xbb, 0xd8, 0x25, 0x16, 0x3e, 0x46, 0xe2, 0x87, 0x16, 0xde, 0x8e, 0x35, 0x6f,
				},
				Values: []CrdsValue{
					{
						Signature: Signature{
							0x96, 0x9f, 0x40, 0x41, 0xdd, 0x80, 0x5e, 0x6f, 0x89, 0x58, 0x21, 0xf7, 0x28, 0xe2, 0x95, 0xb2,
							0x91, 0xfc, 0x85, 0xaa, 0xc2, 0x2e, 0x88, 0x51, 0xea, 0x95, 0x02, 0xab, 0x38, 0x0b, 0x5d, 0x9f,
							0xe7, 0x9e, 0xb1, 0x54, 0x36, 0x78, 0x5c, 0x49, 0xd6, 0x74, 0x1b, 0xf0, 0xb0, 0x59, 0x5f, 0x77,
							0xe0, 0xb7, 0x1b, 0x39, 0xb0, 0x39, 0x68, 0x76, 0x5b, 0x71, 0x81, 0x7f, 0x07, 0x79, 0x15, 0x0f,
						},
						Data: &CrdsData__Version{
							From: Pubkey{
								0x7a, 0x1f, 0xe3, 0x85, 0x3f, 0x19, 0xa1, 0xcc, 0x52, 0x82, 0x2c, 0x0d, 0x03, 0x2d, 0x19, 0x16,
								0xaf, 0x37, 0x50, 0xbb, 0xd8, 0x25, 0x16, 0x3e, 0x46, 0xe2, 0x87, 0x16, 0xde, 0x8e, 0x35, 0x6f,
							},
							Wallclock:  1660658416907,
							Major:      1,
							Minor:      12,
							Patch:      0,
							Commit:     nil,
							FeatureSet: 0x1800dbd1,
						},
					},
				},
			},
		},
		{
			name:    "PullResponse_NodeInstance",
			fixture: "gossip/pull_response_node_instance.bin",
			message: &Message__PullResponse{
				Pubkey: Pubkey{
					0x7a, 0x1f, 0xe3, 0x85, 0x3f, 0x19, 0xa1, 0xcc, 0x52, 0x82, 0x2c, 0x0d, 0x03, 0x2d, 0x19, 0x16,
					0xaf, 0x37, 0x50, 0xbb, 0xd8, 0x25, 0x16, 0x3e, 0x46, 0xe2, 0x87, 0x16, 0xde, 0x8e, 0x35, 0x6f,
				},
				Values: []CrdsValue{
					{
						Signature: Signature{
							0x61, 0x74, 0xf0, 0x84, 0x41, 0xcb, 0xd8, 0x9f, 0xcd, 0xb6, 0xbf, 0xee, 0x92, 0x47, 0x17, 0xec,
							0x28, 0xd8, 0xeb, 0xa3, 0xe4, 0x17, 0x4d, 0x75, 0x45, 0x9f, 0x34, 0x02, 0xa4, 0x91, 0x1f, 0xc0,
							0x15, 0x34, 0xb6, 0x0c, 0xdb, 0x84, 0x4d, 0xe3, 0xaa, 0xf7, 0xcb, 0x3d, 0xf0, 0x4e, 0x71, 0xaa,
							0x24, 0xc2, 0x16, 0xe5, 0x8a, 0x17, 0x37, 0x90, 0xe9, 0x50, 0xbc, 0xd2, 0x4c, 0x5a, 0xc7, 0x02,
						},
						Data: &CrdsData__NodeInstance{
							From: Pubkey{
								0x7a, 0x1f, 0xe3, 0x85, 0x3f, 0x19, 0xa1, 0xcc, 0x52, 0x82, 0x2c, 0x0d, 0x03, 0x2d, 0x19, 0x16,
								0xaf, 0x37, 0x50, 0xbb, 0xd8, 0x25, 0x16, 0x3e, 0x46, 0xe2, 0x87, 0x16, 0xde, 0x8e, 0x35, 0x6f,
							},
							Wallclock: 1660658416907,
							Timestamp: 1660658416429,
							Token:     0x5d229535ca896c95,
						},
					},
				},
			},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			frame := fixtures.Load(t, tc.fixture)
			msg, err := BincodeDeserializeMessage(frame)
			if tc.err == "" {
				require.NoError(t, err)
				assert.Equal(t, tc.message, msg)
			} else {
				assert.Nil(t, tc.message)
				assert.EqualError(t, err, tc.err)
			}
		})
	}
}
