]> git.giacomolli.uk - onechat/blobdiff - whatsapp_fake_helper_test.go
Rebuild agent memory in date order
[onechat] / whatsapp_fake_helper_test.go
index ee5321ed4ab837ad9215d6fb9399e9c46c72452c..7aa558bffa0da8e213d78a1a373c7f044601fbf6 100644 (file)
@@ -69,29 +69,32 @@ var (
 // daemon gives it, and Emit gives events to the handler, as the library does.
 // Each connection keeps its context and sends to connects, and each pairing
 // sends its channel to pairings, so that the test gives the items of the
-// pairing. A connection fails with connectErr, a logout with logoutErr, and a
-// send with sendErr. groups are the groups of the account. A send that does not
-// fail gets the time whatsappTestSendTime.
+// pairing. A connection takes its error from connectErrors, then connectErr. A
+// logout fails with logoutErr, and a send with sendErr. groups are the groups
+// of the account. A send that does not fail gets the time whatsappTestSendTime.
 type WhatsAppFake struct {
        connects chan struct{}
        pairings chan chan whatsmeow.QRChannelItem
 
-       mutex       sync.Mutex
-       handle      func(event any) bool
-       connectErr  error
-       logoutErr   error
-       groups      []*types.GroupInfo
-       sendErr     error
-       phones      []string
-       logouts     int
-       sent        []WhatsAppFakeSend
-       connections []context.Context
+       mutex         sync.Mutex
+       handle        func(event any) bool
+       connectErr    error
+       connectErrors []error
+       connectTimes  []time.Time
+       logoutErr     error
+       groups        []*types.GroupInfo
+       sendErr       error
+       phones        []string
+       logouts       int
+       sent          []WhatsAppFakeSend
+       connections   []context.Context
 }
 
 // ConnectContext keeps ctx and tells the test that the daemon connected.
 func (f *WhatsAppFake) ConnectContext(ctx context.Context) error {
        f.mutex.Lock()
        f.connections = append(f.connections, ctx)
+       f.connectTimes = append(f.connectTimes, time.Now())
        f.mutex.Unlock()
 
        f.connects <- struct{}{}
@@ -99,6 +102,12 @@ func (f *WhatsAppFake) ConnectContext(ctx context.Context) error {
        f.mutex.Lock()
        defer f.mutex.Unlock()
 
+       if len(f.connectErrors) > 0 {
+               err := f.connectErrors[0]
+               f.connectErrors = f.connectErrors[1:]
+               return err
+       }
+
        return f.connectErr
 }
 
@@ -110,6 +119,14 @@ func (f *WhatsAppFake) Connections() []context.Context {
        return slices.Clone(f.connections)
 }
 
+// ConnectionTimes returns the time of each connection call, in order.
+func (f *WhatsAppFake) ConnectionTimes() []time.Time {
+       f.mutex.Lock()
+       defer f.mutex.Unlock()
+
+       return slices.Clone(f.connectTimes)
+}
+
 // Disconnect does nothing.
 func (f *WhatsAppFake) Disconnect() {
 }