225 lines
9.8 KiB
PHP
225 lines
9.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Scribase\Mcp\Tests;
|
||
|
|
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use Scribase\Mcp\ScribaseClient;
|
||
|
|
use Scribase\Mcp\ScribaseTools;
|
||
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
||
|
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validation-logic and wire-shape tests against a `MockHttpClient` — real
|
||
|
|
* JSON bodies this project mocks were captured verbatim from a real, live
|
||
|
|
* run of `scripts/verify-live.php` against production `api.scribase.com`
|
||
|
|
* (see README "Real, live verification" for the full transcript and the
|
||
|
|
* one real bug that run found and fixed: `scribase_memory_task`'s
|
||
|
|
* `forget` action needs an explicit `::uuid` cast because
|
||
|
|
* `SqlLiteral::encode()` builds a `text`-typed expression and Postgres has
|
||
|
|
* no implicit `uuid = text` comparison).
|
||
|
|
*/
|
||
|
|
final class ScribaseToolsTest extends TestCase
|
||
|
|
{
|
||
|
|
private function tools(MockHttpClient $http): ScribaseTools
|
||
|
|
{
|
||
|
|
return new ScribaseTools(new ScribaseClient('test-token', 'https://api.scribase.test', $http));
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- validation, no network involved -----------------------------------
|
||
|
|
|
||
|
|
public function testCreateProjectRequiresOrganizationId(): void
|
||
|
|
{
|
||
|
|
$result = $this->tools(new MockHttpClient())->createProject('', 'proj');
|
||
|
|
self::assertSame('organizationId is required.', $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testCreateProjectRequiresProjectId(): void
|
||
|
|
{
|
||
|
|
$result = $this->tools(new MockHttpClient())->createProject('org', '');
|
||
|
|
self::assertSame('projectId is required.', $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testLintMigrationRequiresUpSql(): void
|
||
|
|
{
|
||
|
|
$result = $this->tools(new MockHttpClient())->lintMigration('org', 'proj', 'production', '');
|
||
|
|
self::assertSame("up is required (the migration's forward SQL to lint).", $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testQueryRequiresAStatement(): void
|
||
|
|
{
|
||
|
|
$result = $this->tools(new MockHttpClient())->query('org', 'proj', 'production', '');
|
||
|
|
self::assertSame('query is required (a single read-only SQL statement).', $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testBranchTaskRejectsAnUnknownAction(): void
|
||
|
|
{
|
||
|
|
$result = $this->tools(new MockHttpClient())->branchTask('teleport', 'org', 'proj');
|
||
|
|
self::assertSame('action must be one of: create, promote, discard.', $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testBranchTaskCreateRejectsABranchThatIsItsOwnBase(): void
|
||
|
|
{
|
||
|
|
$result = $this->tools(new MockHttpClient())->branchTask('create', 'org', 'proj', environmentId: 'production', baseEnvironmentId: 'production');
|
||
|
|
self::assertSame('a branch cannot be its own base environment.', $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testMemoryTaskRejectsAnUnknownAction(): void
|
||
|
|
{
|
||
|
|
$result = $this->tools(new MockHttpClient())->memoryTask('wipe', 'org', 'proj');
|
||
|
|
self::assertStringStartsWith('action must be one of:', $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testMemoryTaskForgetRejectsANonUuidMemoryId(): void
|
||
|
|
{
|
||
|
|
$result = $this->tools(new MockHttpClient())->memoryTask('forget', 'org', 'proj', memoryId: 'not-a-uuid');
|
||
|
|
self::assertSame("memoryId must be a UUID, as returned by a prior action='store' call.", $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- wire-shape / real-response-replay tests -----------------------
|
||
|
|
|
||
|
|
public function testLintMigrationReportsBlockedForMissingRls(): void
|
||
|
|
{
|
||
|
|
// Real body shape captured live, 2026-11-07, from a genuinely
|
||
|
|
// unsafe migration run through this exact tool against production.
|
||
|
|
$http = new MockHttpClient([new MockResponse(json_encode([
|
||
|
|
'lintFindings' => [[
|
||
|
|
'rule' => 'missing_rls',
|
||
|
|
'severity' => 'BLOCK',
|
||
|
|
'subject' => 'public.mcp_verify_notes',
|
||
|
|
'line' => 1,
|
||
|
|
'message' => 'table public.mcp_verify_notes has neither RLS enabled nor any policy.',
|
||
|
|
]],
|
||
|
|
'plan' => ['steps' => [['version' => 20261107011200, 'name' => 'mcp_lint_check', 'direction' => 'up']]],
|
||
|
|
]), ['http_code' => 200])]);
|
||
|
|
|
||
|
|
$result = $this->tools($http)->lintMigration('org', 'proj', 'production', 'create table public.t (id uuid);');
|
||
|
|
|
||
|
|
self::assertSame('blocked', $result['status']);
|
||
|
|
self::assertFalse($result['safe_to_apply']);
|
||
|
|
self::assertCount(1, $result['blocking_findings']);
|
||
|
|
self::assertSame('missing_rls', $result['blocking_findings'][0]['rule']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testQueryRejectsAWriteStatementTheServerRefused(): void
|
||
|
|
{
|
||
|
|
// Real body shape captured live from the server's own
|
||
|
|
// `not_read_only` rejection of a `drop table` sent through the
|
||
|
|
// read-only tool.
|
||
|
|
$http = new MockHttpClient([new MockResponse(json_encode([
|
||
|
|
'type' => 'urn:scribase:error:not_read_only',
|
||
|
|
'title' => 'Invalid request',
|
||
|
|
'status' => 400,
|
||
|
|
'code' => 'not_read_only',
|
||
|
|
'detail' => 'only read-only statements are allowed',
|
||
|
|
]), ['http_code' => 400])]);
|
||
|
|
|
||
|
|
$result = $this->tools($http)->query('org', 'proj', 'production', 'drop table t;');
|
||
|
|
|
||
|
|
self::assertSame('query failed', $result['error']);
|
||
|
|
self::assertSame(400, $result['status']);
|
||
|
|
self::assertSame('not_read_only', $result['detail']['code']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testMemoryStorePreparesTheTableThenSendsAHexEncodedInsert(): void
|
||
|
|
{
|
||
|
|
$capturedQueries = [];
|
||
|
|
$http = new MockHttpClient(function (string $method, string $url, array $options) use (&$capturedQueries) {
|
||
|
|
$body = json_decode($options['body'], true);
|
||
|
|
$capturedQueries[] = $body['query'] ?? null;
|
||
|
|
|
||
|
|
if (str_contains($body['query'], 'create table if not exists')) {
|
||
|
|
return new MockResponse('{}', ['http_code' => 200]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// The insert — reply with a real-shaped `database/execute` body.
|
||
|
|
return new MockResponse(json_encode([
|
||
|
|
'statements' => [[
|
||
|
|
'rows' => [['id' => '11111111-1111-1111-1111-111111111111', 'created_at' => '2026-11-07T00:00:00Z']],
|
||
|
|
]],
|
||
|
|
]), ['http_code' => 200]);
|
||
|
|
});
|
||
|
|
|
||
|
|
$result = $this->tools($http)->memoryTask(
|
||
|
|
'store',
|
||
|
|
'org',
|
||
|
|
'proj',
|
||
|
|
content: 'a memory with a \'quote\' and a backslash \\ and café',
|
||
|
|
);
|
||
|
|
|
||
|
|
self::assertSame('stored', $result['status']);
|
||
|
|
self::assertSame('11111111-1111-1111-1111-111111111111', $result['memory_id']);
|
||
|
|
|
||
|
|
$insertQuery = end($capturedQueries);
|
||
|
|
// The hostile content must never appear as a literal quoted string —
|
||
|
|
// only inside a hex-encoded decode() call.
|
||
|
|
self::assertStringNotContainsString("'a memory", $insertQuery);
|
||
|
|
self::assertStringContainsString('convert_from(decode(', $insertQuery);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testMemoryForgetSendsAnExplicitUuidCastOnTheIdComparison(): void
|
||
|
|
{
|
||
|
|
// Regression test for the real bug `scripts/verify-live.php` found
|
||
|
|
// live against production: without `(...)::uuid`, Postgres refuses
|
||
|
|
// `id = <text expression>` with `42883 operator does not exist:
|
||
|
|
// uuid = text` (`hermes_agent_memories.id` is a real `uuid` column).
|
||
|
|
$capturedQuery = null;
|
||
|
|
$http = new MockHttpClient(function (string $method, string $url, array $options) use (&$capturedQuery) {
|
||
|
|
$capturedQuery = json_decode($options['body'], true)['query'];
|
||
|
|
|
||
|
|
return new MockResponse(json_encode([
|
||
|
|
'statements' => [['rows' => [['id' => '11111111-1111-1111-1111-111111111111']]]],
|
||
|
|
]), ['http_code' => 200]);
|
||
|
|
});
|
||
|
|
|
||
|
|
$result = $this->tools($http)->memoryTask(
|
||
|
|
'forget',
|
||
|
|
'org',
|
||
|
|
'proj',
|
||
|
|
memoryId: '11111111-1111-1111-1111-111111111111',
|
||
|
|
);
|
||
|
|
|
||
|
|
self::assertSame('forgotten', $result['status']);
|
||
|
|
self::assertMatchesRegularExpression(
|
||
|
|
"/where id = \\(convert_from\\(decode\\('[0-9a-f]+', 'hex'\\), 'UTF8'\\)\\)::uuid and scope = /",
|
||
|
|
$capturedQuery,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testCreateProjectOmitsTtlHoursRatherThanSendingAJsonNull(): void
|
||
|
|
{
|
||
|
|
// Regression test for the bug already shipped as a fix in the
|
||
|
|
// companion Python plugin's 1.2.1 (confirmed against the same
|
||
|
|
// production control plane): a production environment create must
|
||
|
|
// never send a literal JSON `null` for `ttl_hours` — the schema
|
||
|
|
// validator 400s on that. This client never sets the key at all.
|
||
|
|
$capturedEnvironmentBody = null;
|
||
|
|
$http = new MockHttpClient(function (string $method, string $url, array $options) use (&$capturedEnvironmentBody) {
|
||
|
|
if (str_contains($url, '/v1/environments') && 'POST' === $method) {
|
||
|
|
$capturedEnvironmentBody = json_decode($options['body'], true);
|
||
|
|
|
||
|
|
return new MockResponse(json_encode(['id' => 'op_123']), ['http_code' => 202]);
|
||
|
|
}
|
||
|
|
if (str_contains($url, '/operations/')) {
|
||
|
|
return new MockResponse(json_encode(['state' => 'succeeded']), ['http_code' => 200]);
|
||
|
|
}
|
||
|
|
if (str_contains($url, '/api-keys')) {
|
||
|
|
return new MockResponse(json_encode(['api_url' => 'https://x.test']), ['http_code' => 200]);
|
||
|
|
}
|
||
|
|
if (str_contains($url, '/connection')) {
|
||
|
|
return new MockResponse(json_encode(['connection_uri' => 'postgres://x']), ['http_code' => 200]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// PUT .../projects/{id}
|
||
|
|
return new MockResponse(json_encode(['id' => 'proj']), ['http_code' => 201]);
|
||
|
|
});
|
||
|
|
|
||
|
|
$result = $this->tools($http)->createProject('org', 'proj');
|
||
|
|
|
||
|
|
self::assertSame('ready', $result['status']);
|
||
|
|
self::assertSame('org', $result['organization_id']);
|
||
|
|
self::assertArrayNotHasKey('ttl_hours', $capturedEnvironmentBody);
|
||
|
|
}
|
||
|
|
}
|