Skip to content
quarry Artifact [68a717ba7a]
Login

Artifact [68a717ba7a]

Artifact 68a717ba7ae55c04a79177110f09b43167f1fb799e32e549834ba8755adf196b:


package api

import (
	"context"
	"testing"

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

// The fleet listing and /r/{repo}/info answer the same question about
// the same flag, so they must not disagree.
//
// repoInfo is shared by both, and only repo.get ever assigned
// Mirrorable. repo.list built its rows without it, so the field
// serialized Go's zero value: all 24 repositories on the forge reported
// mirroring off, including every one that was syncing fine, while
// repo.get reported the truth for each. The listing is the surface a
// person reads first, so the forge looked like sync was off everywhere
// and a real "this repository cannot be cloned" was indistinguishable
// from the noise.
//
// Asserted through both ops rather than over a shared helper: the defect
// was an assignment that did not exist, which a guard stated over the
// thing both paths compute cannot see. This checks what each surface
// actually serves.
func TestListAndInfoAgreeOnMirrorable(t *testing.T) {
	ix := wikiIndex(t)
	private := map[string]bool{"vault": true}
	stored := map[string]RepoMeta{
		"open":   {Mirrorable: true, Chat: true},
		"closed": {Mirrorable: false, Chat: true},
		"vault":  {Mirrorable: true, Chat: true},
	}
	d := Deps{Version: "v", Repos: NewRepos(map[string]*Repo{
		"open": {Index: ix}, "closed": {Index: ix}, "vault": {Index: ix},
	}),
		Private:  func() map[string]bool { return private },
		RepoMeta: func() map[string]RepoMeta { return stored },
		// The effective answer, as the forge wires it: MayMirror is the
		// stored flag AND not private.
		Mirrorable: func(repo string) bool {
			return stored[repo].Mirrorable && !private[repo]
		},
	}
	r := ops.NewRegistry()
	if err := RegisterAll(r, d); err != nil {
		t.Fatal(err)
	}

	got, err := r.Invoke(context.Background(), "repo.list", repoListIn{})
	if err != nil {
		t.Fatal(err)
	}
	listed := map[string]bool{}
	for _, row := range got.(repoListOut).Repos {
		listed[row.Name] = row.Mirrorable
	}

	// A public repository with the flag on is the case that was always
	// reported wrong, so it is asserted by value and not only by
	// agreement — two surfaces that both say false agree and are both
	// wrong.
	if !listed["open"] {
		t.Error("repo.list says a public, mirrorable repository cannot be mirrored: " +
			"the row builder is not assigning Mirrorable")
	}
	if listed["closed"] {
		t.Error("repo.list says a repository with the stored flag off may be mirrored")
	}
	if listed["vault"] {
		t.Error("repo.list says a private repository may be mirrored: a private " +
			"repository is never mirrorable whatever the flag says")
	}

	for _, name := range []string{"open", "closed", "vault"} {
		one, err := r.Invoke(context.Background(), "repo.get", repoGetIn{Repo: name})
		if err != nil {
			t.Fatalf("repo.get %s: %v", name, err)
		}
		if want := one.(repoInfo).Mirrorable; listed[name] != want {
			t.Errorf("%s: repo.list says mirrorable=%v, repo.get says %v — the two "+
				"surfaces disagree about the same flag", name, listed[name], want)
		}
	}
}