|
4 | 4 | "testing" |
5 | 5 |
|
6 | 6 | "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
7 | 8 | ) |
8 | 9 |
|
9 | 10 | func TestBuildStatus_IsTerminal(t *testing.T) { |
@@ -55,3 +56,128 @@ func TestBuildStatus_IsTerminal(t *testing.T) { |
55 | 56 | }) |
56 | 57 | } |
57 | 58 | } |
| 59 | + |
| 60 | +func TestBuild_ToBytes(t *testing.T) { |
| 61 | + build := Build{ |
| 62 | + ID: "build-1", |
| 63 | + BatchID: "batch-1", |
| 64 | + SpeculationPath: SpeculationPathInfo{ |
| 65 | + Base: []string{"batch-0", "batch-prev"}, |
| 66 | + }, |
| 67 | + Score: 0.85, |
| 68 | + Status: BuildStatusQueued, |
| 69 | + } |
| 70 | + |
| 71 | + data, err := build.ToBytes() |
| 72 | + require.NoError(t, err) |
| 73 | + assert.NotEmpty(t, data) |
| 74 | + |
| 75 | + // Verify JSON contains expected fields |
| 76 | + jsonStr := string(data) |
| 77 | + assert.Contains(t, jsonStr, "build-1") |
| 78 | + assert.Contains(t, jsonStr, "batch-1") |
| 79 | + assert.Contains(t, jsonStr, "queued") |
| 80 | +} |
| 81 | + |
| 82 | +func TestBuildFromBytes(t *testing.T) { |
| 83 | + original := Build{ |
| 84 | + ID: "build-42", |
| 85 | + BatchID: "batch-7", |
| 86 | + SpeculationPath: SpeculationPathInfo{ |
| 87 | + Base: []string{"batch-5", "batch-6"}, |
| 88 | + }, |
| 89 | + Score: 0.92, |
| 90 | + Status: BuildStatusRunning, |
| 91 | + } |
| 92 | + |
| 93 | + // Serialize |
| 94 | + data, err := original.ToBytes() |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + // Deserialize |
| 98 | + deserialized, err := BuildFromBytes(data) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + // Verify all fields match |
| 102 | + assert.Equal(t, original.ID, deserialized.ID) |
| 103 | + assert.Equal(t, original.BatchID, deserialized.BatchID) |
| 104 | + assert.Equal(t, original.SpeculationPath.Base, deserialized.SpeculationPath.Base) |
| 105 | + assert.Equal(t, original.Score, deserialized.Score) |
| 106 | + assert.Equal(t, original.Status, deserialized.Status) |
| 107 | +} |
| 108 | + |
| 109 | +func TestBuildFromBytes_InvalidJSON(t *testing.T) { |
| 110 | + invalidJSON := []byte(`{"invalid": json"}`) |
| 111 | + |
| 112 | + _, err := BuildFromBytes(invalidJSON) |
| 113 | + assert.Error(t, err) |
| 114 | +} |
| 115 | + |
| 116 | +func TestBuildFromBytes_EmptyData(t *testing.T) { |
| 117 | + emptyJSON := []byte(`{}`) |
| 118 | + |
| 119 | + build, err := BuildFromBytes(emptyJSON) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + // Empty JSON should deserialize with zero values |
| 123 | + assert.Empty(t, build.ID) |
| 124 | + assert.Empty(t, build.BatchID) |
| 125 | + assert.Equal(t, BuildStatusUnknown, build.Status) |
| 126 | + assert.Equal(t, float32(0), build.Score) |
| 127 | +} |
| 128 | + |
| 129 | +func TestBuild_SerializationRoundTrip(t *testing.T) { |
| 130 | + tests := []struct { |
| 131 | + name string |
| 132 | + build Build |
| 133 | + }{ |
| 134 | + { |
| 135 | + name: "queued build with speculation path", |
| 136 | + build: Build{ |
| 137 | + ID: "build-100", |
| 138 | + BatchID: "batch-50", |
| 139 | + SpeculationPath: SpeculationPathInfo{ |
| 140 | + Base: []string{"batch-48", "batch-49"}, |
| 141 | + }, |
| 142 | + Score: 0.75, |
| 143 | + Status: BuildStatusQueued, |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "passed build with no speculation base", |
| 148 | + build: Build{ |
| 149 | + ID: "build-200", |
| 150 | + BatchID: "batch-60", |
| 151 | + Score: 1.0, |
| 152 | + Status: BuildStatusPassed, |
| 153 | + }, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "failed build with zero score", |
| 157 | + build: Build{ |
| 158 | + ID: "build-300", |
| 159 | + BatchID: "batch-70", |
| 160 | + SpeculationPath: SpeculationPathInfo{ |
| 161 | + Base: []string{"batch-65"}, |
| 162 | + }, |
| 163 | + Score: 0, |
| 164 | + Status: BuildStatusFailed, |
| 165 | + }, |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + for _, tt := range tests { |
| 170 | + t.Run(tt.name, func(t *testing.T) { |
| 171 | + // Serialize |
| 172 | + data, err := tt.build.ToBytes() |
| 173 | + require.NoError(t, err) |
| 174 | + |
| 175 | + // Deserialize |
| 176 | + deserialized, err := BuildFromBytes(data) |
| 177 | + require.NoError(t, err) |
| 178 | + |
| 179 | + // Verify complete equality |
| 180 | + assert.Equal(t, tt.build, deserialized) |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
0 commit comments