package main

import (
	"sync"
	"testing"
)

func TestAtomicOutbox(t *testing.T) {
	s := Store{orders: map[string]int{}}
	if s.Create("x", 0) == nil || len(s.outbox) != 0 {
		t.Fatal("failed transaction published")
	}
	if s.Create("x", 4) != nil || s.Create("x", 4) != nil || len(s.outbox) != 1 {
		t.Fatal("duplicate create")
	}
	if s.Create("x", 5) == nil {
		t.Fatal("conflicting payload")
	}
}
func TestConcurrentReplay(t *testing.T) {
	s := Sink{seen: map[string]int{}}
	var wg sync.WaitGroup
	for range 100 {
		wg.Add(1)
		go func() {
			defer wg.Done()
			_, e := s.Apply(Event{"x", 7})
			if e != nil {
				t.Error(e)
			}
		}()
	}
	wg.Wait()
	if s.Total != 7 {
		t.Fatal(s.Total)
	}
}
func TestBrokenDedupLosesEffect(t *testing.T) {
	s := Sink{seen: map[string]int{"x": 7}}
	ok, e := s.Apply(Event{"x", 7})
	if e != nil || ok || s.Total != 0 {
		t.Fatal("negative fixture changed")
	}
}
