package spindle

import (
	"context"
	"testing"
)

// knot2 only serves records, so a fork has to be read the way ensureRepo reads a repo
func TestSourceRepoOfAKnotThatOnlyServesRecordsResolves(t *testing.T) {
	env := newEnsureEnv(t)
	env.serve("fork")

	got, err := env.s.resolveSourceRepoInfo(context.Background(), testRepoDid)
	if err != nil {
		t.Fatalf("resolveSourceRepoInfo: %v", err)
	}
	if got.Did != testOwner.String() || got.Knot != env.knot.host() || got.Repo == nil || *got.Repo != "fork" {
		t.Fatalf("source repo = %+v, want the owner's fork on the knot", got)
	}
}

// anyone can name a repo did, only the owner's declaration makes the manifest count
func TestSourceRepoWithoutTheOwnersDeclarationIsRefused(t *testing.T) {
	env := newEnsureEnv(t)
	env.serve("fork")
	env.knot.declare(testOwner, "fork", "did:plc:elsewhere")

	if got, err := env.s.resolveSourceRepoInfo(context.Background(), testRepoDid); err == nil {
		t.Fatalf("resolved %+v from a manifest the owner never declared", got)
	}
}
