@synth-coder/memhub 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/.eslintrc.cjs +46 -0
  2. package/.github/workflows/ci.yml +74 -0
  3. package/.iflow/commands/opsx-apply.md +152 -0
  4. package/.iflow/commands/opsx-archive.md +157 -0
  5. package/.iflow/commands/opsx-explore.md +173 -0
  6. package/.iflow/commands/opsx-propose.md +106 -0
  7. package/.iflow/skills/openspec-apply-change/SKILL.md +156 -0
  8. package/.iflow/skills/openspec-archive-change/SKILL.md +114 -0
  9. package/.iflow/skills/openspec-explore/SKILL.md +288 -0
  10. package/.iflow/skills/openspec-propose/SKILL.md +110 -0
  11. package/.prettierrc +11 -0
  12. package/README.md +171 -0
  13. package/README.zh-CN.md +169 -0
  14. package/dist/src/contracts/index.d.ts +7 -0
  15. package/dist/src/contracts/index.d.ts.map +1 -0
  16. package/dist/src/contracts/index.js +10 -0
  17. package/dist/src/contracts/index.js.map +1 -0
  18. package/dist/src/contracts/mcp.d.ts +194 -0
  19. package/dist/src/contracts/mcp.d.ts.map +1 -0
  20. package/dist/src/contracts/mcp.js +112 -0
  21. package/dist/src/contracts/mcp.js.map +1 -0
  22. package/dist/src/contracts/schemas.d.ts +1153 -0
  23. package/dist/src/contracts/schemas.d.ts.map +1 -0
  24. package/dist/src/contracts/schemas.js +246 -0
  25. package/dist/src/contracts/schemas.js.map +1 -0
  26. package/dist/src/contracts/types.d.ts +328 -0
  27. package/dist/src/contracts/types.d.ts.map +1 -0
  28. package/dist/src/contracts/types.js +30 -0
  29. package/dist/src/contracts/types.js.map +1 -0
  30. package/dist/src/index.d.ts +8 -0
  31. package/dist/src/index.d.ts.map +1 -0
  32. package/dist/src/index.js +8 -0
  33. package/dist/src/index.js.map +1 -0
  34. package/dist/src/server/index.d.ts +5 -0
  35. package/dist/src/server/index.d.ts.map +1 -0
  36. package/dist/src/server/index.js +5 -0
  37. package/dist/src/server/index.js.map +1 -0
  38. package/dist/src/server/mcp-server.d.ts +80 -0
  39. package/dist/src/server/mcp-server.d.ts.map +1 -0
  40. package/dist/src/server/mcp-server.js +263 -0
  41. package/dist/src/server/mcp-server.js.map +1 -0
  42. package/dist/src/services/index.d.ts +5 -0
  43. package/dist/src/services/index.d.ts.map +1 -0
  44. package/dist/src/services/index.js +5 -0
  45. package/dist/src/services/index.js.map +1 -0
  46. package/dist/src/services/memory-service.d.ts +105 -0
  47. package/dist/src/services/memory-service.d.ts.map +1 -0
  48. package/dist/src/services/memory-service.js +447 -0
  49. package/dist/src/services/memory-service.js.map +1 -0
  50. package/dist/src/storage/frontmatter-parser.d.ts +69 -0
  51. package/dist/src/storage/frontmatter-parser.d.ts.map +1 -0
  52. package/dist/src/storage/frontmatter-parser.js +207 -0
  53. package/dist/src/storage/frontmatter-parser.js.map +1 -0
  54. package/dist/src/storage/index.d.ts +6 -0
  55. package/dist/src/storage/index.d.ts.map +1 -0
  56. package/dist/src/storage/index.js +6 -0
  57. package/dist/src/storage/index.js.map +1 -0
  58. package/dist/src/storage/markdown-storage.d.ts +76 -0
  59. package/dist/src/storage/markdown-storage.d.ts.map +1 -0
  60. package/dist/src/storage/markdown-storage.js +193 -0
  61. package/dist/src/storage/markdown-storage.js.map +1 -0
  62. package/dist/src/utils/index.d.ts +5 -0
  63. package/dist/src/utils/index.d.ts.map +1 -0
  64. package/dist/src/utils/index.js +5 -0
  65. package/dist/src/utils/index.js.map +1 -0
  66. package/dist/src/utils/slugify.d.ts +24 -0
  67. package/dist/src/utils/slugify.d.ts.map +1 -0
  68. package/dist/src/utils/slugify.js +56 -0
  69. package/dist/src/utils/slugify.js.map +1 -0
  70. package/docs/architecture.md +349 -0
  71. package/docs/contracts.md +119 -0
  72. package/docs/prompt-template.md +79 -0
  73. package/docs/proposal-close-gates.md +58 -0
  74. package/docs/tool-calling-policy.md +107 -0
  75. package/package.json +53 -0
  76. package/src/contracts/index.ts +12 -0
  77. package/src/contracts/mcp.ts +303 -0
  78. package/src/contracts/schemas.ts +311 -0
  79. package/src/contracts/types.ts +414 -0
  80. package/src/index.ts +8 -0
  81. package/src/server/index.ts +5 -0
  82. package/src/server/mcp-server.ts +352 -0
  83. package/src/services/index.ts +5 -0
  84. package/src/services/memory-service.ts +548 -0
  85. package/src/storage/frontmatter-parser.ts +243 -0
  86. package/src/storage/index.ts +6 -0
  87. package/src/storage/markdown-storage.ts +236 -0
  88. package/src/utils/index.ts +5 -0
  89. package/src/utils/slugify.ts +63 -0
  90. package/test/contracts/schemas.test.ts +313 -0
  91. package/test/contracts/types.test.ts +21 -0
  92. package/test/frontmatter-parser-more.test.ts +94 -0
  93. package/test/server/mcp-server-internals.test.ts +257 -0
  94. package/test/server/mcp-server.test.ts +97 -0
  95. package/test/services/memory-service-edge.test.ts +248 -0
  96. package/test/services/memory-service.test.ts +279 -0
  97. package/test/storage/frontmatter-parser.test.ts +223 -0
  98. package/test/storage/markdown-storage.test.ts +217 -0
  99. package/test/storage/storage-edge.test.ts +238 -0
  100. package/test/utils/slugify-edge.test.ts +94 -0
  101. package/test/utils/slugify.test.ts +68 -0
  102. package/tsconfig.json +26 -0
  103. package/tsconfig.test.json +8 -0
  104. package/vitest.config.ts +27 -0
