package ledger

import "testing"

func TestDeltas(t *testing.T) {
	t.Run("Groups", func(t *testing.T) {
		deltas := Deltas{
			{Name: "a1"},
			{Name: "b1"},
			{Name: "b2"},
			{Name: "b3"},
		}

		deltas = deltas.Group(GroupName("^."))
		if len(deltas) != 4 {
			t.Error(len(deltas))
		}
		if deltas[0].Name != "a" {
			t.Error(deltas[0].Name)
		}
		if deltas[1].Name != "b" {
			t.Error(deltas[1].Name)
		}
		if deltas[2].Name != "b" {
			t.Error(deltas[2].Name)
		}
		if deltas[3].Name != "b" {
			t.Error(deltas[3].Name)
		}
	})

	t.Run("balances", func(t *testing.T) {
		deltas := Deltas{
			{Name: "a", Value: 0},
			{Name: "b", Value: 1},
			{Name: "b", Value: -.999999999999999999999999},
			{Name: "b", Value: 1.3},
		}

		balances := deltas.Balances()
		if len(balances) != 2 {
			t.Error(len(balances), balances)
		}
		if balances["a"][""] != 0 {
			t.Error(balances["a"])
		}
		if balances["b"][""] != 1.3 {
			t.Error(balances["b"])
		}
	})

	t.Run("register", func(t *testing.T) {
		deltas := Deltas{
			{Date: "a", Value: 0.1},
			{Date: "a", Value: 2.2},
			{Date: "b", Value: 4.3},
		}
		got := deltas.Register()
		t.Logf("%+v", got)
		if len(got) != 2 {
			t.Error(len(got))
		}
		if int(10*got["a"][""][""]) != 23 {
			t.Error(got["a"][""][""])
		}
		if int(10*got["b"][""][""]) != 66 {
			t.Error(got["b"][""][""])
		}
	})
}
