Skip to content
quarry Artifact [89593ad950]
Login

Artifact [89593ad950]

Artifact 89593ad950ff58b17aa9122c2a6297014a77db3ef9be833039158b71a4fcfd35:


package api

import (
	"errors"
	"reflect"
	"strings"
	"testing"

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

func TestNameClashTable(t *testing.T) {
	existing := []string{"dmestas", "Jva", "e-desktop", "main_bot", "rn-ci"}
	for _, c := range []struct {
		name       string
		same, like []string
	}{
		{"dmestas", []string{"dmestas"}, nil},
		{"DMESTAS", []string{"dmestas"}, nil},
		{"e_desktop", []string{"e-desktop"}, nil},
		{"e.desk-top", []string{"e-desktop"}, nil},
		{"d-mestas", []string{"dmestas"}, nil},
		{"dmestas0", nil, nil},
		{"jva", []string{"Jva"}, nil},
		{"jvа", nil, nil}, // a Cyrillic letter is not in the closed alphabet; no skeleton match
		{"j1a", nil, nil},
		{"e-deskt0p", nil, []string{"e-desktop"}},
		{"E_DESKT0P", nil, []string{"e-desktop"}},
		{"3-desktop", nil, nil},
		{"rnain-bot", nil, []string{"main_bot"}},
		{"rnain_bot", nil, []string{"main_bot"}},
		{"main-b0t", nil, []string{"main_bot"}},
		{"m-ci", nil, []string{"rn-ci"}},
		{"rn-ci", []string{"rn-ci"}, nil},
		{"jva-x", nil, nil},
		{"dmestas", []string{"dmestas"}, nil},
		{"drnestas", nil, []string{"dmestas"}},
		{"dmest1s", nil, nil},
		{"jva", []string{"Jva"}, nil},
		{"jVa", []string{"Jva"}, nil},
	} {
		same, like := nameClash(c.name, existing)
		if !reflect.DeepEqual(same, c.same) || !reflect.DeepEqual(like, c.like) {
			t.Errorf("FAIL: nameClash(%q) = %v, %v; want %v, %v", c.name, same, like, c.same, c.like)
		}
	}
	// The name itself is never listed, and each name once.
	if same, like := nameClash("e-deskt0p", []string{"e-deskt0p", "e-desktop", "e-desktop", "E-Desktop"}); len(same) != 1 || !reflect.DeepEqual(like, []string{"e-desktop", "E-Desktop"}) {
		t.Errorf("FAIL: self or repeats listed: %v, %v", same, like)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestAnExactCollisionIsRefusedAndALookAlikeIsFlagged(t *testing.T) {
	g := newStartRig(t)
	_, pickup := newPickupKey(t)
	// dmestas-bot is an agent of dmestas.
	for _, name := range []string{"Dmestas_Bot", "dmestas.bot", "DMESTAS-BOT", "dmestasbot"} {
		if err := g.deps.setupNameProblem(name); !errors.Is(err, ops.ErrInvalid) {
			t.Errorf("FAIL: %s was not refused: %v", name, err)
		}
	}
	// The API refuses it at ask time.
	status, _ := g.post(tokenCtx("dmestas-bot", "suit"), `{"name":"Dmestas_Bot","kind":"read","repos":"suit"}`, true, "")
	if status < 400 || status >= 500 {
		t.Errorf("FAIL: asking for a colliding name answered %d", status)
	}
	// A look-alike is allowed, and its approval carries the flag as data.
	id := g.ask("dmestas-bot", `{"name":"dmestas-b0t","kind":"read","repos":"suit"}`, "")
	got := g.get("dmestas", id)
	if got.Setup == nil || !reflect.DeepEqual(got.Setup.LookAlike, []string{"dmestas-bot"}) {
		t.Errorf("FAIL: the request's look-alike flag: %+v", got.Setup)
	}
	plain := g.ask("dmestas-bot", `{"name":"e-one","kind":"read","repos":"suit"}`, "")
	if got := g.get("dmestas", plain); got.Setup == nil || len(got.Setup.LookAlike) != 0 {
		t.Errorf("FAIL: a plain name was flagged: %+v", got.Setup)
	}
	// The claim's look says it too; a collision reads as a taken name.
	for name, want := range map[string]string{"dmestas-b0t": "dmestas-bot"} {
		code := g.begin(name, "read", pickup)
		l, err := g.look("dmestas", code)
		if err != nil || l.NameTaken != "" || strings.Join(l.LookAlike, ",") != want {
			t.Errorf("FAIL: look of %s: %v %+v", name, err, l)
		}
	}
	taken := g.begin("zeit-ci", "read", pickup)
	if l, _ := g.look("dmestas", taken); l.NameTaken != "" {
		t.Errorf("FAIL: a free name read as taken")
	}
	coll := g.begin("Dmestas.Bot", "read", pickup)
	if l, _ := g.look("dmestas", coll); l.NameTaken == "" || len(l.LookAlike) != 0 {
		t.Errorf("FAIL: a colliding name was not read as taken")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}