]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server_blackbox_test.go
Delete cmd/mobius-hotline-server/mobius/config/Files/hello.txt
[rbdr/mobius] / hotline / server_blackbox_test.go
index 138a17f285ca782e64c8c2c79811c893ef9789e4..888ca3f0ac86aa6bda2f8f72e348c1e58dc38f82 100644 (file)
@@ -1,30 +1,18 @@
 package hotline
 
 import (
-       "bytes"
+       "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
-
-       core := zapcore.NewCore(
-               zapcore.NewConsoleEncoder(encoderCfg),
-               zapcore.Lock(os.Stdout),
-               zap.DebugLevel,
-       )
-
-       cores := []zapcore.Core{core}
-       l := zap.New(zapcore.NewTee(cores...))
-       defer func() { _ = l.Sync() }()
-       return l.Sugar()
+func NewTestLogger() *slog.Logger {
+       return slog.New(slog.NewTextHandler(os.Stdout, nil))
 }
 
 // assertTransferBytesEqual takes a string with a hexdump in the same format that `hexdump -C` produces and compares with
@@ -37,26 +25,37 @@ func assertTransferBytesEqual(t *testing.T, wantHexDump string, got []byte) bool
                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
-
+       clean := slices.Concat(
+               got[:92],
+               make([]byte, 16),
+               got[108:],
+       )
        return assert.Equal(t, wantHexDump, hex.Dump(clean))
 }
 
-// tranAssertEqual compares equality of transactions slices after stripping out the random ID
-func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool {
+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 transaction Type
+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 bytes.Equal(field.ID, []byte{0x00, 0x6b}) {
+                       if field.Type == FieldRefNum { // FieldRefNum
                                continue
                        }
+                       if field.Type == FieldChatID { // FieldChatID
+                               continue
+                       }
+
                        fs = append(fs, field)
                }
                trans.Fields = fs
@@ -64,17 +63,24 @@ func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool {
        }
 
        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 bytes.Equal(field.ID, []byte{0x00, 0x6b}) {
+                       if field.Type == FieldRefNum { // FieldRefNum
                                continue
                        }
+                       if field.Type == FieldChatID { // 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)
 }