X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/d4c152a4dba0eec7c8ecd13732900909f51b1c97..4d7abe62a6dd692b71090819de0d57a40486bdb2:/hotline/server_blackbox_test.go?ds=inline diff --git a/hotline/server_blackbox_test.go b/hotline/server_blackbox_test.go index 17ad7f4..06e7771 100644 --- a/hotline/server_blackbox_test.go +++ b/hotline/server_blackbox_test.go @@ -1,44 +1,84 @@ package hotline import ( + "cmp" + "encoding/binary" + "encoding/hex" "github.com/stretchr/testify/assert" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + "log/slog" "os" + "slices" "testing" ) -func NewTestLogger() *zap.SugaredLogger { - encoderCfg := zap.NewProductionEncoderConfig() - encoderCfg.TimeKey = "timestamp" - encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder +func NewTestLogger() *slog.Logger { + return slog.New(slog.NewTextHandler(os.Stdout, nil)) +} - core := zapcore.NewCore( - zapcore.NewConsoleEncoder(encoderCfg), - zapcore.Lock(os.Stdout), - zap.DebugLevel, - ) +// assertTransferBytesEqual takes a string with a hexdump in the same format that `hexdump -C` produces and compares with +// a hexdump for the bytes in got, after stripping the create/modify timestamps. +// I don't love this, but as git does not preserve file create/modify timestamps, we either need to fully mock the +// filesystem interactions or work around in this way. +// TODO: figure out a better solution +func assertTransferBytesEqual(t *testing.T, wantHexDump string, got []byte) bool { + if wantHexDump == "" { + return true + } + + var clean []byte + clean = append(clean, got[:92]...) // keep the first 92 bytes + clean = append(clean, make([]byte, 16)...) // replace the next 16 bytes for create/modify timestamps + clean = append(clean, got[108:]...) // keep the rest + + return assert.Equal(t, wantHexDump, hex.Dump(clean)) +} - cores := []zapcore.Core{core} - l := zap.New(zapcore.NewTee(cores...)) - defer func() { _ = l.Sync() }() - return l.Sugar() +var tranSortFunc = func(a, b Transaction) int { + return cmp.Compare( + binary.BigEndian.Uint16(a.clientID[:]), + binary.BigEndian.Uint16(b.clientID[:]), + ) } -// tranAssertEqual compares equality of transactions slices after stripping out the random ID +// tranAssertEqual compares equality of transactions slices after stripping out the random transaction ID func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool { var newT1 []Transaction var newT2 []Transaction + for _, trans := range tran1 { - trans.ID = []byte{0, 0, 0, 0} + trans.ID = [4]byte{0, 0, 0, 0} + var fs []Field + for _, field := range trans.Fields { + if field.ID == [2]byte{0x00, 0x6b} { // FieldRefNum + continue + } + if field.ID == [2]byte{0x00, 0x72} { // FieldChatID + continue + } + fs = append(fs, field) + } + trans.Fields = fs newT1 = append(newT1, trans) } for _, trans := range tran2 { - trans.ID = []byte{0, 0, 0, 0} + trans.ID = [4]byte{0, 0, 0, 0} + var fs []Field + for _, field := range trans.Fields { + if field.ID == [2]byte{0x00, 0x6b} { // FieldRefNum + continue + } + if field.ID == [2]byte{0x00, 0x72} { // FieldChatID + continue + } + fs = append(fs, field) + } + trans.Fields = fs newT2 = append(newT2, trans) - } + slices.SortFunc(newT1, tranSortFunc) + slices.SortFunc(newT2, tranSortFunc) + return assert.Equal(t, newT1, newT2) }