package bridge import ( "context" "encoding/json" "testing" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" "git.novox.be/novox/mesh-sdk/go/keys" ) // Design 47 G §4, §507, A-236; H §739: the watched branches are the registrations' by origin, and before the first // registration the gate's source's, no branch fixed in code; a move of a watched branch is one head-moved naming the // repository, the branch, the head read through the seat git's verb branch, the head held before and start; the read asks // nothing but branch, and an unprotected branch's head is read like any other. func TestAMoveOfAWatchedBranchIsOneHeadMoved(t *testing.T) { w := &Watched{} w.Set([]Registered{{Origin: "novox/mesh-sdk", Branches: []string{"main", "beta"}}}, []string{"main"}) for _, c := range []struct { repo, branch string want bool }{{"novox/mesh-sdk", "beta", true}, {"novox/mesh-sdk", "feat/x", false}, {"novox/other", "main", false}} { if got := w.Watches(c.repo, c.branch); got != c.want { t.Errorf("registered: %s of %s watched %v, want %v", c.branch, c.repo, got, c.want) } } if !watchedOf("main").Watches("novox/other", "main") || watchedOf("main").Watches("novox/other", "beta") { t.Errorf("before the first registration the gate's source's branches are watched, of every repository") } const head, before = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccccccccccccccccccccccc" moved := Moved{Owner: "novox", Repo: "mesh-sdk", Branch: "feat/47-H"} for _, c := range []struct { name string answer string head string fails bool }{ {"a protected branch", `{"head":"` + head + `","protected":true}`, head, false}, {"an unprotected branch, read like any other (ADR 0682 §13)", `{"head":"` + head + `","protected":false}`, head, false}, {"an answer naming no head", `{"head":"abc"}`, "", true}, {"a refusal", `{"error":"no such branch","error-kind":"not-found"}`, "", true}, } { var asked []string ask := func(_ context.Context, key string, body any) (json.RawMessage, error) { b, _ := json.Marshal(body) asked = append(asked, key+" "+string(b)) return json.RawMessage(c.answer), nil } got, err := ReadHead(context.Background(), ask, moved) if (err != nil) != c.fails || got != c.head { t.Errorf("%s: %q %v, want %q, failing %v", c.name, got, err, c.head, c.fails) } if len(asked) != 1 || asked[0] != `seat:git.branch {"branch":"feat/47-H","repository":"novox/mesh-sdk"}` { t.Errorf("%s: asked %v, want only the seat git's branch", c.name, asked) } } for _, c := range []struct { name string before string start bool }{{"the first move", "", false}, {"a later move", before, false}, {"read at the start", before, true}} { msg, err := HeadMoved(moved, head, c.before, c.start) if err != nil { t.Fatalf("%s: %v", c.name, err) } h, err := forgemsg.DecodeHeadMoved(msg.Data) want := forgemsg.HeadMoved{V: forgemsg.Version, Repository: "novox/mesh-sdk", Branch: "feat/47-H", Head: head, Before: c.before, Start: c.start} if err != nil || h != want || msg.Event != "forge.novox.mesh-sdk."+keys.EscapeName("feat/47-H")+".head-moved" { t.Errorf("%s: %s %+v (%v), want %+v", c.name, msg.Event, h, err, want) } } if _, err := HeadMoved(moved, head, head, false); err == nil { t.Errorf("a head equal to the one held makes a message; want none") } }