Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
import { afterEach, describe, expect, it } from 'bun:test';
import { Database } from 'bun:sqlite';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { SessionStore } from '../../../src/services/sqlite/SessionStore.js';
import { CloudSync } from '../../../src/services/sync/CloudSync.js';
import { checkCloudSyncHubUrl, normalizeCloudSyncHubUrl } from '../../../src/shared/cloud-sync-hub-url.js';
import { SyncApply } from '../../../src/services/sync/SyncApply.js';
import { SyncClient } from '../../../src/services/sync/SyncClient.js';
describe('normalizeCloudSyncHubUrl', () => {
it('keeps an https hub and strips trailing slashes', () => {
expect(normalizeCloudSyncHubUrl(' https://sync.cmem.ai/// ')).toBe('https://sync.cmem.ai');
});
it('keeps a loopback http hub for local development', () => {
expect(normalizeCloudSyncHubUrl('http://127.0.0.1:8787')).toBe('http://127.0.0.1:8787');
expect(normalizeCloudSyncHubUrl('http://localhost:8787/')).toBe('http://localhost:8787');
expect(normalizeCloudSyncHubUrl('http://[::1]:8787')).toBe('http://[::1]:8787');
});
it('rejects plain http on a non-loopback host', () => {
expect(normalizeCloudSyncHubUrl('http://hub.test')).toBe('');
expect(normalizeCloudSyncHubUrl('http://sync.cmem.ai')).toBe('');
});
it('rejects non-http(s) schemes', () => {
expect(normalizeCloudSyncHubUrl('file:///etc/passwd')).toBe('');
expect(normalizeCloudSyncHubUrl('ftp://sync.cmem.ai')).toBe('');
});
it('rejects credentials, a query, or a fragment', () => {
expect(normalizeCloudSyncHubUrl('https://user:[email protected]')).toBe('');
expect(normalizeCloudSyncHubUrl('https://sync.cmem.ai/?x=1')).toBe('');
expect(normalizeCloudSyncHubUrl('https://sync.cmem.ai/#x')).toBe('');
});
it('treats empty and malformed values as unconfigured', () => {
expect(normalizeCloudSyncHubUrl('')).toBe('');
expect(normalizeCloudSyncHubUrl(' ')).toBe('');
expect(normalizeCloudSyncHubUrl('not a url')).toBe('');
expect(normalizeCloudSyncHubUrl(undefined)).toBe('');
});
it('names why a value was refused without echoing it', () => {
expect(checkCloudSyncHubUrl('').refusal).toBeNull();
expect(checkCloudSyncHubUrl('https://sync.cmem.ai').refusal).toBeNull();
expect(checkCloudSyncHubUrl('http://192.168.1.5:8787').refusal).toContain('plain http to a non-loopback host');
const credentialed = checkCloudSyncHubUrl('https://user:[email protected]');
expect(credentialed.refusal).toContain('credentials');
expect(credentialed.refusal).not.toContain('secret');
});
});
describe('CloudSync with a refused hub URL', () => {
const cleanups: Array<() => void> = [];
afterEach(() => { for (const cleanup of cleanups.splice(0).reverse()) cleanup(); });
function setup(hubUrl: string, deviceId = 'fixture-device') {
const dir = mkdtempSync(join(tmpdir(), 'cloud-sync-hub-url-'));
const store = new SessionStore(join(dir, 'memory.db'), { syncOpsEnabled: true });
const requests: string[] = [];
const sync = new CloudSync(store.db, {
CLAUDE_MEM_CLOUD_SYNC_TOKEN: 'fixture-token', CLAUDE_MEM_CLOUD_SYNC_USER_ID: 'fixture-user',
CLAUDE_MEM_CLOUD_SYNC_DEVICE_ID: deviceId, CLAUDE_MEM_CLOUD_SYNC_HUB_URL: hubUrl,
}, {
settingsPath: join(dir, 'settings.json'),
debounceMs: 1,
fetchImpl: (async (url: unknown) => {
requests.push(String(url));
return Response.json({ acked: [], head_seq: '0', projected_seq: '0' });
}) as typeof fetch,
});
cleanups.push(() => { sync.stop(); store.close(); rmSync(dir, { recursive: true, force: true }); });
const session = store.createSDKSession('host-session', 'project', 'first');
store.saveUserPrompt('host-session', 1, 'first', session);
return { store, sync, requests };
}
it('sends nothing, keeps queued uploads, and keeps its device id', async () => {
const { sync, requests } = setup('http://192.168.1.5:8787');
sync.start();
sync.notify();
await sync.flush();
await sync.statusWithHubProbe();
await new Promise(resolve => setTimeout(resolve, 20));
expect(requests).toEqual([]);
const status = sync.status();
expect(status.configured).toBe(true);
expect(status.deviceId).toBe('fixture-device');
expect(status.pending.prompts).toBe(1);
expect(sync.requestHubUrl()).toBeNull();
});
it('reports the refusal in status instead of going quiet', () => {
const { sync } = setup('https://user:[email protected]');
const status = sync.status();
expect(status.lastError).toContain('Cloud Sync hub URL refused');
expect(status.hub.error).toContain('Cloud Sync hub URL refused');
expect(status.health.state).toBe('failing');
expect(status.health.message).toContain('credentials');
expect(JSON.stringify(status)).not.toContain('secret');
});
it('still resolves a device id when none was set', () => {
const { sync } = setup('http://hub.example.test', '');
expect(sync.status().deviceId).not.toBe('');
});
it('hands token-bearing clients the validated URL when it is usable', () => {
const { sync } = setup('https://hub.example.test/');
expect(sync.requestHubUrl()).toBe('https://hub.example.test');
expect(sync.status().lastError).toBeNull();
});
});
describe('SyncClient token-bearing requests', () => {
it('refuses to follow redirects on pull', async () => {
const db = new Database(':memory:');
new SessionStore(db);
const inits: RequestInit[] = [];
const client = new SyncClient(new SyncApply(db, { deviceId: 'device-fixture' }), {
hubUrl: 'https://hub.test',
token: 'test-token',
userId: 'user-42',
deviceId: 'device-fixture',
wsEnabled: false,
fetchImpl: (async (_url: unknown, init?: RequestInit) => {
inits.push(init ?? {});
return Response.json({ protocol_version: 2, epoch: '1', ops: [], head_seq: '0', more: false });
}) as typeof fetch,
});
try {
await client.pullOnce({ force: true });
} finally {
client.stop();
db.close();
}
expect(inits.length).toBeGreaterThan(0);
for (const init of inits) expect(init.redirect).toBe('error');
});
});