|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { z } from 'zod/v4'; |
| 3 | +import { MockMemory } from './mock'; |
| 4 | + |
| 5 | +describe('MockMemory working memory merge semantics', () => { |
| 6 | + const threadId = 'thread-1'; |
| 7 | + const resourceId = 'resource-1'; |
| 8 | + |
| 9 | + async function setupMemory(useSchema: boolean) { |
| 10 | + const options: ConstructorParameters<typeof MockMemory>[0] = { |
| 11 | + enableWorkingMemory: true, |
| 12 | + }; |
| 13 | + |
| 14 | + if (useSchema) { |
| 15 | + options.options = { |
| 16 | + workingMemory: { |
| 17 | + enabled: true, |
| 18 | + schema: z.object({ |
| 19 | + name: z.string().optional(), |
| 20 | + age: z.number().optional(), |
| 21 | + location: z.string().optional(), |
| 22 | + }), |
| 23 | + }, |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + const memory = new MockMemory(options); |
| 28 | + |
| 29 | + // Create a thread so the tool doesn't error |
| 30 | + await memory.createThread({ threadId, resourceId }); |
| 31 | + |
| 32 | + return memory; |
| 33 | + } |
| 34 | + |
| 35 | + async function callUpdateTool(memory: MockMemory, input: string) { |
| 36 | + const config = (memory as any).getMergedThreadConfig(); |
| 37 | + const tools = memory.listTools(config); |
| 38 | + const tool = tools.updateWorkingMemory; |
| 39 | + if (!tool) throw new Error('updateWorkingMemory tool not found'); |
| 40 | + |
| 41 | + await (tool as any).execute({ memory: input }, { agent: { threadId, resourceId }, memory }); |
| 42 | + } |
| 43 | + |
| 44 | + it('replaces working memory entirely for template-based (no schema)', async () => { |
| 45 | + const memory = await setupMemory(false); |
| 46 | + |
| 47 | + await callUpdateTool(memory, JSON.stringify({ name: 'Alice', age: 30, location: 'NYC' })); |
| 48 | + await callUpdateTool(memory, JSON.stringify({ location: 'LA' })); |
| 49 | + |
| 50 | + const wm = await memory.getWorkingMemory({ threadId, resourceId }); |
| 51 | + const parsed = JSON.parse(wm!); |
| 52 | + expect(parsed).toEqual({ location: 'LA' }); |
| 53 | + expect(parsed.name).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('merges working memory for schema-based configs', async () => { |
| 57 | + const memory = await setupMemory(true); |
| 58 | + |
| 59 | + await callUpdateTool(memory, JSON.stringify({ name: 'Alice', age: 30, location: 'NYC' })); |
| 60 | + await callUpdateTool(memory, JSON.stringify({ location: 'LA' })); |
| 61 | + |
| 62 | + const wm = await memory.getWorkingMemory({ threadId, resourceId }); |
| 63 | + const parsed = JSON.parse(wm!); |
| 64 | + expect(parsed).toEqual({ name: 'Alice', age: 30, location: 'LA' }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('overwrites fields in schema-based merge when explicitly provided', async () => { |
| 68 | + const memory = await setupMemory(true); |
| 69 | + |
| 70 | + await callUpdateTool(memory, JSON.stringify({ name: 'Alice', age: 30 })); |
| 71 | + await callUpdateTool(memory, JSON.stringify({ name: 'Bob', age: 25 })); |
| 72 | + |
| 73 | + const wm = await memory.getWorkingMemory({ threadId, resourceId }); |
| 74 | + const parsed = JSON.parse(wm!); |
| 75 | + expect(parsed).toEqual({ name: 'Bob', age: 25 }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('handles first write with no existing data in schema mode', async () => { |
| 79 | + const memory = await setupMemory(true); |
| 80 | + |
| 81 | + await callUpdateTool(memory, JSON.stringify({ name: 'Alice' })); |
| 82 | + |
| 83 | + const wm = await memory.getWorkingMemory({ threadId, resourceId }); |
| 84 | + const parsed = JSON.parse(wm!); |
| 85 | + expect(parsed).toEqual({ name: 'Alice' }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('deep-merges nested objects in schema mode', async () => { |
| 89 | + const memory = new MockMemory({ |
| 90 | + enableWorkingMemory: true, |
| 91 | + options: { |
| 92 | + workingMemory: { |
| 93 | + enabled: true, |
| 94 | + schema: z.object({ |
| 95 | + user: z.object({ |
| 96 | + name: z.string().optional(), |
| 97 | + address: z |
| 98 | + .object({ |
| 99 | + city: z.string().optional(), |
| 100 | + state: z.string().optional(), |
| 101 | + }) |
| 102 | + .optional(), |
| 103 | + }), |
| 104 | + }), |
| 105 | + }, |
| 106 | + }, |
| 107 | + }); |
| 108 | + await memory.createThread({ threadId, resourceId }); |
| 109 | + |
| 110 | + await callUpdateTool(memory, JSON.stringify({ user: { name: 'Alice', address: { city: 'NYC', state: 'NY' } } })); |
| 111 | + await callUpdateTool(memory, JSON.stringify({ user: { address: { city: 'LA' } } })); |
| 112 | + |
| 113 | + const wm = await memory.getWorkingMemory({ threadId, resourceId }); |
| 114 | + const parsed = JSON.parse(wm!); |
| 115 | + // Deep merge: name preserved, state preserved, only city changed |
| 116 | + expect(parsed).toEqual({ user: { name: 'Alice', address: { city: 'LA', state: 'NY' } } }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('deletes keys set to null in schema mode', async () => { |
| 120 | + const memory = await setupMemory(true); |
| 121 | + |
| 122 | + await callUpdateTool(memory, JSON.stringify({ name: 'Alice', age: 30, location: 'NYC' })); |
| 123 | + await callUpdateTool(memory, JSON.stringify({ age: null })); |
| 124 | + |
| 125 | + const wm = await memory.getWorkingMemory({ threadId, resourceId }); |
| 126 | + const parsed = JSON.parse(wm!); |
| 127 | + // null deletes the key |
| 128 | + expect(parsed).toEqual({ name: 'Alice', location: 'NYC' }); |
| 129 | + expect(parsed.age).toBeUndefined(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('replaces arrays entirely in schema mode', async () => { |
| 133 | + const memory = new MockMemory({ |
| 134 | + enableWorkingMemory: true, |
| 135 | + options: { |
| 136 | + workingMemory: { |
| 137 | + enabled: true, |
| 138 | + schema: z.object({ |
| 139 | + tags: z.array(z.string()).optional(), |
| 140 | + count: z.number().optional(), |
| 141 | + }), |
| 142 | + }, |
| 143 | + }, |
| 144 | + }); |
| 145 | + await memory.createThread({ threadId, resourceId }); |
| 146 | + |
| 147 | + await callUpdateTool(memory, JSON.stringify({ tags: ['a', 'b', 'c'], count: 3 })); |
| 148 | + await callUpdateTool(memory, JSON.stringify({ tags: ['x'] })); |
| 149 | + |
| 150 | + const wm = await memory.getWorkingMemory({ threadId, resourceId }); |
| 151 | + const parsed = JSON.parse(wm!); |
| 152 | + // Arrays replace, count preserved |
| 153 | + expect(parsed).toEqual({ tags: ['x'], count: 3 }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments