agent/php/tests/Feature/Api/Sync/RoutesTest.php

77 lines
2.3 KiB
PHP
Raw Normal View History

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
use Core\Mod\Agentic\Models\AgentApiKey;
use Core\Mod\Agentic\Models\BrainMemory;
use Core\Mod\Agentic\Models\FleetNode;
use Core\Tenant\Models\Workspace;
beforeEach(function (): void {
require __DIR__.'/../../../../Routes/api.php';
});
function syncRouteKey(
Workspace $workspace,
array $permissions = [AgentApiKey::PERM_SYNC_READ, AgentApiKey::PERM_SYNC_WRITE]
): AgentApiKey {
return createApiKey($workspace, 'Sync Route Key', $permissions);
}
test('agent sync push route stores dispatch history', function (): void {
$workspace = createWorkspace();
$key = syncRouteKey($workspace, [AgentApiKey::PERM_SYNC_WRITE]);
$response = $this
->withHeader('Authorization', 'Bearer '.$key->plainTextKey)
->postJson('/v1/agent/sync/push', [
'agent_id' => 'charon',
'dispatches' => [[
'repo' => 'dappco.re/go/agent',
'workspace' => 'core-agent',
'task' => 'Record the sync alias route',
'status' => 'completed',
]],
]);
$response
->assertCreated()
->assertJsonPath('data.synced', 1);
expect(FleetNode::query()->where('agent_id', 'charon')->exists())->toBeTrue();
});
test('agent sync pull route returns shared context', function (): void {
$workspace = createWorkspace();
$key = syncRouteKey($workspace, [AgentApiKey::PERM_SYNC_READ]);
FleetNode::create([
'workspace_id' => $workspace->id,
'agent_id' => 'charon',
'platform' => 'linux',
'status' => FleetNode::STATUS_ONLINE,
]);
BrainMemory::create([
'workspace_id' => $workspace->id,
'agent_id' => 'charon',
'type' => 'observation',
'content' => 'Shared context for the new pull route.',
'tags' => ['sync'],
'confidence' => 0.8,
'source' => 'test',
]);
$response = $this
->withHeader('Authorization', 'Bearer '.$key->plainTextKey)
->getJson('/v1/agent/sync/pull?agent_id=charon');
$response
->assertOk()
->assertJsonPath('total', 1)
->assertJsonPath('data.0.agent_id', 'charon')
->assertJsonPath('data.0.content', 'Shared context for the new pull route.');
});