ZapMachine/cli_scripts/test.php

192 lines
5.3 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
/**
* ZapMachine Test Script
*
* Usage:
* php test.php Run all tests
* php test.php --brave Test Brave search JSON output
* php test.php --search Test ImgSearch for a word and show JSON results
*/
// Make sure relative paths in init.php resolve against this script's directory
chdir(__DIR__);
// Bootstrap the application
require_once __DIR__ . '/../conf/init.php';
$passed = 0;
$failed = 0;
/**
* Assert a condition and print result
*/
function test($description, $condition) {
global $passed, $failed;
if ($condition) {
echo "$description\n";
$passed++;
} else {
echo "$description\n";
$failed++;
}
}
/**
* Run basic application tests
*/
function runBasicTests() {
echo "\n=== Basic Tests ===\n";
// Version test
test('ZAP_VERSION is defined', defined('ZAP_VERSION'));
test('ZAP_VERSION is 0.9.0', ZAP_VERSION === '0.9.0');
// Directory constants
test('ZAP_APP_BASE_DIR is defined', defined('ZAP_APP_BASE_DIR'));
test('ZAP_APP_BASE_DIR exists', is_dir(ZAP_APP_BASE_DIR));
test('ZAP_RESOURCE_DIR is defined', defined('ZAP_RESOURCE_DIR'));
test('ZAP_UTILS_DIR is defined', defined('ZAP_UTILS_DIR'));
// Resource directory
$resourceDir = ZAP_APP_BASE_DIR . DIRECTORY_SEPARATOR . ZAP_RESOURCE_DIR;
test('Resource directory exists', is_dir($resourceDir));
test('Search interface exists', file_exists($resourceDir . '/Search.interface.php'));
test('Brave class exists', file_exists($resourceDir . '/Brave.class.php'));
// Utils directory
$utilsDir = ZAP_APP_BASE_DIR . DIRECTORY_SEPARATOR . ZAP_UTILS_DIR;
test('Utils directory exists', is_dir($utilsDir));
test('ZAPCurl class exists', file_exists($utilsDir . '/ZAPCurl.class.php'));
// PHP extensions
test('ImageMagick PHP extension (imagick) is installed', extension_loaded('imagick'));
// Config
test('BRAVE_API_KEY is defined', defined('BRAVE_API_KEY'));
test('BRAVE_BASE is defined', defined('BRAVE_BASE'));
global $search_engines;
test('$search_engines is set', isset($search_engines));
test('$search_engines contains brave', isset($search_engines) && in_array('brave', $search_engines));
}
/**
* Test ImgSearch class for a given word
*/
function testImgSearch() {
echo "\n=== ImgSearch Test ===\n";
echo "Enter a search word: ";
$handle = fopen("php://stdin", "r");
$word = trim(fgets($handle));
fclose($handle);
if (empty($word)) {
echo " ⚠ No word entered. Aborting.\n";
return;
}
$folder = ZAP_APP_BASE_DIR . DIRECTORY_SEPARATOR . ZAP_SESSIONS_DIR . DIRECTORY_SEPARATOR . 'search_test_' . ZAP_MOMENT;
if (!is_dir($folder)) {
@mkdir($folder, 0777, true);
}
echo "\nQuery: '$word'\n\n";
// Register configured search engines so ImgSearch can pick one
global $search_engines;
$config = Config::getInstance();
$config->register('engines', $search_engines);
$search = new ImgSearch($folder, $word);
$data = $search->getImageData();
echo "Raw JSON output:\n";
echo json_encode($data, JSON_PRETTY_PRINT) . "\n\n";
test('Result is an array', is_array($data));
test('Has result key', isset($data['result']));
if (isset($data['result']) && $data['result'] === true) {
test('Search returned results', true);
test('Has results array', isset($data['results']));
if (isset($data['results'])) {
$count = count($data['results']);
echo " Results count: $count\n";
}
} else {
echo " ⚠ No results returned. Check your API key and configuration.\n";
}
}
/**
* Test Brave search JSON output
*/
function testBraveSearch() {
echo "\n=== Brave Search JSON Test ===\n";
echo "Query: 'test' | Results: 3\n\n";
$brave = new Brave();
$brave->setQuery('test');
$brave->setParam('size', 3);
$json = $brave->getData();
echo "Raw JSON output:\n";
echo json_encode($json, JSON_PRETTY_PRINT) . "\n\n";
// Validate structure
test('Result is an array', is_array($json));
test('Has result key', isset($json['result']));
if (isset($json['result']) && $json['result'] === true) {
test('Search returned results', true);
test('Has results array', isset($json['results']));
test('Engine is Brave', isset($json['engine']) && $json['engine'] === 'Brave');
if (isset($json['results'])) {
$count = count($json['results']);
test("Results count is 3 (got $count)", $count === 3);
// Check first result structure
if ($count > 0) {
$first = $json['results'][0];
test('First result has clickurl', isset($first['clickurl']));
test('First result has thumb', isset($first['thumb']));
test('First result has referer', isset($first['referer']));
}
}
} else {
echo "\n ⚠ No results returned. Check your API key and CX.\n";
}
}
// Main execution
if (in_array('--search', $argv)) {
testImgSearch();
} elseif (in_array('--brave', $argv)) {
testBraveSearch();
} else {
runBasicTests();
// Ask if user wants to run Brave test
if (defined('BRAVE_API_KEY') && BRAVE_API_KEY !== 'YOUR_BRAVE_API_KEY') {
echo "\nRun Brave search test? (y/n): ";
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
if (trim(strtolower($line)) === 'y') {
testBraveSearch();
}
fclose($handle);
} else {
echo "\n ⚠ Skipping Brave test: API key not configured\n";
}
}
// Summary
echo "\n=== Results ===\n";
echo "Passed: $passed\n";
echo "Failed: $failed\n";
echo ($failed === 0 ? "All tests passed! ✓" : "Some tests failed.") . "\n\n";
exit($failed > 0 ? 1 : 0);
?>