package api
import (
"errors"
"testing"
"craftdesign.group/quarry/internal/lease"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
// The close tag is written and only the ingest after it fails. The
// tag exists, so the close succeeded; and fossil's tag log is
// append-only, so a caller who asks again must not mint a second one.
func TestACloseWhoseIngestFailedIsWrittenOnceAcrossTwoCalls(t *testing.T) {
dir, _, ix, _ := acceptStore(t)
tip := ztip(t, dir, "feat")
writes := 0
r := ops.NewRegistry()
if err := RegisterAll(r, Deps{Version: "v", Repos: NewRepos(map[string]*Repo{"test": {
Index: ix, Zit: zitcli.New(dir),
Guard: func(fn func(lease.Token) error) error { writes++; return fn(testToken) },
Sync: func() (int, error) { return 0, errors.New("ingest round failed") },
}})}); err != nil {
t.Fatal(err)
}
for call := 1; call <= 2; call++ {
out, err := r.Invoke(as("dan", "i"), "checkin.close", checkinCloseIn{Repo: "test", Hash: tip})
if err != nil {
t.Fatalf("FAIL: close call %d = %v; the tag was written, so it succeeded", call, err)
}
if got := out.(checkinCloseOut); !got.Closed || got.Hash != tip {
t.Fatalf("FAIL: close call %d answered %+v", call, got)
}
}
if writes != 1 {
t.Fatalf("FAIL: %d closed tags written across two calls, want 1", writes)
}
t.Log("ok: two calls, one closed tag")
}