Skip to content
quarry Artifact [f95680c5ff]
Login

Artifact [f95680c5ff]

Artifact f95680c5ff90a8f67b08bf29077abe0e1805d194c2dba0e49e819ad26b2da73f:


package events

import (
	"encoding/json"
	"testing"
	"time"

	"github.com/nats-io/nats.go"

	"craftdesign.group/quarry/internal/evt"
)

func testServer(t *testing.T) *Server {
	t.Helper()
	s, err := Start(t.TempDir())
	if err != nil {
		t.Fatalf("start: %v", err)
	}
	t.Cleanup(s.Close)
	return s
}

// A subscriber selects with a NATS wildcard and never opens the body —
// that is the whole point of putting the noun and action in the subject
// (RFC 0005 §2.3.1), and it is what lets a dagnats subject trigger work
// with no code on quarry's side.
func TestWildcardSelectsByNounAndAction(t *testing.T) {
	s := testServer(t)
	for _, tc := range []struct {
		filter string
		want   []evt.Kind
	}{
		{"quarry.evt.*.landing.opened", []evt.Kind{evt.LandingOpened}},
		{"quarry.evt.alpha.>", []evt.Kind{evt.LandingOpened, evt.TicketClosed, evt.CheckinCreated}},
		{"quarry.evt.*.ticket.*", []evt.Kind{evt.TicketClosed}},
	} {
		sub, err := s.js.SubscribeSync(tc.filter, nats.DeliverNew())
		if err != nil {
			t.Fatalf("subscribe %s: %v", tc.filter, err)
		}
		for _, k := range []evt.Kind{evt.LandingOpened, evt.TicketClosed, evt.CheckinCreated} {
			if err := s.Raise(k, "alpha", map[string]any{"actor": "dan"}); err != nil {
				t.Fatalf("raise %s: %v", k, err)
			}
		}
		var got []evt.Kind
		for range tc.want {
			m, err := sub.NextMsg(3 * time.Second)
			if err != nil {
				t.Fatalf("%s: wanted %v, got %v then %v", tc.filter, tc.want, got, err)
			}
			var e struct{ Kind evt.Kind }
			if err := json.Unmarshal(m.Data, &e); err != nil {
				t.Fatalf("body: %v", err)
			}
			got = append(got, e.Kind)
		}
		for i := range tc.want {
			if got[i] != tc.want[i] {
				t.Errorf("%s: got %v, want %v", tc.filter, got, tc.want)
				break
			}
		}
		if m, err := sub.NextMsg(200 * time.Millisecond); err == nil {
			t.Errorf("%s: also matched an event it should not have: %s", tc.filter, m.Subject)
		}
		sub.Unsubscribe()
	}
}

// The reason the vocabulary moved off `zit.evt.` — the ingest pipeline's
// fold consumer subscribes to that root with a trailing wildcard and
// parses whatever arrives as an artifact. A vocabulary event reaching it
// would not be a stray message; it would be fed to a parser.
func TestVocabularyEventsNeverReachTheIngestFilter(t *testing.T) {
	s := testServer(t)
	sub, err := s.js.SubscribeSync("zit.evt.alpha.>", nats.DeliverNew())
	if err != nil {
		t.Fatalf("subscribe: %v", err)
	}
	defer sub.Unsubscribe()
	for _, k := range evt.All {
		if err := s.Raise(k, "alpha", nil); err != nil {
			t.Fatalf("raise %s: %v", k, err)
		}
	}
	if m, err := sub.NextMsg(500 * time.Millisecond); err == nil {
		t.Fatalf("the folder's filter received %s; the vocabulary is back on zit.evt.", m.Subject)
	}
}

func TestRaiseRefusesWhatItCannotName(t *testing.T) {
	s := testServer(t)
	if err := s.Raise(evt.Kind("ticket.exploded"), "alpha", nil); err == nil {
		t.Error("published a kind outside the vocabulary")
	}
	if err := s.Raise(evt.TicketOpened, "", nil); err == nil {
		t.Error("published without a repo; the subject would be malformed")
	}
}