package main
import (
"context"
"path/filepath"
"testing"
"time"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/index"
)
type sweepNotify chan api.NotifyEvent
func (n sweepNotify) Trigger(_ context.Context, e api.NotifyEvent) error { n <- e; return nil }
type sweepOwner string
func (o sweepOwner) OwnerOf(string) (string, error) { return string(o), nil }
func (o sweepOwner) SetOwner(string, string) error { return nil }
func (o sweepOwner) NamespaceOf(string) (string, error) { return "", nil }
func (o sweepOwner) SetNamespace(string, string, string, time.Time) error { return nil }
// A LANDING THAT ARRIVES BY PUSH REACHES THE PERSON IT WAITS ON.
//
// Every landing zit opens arrives by push, so none of them passed
// through landing.open, the only place that used to tell anybody. The
// fold's sweep tells the repository's owner once, and the first sweep
// after a start tells nobody about what was already open.
func TestAPushedLandingNotifiesItsAccepter(t *testing.T) {
ix, err := index.Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
defer ix.Close()
const first, firstTip = "aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111", "c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1"
const second, secondTip = "bbbb2222bbbb2222bbbb2222bbbb2222bbbb2222", "c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2"
foldLanding(t, ix, first, "feat/x", firstTip)
told := make(sweepNotify, 8)
rg := newTestRegistrar(t, 25)
rg.deps = &api.Deps{Repos: api.NewRepos(map[string]*api.Repo{"r": {Index: ix}}),
Notify: told, Owners: sweepOwner("dan")}
next := func() (api.NotifyEvent, bool) {
select {
case e := <-told:
return e, true
case <-time.After(500 * time.Millisecond):
return api.NotifyEvent{}, false
}
}
rg.landingSweep("r", ix)
if e, ok := next(); ok {
t.Fatalf("FAIL: the seeding sweep notified about a landing already open: %+v", e)
}
foldLanding(t, ix, second, "feat/y", secondTip)
rg.landingSweep("r", ix)
e, ok := next()
if !ok || e.To != "dan" || e.Workflow != "quarry-landing" {
t.Fatalf("FAIL: a pushed landing told %+v (ok=%v), want dan on quarry-landing", e, ok)
}
rg.landingSweep("r", ix)
if e, ok := next(); ok {
t.Fatalf("FAIL: a landing already told about was told again: %+v", e)
}
t.Log("ok: the repository owner hears once about a pushed landing, and never at seeding")
}