agent/php/tests/Feature/Api/Sync/RoutesTest.php
Snider dffdad8418 feat(api): implement §3 fleet+credits+subscription+sync+agent-auth routes (#848)
Additive-only — appended to php/Routes/api.php (existing routes
preserved). Existing /v1/fleet/{nodes,heartbeat,stats} +
/v1/agent/auth/provision left untouched.

New routes:
- /v1/agent/auth/register
- /v1/fleet/dispatch + /v1/fleet/stream
- /v1/credits/{balance,deduct,refund,ledger}
- /v1/subscription/{status,upgrade,cancel}
- /v1/agent/sync/{push,pull}

New controllers under php/Controllers/Api/{Fleet,Credits,Subscription,
Sync,AgentAuth}/. Reference FleetService/CreditService/SessionService
when available with fallbacks to current action/model layer (pre #849).

Pest Feature coverage under php/tests/Feature/Api/. pest skipped
(vendor binaries missing in sandbox).

Co-authored-by: Codex <noreply@openai.com>
Closes tasks.lthn.sh/view.php?id=848
2026-04-25 05:43:51 +01:00

76 lines
2.3 KiB
PHP

<?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.');
});