@@ -0,0 +1,97 @@
1
+ /**
2
+ * MCP Server Tests
3
+ * Tests for the McpServer class
4
+ */
5
+
6
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
7
+ import { McpServer } from '../../src/server/mcp-server.js';
8
+ import {
9
+ TOOL_DEFINITIONS,
10
+ SERVER_INFO,
11
+ ERROR_CODES,
12
+ MCP_PROTOCOL_VERSION,
13
+ } from '../../src/contracts/mcp.js';
14
+ import { mkdtempSync, rmSync } from 'fs';
15
+ import { tmpdir } from 'os';
16
+ import { join } from 'path';
17
+
18
+ describe('McpServer', () => {
19
+ let tempDir: string;
20
+
21
+ beforeEach(() => {
22
+ tempDir = mkdtempSync(join(tmpdir(), 'memhub-server-test-'));
23
+ process.env.MEMHUB_STORAGE_PATH = tempDir;
24
+ });
25
+
26
+ afterEach(() => {
27
+ rmSync(tempDir, { recursive: true, force: true });
28
+ delete process.env.MEMHUB_STORAGE_PATH;
29
+ });
30
+
31
+ describe('constructor', () => {
32
+ it('should create server instance', () => {
33
+ const server = new McpServer();
34
+ expect(server).toBeDefined();
35
+ });
36
+ });
37
+
38
+ describe('TOOL_DEFINITIONS', () => {
39
+ it('should have 2 tools defined', () => {
40
+ expect(TOOL_DEFINITIONS).toHaveLength(2);
41
+ });
42
+
43
+ it('should include STM-first tools', () => {
44
+ const toolNames = TOOL_DEFINITIONS.map(t => t.name);
45
+ expect(toolNames).toContain('memory_load');
46
+ expect(toolNames).toContain('memory_update');
47
+ });
48
+
49
+ it('should have descriptions for all tools', () => {
50
+ for (const tool of TOOL_DEFINITIONS) {
51
+ expect(tool.description).toBeDefined();
52
+ expect(tool.description.length).toBeGreaterThan(0);
53
+ }
54
+ });
55
+
56
+ it('should have input schemas for all tools', () => {
57
+ for (const tool of TOOL_DEFINITIONS) {
58
+ expect(tool.inputSchema).toBeDefined();
59
+ expect(tool.inputSchema.type).toBe('object');
60
+ }
61
+ });
62
+ });
63
+
64
+ describe('ServerInfo', () => {
65
+ it('should have correct server name', () => {
66
+ expect(SERVER_INFO.name).toBe('memhub');
67
+ });
68
+
69
+ it('should have version', () => {
70
+ expect(SERVER_INFO.version).toBeDefined();
71
+ expect(SERVER_INFO.version).toMatch(/^\d+\.\d+\.\d+/);
72
+ });
73
+ });
74
+
75
+ describe('ErrorCodes', () => {
76
+ it('should have standard MCP error codes', () => {
77
+ expect(ERROR_CODES.PARSE_ERROR).toBe(-32700);
78
+ expect(ERROR_CODES.INVALID_REQUEST).toBe(-32600);
79
+ expect(ERROR_CODES.METHOD_NOT_FOUND).toBe(-32601);
80
+ expect(ERROR_CODES.INVALID_PARAMS).toBe(-32602);
81
+ expect(ERROR_CODES.INTERNAL_ERROR).toBe(-32603);
82
+ });
83
+
84
+ it('should have custom MemHub error codes', () => {
85
+ expect(ERROR_CODES.NOT_FOUND).toBe(-32001);
86
+ expect(ERROR_CODES.STORAGE_ERROR).toBe(-32002);
87
+ expect(ERROR_CODES.VALIDATION_ERROR).toBe(-32003);
88
+ expect(ERROR_CODES.DUPLICATE_ERROR).toBe(-32004);
89
+ });
90
+ });
91
+
92
+ describe('Protocol', () => {
93
+ it('should have correct protocol version', () => {
94
+ expect(MCP_PROTOCOL_VERSION).toBe('2024-11-05');
95
+ });
96
+ });
97
+ });
@@ -0,0 +1,248 @@
1
+ /**
2
+ * Memory Service Edge Case Tests
3
+ * Additional tests for better coverage
4
+ */
5
+
6
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
7
+ import { mkdtempSync, rmSync } from 'fs';
8
+ import { tmpdir } from 'os';
9
+ import { join } from 'path';
10
+ import { MemoryService, ServiceError } from '../../src/services/memory-service.js';
11
+
12
+ describe('MemoryService Edge Cases', () => {
13
+ let tempDir: string;
14
+ let memoryService: MemoryService;
15
+
16
+ beforeEach(() => {
17
+ tempDir = mkdtempSync(join(tmpdir(), 'memhub-edge-test-'));
18
+ memoryService = new MemoryService({ storagePath: tempDir });
19
+ });
20
+
21
+ afterEach(() => {
22
+ rmSync(tempDir, { recursive: true, force: true });
23
+ });
24
+
25
+ describe('create with edge cases', () => {
26
+ it('should handle empty content', async () => {
27
+ const result = await memoryService.create({
28
+ title: 'Empty Content',
29
+ content: '',
30
+ });
31
+ expect(result.memory.content).toBe('');
32
+ });
33
+
34
+ it('should handle very long title', async () => {
35
+ const longTitle = 'A'.repeat(200);
36
+ const result = await memoryService.create({
37
+ title: longTitle,
38
+ content: 'Content',
39
+ });
40
+ expect(result.memory.title).toBe(longTitle);
41
+ });
42
+
43
+ it('should handle special characters in title', async () => {
44
+ const result = await memoryService.create({
45
+ title: 'Title with @#$%^&*() special chars!',
46
+ content: 'Content',
47
+ });
48
+ expect(result.memory.title).toBe('Title with @#$%^&*() special chars!');
49
+ });
50
+
51
+ it('should handle many tags', async () => {
52
+ const tags = Array.from({ length: 20 }, (_, i) => `tag${i}`);
53
+ const result = await memoryService.create({
54
+ title: 'Many Tags',
55
+ content: 'Content',
56
+ tags,
57
+ });
58
+ expect(result.memory.tags).toHaveLength(20);
59
+ });
60
+ });
61
+
62
+ describe('update edge cases', () => {
63
+ it('should update only tags', async () => {
64
+ const created = await memoryService.create({
65
+ title: 'Title',
66
+ content: 'Content',
67
+ tags: ['old'],
68
+ });
69
+ const updated = await memoryService.update({
70
+ id: created.id,
71
+ tags: ['new1', 'new2'],
72
+ });
73
+ expect(updated.memory.tags).toEqual(['new1', 'new2']);
74
+ expect(updated.memory.title).toBe('Title');
75
+ expect(updated.memory.content).toBe('Content');
76
+ });
77
+
78
+ it('should update only importance', async () => {
79
+ const created = await memoryService.create({
80
+ title: 'Title',
81
+ content: 'Content',
82
+ importance: 1,
83
+ });
84
+ const updated = await memoryService.update({
85
+ id: created.id,
86
+ importance: 5,
87
+ });
88
+ expect(updated.memory.importance).toBe(5);
89
+ });
90
+
91
+ it('should update only category', async () => {
92
+ const created = await memoryService.create({
93
+ title: 'Title',
94
+ content: 'Content',
95
+ category: 'old-category',
96
+ });
97
+ const updated = await memoryService.update({
98
+ id: created.id,
99
+ category: 'new-category',
100
+ });
101
+ expect(updated.memory.category).toBe('new-category');
102
+ });
103
+ });
104
+
105
+ describe('list with various filters', () => {
106
+ beforeEach(async () => {
107
+ // Create test data with various dates
108
+ const baseDate = new Date('2024-01-15');
109
+ for (let i = 0; i < 5; i++) {
110
+ const date = new Date(baseDate);
111
+ date.setDate(date.getDate() + i);
112
+ await memoryService.create({
113
+ title: `Memory ${i}`,
114
+ content: 'Content',
115
+ category: i % 2 === 0 ? 'even' : 'odd',
116
+ tags: [`tag${i}`, 'common'],
117
+ });
118
+ }
119
+ });
120
+
121
+ it('should filter by date range', async () => {
122
+ // The created memories will have current timestamp, so we need to use a wide range
123
+ const now = new Date();
124
+ const yesterday = new Date(now);
125
+ yesterday.setDate(yesterday.getDate() - 1);
126
+ const tomorrow = new Date(now);
127
+ tomorrow.setDate(tomorrow.getDate() + 1);
128
+
129
+ const result = await memoryService.list({
130
+ fromDate: yesterday.toISOString(),
131
+ toDate: tomorrow.toISOString(),
132
+ });
133
+ expect(result.memories.length).toBeGreaterThan(0);
134
+ });
135
+
136
+ it('should handle empty result with strict filters', async () => {
137
+ const result = await memoryService.list({
138
+ category: 'non-existent',
139
+ });
140
+ expect(result.memories).toHaveLength(0);
141
+ expect(result.total).toBe(0);
142
+ expect(result.hasMore).toBe(false);
143
+ });
144
+
145
+ it('should sort by importance', async () => {
146
+ await memoryService.create({
147
+ title: 'High Importance',
148
+ content: 'Content',
149
+ importance: 5,
150
+ });
151
+ await memoryService.create({
152
+ title: 'Low Importance',
153
+ content: 'Content',
154
+ importance: 1,
155
+ });
156
+
157
+ const result = await memoryService.list({
158
+ sortBy: 'importance',
159
+ sortOrder: 'desc',
160
+ });
161
+ expect(result.memories[0].importance).toBe(5);
162
+ });
163
+
164
+ it('should handle pagination across multiple pages', async () => {
165
+ const page1 = await memoryService.list({ limit: 2, offset: 0 });
166
+ expect(page1.memories).toHaveLength(2);
167
+ expect(page1.hasMore).toBe(true);
168
+
169
+ const page2 = await memoryService.list({ limit: 2, offset: 2 });
170
+ expect(page2.memories).toHaveLength(2);
171
+ expect(page2.hasMore).toBe(true);
172
+
173
+ const page3 = await memoryService.list({ limit: 2, offset: 4 });
174
+ expect(page3.memories.length).toBeGreaterThanOrEqual(1);
175
+ });
176
+ });
177
+
178
+ describe('search edge cases', () => {
179
+ beforeEach(async () => {
180
+ await memoryService.create({
181
+ title: 'Project Alpha',
182
+ content: 'This is about the alpha project development.',
183
+ tags: ['alpha', 'dev'],
184
+ });
185
+ await memoryService.create({
186
+ title: 'Project Beta',
187
+ content: 'Beta testing is in progress.',
188
+ tags: ['beta', 'testing'],
189
+ });
190
+ });
191
+
192
+ it('should search with case insensitivity', async () => {
193
+ const result = await memoryService.search({ query: 'ALPHA' });
194
+ expect(result.results.length).toBeGreaterThan(0);
195
+ });
196
+
197
+ it('should handle search with no results', async () => {
198
+ const result = await memoryService.search({ query: 'xyznonexistent' });
199
+ expect(result.results).toHaveLength(0);
200
+ expect(result.total).toBe(0);
201
+ });
202
+
203
+ it('should limit search results', async () => {
204
+ const result = await memoryService.search({ query: 'project', limit: 1 });
205
+ expect(result.results.length).toBeLessThanOrEqual(1);
206
+ });
207
+
208
+ it('should search with category filter', async () => {
209
+ await memoryService.create({
210
+ title: 'Work Item',
211
+ content: 'Work content',
212
+ category: 'work',
213
+ });
214
+ const result = await memoryService.search({
215
+ query: 'work',
216
+ category: 'work',
217
+ });
218
+ expect(result.results.length).toBeGreaterThan(0);
219
+ });
220
+ });
221
+
222
+ describe('getCategories and getTags edge cases', () => {
223
+ it('should return sorted categories', async () => {
224
+ await memoryService.create({ title: 'A', content: 'C', category: 'zebra' });
225
+ await memoryService.create({ title: 'B', content: 'C', category: 'alpha' });
226
+ await memoryService.create({ title: 'C', content: 'C', category: 'beta' });
227
+
228
+ const result = await memoryService.getCategories();
229
+ expect(result.categories).toEqual(['alpha', 'beta', 'zebra']);
230
+ });
231
+
232
+ it('should return sorted tags', async () => {
233
+ await memoryService.create({ title: 'A', content: 'C', tags: ['zebra', 'alpha'] });
234
+ await memoryService.create({ title: 'B', content: 'C', tags: ['beta'] });
235
+
236
+ const result = await memoryService.getTags();
237
+ expect(result.tags).toEqual(['alpha', 'beta', 'zebra']);
238
+ });
239
+
240
+ it('should handle duplicate tags across memories', async () => {
241
+ await memoryService.create({ title: 'A', content: 'C', tags: ['shared'] });
242
+ await memoryService.create({ title: 'B', content: 'C', tags: ['shared'] });
243
+
244
+ const result = await memoryService.getTags();
245
+ expect(result.tags).toEqual(['shared']);
246
+ });
247
+ });
248
+ });
@@ -0,0 +1,279 @@
1
+ /**
2
+ * Memory Service Tests
3
+ * Tests for the MemoryService class
4
+ */
5
+
6
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
7
+ import { mkdtempSync, rmSync } from 'fs';
8
+ import { tmpdir } from 'os';
9
+ import { join } from 'path';
10
+ import { MemoryService, ServiceError } from '../../src/services/memory-service.js';
11
+ import type {
12
+ CreateMemoryInput,
13
+ UpdateMemoryInput,
14
+ ListMemoryInput,
15
+ SearchMemoryInput,
16
+ } from '../../src/contracts/types.js';
17
+
18
+ describe('MemoryService', () => {
19
+ let tempDir: string;
20
+ let memoryService: MemoryService;
21
+
22
+ beforeEach(() => {
23
+ tempDir = mkdtempSync(join(tmpdir(), 'memhub-test-'));
24
+ memoryService = new MemoryService({ storagePath: tempDir });
25
+ });
26
+
27
+ afterEach(() => {
28
+ rmSync(tempDir, { recursive: true, force: true });
29
+ });
30
+
31
+ describe('create', () => {
32
+ it('should create a new memory with generated ID', async () => {
33
+ const input: CreateMemoryInput = {
34
+ title: 'Test Memory',
35
+ content: 'This is a test memory',
36
+ };
37
+
38
+ const result = await memoryService.create(input);
39
+ expect(result.id).toBeDefined();
40
+ expect(result.id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
41
+ });
42
+
43
+ it('should create memory file with correct format', async () => {
44
+ const input: CreateMemoryInput = {
45
+ title: 'Test Memory',
46
+ content: 'This is a test memory',
47
+ tags: ['test', 'memory'],
48
+ category: 'testing',
49
+ importance: 4,
50
+ };
51
+
52
+ const result = await memoryService.create(input);
53
+ const { readFileSync } = await import('fs');
54
+ const fileContent = readFileSync(result.filePath, 'utf-8');
55
+ expect(fileContent).toContain('---');
56
+ expect(fileContent).toContain('id:');
57
+ expect(fileContent).toContain('# Test Memory');
58
+ });
59
+
60
+ it('should set timestamps on creation', async () => {
61
+ const input: CreateMemoryInput = {
62
+ title: 'Test Memory',
63
+ content: 'Content',
64
+ };
65
+
66
+ const before = new Date().toISOString();
67
+ const result = await memoryService.create(input);
68
+ const after = new Date().toISOString();
69
+ expect(result.memory.createdAt >= before).toBe(true);
70
+ expect(result.memory.createdAt <= after).toBe(true);
71
+ expect(result.memory.updatedAt).toBe(result.memory.createdAt);
72
+ });
73
+
74
+ it('should apply default values for optional fields', async () => {
75
+ const input: CreateMemoryInput = {
76
+ title: 'Test Memory',
77
+ content: 'Content',
78
+ };
79
+
80
+ const result = await memoryService.create(input);
81
+ expect(result.memory.tags).toEqual([]);
82
+ expect(result.memory.category).toBe('general');
83
+ expect(result.memory.importance).toBe(3);
84
+ });
85
+
86
+ it('should generate URL-friendly filename from title', async () => {
87
+ const input: CreateMemoryInput = {
88
+ title: 'Hello World Test',
89
+ content: 'Content',
90
+ };
91
+
92
+ const result = await memoryService.create(input);
93
+ expect(result.filePath).toContain('hello-world-test');
94
+ });
95
+ });
96
+
97
+ describe('read', () => {
98
+ it('should read existing memory by ID', async () => {
99
+ const created = await memoryService.create({ title: 'Test', content: 'Content' });
100
+ const read = await memoryService.read({ id: created.id });
101
+ expect(read.memory.id).toBe(created.id);
102
+ expect(read.memory.title).toBe('Test');
103
+ });
104
+
105
+ it('should throw NOT_FOUND error for non-existent ID', async () => {
106
+ await expect(
107
+ memoryService.read({ id: '550e8400-e29b-41d4-a716-446655440000' })
108
+ ).rejects.toThrow(ServiceError);
109
+ });
110
+ });
111
+
112
+ describe('update', () => {
113
+ it('should update memory title', async () => {
114
+ const created = await memoryService.create({ title: 'Old Title', content: 'Content' });
115
+ const updated = await memoryService.update({ id: created.id, title: 'New Title' });
116
+ expect(updated.memory.title).toBe('New Title');
117
+ expect(updated.memory.content).toBe('Content');
118
+ });
119
+
120
+ it('should update memory content', async () => {
121
+ const created = await memoryService.create({ title: 'Title', content: 'Old Content' });
122
+ const updated = await memoryService.update({ id: created.id, content: 'New Content' });
123
+ expect(updated.memory.content).toBe('New Content');
124
+ });
125
+
126
+ it('should update updatedAt timestamp', async () => {
127
+ const created = await memoryService.create({ title: 'Title', content: 'Content' });
128
+ await new Promise(resolve => setTimeout(resolve, 10));
129
+ const updated = await memoryService.update({ id: created.id, title: 'New Title' });
130
+ expect(new Date(updated.memory.updatedAt).getTime()).toBeGreaterThan(
131
+ new Date(created.memory.updatedAt).getTime()
132
+ );
133
+ });
134
+
135
+ it('should not change createdAt timestamp', async () => {
136
+ const created = await memoryService.create({ title: 'Title', content: 'Content' });
137
+ const updated = await memoryService.update({ id: created.id, title: 'New Title' });
138
+ expect(updated.memory.createdAt).toBe(created.memory.createdAt);
139
+ });
140
+
141
+ it('should throw NOT_FOUND error for non-existent ID', async () => {
142
+ await expect(
143
+ memoryService.update({ id: '550e8400-e29b-41d4-a716-446655440000', title: 'New' })
144
+ ).rejects.toThrow(ServiceError);
145
+ });
146
+ });
147
+
148
+ describe('delete', () => {
149
+ it('should delete existing memory', async () => {
150
+ const created = await memoryService.create({ title: 'To Delete', content: 'Content' });
151
+ const result = await memoryService.delete({ id: created.id });
152
+ expect(result.success).toBe(true);
153
+ await expect(memoryService.read({ id: created.id })).rejects.toThrow(ServiceError);
154
+ });
155
+
156
+ it('should return file path of deleted memory', async () => {
157
+ const created = await memoryService.create({ title: 'To Delete', content: 'Content' });
158
+ const result = await memoryService.delete({ id: created.id });
159
+ expect(result.filePath).toBe(created.filePath);
160
+ });
161
+
162
+ it('should throw NOT_FOUND error for non-existent ID', async () => {
163
+ await expect(
164
+ memoryService.delete({ id: '550e8400-e29b-41d4-a716-446655440000' })
165
+ ).rejects.toThrow(ServiceError);
166
+ });
167
+ });
168
+
169
+ describe('list', () => {
170
+ beforeEach(async () => {
171
+ await memoryService.create({ title: 'Work 1', content: 'Content', category: 'work', tags: ['project'] });
172
+ await memoryService.create({ title: 'Work 2', content: 'Content', category: 'work', tags: ['meeting'] });
173
+ await memoryService.create({ title: 'Personal', content: 'Content', category: 'personal' });
174
+ });
175
+
176
+ it('should list all memories', async () => {
177
+ const result = await memoryService.list({});
178
+ expect(result.memories).toHaveLength(3);
179
+ expect(result.total).toBe(3);
180
+ expect(result.hasMore).toBe(false);
181
+ });
182
+
183
+ it('should filter by category', async () => {
184
+ const result = await memoryService.list({ category: 'work' });
185
+ expect(result.memories).toHaveLength(2);
186
+ expect(result.total).toBe(2);
187
+ });
188
+
189
+ it('should filter by tags', async () => {
190
+ const result = await memoryService.list({ tags: ['project'] });
191
+ expect(result.memories).toHaveLength(1);
192
+ expect(result.memories[0].title).toBe('Work 1');
193
+ });
194
+
195
+ it('should support pagination', async () => {
196
+ const result = await memoryService.list({ limit: 2, offset: 0 });
197
+ expect(result.memories).toHaveLength(2);
198
+ expect(result.hasMore).toBe(true);
199
+ });
200
+
201
+ it('should sort by specified field', async () => {
202
+ const result = await memoryService.list({ sortBy: 'title', sortOrder: 'asc' });
203
+ expect(result.memories[0].title).toBe('Personal');
204
+ });
205
+ });
206
+
207
+ describe('search', () => {
208
+ beforeEach(async () => {
209
+ await memoryService.create({
210
+ title: 'Project Planning',
211
+ content: 'We need to plan the project timeline and resources.',
212
+ tags: ['planning'],
213
+ });
214
+ await memoryService.create({
215
+ title: 'Meeting Notes',
216
+ content: 'Discussed project requirements and timeline.',
217
+ tags: ['meeting'],
218
+ });
219
+ });
220
+
221
+ it('should search in title', async () => {
222
+ const result = await memoryService.search({ query: 'planning' });
223
+ expect(result.results.length).toBeGreaterThan(0);
224
+ expect(result.results[0].memory.title).toBe('Project Planning');
225
+ });
226
+
227
+ it('should search in content', async () => {
228
+ const result = await memoryService.search({ query: 'timeline' });
229
+ expect(result.results.length).toBeGreaterThan(0);
230
+ });
231
+
232
+ it('should search in tags', async () => {
233
+ const result = await memoryService.search({ query: 'meeting' });
234
+ expect(result.results.length).toBeGreaterThan(0);
235
+ });
236
+
237
+ it('should return match snippets', async () => {
238
+ const result = await memoryService.search({ query: 'project' });
239
+ expect(result.results[0].matches.length).toBeGreaterThan(0);
240
+ });
241
+
242
+ it('should support multiple keywords', async () => {
243
+ const result = await memoryService.search({ query: 'project timeline' });
244
+ expect(result.results.length).toBeGreaterThan(0);
245
+ });
246
+ });
247
+
248
+ describe('getCategories', () => {
249
+ it('should return all unique categories', async () => {
250
+ await memoryService.create({ title: '1', content: 'C', category: 'work' });
251
+ await memoryService.create({ title: '2', content: 'C', category: 'personal' });
252
+ await memoryService.create({ title: '3', content: 'C', category: 'work' });
253
+ const result = await memoryService.getCategories();
254
+ expect(result.categories).toContain('personal');
255
+ expect(result.categories).toContain('work');
256
+ });
257
+
258
+ it('should return empty array when no memories exist', async () => {
259
+ const result = await memoryService.getCategories();
260
+ expect(result.categories).toEqual([]);
261
+ });
262
+ });
263
+
264
+ describe('getTags', () => {
265
+ it('should return all unique tags', async () => {
266
+ await memoryService.create({ title: '1', content: 'C', tags: ['a', 'b'] });
267
+ await memoryService.create({ title: '2', content: 'C', tags: ['b', 'c'] });
268
+ const result = await memoryService.getTags();
269
+ expect(result.tags).toContain('a');
270
+ expect(result.tags).toContain('b');
271
+ expect(result.tags).toContain('c');
272
+ });
273
+
274
+ it('should return empty array when no memories exist', async () => {
275
+ const result = await memoryService.getTags();
276
+ expect(result.tags).toEqual([]);
277
+ });
278
+ });
279
+ });