package main

import (
	"reflect"
	"testing"
)

func TestAckNeedsEnoughCaughtUpReplicas(t *testing.T) {
	for _, x := range []struct {
		name     string
		replicas []int
		want     bool
	}{
		{"enough", []int{101, 101}, true},
		{"behind", []int{101, 100}, false},
		{"too-few", []int{101}, false},
	} {
		t.Run(x.name, func(t *testing.T) {
			if got := canAck(2, 101, x.replicas...); got != x.want {
				t.Fatalf("got %v", got)
			}
		})
	}
}

func TestOpenTransactionBlocksLaterCommittedRecord(t *testing.T) {
	log := []Record{{0, "committed"}, {1, "aborted"}, {2, "open"}, {3, "committed"}}
	if got := readCommitted(log); !reflect.DeepEqual(got, []int{0}) {
		t.Fatal(got)
	}
	log[2].State = "committed"
	if got := readCommitted(log); !reflect.DeepEqual(got, []int{0, 2, 3}) {
		t.Fatal(got)
	}
}
