package core

import (
	"bytes"
	"context"
	"testing"

	"github.com/chainmint/core/txdb"
	"github.com/chainmint/database/pg/pgtest"
	"github.com/chainmint/protocol/prottest"
	"github.com/chainmint/testutil"
)

func TestGetBlock(t *testing.T) {
	_, db := pgtest.NewDB(t, pgtest.SchemaPath)
	ctx := context.Background()
	store := txdb.NewStore(db)
	chain := prottest.NewChain(t, prottest.WithStore(store))
	api := &API{chain: chain, store: store}

	block, err := api.getBlockRPC(ctx, 1)
	if err != nil {
		t.Fatal(err)
	}
	if block == nil {
		t.Error("expected 1 (initial) block, got none")
	}

	newBlock := prottest.MakeBlock(t, chain, nil)
	buf := new(bytes.Buffer)
	_, err = newBlock.WriteTo(buf)
	if err != nil {
		t.Fatal(err)
	}

	block, err = api.getBlockRPC(ctx, 2)
	if err != nil {
		testutil.FatalErr(t, err)
	}

	if block == nil {
		t.Error("expected 1 block, got none")
	}
	if !bytes.Equal(block, buf.Bytes()) {
		t.Errorf("got=%x, want=%s", block, buf.Bytes())
	}
}
