package api
import (
"context"
"os"
"os/exec"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
// A ticket type's template is a Markdown file under
// .quarry/ticket-templates/, read at the tip of trunk and handed out by
// ticket.config when asked, so the form and an agent start from the
// same outline. A type with no file has none; default.md serves a
// ticket with no type; the vocabulary alone never pays for the reads.
func TestATicketTypeBringsItsTemplate(t *testing.T) {
if _, err := exec.LookPath("zit"); err != nil {
t.Skip("zit not on PATH")
}
dir := t.TempDir()
zrun(t, dir, "init", ".")
if err := os.MkdirAll(filepath.Join(dir, ".quarry", "ticket-templates"), 0o755); err != nil {
t.Fatal(err)
}
zwrite(t, dir, ".quarry/ticket-templates/Code_Defect.md", "### What happened\n")
zwrite(t, dir, ".quarry/ticket-templates/default.md", "### Describe it\n")
zrun(t, dir, "add", ".quarry/ticket-templates/Code_Defect.md", ".quarry/ticket-templates/default.md")
zrun(t, dir, "commit", "-m", "templates", "--user", "u")
r := ops.NewRegistry()
if err := RegisterAll(r, Deps{Version: "v", Repos: NewRepos(map[string]*Repo{"alpha": {Zit: zitcli.New(dir)}})}); err != nil {
t.Fatal(err)
}
ctx := context.Background()
out, err := r.Invoke(ctx, "ticket.config", ticketConfigIn{Repo: "alpha", Templates: true})
if err != nil {
t.Fatal(err)
}
got := map[string]string{}
for _, tp := range out.(ticketConfigOut).Templates {
got[tp.Type] = tp.Body
}
if got["Code_Defect"] != "### What happened\n" || got[""] != "### Describe it\n" || len(got) != 2 {
t.Fatalf("FAIL: templates %q, want Code_Defect and the default only", got)
}
plain, _ := r.Invoke(ctx, "ticket.config", ticketConfigIn{Repo: "alpha"})
if n := len(plain.(ticketConfigOut).Templates); n != 0 {
t.Fatalf("FAIL: the vocabulary alone carried %d templates", n)
}
t.Log("ok: Code_Defect and default come back when asked; types without a file have none; not asked, none")
}