|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { MemoryStore } from '../MemoryStore'; |
| 3 | +import { fakeEmbed } from './helpers/fakeEmbed'; |
| 4 | +import { mockClient } from './helpers/mockClient'; |
| 5 | + |
| 6 | +const now = Date.now(); |
| 7 | + |
| 8 | +interface HitSpec { |
| 9 | + importance: number; |
| 10 | + ageSeconds: number; |
| 11 | + source?: string; |
| 12 | +} |
| 13 | + |
| 14 | +function itemHit(id: string, spec: HitSpec): [string, string[]] { |
| 15 | + const created = now - spec.ageSeconds * 1000; |
| 16 | + const fields: Record<string, string> = { |
| 17 | + content: `c-${id}`, |
| 18 | + importance: String(spec.importance), |
| 19 | + created_at: String(created), |
| 20 | + last_accessed_at: String(created), |
| 21 | + access_count: '0', |
| 22 | + }; |
| 23 | + if (spec.source !== undefined) { |
| 24 | + fields.source = spec.source; |
| 25 | + } |
| 26 | + const flat: string[] = []; |
| 27 | + for (const [field, value] of Object.entries(fields)) { |
| 28 | + flat.push(field, value); |
| 29 | + } |
| 30 | + return [`mem:mem:${id}`, flat]; |
| 31 | +} |
| 32 | + |
| 33 | +function searchReply(hits: Array<[string, string[]]>): unknown[] { |
| 34 | + const out: unknown[] = [String(hits.length)]; |
| 35 | + for (const [key, flat] of hits) { |
| 36 | + out.push(key, flat); |
| 37 | + } |
| 38 | + return out; |
| 39 | +} |
| 40 | + |
| 41 | +function consolidatingClient(hits: Array<[string, string[]]>) { |
| 42 | + return mockClient((command, ...args) => { |
| 43 | + if (command === 'FT.SEARCH') { |
| 44 | + return searchReply(hits); |
| 45 | + } |
| 46 | + if (command === 'DEL') { |
| 47 | + return args.length; |
| 48 | + } |
| 49 | + return 'OK'; |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +function fieldValue(call: unknown[] | undefined, field: string): string | undefined { |
| 54 | + if (!call) { |
| 55 | + return undefined; |
| 56 | + } |
| 57 | + const idx = call.indexOf(field); |
| 58 | + return idx >= 0 ? (call[idx + 1] as string) : undefined; |
| 59 | +} |
| 60 | + |
| 61 | +describe('MemoryStore.consolidate', () => { |
| 62 | + it('summarizes matching candidates, writes a summary, deletes sources, returns counts', async () => { |
| 63 | + const summarize = vi.fn(async (items) => `summary of ${items.length}`); |
| 64 | + const client = consolidatingClient([ |
| 65 | + itemHit('a', { importance: 0.2, ageSeconds: 100000 }), |
| 66 | + itemHit('b', { importance: 0.3, ageSeconds: 200000 }), |
| 67 | + ]); |
| 68 | + const store = new MemoryStore({ client, name: 'mem', embedFn: fakeEmbed(8) }); |
| 69 | + |
| 70 | + const result = await store.consolidate({ |
| 71 | + namespace: 'u1', |
| 72 | + olderThanSeconds: 3600, |
| 73 | + maxImportance: 0.5, |
| 74 | + summarize, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(summarize).toHaveBeenCalledTimes(1); |
| 78 | + expect(summarize.mock.calls[0][0].map((i: { id: string }) => i.id)).toEqual(['a', 'b']); |
| 79 | + expect(result.consolidated).toBe(2); |
| 80 | + expect(result.created).toHaveLength(1); |
| 81 | + expect(result.deleted).toBe(2); |
| 82 | + |
| 83 | + const hset = client.call.mock.calls.find((c) => c[0] === 'HSET'); |
| 84 | + expect(fieldValue(hset, 'content')).toBe('summary of 2'); |
| 85 | + expect(fieldValue(hset, 'source')).toBe('summary'); |
| 86 | + expect(hset?.[1]).toBe(`mem:mem:${result.created[0]}`); |
| 87 | + |
| 88 | + const del = client.call.mock.calls.find((c) => c[0] === 'DEL'); |
| 89 | + expect(del?.slice(1).sort()).toEqual(['mem:mem:a', 'mem:mem:b']); |
| 90 | + }); |
| 91 | + |
| 92 | + it('selects only candidates older than olderThanSeconds', async () => { |
| 93 | + const summarize = vi.fn(async () => 'summary'); |
| 94 | + const client = consolidatingClient([ |
| 95 | + itemHit('old', { importance: 0.2, ageSeconds: 100000 }), |
| 96 | + itemHit('recent', { importance: 0.2, ageSeconds: 10 }), |
| 97 | + ]); |
| 98 | + const store = new MemoryStore({ client, name: 'mem', embedFn: fakeEmbed(8) }); |
| 99 | + |
| 100 | + const result = await store.consolidate({ olderThanSeconds: 3600, summarize }); |
| 101 | + |
| 102 | + expect(summarize.mock.calls[0][0].map((i: { id: string }) => i.id)).toEqual(['old']); |
| 103 | + expect(result.consolidated).toBe(1); |
| 104 | + const del = client.call.mock.calls.find((c) => c[0] === 'DEL'); |
| 105 | + expect(del?.slice(1)).toEqual(['mem:mem:old']); |
| 106 | + }); |
| 107 | + |
| 108 | + it('selects only candidates at or below maxImportance', async () => { |
| 109 | + const summarize = vi.fn(async () => 'summary'); |
| 110 | + const client = consolidatingClient([ |
| 111 | + itemHit('low', { importance: 0.2, ageSeconds: 100000 }), |
| 112 | + itemHit('high', { importance: 0.9, ageSeconds: 100000 }), |
| 113 | + ]); |
| 114 | + const store = new MemoryStore({ client, name: 'mem', embedFn: fakeEmbed(8) }); |
| 115 | + |
| 116 | + const result = await store.consolidate({ maxImportance: 0.5, summarize }); |
| 117 | + |
| 118 | + expect(summarize.mock.calls[0][0].map((i: { id: string }) => i.id)).toEqual(['low']); |
| 119 | + expect(result.consolidated).toBe(1); |
| 120 | + }); |
| 121 | + |
| 122 | + it('writes the summary scoped to the request at summaryImportance', async () => { |
| 123 | + const summarize = vi.fn(async () => 'merged'); |
| 124 | + const client = consolidatingClient([itemHit('a', { importance: 0.1, ageSeconds: 100000 })]); |
| 125 | + const store = new MemoryStore({ client, name: 'mem', embedFn: fakeEmbed(8) }); |
| 126 | + |
| 127 | + await store.consolidate({ namespace: 'u1', summarize, summaryImportance: 0.9 }); |
| 128 | + |
| 129 | + const hset = client.call.mock.calls.find((c) => c[0] === 'HSET'); |
| 130 | + expect(fieldValue(hset, 'importance')).toBe('0.9'); |
| 131 | + expect(fieldValue(hset, 'namespace')).toBe('u1'); |
| 132 | + expect(fieldValue(hset, 'source')).toBe('summary'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('keeps sources when deleteSources is false', async () => { |
| 136 | + const summarize = vi.fn(async () => 'summary'); |
| 137 | + const client = consolidatingClient([ |
| 138 | + itemHit('a', { importance: 0.2, ageSeconds: 100000 }), |
| 139 | + itemHit('b', { importance: 0.2, ageSeconds: 100000 }), |
| 140 | + ]); |
| 141 | + const store = new MemoryStore({ client, name: 'mem', embedFn: fakeEmbed(8) }); |
| 142 | + |
| 143 | + const result = await store.consolidate({ |
| 144 | + summarize, |
| 145 | + deleteSources: false, |
| 146 | + olderThanSeconds: 3600, |
| 147 | + }); |
| 148 | + |
| 149 | + expect(result.consolidated).toBe(2); |
| 150 | + expect(result.created).toHaveLength(1); |
| 151 | + expect(result.deleted).toBe(0); |
| 152 | + expect(client.call.mock.calls.some((c) => c[0] === 'DEL')).toBe(false); |
| 153 | + }); |
| 154 | + |
| 155 | + it('returns zeros and does not summarize or write when nothing matches', async () => { |
| 156 | + const summarize = vi.fn(async () => 'summary'); |
| 157 | + const client = consolidatingClient([]); |
| 158 | + const store = new MemoryStore({ client, name: 'mem', embedFn: fakeEmbed(8) }); |
| 159 | + |
| 160 | + const result = await store.consolidate({ olderThanSeconds: 3600, summarize }); |
| 161 | + |
| 162 | + expect(summarize).not.toHaveBeenCalled(); |
| 163 | + expect(result).toEqual({ consolidated: 0, created: [], deleted: 0 }); |
| 164 | + expect(client.call.mock.calls.some((c) => c[0] === 'HSET')).toBe(false); |
| 165 | + expect(client.call.mock.calls.some((c) => c[0] === 'DEL')).toBe(false); |
| 166 | + }); |
| 167 | + |
| 168 | + it('throws when given no scope, tags, or selection criteria (prevents whole-store consolidation)', async () => { |
| 169 | + const summarize = vi.fn(async () => 'summary'); |
| 170 | + const store = new MemoryStore({ client: mockClient(), name: 'mem', embedFn: fakeEmbed(8) }); |
| 171 | + |
| 172 | + await expect(store.consolidate({ summarize })).rejects.toThrow(/scope|criteria/i); |
| 173 | + expect(summarize).not.toHaveBeenCalled(); |
| 174 | + }); |
| 175 | + |
| 176 | + it('defaults summaryImportance to 0.7 and deletes sources by default', async () => { |
| 177 | + const summarize = vi.fn(async () => 'summary'); |
| 178 | + const client = consolidatingClient([itemHit('a', { importance: 0.2, ageSeconds: 100000 })]); |
| 179 | + const store = new MemoryStore({ client, name: 'mem', embedFn: fakeEmbed(8) }); |
| 180 | + |
| 181 | + const result = await store.consolidate({ summarize, olderThanSeconds: 3600 }); |
| 182 | + |
| 183 | + const hset = client.call.mock.calls.find((c) => c[0] === 'HSET'); |
| 184 | + expect(fieldValue(hset, 'importance')).toBe('0.7'); |
| 185 | + expect(result.deleted).toBe(1); |
| 186 | + }); |
| 187 | +}); |
0 commit comments