package bridge import ( "context" "encoding/json" "errors" "strings" "testing" "mesh-delivery-forge-bridge/internal/gitfixture" ) // gitFixture is one answer of the seat git's fixtures, by its name and what it is of. func gitFixture(t *testing.T, name, of string) json.RawMessage { t.Helper() return gitfixture.Given(t, name, of) } func TestBridgeTagsAnswersAPackagesTagsByPrefixPageByPage(t *testing.T) { // DLV-FB1, design 51 *The floor*; design 47 *The seat git* more := gitFixture(t, "tags answers", "verb:tags.answer") // go/v0.1.15, go/v0.1.14, more failed := gitFixture(t, "tags answers an error, never none", "verb:tags.answer") last := json.RawMessage(`{"repository":"novox/mesh-sdk","tags":["go/v0.1.2","v0.0.1"],"page":2,"bound":50}`) type asked struct { key string in string } for _, row := range []struct { name string answer func(page int) (json.RawMessage, error) repo string tags []string more bool pages int failing string }{ {name: "two pages, the second says no more", repo: "novox/mesh-sdk", tags: []string{"go/v0.1.14", "go/v0.1.15", "go/v0.1.2"}, pages: 2, answer: func(p int) (json.RawMessage, error) { if p == 1 { return more, nil } return last, nil }}, {name: "every page says more: stopped at the bound", repo: "novox/mesh-sdk", more: true, pages: TagsMaxPages, answer: func(int) (json.RawMessage, error) { return json.RawMessage(`{"tags":[],"more":true}`), nil }}, {name: "the seat does not answer", repo: "novox/mesh-sdk", failing: "page 1", pages: 1, answer: func(int) (json.RawMessage, error) { return nil, errors.New("down") }}, {name: "an error answer is an error, never none", repo: "novox/mesh-sdk", failing: "404", pages: 1, answer: func(int) (json.RawMessage, error) { return failed, nil }}, {name: "a repository with no owner", repo: "mesh-sdk", failing: "/"}, } { var calls []asked call := func(_ context.Context, key string, body any) (json.RawMessage, error) { in, _ := json.Marshal(body) calls = append(calls, asked{key, string(in)}) var p struct{ Page int } _ = json.Unmarshal(in, &p) return row.answer(p.Page) } a, err := Tags(context.Background(), call, row.repo, "go/v") if row.failing != "" { if err == nil || !strings.Contains(err.Error(), row.failing) { t.Errorf("%s: %v, want an error naming %q", row.name, err, row.failing) } } else if err != nil || !slicesEqual(a.Tags, row.tags) || a.More != row.more { t.Errorf("%s: Tags = %+v, %v", row.name, a, err) } if len(calls) != row.pages { t.Errorf("%s: %d calls, want %d", row.name, len(calls), row.pages) } if len(calls) > 0 && (calls[0].key != "seat:git.tags" || calls[0].in != `{"page":1,"prefix":"go/v","repository":"novox/mesh-sdk"}`) { t.Errorf("%s: first call %+v", row.name, calls[0]) } } } func slicesEqual(a, b []string) bool { if len(a) != len(b) { return false } for i := range a { if a[i] != b[i] { return false } } return true } // watchesMain watches the branch main of every repository, as the registrations of a test name it. var watchesMain = watchedOf("main") // watchedOf watches the branches of every repository, before the first registration, as the gate's source names them. func watchedOf(branches ...string) *Watched { w := &Watched{} w.Set(nil, branches) return w }