From 969a7d41e78ec6f4fcc0fa333faa6483eec1fa5d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 17 Feb 2026 18:32:35 +0000 Subject: [PATCH] test(memory): add unit tests for unpinMemory Added unit tests to `test/memory-pinning.test.mjs` to cover the `unpinMemory` function. Tests verify: - Successful unpinning and cache clearing. - Handling of non-existent IDs (returns null) and cache clearing. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- test/memory-pinning.test.mjs | 71 +++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/test/memory-pinning.test.mjs b/test/memory-pinning.test.mjs index 2c6ae90..88869d3 100644 --- a/test/memory-pinning.test.mjs +++ b/test/memory-pinning.test.mjs @@ -1,6 +1,6 @@ import test from 'node:test'; import assert from 'node:assert/strict'; -import { getRelevantMemories, pinMemory } from '../src/services/memory/index.js'; +import { getRelevantMemories, pinMemory, unpinMemory } from '../src/services/memory/index.js'; test('pinMemory pins the memory and clears the cache', async () => { const records = [ @@ -70,3 +70,72 @@ test('pinMemory returns null for non-existent ID but still clears cache', async await getRelevantMemories({ agent_id: 'agent-b', repository }); assert.equal(listMemoriesCallCount, 2, 'Cache should be cleared even if ID not found'); }); + +test('unpinMemory unpins the memory and clears the cache', async () => { + const records = [ + { id: 'm1', agent_id: 'agent-c', type: 'event', tags: ['ops'], pinned: true, last_seen: 10, merged_into: null }, + ]; + + let listMemoriesCallCount = 0; + const repository = { + async listMemories() { + listMemoriesCallCount++; + return records; + }, + async getMemoryById(id) { + return records.find((record) => record.id === id) ?? null; + }, + async setPinned(id, pinned) { + const index = records.findIndex((record) => record.id === id); + if (index < 0) return null; + records[index] = { ...records[index], pinned }; + return records[index]; + }, + async updateMemoryUsage() { return null; } + }; + + // 1. Populate cache + await getRelevantMemories({ agent_id: 'agent-c', repository }); + assert.equal(listMemoriesCallCount, 1, 'Repository should be queried initially'); + + // 2. Verify cache hit + await getRelevantMemories({ agent_id: 'agent-c', repository }); + assert.equal(listMemoriesCallCount, 1, 'Repository should NOT be queried on cache hit'); + + // 3. Unpin memory + const updated = await unpinMemory('m1', { repository }); + assert.ok(updated); + assert.equal(updated.pinned, false); + assert.equal(records[0].pinned, false); + + // 4. Verify cache miss (cleared) + await getRelevantMemories({ agent_id: 'agent-c', repository }); + assert.equal(listMemoriesCallCount, 2, 'Repository should be queried again after cache clear'); +}); + +test('unpinMemory returns null for non-existent ID but still clears cache', async () => { + const records = []; + let listMemoriesCallCount = 0; + const repository = { + async listMemories() { + listMemoriesCallCount++; + return records; + }, + async setPinned(id, pinned) { + return null; + }, + async updateMemoryUsage() { return null; } + }; + + // 1. Populate cache (use different agent_id to avoid cache collision with previous test) + await getRelevantMemories({ agent_id: 'agent-d', repository }); + assert.equal(listMemoriesCallCount, 1); + + // 2. Unpin non-existent memory + const result = await unpinMemory('non-existent', { repository }); + assert.equal(result, null); + + // 3. Verify cache cleared + await getRelevantMemories({ agent_id: 'agent-d', repository }); + assert.equal(listMemoriesCallCount, 2, 'Cache should be cleared even if ID not found'); +});