]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
625b0580 | 3 | import ( |
a2ef262a JH |
4 | "cmp" |
5 | "encoding/binary" | |
7cd900d6 | 6 | "encoding/hex" |
00d1ef67 | 7 | "github.com/stretchr/testify/assert" |
a6216dd8 | 8 | "log/slog" |
625b0580 | 9 | "os" |
a2ef262a | 10 | "slices" |
625b0580 JH |
11 | "testing" |
12 | ) | |
13 | ||
a6216dd8 JH |
14 | func NewTestLogger() *slog.Logger { |
15 | return slog.New(slog.NewTextHandler(os.Stdout, nil)) | |
625b0580 JH |
16 | } |
17 | ||
7cd900d6 JH |
18 | // assertTransferBytesEqual takes a string with a hexdump in the same format that `hexdump -C` produces and compares with |
19 | // a hexdump for the bytes in got, after stripping the create/modify timestamps. | |
20 | // I don't love this, but as git does not preserve file create/modify timestamps, we either need to fully mock the | |
21 | // filesystem interactions or work around in this way. | |
22 | // TODO: figure out a better solution | |
23 | func assertTransferBytesEqual(t *testing.T, wantHexDump string, got []byte) bool { | |
24 | if wantHexDump == "" { | |
25 | return true | |
26 | } | |
27 | ||
28 | var clean []byte | |
29 | clean = append(clean, got[:92]...) // keep the first 92 bytes | |
30 | clean = append(clean, make([]byte, 16)...) // replace the next 16 bytes for create/modify timestamps | |
31 | clean = append(clean, got[108:]...) // keep the rest | |
32 | ||
33 | return assert.Equal(t, wantHexDump, hex.Dump(clean)) | |
34 | } | |
35 | ||
a2ef262a JH |
36 | var tranSortFunc = func(a, b Transaction) int { |
37 | return cmp.Compare( | |
38 | binary.BigEndian.Uint16(a.clientID[:]), | |
39 | binary.BigEndian.Uint16(b.clientID[:]), | |
40 | ) | |
41 | } | |
42 | ||
f8e4cd54 | 43 | // tranAssertEqual compares equality of transactions slices after stripping out the random transaction ID |
00d1ef67 JH |
44 | func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool { |
45 | var newT1 []Transaction | |
46 | var newT2 []Transaction | |
7cd900d6 | 47 | |
92a7e455 | 48 | for _, trans := range tran1 { |
153e2eac | 49 | trans.ID = [4]byte{0, 0, 0, 0} |
7cd900d6 JH |
50 | var fs []Field |
51 | for _, field := range trans.Fields { | |
f8e4cd54 JH |
52 | if field.ID == [2]byte{0x00, 0x6b} { // FieldRefNum |
53 | continue | |
54 | } | |
55 | if field.ID == [2]byte{0x00, 0x72} { // FieldChatID | |
7cd900d6 JH |
56 | continue |
57 | } | |
a2ef262a | 58 | fs = append(fs, field) |
7cd900d6 JH |
59 | } |
60 | trans.Fields = fs | |
00d1ef67 JH |
61 | newT1 = append(newT1, trans) |
62 | } | |
625b0580 | 63 | |
92a7e455 | 64 | for _, trans := range tran2 { |
153e2eac | 65 | trans.ID = [4]byte{0, 0, 0, 0} |
7cd900d6 JH |
66 | var fs []Field |
67 | for _, field := range trans.Fields { | |
f8e4cd54 JH |
68 | if field.ID == [2]byte{0x00, 0x6b} { // FieldRefNum |
69 | continue | |
70 | } | |
71 | if field.ID == [2]byte{0x00, 0x72} { // FieldChatID | |
7cd900d6 JH |
72 | continue |
73 | } | |
a2ef262a | 74 | fs = append(fs, field) |
7cd900d6 JH |
75 | } |
76 | trans.Fields = fs | |
00d1ef67 | 77 | newT2 = append(newT2, trans) |
00d1ef67 | 78 | } |
625b0580 | 79 | |
a2ef262a JH |
80 | slices.SortFunc(newT1, tranSortFunc) |
81 | slices.SortFunc(newT2, tranSortFunc) | |
82 | ||
00d1ef67 | 83 | return assert.Equal(t, newT1, newT2) |
92a7e455 | 84 | } |