Skip to content
quarry Artifact [ba495afb7d]
Login

Artifact [ba495afb7d]

Artifact ba495afb7d725042ece9826e0cbd00ea733428fe14bb2707ce75bd0f0197dc27:


package api

import (
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/index"
	"craftdesign.group/quarry/internal/oltp"
)

// closeRecorder is a local tier holding one open ticket and remembering
// every change written to it.
type closeRecorder struct {
	countingLocal
	id   string
	sets []closeSet
}

type closeSet struct {
	repo, id, user string
	fields         map[string]string
}

func (c *closeRecorder) LocalTicketByPrefix(_, prefix string) (oltp.LocalTicket, bool, error) {
	if !strings.HasPrefix(c.id, prefix) {
		return oltp.LocalTicket{}, false, nil
	}
	return oltp.LocalTicket{ID: c.id, Fields: map[string]string{"status": "Open"}}, true, nil
}

func (c *closeRecorder) LocalTicketSet(repo, id, user string, fields map[string]string) error {
	c.sets = append(c.sets, closeSet{repo, id, user, fields})
	return nil
}

// A default-branch check-in that says it closes a ticket closes it, as
// the check-in's author, and the ticket names the check-in that did.
func TestACheckinClosesTheTicketItNames(t *testing.T) {
	local := &closeRecorder{id: openTicket}
	d := Deps{Repos: testRepos(wikiIndex(t)), Local: local}
	c := index.TimelineEntry{Hash: "0e6de71dad35aaaabbbb", Branch: "trunk", User: "jva",
		Comment: "fix the pager, closes " + openTicket[:10]}
	d.CloseFromCheckin("test", c)
	if len(local.sets) != 1 {
		t.Fatalf("FAIL: %d ticket changes, want 1", len(local.sets))
	}
	s := local.sets[0]
	if s.repo != "test" || s.id != openTicket || s.user != "jva" {
		t.Errorf("FAIL: change = %s %s by %s", s.repo, s.id, s.user)
	}
	if s.fields["status"] != "Closed" {
		t.Errorf("FAIL: status = %q", s.fields["status"])
	}
	if !strings.Contains(s.fields["icomment"], shortHash(c.Hash)) {
		t.Errorf("FAIL: the note does not name the check-in: %q", s.fields["icomment"])
	}
	t.Log("ok: closed by the check-in, as its author")
}

func TestACheckinThatClosesNothingWritesNothing(t *testing.T) {
	local := &closeRecorder{id: openTicket}
	d := Deps{Repos: testRepos(wikiIndex(t)), Local: local}
	d.CloseFromCheckin("test", index.TimelineEntry{Hash: "0e6de71dad35", Branch: "trunk",
		User: "jva", Comment: "see " + openTicket[:10] + " for the numbers"})
	if len(local.sets) != 0 {
		t.Fatalf("FAIL: prose wrote %d ticket changes", len(local.sets))
	}
}