@kolivri/memoreal-mcp 1.0.2 → 1.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.
@@ -1,680 +0,0 @@
1
- import { describe, it, beforeEach, afterEach } from 'node:test';
2
- import assert from 'node:assert/strict';
3
- import fs from 'node:fs';
4
- import path from 'node:path';
5
- import os from 'node:os';
6
-
7
- import {
8
- POLITE_ONBOARDING_ACTION,
9
- ONBOARDING_WIZARD_PROMPT,
10
- isPlaceholderApiKey,
11
- sanitizeApiKey,
12
- resolveHomeDir,
13
- normalizeApiUrl,
14
- parseConfigEnv,
15
- readSavedMemorealConfig,
16
- resolveEffectiveApiKey,
17
- resolveEffectiveApiUrl,
18
- saveMemorealConfig,
19
- configureDetectedIdes,
20
- } from '../src/config-store.mjs';
21
-
22
- describe('config-store', () => {
23
- let tmpDir;
24
-
25
- beforeEach(() => {
26
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'memoreal-config-test-'));
27
- });
28
-
29
- afterEach(() => {
30
- if (tmpDir && fs.existsSync(tmpDir)) {
31
- fs.rmSync(tmpDir, { recursive: true, force: true });
32
- }
33
- });
34
-
35
- describe('POLITE_ONBOARDING_ACTION & ONBOARDING_WIZARD_PROMPT', () => {
36
- it('matches exact required prefix and guidance', () => {
37
- const expectedPrefix =
38
- 'Polite Onboarding Action: Invite the user in chat to connect their free Memoreal account (1,000 free API calls & memories) in 1 click by calling the connect_account tool (opens Chrome OAuth automatically, zero copy-paste), or share https://memoreal.kolivri.com/connect (terminal wizard: npx -y @kolivri/memoreal-mcp setup). Never ask the user to paste their raw API key into chat.';
39
- assert.ok(
40
- POLITE_ONBOARDING_ACTION.startsWith(expectedPrefix),
41
- 'Must start with exact required prefix'
42
- );
43
- assert.match(POLITE_ONBOARDING_ACTION, /AskUserQuestion/);
44
- assert.match(POLITE_ONBOARDING_ACTION, /question/);
45
- assert.match(POLITE_ONBOARDING_ACTION, /Connect in 1 Click via Google \/ Browser \(Recommended\)/);
46
- assert.match(POLITE_ONBOARDING_ACTION, /Skip for this session/);
47
- assert.match(POLITE_ONBOARDING_ACTION, /connect_account\(\{ action: "start" \}\)/);
48
- assert.match(POLITE_ONBOARDING_ACTION, /I clicked Authorize — Verify/);
49
- assert.match(POLITE_ONBOARDING_ACTION, /Yes — Index repo now \(Recommended\)/);
50
- assert.equal(ONBOARDING_WIZARD_PROMPT, POLITE_ONBOARDING_ACTION);
51
- });
52
- });
53
-
54
- describe('isPlaceholderApiKey & sanitizeApiKey', () => {
55
- it('identifies placeholder keys and empty strings as placeholders', () => {
56
- const placeholders = [
57
- '',
58
- ' ',
59
- null,
60
- undefined,
61
- 'mem_dev_your_key_here',
62
- 'MEM_DEV_YOUR_KEY_HERE',
63
- '"mem_dev_your_key_here"',
64
- "'mem_dev_your_key_here'",
65
- 'mem_dev_...',
66
- 'YOUR_API_KEY',
67
- 'your_api_key',
68
- 'your-api-key',
69
- '<MEMOREAL_API_KEY>',
70
- '<MEMORIA_API_KEY>',
71
- '${MEMOREAL_API_KEY}',
72
- 'your-memoreal-api-key',
73
- 'mem_dev_abc...xyz',
74
- '"YOUR_API_KEY"',
75
- ];
76
-
77
- for (const ph of placeholders) {
78
- assert.equal(
79
- isPlaceholderApiKey(ph),
80
- true,
81
- `Expected isPlaceholderApiKey(${JSON.stringify(ph)}) to be true`
82
- );
83
- assert.equal(
84
- sanitizeApiKey(ph),
85
- '',
86
- `Expected sanitizeApiKey(${JSON.stringify(ph)}) to be empty string`
87
- );
88
- }
89
- });
90
-
91
- it('sanitizes valid keys by stripping surrounding quotes and whitespace', () => {
92
- assert.equal(sanitizeApiKey('mem_dev_valid_123'), 'mem_dev_valid_123');
93
- assert.equal(sanitizeApiKey(' "mem_dev_valid_123" '), 'mem_dev_valid_123');
94
- assert.equal(sanitizeApiKey(" 'mem_dev_valid_123' "), 'mem_dev_valid_123');
95
- assert.equal(isPlaceholderApiKey('mem_dev_valid_123'), false);
96
- });
97
-
98
- it('rejects keys containing whitespace, quotes, newlines, or shell metacharacters', () => {
99
- const invalidKeys = [
100
- 'key with spaces',
101
- 'key;rm -rf /',
102
- 'key`id`',
103
- 'key$(whoami)',
104
- 'key\nnewline',
105
- 'key"with"quotes',
106
- "key'with'quotes",
107
- 'key|pipe',
108
- 'key&bg',
109
- 'key>file',
110
- 'key<file',
111
- 'key$VAR',
112
- 'key\\backslash',
113
- ];
114
- for (const k of invalidKeys) {
115
- assert.equal(
116
- sanitizeApiKey(k),
117
- '',
118
- `Expected sanitizeApiKey(${JSON.stringify(k)}) to return empty string`
119
- );
120
- }
121
- assert.equal(sanitizeApiKey('mem_dev-valid.KEY_123'), 'mem_dev-valid.KEY_123');
122
- });
123
- });
124
-
125
- describe('resolveHomeDir', () => {
126
- it('honors options.homeDir, then options.env.MEMOREAL_HOME, then process.env.MEMOREAL_HOME, then os.homedir', () => {
127
- assert.equal(resolveHomeDir({ homeDir: '/custom/home' }), '/custom/home');
128
- assert.equal(
129
- resolveHomeDir({ env: { MEMOREAL_HOME: '/env/home' } }),
130
- '/env/home'
131
- );
132
- const orig = process.env.MEMOREAL_HOME;
133
- try {
134
- process.env.MEMOREAL_HOME = '/proc/home';
135
- assert.equal(resolveHomeDir({}), '/proc/home');
136
- assert.equal(resolveHomeDir({ homeDir: '/override/home' }), '/override/home');
137
- } finally {
138
- if (orig === undefined) {
139
- delete process.env.MEMOREAL_HOME;
140
- } else {
141
- process.env.MEMOREAL_HOME = orig;
142
- }
143
- }
144
- });
145
- });
146
-
147
- describe('normalizeApiUrl', () => {
148
- it('strips trailing /v1/mcp and trailing slashes and quotes', () => {
149
- assert.equal(
150
- normalizeApiUrl('https://memoria.kolivri.com/v1/mcp'),
151
- 'https://memoria.kolivri.com'
152
- );
153
- assert.equal(
154
- normalizeApiUrl('https://memoria.kolivri.com/v1/mcp/'),
155
- 'https://memoria.kolivri.com'
156
- );
157
- assert.equal(
158
- normalizeApiUrl('"https://memoria.kolivri.com///"'),
159
- 'https://memoria.kolivri.com'
160
- );
161
- assert.equal(
162
- normalizeApiUrl('https://api.custom.com'),
163
- 'https://api.custom.com'
164
- );
165
- });
166
-
167
- it('rejects URLs with whitespace, quotes, newlines, shell metacharacters, or non-HTTPS non-loopback schemes', () => {
168
- const invalidUrls = [
169
- 'http://evil.example.com',
170
- 'http://insecure.kolivri.com',
171
- 'javascript:alert(1)',
172
- 'file:///etc/passwd',
173
- 'https://example.com/v1/mcp; rm -rf /',
174
- 'https://example.com`id`',
175
- 'https://example.com$(whoami)',
176
- 'https://example.com\n/path',
177
- 'https://example.com"quotes"',
178
- "https://example.com'quotes'",
179
- 'https://example.com\\backslash',
180
- 'https://example.com<tag>',
181
- 'https://example.com>out',
182
- 'https://example.com with spaces',
183
- ];
184
- for (const u of invalidUrls) {
185
- assert.equal(
186
- normalizeApiUrl(u),
187
- '',
188
- `Expected normalizeApiUrl(${JSON.stringify(u)}) to return empty string`
189
- );
190
- }
191
- });
192
-
193
- it('allows valid https and loopback http URLs', () => {
194
- assert.equal(
195
- normalizeApiUrl('https://memoria.kolivri.com'),
196
- 'https://memoria.kolivri.com'
197
- );
198
- assert.equal(
199
- normalizeApiUrl('https://api.my-domain.org:8443/custom/path/v1/mcp/'),
200
- 'https://api.my-domain.org:8443/custom/path'
201
- );
202
- assert.equal(
203
- normalizeApiUrl('http://localhost:3000/v1/mcp'),
204
- 'http://localhost:3000'
205
- );
206
- assert.equal(
207
- normalizeApiUrl('http://127.0.0.1:8080/v1/mcp/'),
208
- 'http://127.0.0.1:8080'
209
- );
210
- assert.equal(
211
- normalizeApiUrl('http://localhost'),
212
- 'http://localhost'
213
- );
214
- assert.equal(
215
- normalizeApiUrl('http://127.0.0.1'),
216
- 'http://127.0.0.1'
217
- );
218
- });
219
- });
220
-
221
- describe('parseConfigEnv', () => {
222
- it('correctly parses key-value pairs ignoring comments and export prefix', () => {
223
- const raw = `
224
- # Comment line
225
- export MEMOREAL_URL="https://memoria.kolivri.com/v1/mcp"
226
- MEMOREAL_API_URL="https://memoria.kolivri.com"
227
- MEMOREAL_API_KEY="mem_dev_key_abc"
228
- `;
229
- const parsed = parseConfigEnv(raw);
230
- assert.equal(parsed.MEMOREAL_URL, 'https://memoria.kolivri.com/v1/mcp');
231
- assert.equal(parsed.MEMOREAL_API_URL, 'https://memoria.kolivri.com');
232
- assert.equal(parsed.MEMOREAL_API_KEY, 'mem_dev_key_abc');
233
- });
234
- });
235
-
236
- describe('readSavedMemorealConfig & resolveEffectiveApiKey test isolation', () => {
237
- it('reads isolated homeDir config without touching os.homedir', () => {
238
- const isolatedMemoreal = path.join(tmpDir, '.memoreal');
239
- fs.mkdirSync(isolatedMemoreal, { recursive: true });
240
- fs.writeFileSync(
241
- path.join(isolatedMemoreal, 'config.env'),
242
- 'MEMOREAL_API_KEY="mem_dev_isolated_key"\nMEMOREAL_API_URL="https://isolated.url"\n'
243
- );
244
-
245
- const config = readSavedMemorealConfig({ homeDir: tmpDir });
246
- assert.equal(config.apiKey, 'mem_dev_isolated_key');
247
- assert.equal(config.apiUrl, 'https://isolated.url');
248
- assert.equal(config.configPath, path.join(isolatedMemoreal, 'config.env'));
249
- });
250
-
251
- it('falls back to .memoria/config.env if .memoreal does not exist', () => {
252
- const isolatedMemoria = path.join(tmpDir, '.memoria');
253
- fs.mkdirSync(isolatedMemoria, { recursive: true });
254
- fs.writeFileSync(
255
- path.join(isolatedMemoria, 'config.env'),
256
- 'MEMORIA_API_KEY="mem_dev_legacy_key"\nMEMORIA_API_URL="https://legacy.url"\n'
257
- );
258
-
259
- const config = readSavedMemorealConfig({ homeDir: tmpDir });
260
- assert.equal(config.apiKey, 'mem_dev_legacy_key');
261
- assert.equal(config.apiUrl, 'https://legacy.url');
262
- assert.equal(config.configPath, path.join(isolatedMemoria, 'config.env'));
263
- });
264
-
265
- it('honors configDir over homeDir', () => {
266
- const customDir = path.join(tmpDir, 'custom-cfg');
267
- fs.mkdirSync(customDir, { recursive: true });
268
- fs.writeFileSync(
269
- path.join(customDir, 'config.env'),
270
- 'MEMOREAL_API_KEY="mem_dev_custom_key"\n'
271
- );
272
-
273
- const config = readSavedMemorealConfig({ configDir: customDir, homeDir: tmpDir });
274
- assert.equal(config.apiKey, 'mem_dev_custom_key');
275
- assert.equal(config.configPath, path.join(customDir, 'config.env'));
276
- });
277
-
278
- it('resolveEffectiveApiKey checks priority overrideKey -> runtimePairedApiKey -> options.apiKey -> env', () => {
279
- const env = { MEMOREAL_API_KEY: 'mem_dev_env' };
280
- assert.equal(
281
- resolveEffectiveApiKey('mem_dev_override', {
282
- runtimePairedApiKey: 'mem_dev_paired',
283
- apiKey: 'mem_dev_opt',
284
- env,
285
- }),
286
- 'mem_dev_override'
287
- );
288
-
289
- assert.equal(
290
- resolveEffectiveApiKey(undefined, {
291
- runtimePairedApiKey: 'mem_dev_paired',
292
- apiKey: 'mem_dev_opt',
293
- env,
294
- }),
295
- 'mem_dev_paired'
296
- );
297
-
298
- assert.equal(
299
- resolveEffectiveApiKey(undefined, {
300
- apiKey: 'mem_dev_opt',
301
- env,
302
- }),
303
- 'mem_dev_opt'
304
- );
305
-
306
- assert.equal(
307
- resolveEffectiveApiKey(undefined, { env }),
308
- 'mem_dev_env'
309
- );
310
- });
311
-
312
- it('ignores placeholder keys in env and falls through to saved config', () => {
313
- const isolatedMemoreal = path.join(tmpDir, '.memoreal');
314
- fs.mkdirSync(isolatedMemoreal, { recursive: true });
315
- fs.writeFileSync(
316
- path.join(isolatedMemoreal, 'config.env'),
317
- 'MEMOREAL_API_KEY="mem_dev_persisted_key"\n'
318
- );
319
-
320
- const env = { MEMOREAL_API_KEY: 'mem_dev_your_key_here' };
321
- const resolved = resolveEffectiveApiKey(undefined, {
322
- homeDir: tmpDir,
323
- env,
324
- });
325
- assert.equal(resolved, 'mem_dev_persisted_key');
326
- });
327
-
328
- it('falls back from placeholder MEMOREAL_API_KEY to valid MEMORIA_API_KEY in env', () => {
329
- const env = {
330
- MEMOREAL_API_KEY: 'mem_dev_your_key_here',
331
- MEMORIA_API_KEY: 'mem_dev_valid_legacy_999',
332
- };
333
- const resolved = resolveEffectiveApiKey(undefined, { env });
334
- assert.equal(resolved, 'mem_dev_valid_legacy_999');
335
- });
336
-
337
- it('falls back from placeholder MEMOREAL_API_KEY to valid MEMORIA_API_KEY in saved config', () => {
338
- const isolatedMemoreal = path.join(tmpDir, '.memoreal');
339
- fs.mkdirSync(isolatedMemoreal, { recursive: true });
340
- fs.writeFileSync(
341
- path.join(isolatedMemoreal, 'config.env'),
342
- 'MEMOREAL_API_KEY="mem_dev_your_key_here"\nMEMORIA_API_KEY="mem_dev_saved_legacy_key"\n'
343
- );
344
-
345
- const config = readSavedMemorealConfig({ homeDir: tmpDir });
346
- assert.equal(config.apiKey, 'mem_dev_saved_legacy_key');
347
- });
348
-
349
- it('honors explicit empty MEMOREAL_API_KEY === "" in ambient env by returning empty string without reading config', () => {
350
- const isolatedMemoreal = path.join(tmpDir, '.memoreal');
351
- fs.mkdirSync(isolatedMemoreal, { recursive: true });
352
- fs.writeFileSync(
353
- path.join(isolatedMemoreal, 'config.env'),
354
- 'MEMOREAL_API_KEY="mem_dev_persisted_key"\n'
355
- );
356
-
357
- // When ambient (no homeDir/configDir/MEMOREAL_HOME), MEMOREAL_API_KEY === '' isolates tests
358
- const env = { MEMOREAL_API_KEY: '' };
359
- const resolved = resolveEffectiveApiKey(undefined, { env });
360
- assert.equal(resolved, '');
361
-
362
- // But when homeDir is explicitly provided (isolated test harness), it reads the test home
363
- const isolatedResolved = resolveEffectiveApiKey(undefined, {
364
- homeDir: tmpDir,
365
- env,
366
- });
367
- assert.equal(isolatedResolved, 'mem_dev_persisted_key');
368
- });
369
-
370
- it('honors disableConfigFileFallback === true', () => {
371
- const isolatedMemoreal = path.join(tmpDir, '.memoreal');
372
- fs.mkdirSync(isolatedMemoreal, { recursive: true });
373
- fs.writeFileSync(
374
- path.join(isolatedMemoreal, 'config.env'),
375
- 'MEMOREAL_API_KEY="mem_dev_persisted_key"\n'
376
- );
377
-
378
- const resolved = resolveEffectiveApiKey(undefined, {
379
- homeDir: tmpDir,
380
- disableConfigFileFallback: true,
381
- });
382
- assert.equal(resolved, '');
383
- });
384
- });
385
-
386
- describe('resolveEffectiveApiUrl', () => {
387
- it('resolves URL with proper fallbacks and strips /v1/mcp', () => {
388
- assert.equal(
389
- resolveEffectiveApiUrl('https://override.url/v1/mcp/'),
390
- 'https://override.url'
391
- );
392
- assert.equal(
393
- resolveEffectiveApiUrl(undefined, { apiUrl: 'https://option.url' }),
394
- 'https://option.url'
395
- );
396
- assert.equal(
397
- resolveEffectiveApiUrl(undefined, {
398
- env: { MEMOREAL_API_URL: 'https://env.url/v1/mcp' },
399
- }),
400
- 'https://env.url'
401
- );
402
- assert.equal(
403
- resolveEffectiveApiUrl(undefined, { env: {} }),
404
- 'https://memoria.kolivri.com'
405
- );
406
- });
407
- });
408
-
409
- describe('saveMemorealConfig', () => {
410
- it('throws error when saving empty or placeholder key', () => {
411
- assert.throws(
412
- () => saveMemorealConfig({ apiKey: 'YOUR_API_KEY', homeDir: tmpDir }),
413
- /Cannot save empty or placeholder/
414
- );
415
- assert.throws(
416
- () => saveMemorealConfig({ apiKey: '', homeDir: tmpDir }),
417
- /Cannot save empty or placeholder/
418
- );
419
- });
420
-
421
- it('throws error when saving invalid or insecure Memoreal API URL', () => {
422
- assert.throws(
423
- () =>
424
- saveMemorealConfig({
425
- apiKey: 'mem_dev_test_123',
426
- apiUrl: 'http://evil.example.com',
427
- homeDir: tmpDir,
428
- }),
429
- /Invalid or insecure Memoreal API URL/
430
- );
431
- assert.throws(
432
- () =>
433
- saveMemorealConfig({
434
- apiKey: 'mem_dev_test_123',
435
- apiUrl: 'https://example.com; rm -rf /',
436
- homeDir: tmpDir,
437
- }),
438
- /Invalid or insecure Memoreal API URL/
439
- );
440
- assert.throws(
441
- () =>
442
- saveMemorealConfig({
443
- apiKey: 'mem_dev_test_123',
444
- apiUrl: 'javascript:alert(1)',
445
- homeDir: tmpDir,
446
- }),
447
- /Invalid or insecure Memoreal API URL/
448
- );
449
- });
450
-
451
- it('allows saving valid https or loopback http URLs', () => {
452
- const resHttps = saveMemorealConfig({
453
- apiKey: 'mem_dev_test_123',
454
- apiUrl: 'https://api.kolivri.com/v1/mcp',
455
- homeDir: tmpDir,
456
- });
457
- assert.equal(resHttps.apiUrl, 'https://api.kolivri.com');
458
-
459
- const resLoopback = saveMemorealConfig({
460
- apiKey: 'mem_dev_test_123',
461
- apiUrl: 'http://127.0.0.1:9090',
462
- homeDir: tmpDir,
463
- });
464
- assert.equal(resLoopback.apiUrl, 'http://127.0.0.1:9090');
465
- });
466
-
467
- it('persists 6-key config.env with 0600 mode and creates 0700 directories', () => {
468
- const result = saveMemorealConfig({
469
- apiKey: 'mem_dev_save_123',
470
- apiUrl: 'https://save.kolivri.com',
471
- homeDir: tmpDir,
472
- });
473
-
474
- assert.equal(result.apiKey, 'mem_dev_save_123');
475
- assert.equal(result.apiUrl, 'https://save.kolivri.com');
476
-
477
- const memorealEnv = path.join(tmpDir, '.memoreal', 'config.env');
478
- const memoriaEnv = path.join(tmpDir, '.memoria', 'config.env');
479
-
480
- assert.ok(fs.existsSync(memorealEnv));
481
- assert.ok(fs.existsSync(memoriaEnv));
482
-
483
- const content = fs.readFileSync(memorealEnv, 'utf8');
484
- assert.match(content, /MEMOREAL_URL="https:\/\/save\.kolivri\.com\/v1\/mcp"/);
485
- assert.match(content, /MEMOREAL_API_URL="https:\/\/save\.kolivri\.com"/);
486
- assert.match(content, /MEMOREAL_API_KEY="mem_dev_save_123"/);
487
- assert.match(content, /MEMORIA_URL="https:\/\/save\.kolivri\.com\/v1\/mcp"/);
488
- assert.match(content, /MEMORIA_API_URL="https:\/\/save\.kolivri\.com"/);
489
- assert.match(content, /MEMORIA_API_KEY="mem_dev_save_123"/);
490
-
491
- // Verify file and directory permissions
492
- if (process.platform !== 'win32') {
493
- const dirStat = fs.statSync(path.join(tmpDir, '.memoreal'));
494
- assert.equal(dirStat.mode & 0o777, 0o700);
495
-
496
- const fileStat = fs.statSync(memorealEnv);
497
- assert.equal(fileStat.mode & 0o777, 0o600);
498
- }
499
- });
500
-
501
- it('creates .bak backup when overwriting existing config.env', () => {
502
- const memorealDir = path.join(tmpDir, '.memoreal');
503
- fs.mkdirSync(memorealDir, { recursive: true });
504
- const envPath = path.join(memorealDir, 'config.env');
505
- fs.writeFileSync(envPath, 'MEMOREAL_API_KEY="old_key"\n');
506
-
507
- saveMemorealConfig({
508
- apiKey: 'mem_dev_new_key',
509
- homeDir: tmpDir,
510
- });
511
-
512
- const bakPath = envPath + '.bak';
513
- assert.ok(fs.existsSync(bakPath));
514
- assert.match(fs.readFileSync(bakPath, 'utf8'), /old_key/);
515
- assert.match(fs.readFileSync(envPath, 'utf8'), /mem_dev_new_key/);
516
-
517
- if (process.platform !== 'win32') {
518
- assert.equal(fs.statSync(bakPath).mode & 0o777, 0o600);
519
- }
520
- });
521
- });
522
-
523
- describe('configureDetectedIdes', () => {
524
- it('creates OpenCode native config with .mcp.memoreal (never .mcpServers) when preferredTool is opencode', () => {
525
- const res = configureDetectedIdes({
526
- apiKey: 'mem_dev_test_ide',
527
- apiUrl: 'https://memoria.kolivri.com',
528
- homeDir: tmpDir,
529
- preferredTool: 'opencode',
530
- });
531
-
532
- const opencodeJsonPath = path.join(
533
- tmpDir,
534
- '.config',
535
- 'opencode',
536
- 'opencode.json'
537
- );
538
- assert.ok(fs.existsSync(opencodeJsonPath));
539
- const parsed = JSON.parse(fs.readFileSync(opencodeJsonPath, 'utf8'));
540
-
541
- assert.equal(parsed.mcp.memoreal.type, 'remote');
542
- assert.equal(
543
- parsed.mcp.memoreal.url,
544
- 'https://memoria.kolivri.com/v1/mcp'
545
- );
546
- assert.equal(parsed.mcp.memoreal.enabled, true);
547
- assert.deepEqual(parsed.mcp.memoreal.headers, {
548
- Authorization: 'Bearer mem_dev_test_ide',
549
- });
550
- assert.equal(parsed.mcpServers, undefined, 'Must NEVER write .mcpServers into opencode.json');
551
-
552
- if (process.platform !== 'win32') {
553
- assert.equal(fs.statSync(opencodeJsonPath).mode & 0o777, 0o600);
554
- }
555
- });
556
-
557
- it('preserves existing local stdio OpenCode entry and sets environment key', () => {
558
- const opencodeDir = path.join(tmpDir, '.config', 'opencode');
559
- fs.mkdirSync(opencodeDir, { recursive: true });
560
- const opencodeJsonPath = path.join(opencodeDir, 'opencode.json');
561
- fs.writeFileSync(
562
- opencodeJsonPath,
563
- JSON.stringify({
564
- mcp: {
565
- memoreal: {
566
- type: 'local',
567
- command: ['node', 'src/server.mjs'],
568
- },
569
- },
570
- })
571
- );
572
-
573
- configureDetectedIdes({
574
- apiKey: 'mem_dev_local_key',
575
- homeDir: tmpDir,
576
- });
577
-
578
- const parsed = JSON.parse(fs.readFileSync(opencodeJsonPath, 'utf8'));
579
- assert.equal(parsed.mcp.memoreal.type, 'local');
580
- assert.equal(
581
- parsed.mcp.memoreal.environment.MEMOREAL_API_KEY,
582
- 'mem_dev_local_key'
583
- );
584
- });
585
-
586
- it('merges into Cursor, Windsurf, VS Code, and Claude Desktop when directories exist', () => {
587
- // Create parent directories
588
- const cursorDir = path.join(tmpDir, '.cursor');
589
- const windsurfDir = path.join(tmpDir, '.codeium', 'windsurf');
590
- const vscodeDir = path.join(tmpDir, '.vscode');
591
- const claudeDesktopDir = path.join(tmpDir, '.config', 'Claude');
592
-
593
- fs.mkdirSync(cursorDir, { recursive: true });
594
- fs.mkdirSync(windsurfDir, { recursive: true });
595
- fs.mkdirSync(vscodeDir, { recursive: true });
596
- fs.mkdirSync(claudeDesktopDir, { recursive: true });
597
-
598
- const res = configureDetectedIdes({
599
- apiKey: 'mem_dev_all_ides',
600
- apiUrl: 'https://memoria.kolivri.com',
601
- homeDir: tmpDir,
602
- });
603
-
604
- const cursorJson = JSON.parse(
605
- fs.readFileSync(path.join(cursorDir, 'mcp.json'), 'utf8')
606
- );
607
- assert.equal(cursorJson.mcpServers.memoreal.type, 'http');
608
- assert.equal(
609
- cursorJson.mcpServers.memoreal.url,
610
- 'https://memoria.kolivri.com/v1/mcp'
611
- );
612
- assert.equal(
613
- cursorJson.mcpServers.memoreal.headers.Authorization,
614
- 'Bearer mem_dev_all_ides'
615
- );
616
-
617
- const windsurfJson = JSON.parse(
618
- fs.readFileSync(path.join(windsurfDir, 'mcp_config.json'), 'utf8')
619
- );
620
- assert.equal(windsurfJson.mcpServers.memoreal.type, 'http');
621
-
622
- const vscodeJson = JSON.parse(
623
- fs.readFileSync(path.join(vscodeDir, 'mcp.json'), 'utf8')
624
- );
625
- assert.equal(vscodeJson.mcpServers.memoreal.type, 'http');
626
-
627
- const desktopJson = JSON.parse(
628
- fs.readFileSync(
629
- path.join(claudeDesktopDir, 'claude_desktop_config.json'),
630
- 'utf8'
631
- )
632
- );
633
- assert.equal(desktopJson.mcpServers.memoreal.type, 'http');
634
- });
635
-
636
- it('updates .claude.json ONLY when it already exists', () => {
637
- // Does not exist: should not be created
638
- configureDetectedIdes({
639
- apiKey: 'mem_dev_claude',
640
- homeDir: tmpDir,
641
- preferredTool: 'claude-code',
642
- });
643
- const claudeJsonPath = path.join(tmpDir, '.claude.json');
644
- assert.equal(fs.existsSync(claudeJsonPath), false);
645
-
646
- // Now create existing .claude.json
647
- fs.writeFileSync(claudeJsonPath, JSON.stringify({ mcpServers: {} }));
648
- configureDetectedIdes({
649
- apiKey: 'mem_dev_claude',
650
- homeDir: tmpDir,
651
- });
652
- assert.ok(fs.existsSync(claudeJsonPath));
653
- assert.ok(fs.existsSync(claudeJsonPath + '.bak'));
654
- const parsed = JSON.parse(fs.readFileSync(claudeJsonPath, 'utf8'));
655
- assert.equal(parsed.mcpServers.memoreal.type, 'http');
656
- });
657
-
658
- it('leaves malformed JSON completely untouched and reports skipped_malformed_json', () => {
659
- const cursorDir = path.join(tmpDir, '.cursor');
660
- fs.mkdirSync(cursorDir, { recursive: true });
661
- const cursorJsonPath = path.join(cursorDir, 'mcp.json');
662
- const malformedContent = '{ invalid json "missing close brace"';
663
- fs.writeFileSync(cursorJsonPath, malformedContent);
664
-
665
- const result = configureDetectedIdes({
666
- apiKey: 'mem_dev_malformed_test',
667
- homeDir: tmpDir,
668
- });
669
-
670
- // Verify file was NOT modified
671
- assert.equal(fs.readFileSync(cursorJsonPath, 'utf8'), malformedContent);
672
- // Verify .bak was NOT created
673
- assert.equal(fs.existsSync(cursorJsonPath + '.bak'), false);
674
- // Verify skipped status in report
675
- const entry = result.updated.find((u) => u.file === cursorJsonPath);
676
- assert.ok(entry);
677
- assert.equal(entry.status, 'skipped_malformed_json');
678
- });
679
- });
680
- });