package api
import (
"context"
"strings"
"testing"
"craftdesign.group/quarry/internal/ops"
)
// The one question the forge cannot answer for somebody is whether a
// push reaches their screen. me.push.test asks it the only way there
// is: by sending one.
func TestPushTestSendsThroughTheOrdinaryPath(t *testing.T) {
np := &fakeNotify{}
r := ops.NewRegistry()
if err := RegisterAll(r, Deps{Version: "t", Notify: np, Origin: "https://forge.example"}); err != nil {
t.Fatal(err)
}
out, err := r.Invoke(ops.WithUser(context.Background(), "dan"), "me.push.test", mePushTestIn{})
if err != nil {
t.Fatalf("FAIL: %v", err)
}
if !out.(mePushTestOut).Sent {
t.Error("FAIL: the op did not report the test as sent")
}
sent := np.snapshot()
if len(sent) != 1 {
t.Fatalf("FAIL: %d triggers, want 1", len(sent))
}
if sent[0].Workflow != "quarry-test" {
t.Errorf("FAIL: the test rode on %q, want its own workflow", sent[0].Workflow)
}
if sent[0].To != "dan" {
t.Errorf("FAIL: the test went to %q, not the person who asked", sent[0].To)
}
// A test that is idempotent answers the second ask with silence,
// which is indistinguishable from the failure it is meant to find.
if _, err := r.Invoke(ops.WithUser(context.Background(), "dan"), "me.push.test", mePushTestIn{}); err != nil {
t.Fatal(err)
}
again := np.snapshot()
if len(again) != 2 || again[0].TxnID == again[1].TxnID {
t.Error("FAIL: asking twice did not send twice")
}
// The push carries somewhere to land, like every other one.
if link, _ := again[0].Payload["link"].(string); !strings.HasPrefix(link, "https://forge.example") {
t.Errorf("FAIL: the test push leads to %q", link)
}
t.Log("ok: a test push is its own workflow, never deduped, and leads somewhere")
}
// Nobody signed in is nobody to push to.
func TestPushTestNeedsAnAccount(t *testing.T) {
r := ops.NewRegistry()
if err := RegisterAll(r, Deps{Version: "t", Notify: &fakeNotify{}}); err != nil {
t.Fatal(err)
}
if _, err := r.Invoke(context.Background(), "me.push.test", mePushTestIn{}); err == nil {
t.Error("FAIL: an anonymous request was allowed to send a push")
}
t.Log("ok: a test push needs an account")
}