15 "github.com/stretchr/testify/require"
17 waAdv "go.mau.fi/whatsmeow/proto/waAdv"
18 "go.mau.fi/whatsmeow/proto/waE2E"
19 "go.mau.fi/whatsmeow/store"
20 "go.mau.fi/whatsmeow/store/sqlstore"
21 "go.mau.fi/whatsmeow/types"
22 "go.mau.fi/whatsmeow/types/events"
23 waLog "go.mau.fi/whatsmeow/util/log"
27 // whatsappTestNumber is the phone number of the own account in the
28 // tests. It is a number for fiction.
29 whatsappTestNumber = "15550001111"
31 // whatsappTestLID is the LID of the own account in the tests.
32 whatsappTestLID = "987654321012345"
34 // whatsappTestCode is the pairing code of the fake.
35 whatsappTestCode = "ABCD-EFGH"
39 // whatsappTestOwn is the own account in the tests.
40 whatsappTestOwn = types.NewJID(whatsappTestNumber, types.DefaultUserServer)
42 // whatsappTestAna is a contact in the tests.
43 whatsappTestAna = types.NewJID("15550002222", types.DefaultUserServer)
45 // whatsappTestBruno is a member of the test group.
46 whatsappTestBruno = types.NewJID("15550003333", types.DefaultUserServer)
48 // whatsappTestFamily is the test group.
49 whatsappTestFamily = types.NewJID("120363000000000001", types.GroupServer)
51 // whatsappTestChat is the private chat with whatsappTestAna.
52 whatsappTestChat = WhatsAppChat{
53 ID: whatsappTestAna.String(),
57 // whatsappTestSendTime is the time of the sends of the fake.
58 whatsappTestSendTime = time.Date(2026, 1, 1, 12, 5, 0, 0, time.UTC)
60 // whatsappTestGroup is the chat of whatsappTestFamily.
61 whatsappTestGroup = WhatsAppChat{
62 ID: whatsappTestFamily.String(),
68 // WhatsAppFake is a fake WhatsApp client. It keeps the event handler that the
69 // daemon gives it, and Emit gives events to the handler, as the library does.
70 // Each connection sends to connects, and each pairing sends its channel to
71 // pairings, so that the test gives the items of the pairing. A connection fails
72 // with connectErr, a logout with logoutErr, and a send with sendErr. groups are
73 // the groups of the account. A send that does not fail gets the time
74 // whatsappTestSendTime.
75 type WhatsAppFake struct {
76 connects chan struct{}
77 pairings chan chan whatsmeow.QRChannelItem
80 handle func(event any) bool
83 groups []*types.GroupInfo
87 sent []WhatsAppFakeSend
90 // ConnectContext tells the test that the daemon connected.
91 func (f *WhatsAppFake) ConnectContext(ctx context.Context) error {
92 f.connects <- struct{}{}
95 defer f.mutex.Unlock()
100 // Disconnect does nothing.
101 func (f *WhatsAppFake) Disconnect() {
104 // GetQRChannel gives a new channel of pairing items to the test.
105 func (f *WhatsAppFake) GetQRChannel(ctx context.Context) (<-chan whatsmeow.QRChannelItem, error) {
106 items := make(chan whatsmeow.QRChannelItem, 8)
112 // PairPhone keeps phone and returns whatsappTestCode.
113 func (f *WhatsAppFake) PairPhone(ctx context.Context, phone string, showPushNotification bool, clientType whatsmeow.PairClientType, clientDisplayName string) (string, error) {
115 defer f.mutex.Unlock()
117 f.phones = append(f.phones, phone)
119 return whatsappTestCode, nil
122 // Logout counts the logouts and returns logoutErr.
123 func (f *WhatsAppFake) Logout(ctx context.Context) error {
125 defer f.mutex.Unlock()
132 // GetJoinedGroups returns groups.
133 func (f *WhatsAppFake) GetJoinedGroups(ctx context.Context) ([]*types.GroupInfo, error) {
135 defer f.mutex.Unlock()
140 // GenerateMessageID returns a new message ID.
141 func (f *WhatsAppFake) GenerateMessageID() types.MessageID {
143 defer f.mutex.Unlock()
145 return fmt.Sprintf("MSG%d", len(f.sent)+1)
148 // SendMessage keeps the send and returns sendErr.
149 func (f *WhatsAppFake) SendMessage(ctx context.Context, to types.JID, message *waE2E.Message, extra ...whatsmeow.SendRequestExtra) (whatsmeow.SendResponse, error) {
151 defer f.mutex.Unlock()
153 f.sent = append(f.sent, WhatsAppFakeSend{
155 text: message.GetConversation(),
159 if f.sendErr != nil {
160 return whatsmeow.SendResponse{}, f.sendErr
163 return whatsmeow.SendResponse{
164 Timestamp: whatsappTestSendTime,
168 // Sent returns the sends.
169 func (f *WhatsAppFake) Sent() []WhatsAppFakeSend {
171 defer f.mutex.Unlock()
173 return slices.Clone(f.sent)
176 // Phones returns the phone numbers of the pairings.
177 func (f *WhatsAppFake) Phones() []string {
179 defer f.mutex.Unlock()
181 return append([]string{}, f.phones...)
184 // Logouts returns the number of logouts.
185 func (f *WhatsAppFake) Logouts() int {
187 defer f.mutex.Unlock()
192 // Emit gives event to the handler of the daemon and returns its answer.
193 func (f *WhatsAppFake) Emit(event any) bool {
201 // newClient keeps handle and returns the fake as the client of device.
202 func (f *WhatsAppFake) newClient(device *store.Device, handle func(event any) bool) WhatsAppClient {
204 defer f.mutex.Unlock()
211 // WhatsAppFakeSend is one send of the fake: the chat, the text, and the message
213 type WhatsAppFakeSend struct {
219 // whatsappStartTest opens the WhatsApp of the data directory dir with a fake
220 // client and the test clock, and closes it at the end of the test. The daemon
221 // log goes to the returned buffer.
222 func whatsappStartTest(t *testing.T, dir string, clock *TestClock) (*WhatsApp, *WhatsAppFake, *SyncBuffer) {
225 fake := &WhatsAppFake{
226 connects: make(chan struct{}, 16),
227 pairings: make(chan chan whatsmeow.QRChannelItem, 16),
230 logs := &SyncBuffer{}
231 whatsapp, err := openWhatsApp(t.Context(), dir, clock.Now, log.New(logs, "", 0), fake.newClient)
232 require.NoError(t, err)
235 require.NoError(t, whatsapp.Close())
238 return whatsapp, fake, logs
241 // whatsappTestSession writes a session database with mode 0600 to the data
242 // directory dir. When linked is true, it holds a device linked to
243 // whatsappTestNumber and whatsappTestLID, and else no device.
244 func whatsappTestSession(t *testing.T, dir string, linked bool) {
247 path := filepath.Join(dir, whatsappDirName, whatsappSessionFileName)
248 require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700))
249 require.NoError(t, os.WriteFile(path, nil, 0o600))
251 address, err := whatsappSessionAddress(path)
252 require.NoError(t, err)
254 container, err := sqlstore.New(t.Context(), "sqlite", address, waLog.Noop)
255 require.NoError(t, err)
258 device := container.NewDevice()
259 jid := types.NewADJID(whatsappTestNumber, 0, 1)
261 device.LID = types.NewADJID(whatsappTestLID, 0, 1)
262 device.LID.Server = types.HiddenUserServer
263 device.Account = &waAdv.ADVSignedDeviceIdentity{
265 AccountSignature: make([]byte, 64),
266 AccountSignatureKey: make([]byte, 32),
267 DeviceSignature: make([]byte, 64),
269 require.NoError(t, device.Save(t.Context()))
272 require.NoError(t, container.Close())
275 // whatsappDiscardLog is a logger for a WhatsApp whose log a test does not read.
276 func whatsappDiscardLog() *log.Logger {
277 return log.New(io.Discard, "", 0)
280 // whatsappTestMessage returns the event of the text message id of sender in
281 // chat at the time at. A sender with whatsappTestNumber is the user.
282 func whatsappTestMessage(chat, sender types.JID, id, text string, at time.Time) *events.Message {
283 return &events.Message{
284 Info: types.MessageInfo{
285 MessageSource: types.MessageSource{
288 IsFromMe: sender.User == whatsappTestNumber,
289 IsGroup: chat.Server == types.GroupServer,
294 Message: &waE2E.Message{
295 Conversation: new(text),