File size: 3,942 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
<?php

declare(strict_types=1);

namespace OpenWA\Tests;

use PHPUnit\Framework\TestCase;

class SearchTest extends TestCase
{
    public function testSearchSendsGetWithQueryParams(): void
    {
        $hit = [
            'messageId' => 'm1',
            'waMessageId' => 'wam1',
            'sessionId' => 's1',
            'chatId' => '628123456789@c.us',
            'body' => 'hello world',
            'snippet' => '<mark>hello</mark> world',
            'timestamp' => 1717900000,
            'type' => 'chat',
            'direction' => 'incoming',
            'from' => '628123456789@c.us',
            'score' => 1.5,
        ];
        $backend = (new MockBackend())->on(200, [
            'hits' => [$hit],
            'total' => 1,
            'tookMs' => 3,
            'provider' => 'builtin-fts',
        ]);
        $client = $backend->makeClient();

        $result = $client->search->search([
            'q' => 'hello',
            'sessionId' => 's1',
            'limit' => 10,
            'offset' => 0,
            'direction' => 'incoming',
        ]);

        $call = $backend->lastCall();
        $this->assertSame('GET', $call['method']);
        $this->assertSame('/api/search', $call['path']);
        $this->assertStringContainsString('q=hello', $call['query']);
        $this->assertStringContainsString('sessionId=s1', $call['query']);
        $this->assertStringContainsString('limit=10', $call['query']);
        $this->assertStringContainsString('offset=0', $call['query']);
        $this->assertStringContainsString('direction=incoming', $call['query']);

        $this->assertSame('builtin-fts', $result['provider']);
        $this->assertSame(1, $result['total']);
        $this->assertSame(3, $result['tookMs']);
        $this->assertCount(1, $result['hits']);
        $this->assertSame($hit, $result['hits'][0]);
    }

    public function testSearchDropsNullOptionalsFromQueryString(): void
    {
        $backend = (new MockBackend())->on(200, [
            'hits' => [],
            'total' => 0,
            'tookMs' => 0,
            'provider' => 'builtin-fts',
        ]);
        $client = $backend->makeClient();

        $client->search->search([
            'q' => 'term',
            'chatId' => null,
            'type' => 'chat',
            'dateFrom' => null,
        ]);

        $query = $backend->lastCall()['query'];
        // `q` and a present `type` are sent; null `chatId`/`dateFrom` are omitted.
        $this->assertStringContainsString('q=term', $query);
        $this->assertStringContainsString('type=chat', $query);
        $this->assertStringNotContainsString('chatId=', $query);
        $this->assertStringNotContainsString('dateFrom=', $query);
    }

    public function testSearchSendsDateAndNumericFilters(): void
    {
        $backend = (new MockBackend())->on(200, [
            'hits' => [],
            'total' => 0,
            'tookMs' => 1,
            'provider' => 'builtin-fts',
        ]);
        $client = $backend->makeClient();

        $client->search->search([
            'q' => 'invoice',
            'dateFrom' => 1717200000000,
            'dateTo' => 1717900000000,
            'limit' => 50,
            'offset' => 100,
        ]);

        $query = $backend->lastCall()['query'];
        $this->assertStringContainsString('dateFrom=1717200000000', $query);
        $this->assertStringContainsString('dateTo=1717900000000', $query);
        $this->assertStringContainsString('limit=50', $query);
        $this->assertStringContainsString('offset=100', $query);
    }

    public function testSearchReturnsEmptyArrayOnNullBody(): void
    {
        // A 204 or empty body resolves to null at the transport; search() falls back to [].
        $backend = (new MockBackend())->on(204);
        $client = $backend->makeClient();

        $result = $client->search->search(['q' => 'anything']);

        $this->assertSame([], $result);
    }
}