Files
api/services/nginx/app/tests/bird/VoiceCallsApiTest.php
T

88 lines
3.2 KiB
PHP

<?php
if (!defined('WD')) { define('WD', dirname(__DIR__, 2)); }
require_once WD . '/classes/bird.php';
use classes\bird as BirdClient;
class DummyVar
{
public function __construct(private mixed $value) {}
public function isTrue(): bool { return $this->value === true || $this->value === 'true' || $this->value === 1 || $this->value === '1'; }
public function getVariableValue(): mixed { return $this->value; }
}
class DummyConfig
{
public DummyVar $enabled;
public DummyVar $api_key;
public DummyVar $server_url;
public function __construct()
{
$this->enabled = new DummyVar('true');
$this->api_key = new DummyVar('test_api_key');
$this->server_url = new DummyVar('https://example.test');
}
public function getModuleName(): string { return 'bird'; }
}
class FakeBird extends BirdClient
{
public array $last = [];
public function __construct()
{
// Bypass DB-backed config classes with our dummy config
$this->config = new DummyConfig();
}
protected function doHttpRequest(string $method, string $url, array $headers, string $body = ''): array
{
$this->last = compact('method','url','headers','body');
// Return a canned success response
$resp = ['id' => 'call_123', 'status' => 'queued'];
return ['status_code' => 200, 'body' => json_encode($resp)];
}
}
function assert_true($cond, $msg)
{
if ($cond) {
echo "✔ $msg\n";
} else {
echo "✘ $msg\n"; exit(1);
}
}
// Test: create call (POST /workspaces/{ws}/channels/{ch}/calls)
$client = new FakeBird();
$payload = [
'to' => '+4511122233',
'from' => '+4599988877',
'tts' => [ 'message' => 'Hello from test' ],
];
$res = $client->createVoiceCall('ws_123', 'ch_123', $payload);
assert_true(is_object($res) || is_array($res), 'Response is JSON-decodable');
assert_true(str_contains($client->last['url'], '/workspaces/ws_123/channels/ch_123/calls'), 'POST URL composed correctly');
assert_true($client->last['method'] === 'POST', 'HTTP method is POST');
assert_true(in_array('Authorization: AccessKey test_api_key', $client->last['headers'], true), 'Authorization header is set');
assert_true(json_decode($client->last['body'], true)['to'] === '+4511122233', 'POST body encoded correctly');
// Test: get call (GET /workspaces/{ws}/channels/{ch}/calls/{id})
$client2 = new FakeBird();
$res2 = $client2->getVoiceCall('ws_123', 'ch_123', 'call_123');
assert_true(str_contains($client2->last['url'], '/workspaces/ws_123/channels/ch_123/calls/call_123'), 'GET URL composed correctly');
assert_true($client2->last['method'] === 'GET', 'HTTP method is GET');
assert_true(empty($client2->last['body']), 'GET body is empty');
// Test: hangup with empty payload encodes an object body ({}), not array body ([]).
$client3 = new FakeBird();
$client3->hangupVoiceCall('ws_123', 'ch_123', 'call_123');
assert_true(str_ends_with($client3->last['url'], '/workspaces/ws_123/channels/ch_123/calls/call_123/hangup'), 'Hangup URL composed correctly');
assert_true($client3->last['method'] === 'POST', 'Hangup uses POST');
assert_true(trim((string)$client3->last['body']) === '{}', 'Hangup empty body is encoded as JSON object');
echo "VoiceCallsApiTest completed.\n";