File size: 5,227 Bytes
4c34106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * This file is part of WPPConnect.
 *
 * WPPConnect is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WPPConnect is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with WPPConnect.  If not, see <https://www.gnu.org/licenses/>.
 */
import * as assert from 'assert';
import { sleep } from '../utils/sleep';
import { describeAuthenticatedTest, testUserId } from './common';

describeAuthenticatedTest('Chat functions', function (getClient) {
  it('star message', async function () {
    const client = getClient();

    await client.sendText(testUserId, 'Message 1');
    const msg2 = await client.sendText(testUserId, 'Message 2');
    await client.sendText(testUserId, 'Message 3');

    let msg = await client.getMessageById(msg2.id);
    assert.strictEqual(msg.star, false);

    let result = await client.starMessage(msg2.id, true);
    assert.strictEqual(result, 1);

    // Star a starred message
    result = await client.starMessage(msg2.id, true);
    assert.strictEqual(result, 0);

    msg = await client.getMessageById(msg2.id);
    assert.strictEqual(msg.star, true);

    result = await client.starMessage(msg2.id, false);
    assert.strictEqual(result, 1);

    // Unstar a unstarred message
    result = await client.starMessage(msg2.id, false);
    assert.strictEqual(result, 0);

    msg = await client.getMessageById(msg2.id);
    assert.strictEqual(msg.star, false);
  });

  it('clear chat', async function () {
    const client = getClient();

    const host = await client.getHostDevice();

    const chatId = host.wid._serialized;

    const msgs1 = await client.getAllMessagesInChat(chatId, true, false);

    await client.sendText(chatId, 'Message 1');
    const msg2 = await client.sendText(chatId, 'Message 2');
    await client.sendText(chatId, 'Message 3');

    await sleep(2000);

    await client.starMessage(msg2.id, true);

    await sleep(2000);

    const msgs2 = await client.getAllMessagesInChat(chatId, true, false);

    assert.ok(msgs2.length > msgs1.length);

    await sleep(2000);

    await client.clearChat(chatId, true);

    await sleep(2000);

    const msgs3 = await client.getAllMessagesInChat(chatId, true, false);
    assert.strictEqual(msgs3.length, 1);

    await client.clearChat(chatId, false);

    await sleep(2000);

    const msgs4 = await client.getAllMessagesInChat(chatId, true, false);
    assert.strictEqual(msgs4.length, 0);
  });

  it('forward a single message', async function () {
    const client = getClient();

    const host = await client.getHostDevice();

    assert.ok(host);

    const msg = await client.sendText(
      host.wid._serialized,
      'Message to forward'
    );

    const r = await client.forwardMessages(testUserId, msg.id, false);

    assert.strictEqual(r.length, 1);

    const fmsg = await client.getMessageById(r[0]);

    assert.strictEqual(fmsg.body, 'Message to forward');
  });

  it('forward multiple messages', async function () {
    const client = getClient();

    const host = await client.getHostDevice();

    assert.ok(host);

    const msg1 = await client.sendText(
      host.wid._serialized,
      'Message 1 to forward'
    );
    const msg2 = await client.sendText(
      host.wid._serialized,
      'Message 2 to forward'
    );

    const r = await client.forwardMessages(
      testUserId,
      [msg1.id, msg2.id],
      false
    );

    assert.strictEqual(r.length, 2);

    const fmsg1 = await client.getMessageById(r[0]);
    const fmsg2 = await client.getMessageById(r[1]);

    assert.strictEqual(fmsg1.body, 'Message 1 to forward');
    assert.strictEqual(fmsg2.body, 'Message 2 to forward');
  });

  it('archive chat', async function () {
    const client = getClient();

    // Ensure chat is not archived
    await client.sendText(testUserId, 'unarchive chat');

    await sleep(1000);
    let chat = await client.getChatById(testUserId);

    assert.strictEqual(chat.archive, false);

    // ensure the first archive is OK
    let result = await client.archiveChat(testUserId, true);

    assert.strictEqual(result, true);

    await sleep(1000);
    // ensure the second archive is not OK
    result = await client.archiveChat(testUserId, true);

    assert.strictEqual(result, false);

    await sleep(1000);
    chat = await client.getChatById(testUserId);

    assert.strictEqual(chat.archive, true);

    // ensure the first unarchive is OK
    result = await client.archiveChat(testUserId, false);

    assert.strictEqual(result, true);

    await sleep(1000);
    chat = await client.getChatById(testUserId);

    assert.strictEqual(chat.archive, false);

    // ensure the second unarchive is not OK
    result = await client.archiveChat(testUserId, false);

    assert.strictEqual(result, false);
  });
});