|
| 1 | +package llo |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "bytes" |
| 10 | + |
| 11 | + "github.com/shopspring/decimal" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + llotypes "github.com/smartcontractkit/chainlink-common/pkg/types/llo" |
| 15 | +) |
| 16 | + |
| 17 | +// Test_LLOOutcomeProtoV1_SerializationComparison compares the serialized format |
| 18 | +// of LLOOutcomeProtoV1 between v0.1.6 and onwards |
| 19 | +func Test_LLOOutcomeProtoV1_SerializationComparison(t *testing.T) { |
| 20 | + codec := protoOutcomeCodecV1{} |
| 21 | + |
| 22 | + testCases := []struct { |
| 23 | + name string |
| 24 | + outcome Outcome |
| 25 | + goldenFile string |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "empty", |
| 29 | + outcome: Outcome{ |
| 30 | + LifeCycleStage: "", |
| 31 | + ObservationTimestampNanoseconds: 0, |
| 32 | + ChannelDefinitions: nil, |
| 33 | + ValidAfterNanoseconds: nil, |
| 34 | + StreamAggregates: nil, |
| 35 | + }, |
| 36 | + goldenFile: "empty.bin", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "minimal", |
| 40 | + outcome: Outcome{ |
| 41 | + LifeCycleStage: LifeCycleStageProduction, |
| 42 | + ObservationTimestampNanoseconds: 1234567890, |
| 43 | + ChannelDefinitions: map[llotypes.ChannelID]llotypes.ChannelDefinition{ |
| 44 | + 1: { |
| 45 | + ReportFormat: llotypes.ReportFormatJSON, |
| 46 | + Streams: []llotypes.Stream{ |
| 47 | + {StreamID: 1, Aggregator: llotypes.AggregatorMedian}, |
| 48 | + }, |
| 49 | + Opts: []byte(`{}`), |
| 50 | + }, |
| 51 | + }, |
| 52 | + ValidAfterNanoseconds: map[llotypes.ChannelID]uint64{ |
| 53 | + 1: 1000000000, |
| 54 | + }, |
| 55 | + StreamAggregates: map[llotypes.StreamID]map[llotypes.Aggregator]StreamValue{ |
| 56 | + 1: { |
| 57 | + llotypes.AggregatorMedian: ToDecimal(decimal.NewFromInt(123)), |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + goldenFile: "minimal.bin", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "full", |
| 65 | + outcome: Outcome{ |
| 66 | + LifeCycleStage: LifeCycleStageProduction, |
| 67 | + ObservationTimestampNanoseconds: 9876543210, |
| 68 | + ChannelDefinitions: map[llotypes.ChannelID]llotypes.ChannelDefinition{ |
| 69 | + 1: { |
| 70 | + ReportFormat: llotypes.ReportFormatJSON, |
| 71 | + Streams: []llotypes.Stream{ |
| 72 | + {StreamID: 1, Aggregator: llotypes.AggregatorMedian}, |
| 73 | + {StreamID: 2, Aggregator: llotypes.AggregatorQuote}, |
| 74 | + }, |
| 75 | + Opts: []byte(`{"foo":"bar"}`), |
| 76 | + }, |
| 77 | + 2: { |
| 78 | + ReportFormat: llotypes.ReportFormatJSON, |
| 79 | + Streams: []llotypes.Stream{ |
| 80 | + {StreamID: 3, Aggregator: llotypes.AggregatorMedian}, |
| 81 | + }, |
| 82 | + Opts: []byte(`{"baz":"qux"}`), |
| 83 | + }, |
| 84 | + }, |
| 85 | + ValidAfterNanoseconds: map[llotypes.ChannelID]uint64{ |
| 86 | + 1: 5000000000, |
| 87 | + 2: 6000000000, |
| 88 | + }, |
| 89 | + StreamAggregates: map[llotypes.StreamID]map[llotypes.Aggregator]StreamValue{ |
| 90 | + 1: { |
| 91 | + llotypes.AggregatorMedian: ToDecimal(decimal.NewFromInt(12345)), |
| 92 | + }, |
| 93 | + 2: { |
| 94 | + llotypes.AggregatorQuote: &Quote{ |
| 95 | + Bid: decimal.NewFromInt(1010), |
| 96 | + Benchmark: decimal.NewFromInt(1011), |
| 97 | + Ask: decimal.NewFromInt(1012), |
| 98 | + }, |
| 99 | + }, |
| 100 | + 3: { |
| 101 | + llotypes.AggregatorMedian: ToDecimal(decimal.NewFromFloat(123.456)), |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + goldenFile: "full.bin", |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + for _, tc := range testCases { |
| 110 | + t.Run(tc.name, func(t *testing.T) { |
| 111 | + // Serialize with current code |
| 112 | + currentSerialized, err := codec.Encode(tc.outcome) |
| 113 | + require.NoError(t, err, "failed to encode outcome with current code") |
| 114 | + |
| 115 | + // Read golden file from v0.1.6 |
| 116 | + goldenPath := filepath.Join("testdata", "outcome_v1_serialization", "v0.1.6", tc.goldenFile) |
| 117 | + goldenBytes, err := os.ReadFile(goldenPath) |
| 118 | + if err != nil { |
| 119 | + if os.IsNotExist(err) { |
| 120 | + t.Skipf("golden file %s does not exist. Run generate_golden_files.sh to create it.", goldenPath) |
| 121 | + return |
| 122 | + } |
| 123 | + require.NoError(t, err, "failed to read golden file") |
| 124 | + } |
| 125 | + |
| 126 | + // Compare byte-by-byte |
| 127 | + if !bytes.Equal(currentSerialized, goldenBytes) { |
| 128 | + t.Errorf("Serialization mismatch for test case %q\n", tc.name) |
| 129 | + t.Errorf("Current length: %d bytes\n", len(currentSerialized)) |
| 130 | + t.Errorf("Golden (v0.1.6) length: %d bytes\n", len(goldenBytes)) |
| 131 | + t.Errorf("Current hex: %s\n", hex.EncodeToString(currentSerialized)) |
| 132 | + t.Errorf("Golden (v0.1.6) hex: %s\n", hex.EncodeToString(goldenBytes)) |
| 133 | + |
| 134 | + // Find first difference |
| 135 | + minLen := len(currentSerialized) |
| 136 | + if len(goldenBytes) < minLen { |
| 137 | + minLen = len(goldenBytes) |
| 138 | + } |
| 139 | + for i := 0; i < minLen; i++ { |
| 140 | + if currentSerialized[i] != goldenBytes[i] { |
| 141 | + t.Errorf("First difference at byte %d: current=0x%02x, golden=0x%02x\n", i, currentSerialized[i], goldenBytes[i]) |
| 142 | + break |
| 143 | + } |
| 144 | + } |
| 145 | + if len(currentSerialized) != len(goldenBytes) { |
| 146 | + t.Errorf("Length mismatch: current has %d bytes, golden has %d bytes\n", len(currentSerialized), len(goldenBytes)) |
| 147 | + } |
| 148 | + t.FailNow() |
| 149 | + } |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments