package api
import (
"context"
"os"
"os/exec"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
// A zit that fails with exit 1 for a reason other than a missing
// artifact — out of memory, a store it cannot open — is a fault that
// may clear, and must not be remembered as the blob being absent.
// Only zit's own no-match answer is.
func TestOnlyAMissingArtifactIsRememberedAbsent(t *testing.T) {
ix := attachedIndex(t)
r := ops.NewRegistry()
if err := RegisterAll(r, Deps{Version: "v", Repos: NewRepos(map[string]*Repo{
"test": {Index: ix, Zit: zitcli.New(filepath.Join(t.TempDir(), "store"))}})}); err != nil {
t.Fatal(err)
}
fake := func(script string) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "zit"), []byte("#!/bin/sh\n"+script+"\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
if _, err := exec.LookPath("zit"); err != nil {
t.Fatal(err)
}
}
head := func() {
_, _ = r.Invoke(context.Background(), "attachment.head", attachmentHeadIn{Repo: "test", Hash: "blobw"})
}
fake(`echo "zit artifact: out of memory" >&2; exit 1`)
head()
if absent, _ := ix.AttachmentAbsent("blobw"); absent {
t.Fatal("FAIL: an out-of-memory exit was remembered as the blob being absent")
}
fake(`echo "zit artifact: no artifact matches 'blobw'" >&2; exit 1`)
head()
if absent, _ := ix.AttachmentAbsent("blobw"); !absent {
t.Fatal("FAIL: zit's no-match answer was not remembered")
}
t.Log("ok: only a missing artifact is remembered absent")
}