]> git.r.bdr.sh - rbdr/mobius/blob - hotline/server_blackbox_test.go
Replace zap logger with slog
[rbdr/mobius] / hotline / server_blackbox_test.go
1 package hotline
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "github.com/stretchr/testify/assert"
7 "log/slog"
8 "os"
9 "testing"
10 )
11
12 func NewTestLogger() *slog.Logger {
13 return slog.New(slog.NewTextHandler(os.Stdout, nil))
14 }
15
16 // assertTransferBytesEqual takes a string with a hexdump in the same format that `hexdump -C` produces and compares with
17 // a hexdump for the bytes in got, after stripping the create/modify timestamps.
18 // I don't love this, but as git does not preserve file create/modify timestamps, we either need to fully mock the
19 // filesystem interactions or work around in this way.
20 // TODO: figure out a better solution
21 func assertTransferBytesEqual(t *testing.T, wantHexDump string, got []byte) bool {
22 if wantHexDump == "" {
23 return true
24 }
25
26 var clean []byte
27 clean = append(clean, got[:92]...) // keep the first 92 bytes
28 clean = append(clean, make([]byte, 16)...) // replace the next 16 bytes for create/modify timestamps
29 clean = append(clean, got[108:]...) // keep the rest
30
31 return assert.Equal(t, wantHexDump, hex.Dump(clean))
32 }
33
34 // tranAssertEqual compares equality of transactions slices after stripping out the random ID
35 func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool {
36 var newT1 []Transaction
37 var newT2 []Transaction
38
39 for _, trans := range tran1 {
40 trans.ID = []byte{0, 0, 0, 0}
41 var fs []Field
42 for _, field := range trans.Fields {
43 if bytes.Equal(field.ID, []byte{0x00, 0x6b}) {
44 continue
45 }
46 fs = append(fs, field)
47 }
48 trans.Fields = fs
49 newT1 = append(newT1, trans)
50 }
51
52 for _, trans := range tran2 {
53 trans.ID = []byte{0, 0, 0, 0}
54 var fs []Field
55 for _, field := range trans.Fields {
56 if bytes.Equal(field.ID, []byte{0x00, 0x6b}) {
57 continue
58 }
59 fs = append(fs, field)
60 }
61 trans.Fields = fs
62 newT2 = append(newT2, trans)
63 }
64
65 return assert.Equal(t, newT1, newT2)
66 }