X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/2e2524881dfe8f6c5cabfd9d342d5d3492953bd4..53c6c8a5a17e2dfc8bd3ff02ab99d29d709dda2f:/whatsapp_fake_helper_test.go?ds=inline diff --git a/whatsapp_fake_helper_test.go b/whatsapp_fake_helper_test.go index ee5321e..7aa558b 100644 --- a/whatsapp_fake_helper_test.go +++ b/whatsapp_fake_helper_test.go @@ -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() { }