package api
import (
"sync"
"testing"
"time"
)
// A second Land on a repository waits for the first, so it reads the
// stack as the first left it rather than half landed (2026-10-30: the
// second press armed a rung the first was merging and said "0 rungs
// landed"). Another repository's land does not wait.
func TestLandsOnARepositoryRunOneAtATime(t *testing.T) {
unlock := landLock("quarry")
done := make(chan string, 2)
var wg sync.WaitGroup
wg.Add(2)
go func() { defer wg.Done(); u := landLock("quarry"); done <- "second"; u() }()
go func() { defer wg.Done(); u := landLock("other"); done <- "other"; u() }()
if got := <-done; got != "other" {
t.Fatalf("FAIL: %s ran while the first land held quarry", got)
}
select {
case got := <-done:
t.Fatalf("FAIL: %s ran while the first land held quarry", got)
case <-time.After(50 * time.Millisecond):
}
unlock()
wg.Wait()
if got := <-done; got != "second" {
t.Fatalf("FAIL: got %s", got)
}
if LandingNow("quarry", "x") {
t.Error("FAIL: a landing reads as landing with no land running")
}
}