agent/php/tests/Feature/Jobs/DeleteFromIndexTest.php
Snider 8d520adb5e feat(brain): add DeleteFromIndex job
Inverse of the EmbedMemory job (#56): removes a memory from Qdrant (and
the future Elasticsearch index) when brain_forget fires or a memory is
soft-deleted.

- php/Jobs/DeleteFromIndex.php — Laravel Job, 3 retries with backoff
- BrainService: qdrantDelete() private→public and now throws on HTTP
  failure (was silent Log::warning — wouldn't trigger Job retry)
- elasticDelete() stub added (fills in with the ES integration ticket)
- php/tests/Feature/Jobs/DeleteFromIndexTest.php — success + HTTP-failure
  paths via mocked Http

Co-authored-by: Codex <noreply@openai.com>

Closes tasks.lthn.sh/view.php?id=57
2026-04-23 12:55:45 +01:00

75 lines
2.5 KiB
PHP

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
use Core\Mod\Agentic\Jobs\DeleteFromIndex;
use Core\Mod\Agentic\Models\BrainMemory;
use Core\Mod\Agentic\Services\BrainService;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
function deleteFromIndexService(): BrainService
{
return new BrainService(
ollamaUrl: 'https://ollama.test',
qdrantUrl: 'https://qdrant.test',
collection: 'openbrain',
embeddingModel: 'embeddinggemma',
verifySsl: false,
);
}
test('DeleteFromIndex_handle_Good_deletes_memory_from_qdrant', function (): void {
$memoryId = Str::uuid()->toString();
Http::fake([
'https://qdrant.test/collections/openbrain/points/delete' => Http::response(['result' => ['status' => 'ok']]),
]);
(new DeleteFromIndex($memoryId))->handle(deleteFromIndexService());
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://qdrant.test/collections/openbrain/points/delete'
&& $request->method() === 'POST'
&& $request['points'] === [$memoryId]);
});
test('DeleteFromIndex_handle_Bad_deletes_soft_deleted_memory_from_qdrant', function (): void {
$workspace = createWorkspace();
Http::fake([
'https://qdrant.test/collections/openbrain/points/delete' => Http::response(['result' => ['status' => 'ok']]),
]);
$memory = BrainMemory::create([
'workspace_id' => $workspace->id,
'agent_id' => 'virgil',
'type' => 'observation',
'content' => 'Soft-deleted memories should still be removed from the index.',
'confidence' => 0.8,
]);
$memory->delete();
(new DeleteFromIndex($memory->id))->handle(deleteFromIndexService());
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://qdrant.test/collections/openbrain/points/delete'
&& $request->method() === 'POST'
&& $request['points'] === [$memory->id]);
});
test('DeleteFromIndex_handle_Ugly_throws_when_qdrant_delete_fails', function (): void {
$memoryId = Str::uuid()->toString();
Http::fake([
'https://qdrant.test/collections/openbrain/points/delete' => Http::response(['error' => 'unavailable'], 500),
]);
try {
(new DeleteFromIndex($memoryId))->handle(deleteFromIndexService());
$this->fail('Expected Qdrant failure to throw.');
} catch (RuntimeException $exception) {
expect($exception->getMessage())->toBe('Qdrant delete failed: 500');
}
});