package main

import (
	"context"
	"sync"
	"testing"
)

func TestModuleContract(t *testing.T) {
	s := &Stock{available: 3, reservations: map[string]int{}}
	o := OrderService{s}
	var wg sync.WaitGroup
	for range 10 {
		wg.Add(1)
		go func() {
			defer wg.Done()
			if e := o.Place(context.Background(), "o1", 2); e != nil {
				t.Error(e)
			}
		}()
	}
	wg.Wait()
	if s.available != 1 || len(s.reservations) != 1 {
		t.Fatal("non-idempotent")
	}
	if o.Place(context.Background(), "o1", 1) == nil {
		t.Fatal("payload conflict")
	}
	if o.Place(context.Background(), "o2", 2) == nil {
		t.Fatal("oversold")
	}
}
