]> git.r.bdr.sh - rbdr/mobius/blob - hotline/server_blackbox_test.go
Allow to delete home files / replace files
[rbdr/mobius] / hotline / server_blackbox_test.go
1 package hotline
2
3 import (
4 "cmp"
5 "encoding/binary"
6 "encoding/hex"
7 "github.com/stretchr/testify/assert"
8 "log/slog"
9 "os"
10 "slices"
11 "testing"
12 )
13
14 func NewTestLogger() *slog.Logger {
15 return slog.New(slog.NewTextHandler(os.Stdout, nil))
16 }
17
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 clean := slices.Concat(
29 got[:92],
30 make([]byte, 16),
31 got[108:],
32 )
33 return assert.Equal(t, wantHexDump, hex.Dump(clean))
34 }
35
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
43 // TranAssertEqual compares equality of transactions slices after stripping out the random transaction Type
44 func TranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool {
45 var newT1 []Transaction
46 var newT2 []Transaction
47
48 for _, trans := range tran1 {
49 trans.ID = [4]byte{0, 0, 0, 0}
50 var fs []Field
51 for _, field := range trans.Fields {
52 if field.Type == FieldRefNum { // FieldRefNum
53 continue
54 }
55 if field.Type == FieldChatID { // FieldChatID
56 continue
57 }
58
59 fs = append(fs, field)
60 }
61 trans.Fields = fs
62 newT1 = append(newT1, trans)
63 }
64
65 for _, trans := range tran2 {
66 trans.ID = [4]byte{0, 0, 0, 0}
67 var fs []Field
68 for _, field := range trans.Fields {
69 if field.Type == FieldRefNum { // FieldRefNum
70 continue
71 }
72 if field.Type == FieldChatID { // FieldChatID
73 continue
74 }
75
76 fs = append(fs, field)
77 }
78 trans.Fields = fs
79 newT2 = append(newT2, trans)
80 }
81
82 slices.SortFunc(newT1, tranSortFunc)
83 slices.SortFunc(newT2, tranSortFunc)
84
85 return assert.Equal(t, newT1, newT2)
86 }