netgescon-day0/tests/Feature/ControlTowerPollCommandTest.php

173 lines
5.8 KiB
PHP

<?php
use Illuminate\Support\Facades\Http;
use Illuminate\Console\Command;
beforeEach(function () {
Http::preventStrayRequests();
});
it('publishes report when wrapper returns NETTOWER_AGY_ULTRA_WRAPPER_OK', function () {
$taskId = 'task-test-001';
Http::fake([
'http://192.168.0.53:4174/api/tasks/next*' => Http::response(['task' => ['id' => $taskId, 'title' => 'Test Task', 'description' => 'Test Desc']], 200),
'http://192.168.0.53:4174/api/reports' => Http::response(['id' => 'rep-123'], 201),
'http://192.168.0.53:4174/api/tasks/status' => Http::response(['status' => 'done'], 200),
'http://192.168.0.53:4174/api/machines/heartbeat' => Http::response(['status' => 'online'], 200),
]);
$mockScript = tempnam(sys_get_temp_dir(), 'mock_wrapper_') . '.sh';
$jsonOutput = json_encode([
'ok' => true,
'parsed' => [
'task_id' => $taskId,
'esito_205' => 'riuscito',
'repository' => 'ssh://git@192.168.0.53:2222/michele/netgescon-day0.git',
'branch' => 'stabilization/205-zero',
'commit' => 'a159c03799bd942bb6c8a74e50eb9ddf9a74aefb',
'consolidated_unit_id' => '1545',
'blocco_dati' => 'no',
'dates_used' => '2026-08-10',
'test_eseguiti' => 'NETTOWER_AGY_ULTRA_WRAPPER_OK',
'note' => 'Test OK',
'absorbed_legacy_fragments' => 'id_cond=12',
'raw_text' => 'NETTOWER_AGY_ULTRA_WRAPPER_OK',
],
], JSON_UNESCAPED_SLASHES);
file_put_contents($mockScript, "#!/usr/bin/env bash\ncat - >/dev/null\necho '{$jsonOutput}'\nexit 0\n");
chmod($mockScript, 0755);
$exitCode = $this->artisan('netgescon:control-tower-poll', [
'--task-id' => $taskId,
'--wrapper-script' => $mockScript,
])->run();
@unlink($mockScript);
expect($exitCode)->toBe(Command::SUCCESS);
Http::assertSent(function ($request) use ($taskId) {
return str_contains($request->url(), '/api/reports') &&
($request['task_id'] ?? null) === $taskId &&
str_contains($request['raw_text'] ?? '', 'NETTOWER_AGY_ULTRA_WRAPPER_OK');
});
});
it('fails when wrapper returns old A/11 template non-JSON', function () {
$taskId = 'task-test-001';
Http::fake([
'http://192.168.0.53:4174/api/tasks/next*' => Http::response(['task' => ['id' => $taskId]], 200),
]);
$mockScript = tempnam(sys_get_temp_dir(), 'mock_wrapper_') . '.sh';
$oldA11Report = "ESITO_205: riuscito\nTASK_ID: {$taskId}\nUNITA_ID_A11: 1749\nCODICE_BEFORE: 0021-A-220\nCODICE_AFTER: 0021-A-11";
file_put_contents($mockScript, "#!/usr/bin/env bash\ncat - >/dev/null\necho '{$oldA11Report}'\nexit 0\n");
chmod($mockScript, 0755);
$exitCode = $this->artisan('netgescon:control-tower-poll', [
'--task-id' => $taskId,
'--wrapper-script' => $mockScript,
])->run();
@unlink($mockScript);
expect($exitCode)->toBe(Command::FAILURE);
Http::assertNotSent(function ($request) {
return str_contains($request->url(), '/api/reports');
});
});
it('fails and does not publish report when wrapper exits with code 1', function () {
$taskId = 'task-test-001';
Http::fake([
'http://192.168.0.53:4174/api/tasks/next*' => Http::response(['task' => ['id' => $taskId]], 200),
]);
$mockScript = tempnam(sys_get_temp_dir(), 'mock_wrapper_') . '.sh';
file_put_contents($mockScript, "#!/usr/bin/env bash\necho 'ERROR: CLI failed' >&2\nexit 1\n");
chmod($mockScript, 0755);
$exitCode = $this->artisan('netgescon:control-tower-poll', [
'--task-id' => $taskId,
'--wrapper-script' => $mockScript,
])->run();
@unlink($mockScript);
expect($exitCode)->toBe(Command::FAILURE);
Http::assertNotSent(function ($request) {
return str_contains($request->url(), '/api/reports');
});
});
it('fails and does not publish report when parsed task_id differs from current task', function () {
$taskId = 'task-test-001';
Http::fake([
'http://192.168.0.53:4174/api/tasks/next*' => Http::response(['task' => ['id' => $taskId]], 200),
]);
$mockScript = tempnam(sys_get_temp_dir(), 'mock_wrapper_') . '.sh';
$jsonOutput = json_encode([
'ok' => true,
'parsed' => [
'task_id' => 'DIFFERENT_TASK_999',
'esito_205' => 'riuscito',
],
], JSON_UNESCAPED_SLASHES);
file_put_contents($mockScript, "#!/usr/bin/env bash\necho '{$jsonOutput}'\nexit 0\n");
chmod($mockScript, 0755);
$exitCode = $this->artisan('netgescon:control-tower-poll', [
'--task-id' => $taskId,
'--wrapper-script' => $mockScript,
])->run();
@unlink($mockScript);
expect($exitCode)->toBe(Command::FAILURE);
Http::assertNotSent(function ($request) {
return str_contains($request->url(), '/api/reports');
});
});
it('fails and does not publish report when JSON ok=false', function () {
$taskId = 'task-test-001';
Http::fake([
'http://192.168.0.53:4174/api/tasks/next*' => Http::response(['task' => ['id' => $taskId]], 200),
]);
$mockScript = tempnam(sys_get_temp_dir(), 'mock_wrapper_') . '.sh';
$jsonOutput = json_encode([
'ok' => false,
'missing' => ['task_id', 'esito_205'],
'parsed' => [],
], JSON_UNESCAPED_SLASHES);
file_put_contents($mockScript, "#!/usr/bin/env bash\necho '{$jsonOutput}'\nexit 0\n");
chmod($mockScript, 0755);
$exitCode = $this->artisan('netgescon:control-tower-poll', [
'--task-id' => $taskId,
'--wrapper-script' => $mockScript,
])->run();
@unlink($mockScript);
expect($exitCode)->toBe(Command::FAILURE);
Http::assertNotSent(function ($request) {
return str_contains($request->url(), '/api/reports');
});
});