package protocol

import (
	"net/http"
	"testing"
)

func TestCompat(t *testing.T) {
	cases := []struct {
		bg, bgMin, bk, bkMin int
		want                 string
	}{
		{1, 1, 1, 1, CompatOK},
		{2, 1, 1, 1, CompatOK},
		{1, 1, 2, 1, CompatOK},
		{3, 2, 1, 1, CompatBackupTooOld},
		{1, 1, 3, 2, CompatBlueGitTooOld},
	}
	for _, c := range cases {
		if got := Compat(c.bg, c.bgMin, c.bk, c.bkMin); got != c.want {
			t.Errorf("Compat(%d,%d,%d,%d) = %s, wil %s", c.bg, c.bgMin, c.bk, c.bkMin, got, c.want)
		}
	}
}

func TestBlueGitFromHeader(t *testing.T) {
	if p, ok := BlueGitFromHeader(http.Header{}); ok || p.Protocol != 1 || p.MinProtocol != 1 {
		t.Fatalf("zonder koppen: %+v", p)
	}
	h := http.Header{}
	h.Set(HeaderBlueGitProtocol, "4")
	h.Set(HeaderBlueGitMinProtocol, "3")
	h.Set(HeaderBlueGitVersion, "abc<def")
	p, ok := BlueGitFromHeader(h)
	if !ok || p.Protocol != 4 || p.MinProtocol != 3 || p.Version != "abcdef" {
		t.Fatalf("%+v", p)
	}
	if p.Compat() != CompatBackupTooOld {
		t.Fatalf("BlueGit 4 vraagt 3, wij spreken 1: %s", p.Compat())
	}
}
