File size: 6,760 Bytes
46252cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace OpenWA\Tests;

use OpenWA\Exceptions\OpenWANotFoundException;
use PHPUnit\Framework\TestCase;

class MessagesTest extends TestCase
{
    public function testSendTextUsesSendTextPath(): void
    {
        $backend = (new MockBackend())->on(201, ['messageId' => 'm1', 'timestamp' => 1]);
        $client = $backend->makeClient();
        $client->messages->sendText('s1', ['chatId' => 'a@c.us', 'text' => 'hi']);
        $call = $backend->lastCall();
        // Guzzle exposes the relative path at the handler layer; the host is
        // statically configured via base_uri, so we assert on path + body.
        $this->assertSame('/api/sessions/s1/messages/send-text', $call['path']);
        $this->assertSame(['chatId' => 'a@c.us', 'text' => 'hi'], $call['body']);
    }

    public function testListReturnsMessagesPageEnvelope(): void
    {
        // The server responds with a {messages, total} page, not a flat array. Pin the shape so a
        // future change — or a misunderstood "fix" that unwraps it and drops `total` — is caught.
        $backend = (new MockBackend())->on(200, [
            'messages' => [['id' => 'm1', 'body' => 'hi', 'chatId' => 'a@c.us']],
            'total' => 1,
        ]);
        $client = $backend->makeClient();
        $page = $client->messages->list('s1', ['limit' => 10]);
        $this->assertArrayHasKey('messages', $page);
        $this->assertIsArray($page['messages']);
        $this->assertSame('m1', $page['messages'][0]['id']);
        $this->assertSame(1, $page['total']);
        $this->assertStringContainsString('/messages', $backend->lastCall()['path']);
        $this->assertStringContainsString('limit=10', $backend->lastCall()['query']);
    }

    public static function mediaSegments(): array
    {
        return [
            'sendImage'    => ['sendImage',    'send-image'],
            'sendVideo'    => ['sendVideo',    'send-video'],
            'sendAudio'    => ['sendAudio',    'send-audio'],
            'sendDocument' => ['sendDocument', 'send-document'],
            'sendSticker'  => ['sendSticker',  'send-sticker'],
        ];
    }

    /**
     * @dataProvider mediaSegments
     */
    public function testMediaSegmentsUseCorrectPaths(string $method, string $segment): void
    {
        $backend = (new MockBackend())->on(201, ['messageId' => 'm', 'timestamp' => 2]);
        $client = $backend->makeClient();
        $client->messages->$method('s', ['chatId' => 'a@c.us', 'url' => 'u']);
        $this->assertStringContainsString("/messages/{$segment}", $backend->lastCall()['path']);
    }

    public function testReplyForwardReactDelete(): void
    {
        $backend = new MockBackend();
        $backend->on(201, ['messageId' => 'm', 'timestamp' => 1]);
        $backend->on(201, ['messageId' => 'm', 'timestamp' => 1]);
        $backend->on(200, ['success' => true]);
        $backend->on(200, ['success' => true]);
        $client = $backend->makeClient();
        $client->messages->reply('s', ['chatId' => 'a@c.us', 'quotedMessageId' => 'q', 'text' => 'r']);
        $this->assertStringContainsString('/messages/reply', $backend->calls()[0]['url']);
        $client->messages->forward('s', ['fromChatId' => 'a@c.us', 'toChatId' => 'b@c.us', 'messageId' => 'm']);
        $this->assertStringContainsString('/messages/forward', $backend->calls()[1]['url']);
        $client->messages->react('s', ['chatId' => 'a@c.us', 'messageId' => 'm', 'emoji' => '👍']);
        $this->assertStringContainsString('/messages/react', $backend->calls()[2]['url']);
        $client->messages->delete('s', ['chatId' => 'a@c.us', 'messageId' => 'm']);
        $this->assertStringContainsString('/messages/delete', $backend->calls()[3]['url']);
    }

    public function testEditMessageUsesEditPath(): void
    {
        $backend = (new MockBackend())->on(200, ['messageId' => 'm1', 'timestamp' => 123]);
        $client = $backend->makeClient();
        $result = $client->messages->editMessage('s1', ['chatId' => 'a@c.us', 'messageId' => 'm1', 'body' => 'edited']);
        $call = $backend->lastCall();
        $this->assertSame('POST', $call['method']);
        $this->assertSame('/api/sessions/s1/messages/edit', $call['path']);
        $this->assertSame(['chatId' => 'a@c.us', 'messageId' => 'm1', 'body' => 'edited'], $call['body']);
        $this->assertSame('m1', $result['messageId']);
    }

    public function testEditMessage404MapsToNotFoundException(): void
    {
        $backend = (new MockBackend())->on(404, [
            'statusCode' => 404,
            'message' => 'Message not found',
            'error' => 'Not Found',
        ]);
        $this->expectException(OpenWANotFoundException::class);
        $backend->makeClient()->messages->editMessage('s1', ['chatId' => 'a@c.us', 'messageId' => 'missing', 'body' => 'x']);
    }

    public function testHistoryAndReactionsPath(): void
    {
        $backend = new MockBackend();
        $backend->on(200, []);
        $backend->on(200, []);
        $client = $backend->makeClient();
        $client->messages->history('s', 'a@c.us', ['limit' => 5]);
        $this->assertStringContainsString('/messages/a@c.us/history', $backend->calls()[0]['url']);
        $this->assertStringContainsString('limit=5', $backend->calls()[0]['url']);
        $client->messages->reactions('s', 'a@c.us', 'm1');
        $this->assertStringContainsString('/messages/a@c.us/m1/reactions', $backend->calls()[1]['url']);
    }

    public function testSendBulkBatchStatusCancelBatch(): void
    {
        $backend = new MockBackend();
        $backend->on(202, ['batchId' => 'b', 'status' => 'queued', 'totalMessages' => 1, 'estimatedCompletionTime' => 't', 'statusUrl' => '/u']);
        $backend->on(200, ['batchId' => 'b', 'status' => 'done', 'progress' => 100, 'results' => [], 'startedAt' => 's', 'completedAt' => 'c']);
        $backend->on(200, ['batchId' => 'b', 'status' => 'cancelled', 'progress' => 50]);
        $client = $backend->makeClient();
        $client->messages->sendBulk('s', ['messages' => [['chatId' => 'a@c.us', 'type' => 'text', 'content' => ['text' => 'x']]]]);
        $this->assertStringContainsString('/messages/send-bulk', $backend->calls()[0]['url']);
        $status = $client->messages->batchStatus('s', 'b');
        $this->assertSame(100, $status['progress']);
        $this->assertStringContainsString('/messages/batch/b', $backend->calls()[1]['url']);
        $cancelled = $client->messages->cancelBatch('s', 'b');
        $this->assertSame('cancelled', $cancelled['status']);
        $this->assertSame('POST', $backend->calls()[2]['method']);
        $this->assertStringContainsString('/messages/batch/b/cancel', $backend->calls()[2]['url']);
    }
}