|
| 1 | +// Copyright (c) 2025 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package messagequeue |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + "google.golang.org/protobuf/proto" |
| 23 | + |
| 24 | + changepb "github.com/uber/submitqueue/api/base/change/protopb" |
| 25 | + strategypb "github.com/uber/submitqueue/api/base/mergestrategy/protopb" |
| 26 | +) |
| 27 | + |
| 28 | +func TestMergeRequestRoundTrip(t *testing.T) { |
| 29 | + req := &MergeRequest{ |
| 30 | + Id: "queue-a/42", |
| 31 | + QueueName: "queue-a", |
| 32 | + Steps: []*MergeStep{ |
| 33 | + { |
| 34 | + StepId: "queue-a/1", |
| 35 | + Changes: []*changepb.Change{{Uris: []string{"github://uber/repo/pull/1/0123456789abcdef0123456789abcdef01234567"}}}, |
| 36 | + Strategy: strategypb.Strategy_REBASE, |
| 37 | + }, |
| 38 | + { |
| 39 | + StepId: "queue-a/2", |
| 40 | + Changes: []*changepb.Change{{Uris: []string{"github://uber/repo/pull/2/89abcdef0123456789abcdef0123456789abcdef"}}}, |
| 41 | + Strategy: strategypb.Strategy_MERGE, |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + data, err := MergeRequestToBytes(req) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + got, err := MergeRequestFromBytes(data) |
| 50 | + require.NoError(t, err) |
| 51 | + assert.True(t, proto.Equal(req, got), "round-tripped MergeRequest should equal the original") |
| 52 | +} |
| 53 | + |
| 54 | +func TestMergeResultRoundTrip(t *testing.T) { |
| 55 | + // A committing merge reports the revisions each step produced on the target; |
| 56 | + // a dry-run check leaves output_ids empty and reports a per-step reason on |
| 57 | + // failure. Both shapes share the one MergeResult contract. |
| 58 | + cases := map[string]*MergeResult{ |
| 59 | + "merged with produced revisions": { |
| 60 | + Id: "queue-a/42", |
| 61 | + Success: true, |
| 62 | + Steps: []*StepResult{ |
| 63 | + {StepId: "queue-a/1", OutputIds: []string{"0123456789abcdef0123456789abcdef01234567"}}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + "failed with per-step reason": { |
| 67 | + Id: "queue-a/42", |
| 68 | + Success: false, |
| 69 | + Reason: "conflict in foo.go", |
| 70 | + Steps: []*StepResult{{StepId: "queue-a/2", Reason: "conflict in foo.go"}}, |
| 71 | + }, |
| 72 | + "minimal": { |
| 73 | + Id: "queue-a/42", |
| 74 | + Success: true, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for name, res := range cases { |
| 79 | + t.Run(name, func(t *testing.T) { |
| 80 | + data, err := MergeResultToBytes(res) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + got, err := MergeResultFromBytes(data) |
| 84 | + require.NoError(t, err) |
| 85 | + assert.True(t, proto.Equal(res, got), "round-tripped MergeResult should equal the original") |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// TestWireFormat locks the two protojson encoding decisions the contract relies |
| 91 | +// on: snake_case field names (UseProtoNames) and proto-conventional UPPER_SNAKE |
| 92 | +// enum values on the wire. |
| 93 | +func TestWireFormat(t *testing.T) { |
| 94 | + data, err := MergeRequestToBytes(&MergeRequest{ |
| 95 | + Id: "queue-a/42", |
| 96 | + QueueName: "queue-a", |
| 97 | + Steps: []*MergeStep{{StepId: "queue-a/1", Strategy: strategypb.Strategy_SQUASH_REBASE}}, |
| 98 | + }) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + assert.Contains(t, string(data), `"queue_name"`, "fields must serialize as snake_case") |
| 102 | + assert.Contains(t, string(data), `"SQUASH_REBASE"`, "enums must serialize as their UPPER_SNAKE value name") |
| 103 | +} |
| 104 | + |
| 105 | +// TestTopicsBindEveryTopicKey is the topic-binding drift guard: every Runway |
| 106 | +// topic key is carried by exactly one message's topics option, and no topics |
| 107 | +// option names an unknown topic. |
| 108 | +func TestTopicsBindEveryTopicKey(t *testing.T) { |
| 109 | + bound := map[string]int{} |
| 110 | + for _, m := range []proto.Message{&MergeRequest{}, &MergeResult{}} { |
| 111 | + topics := Topics(m) |
| 112 | + require.NotEmpty(t, topics, "message must declare a non-empty topics option") |
| 113 | + for _, topic := range topics { |
| 114 | + bound[topic]++ |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + keys := []TopicKey{ |
| 119 | + TopicKeyMergeConflictCheck, |
| 120 | + TopicKeyMergeConflictCheckSignal, |
| 121 | + TopicKeyMerge, |
| 122 | + TopicKeyMergeSignal, |
| 123 | + } |
| 124 | + |
| 125 | + valid := map[string]bool{} |
| 126 | + for _, k := range keys { |
| 127 | + valid[k.String()] = true |
| 128 | + assert.Equalf(t, 1, bound[k.String()], "topic key %q must be bound to exactly one message via the topics option", k) |
| 129 | + } |
| 130 | + for topic := range bound { |
| 131 | + assert.Truef(t, valid[topic], "topics option names unknown topic %q", topic) |
| 132 | + } |
| 133 | +} |
0 commit comments