import test from 'node:test'; import assert from 'node:assert/strict'; import { randomUUID } from 'node:crypto'; import fs from 'node:fs'; import { MnemoClient, MNEMO_PROTOCOL, sourceDigest, eventDigest, manifestDigest, contextDigest, bridgeOrigin, checkRecall } from '../src/core/memory/mnemo-protocol.mjs'; const vectors = JSON.parse(fs.readFileSync(new URL('./fixtures/mnemo-bridge/digest-vectors.json', import.meta.url))); test('Mnemo V1 uses the independently frozen ordered Unicode/newline digest vectors', () => { for (const name of ['confirmed', 'archived', 'secondConfirmed']) { const v = vectors[name]; assert.equal(sourceDigest(v.input.source, v.input.memory), v.sourceDigest); assert.equal(eventDigest(v.input), v.eventDigest); } assert.equal(manifestDigest(vectors.manifest.input), vectors.manifest.digest); assert.equal(contextDigest(vectors.recall.entries), vectors.contextDigest); }); test('bridge endpoints reject credentials, redirects-to-path configuration and nonlocal plaintext', () => { for (const value of ['http://example.com', 'https://a/x', 'https://a/?q=1', 'https://user:pass@a/', 'https://a/#x']) assert.throws(() => bridgeOrigin(value)); assert.equal(bridgeOrigin('http://127.0.0.1:12345'), 'http://127.0.0.1:12345'); }); const input = () => ({ ...structuredClone(vectors.confirmed.input), requestKey: randomUUID() }); const options = transport => ({ enabled: true, baseUrl: 'http://127.0.0.1:12345', bindingId: vectors.confirmed.input.bindingId, producerId: randomUUID(), projectId: 'fixture', token: 'only-synthetic', fetch: transport }); test('transport failures and incomplete/mismatched sync receipts are UNKNOWN, not local 409 rejections', async () => { for (const fetch of [async () => { throw new TypeError('connection reset'); }, async () => new Response('{'), async () => Response.json({ protocol: MNEMO_PROTOCOL })]) { const client = new MnemoClient(options(fetch)); await assert.rejects(client.sync(input()), { code: 'PROJECT_SYNC_UNKNOWN' }); } const client = new MnemoClient(options(async () => Response.json({ code: 'PROJECT_SEQUENCE_CONFLICT' }, { status: 409 }))); await assert.rejects(client.sync(input()), { code: 'PROJECT_SEQUENCE_CONFLICT', status: 409 }); }); test('default-off sends zero bridge requests and an invalid body never changes original strings', async () => { let calls = 0; const client = new MnemoClient({ ...options(async () => { calls++; }), enabled: false }); await assert.rejects(client.capabilities(), { code: 'PROJECT_BRIDGE_DISABLED' }); assert.equal(calls, 0); const original = input(); original.memory.content = '中\n'.repeat(6001); const bytes = JSON.stringify(original); await assert.rejects(client.sync(original)); assert.equal(JSON.stringify(original), bytes); assert.equal(calls, 0); }); test('received HTTP rejection retains upstream codes outside the local namespace and original diagnostic', async () => { const trace = [], raw = { code: 'AUTH_GRANT_REVOKED', message: 'Project grant revoked' }; const client = new MnemoClient({ ...options(async () => Response.json(raw, { status: 401 })), observe: value => trace.push(value) }); await assert.rejects(client.capabilities(), { code: raw.code, status: 401, message: '项目记忆服务拒绝本次操作(HTTP 401)。' }); assert.equal(trace[0].httpStatus, 401); assert.deepEqual(trace[0].response, raw); await assert.rejects(client.sync(input()), { code: raw.code, status: 401 }); assert.equal(trace.length, 2); }); test('200 recall cannot authorize corrupted content, mismatched context or foreign projects', () => { const v = vectors.confirmed.input, now = new Date().toISOString(); const manifest = { projectId: 'fixture', projectFingerprint: 'b'.repeat(64), checkedAt: now, complete: true, sources: [{ id: v.source.id, sequence: v.source.sequence, digest: vectors.confirmed.sourceDigest }] }; const entry = { origin: 'authorized_project_mirror', bindingId: v.bindingId, projectId: 'fixture', memoryId: randomUUID(), revision: 1, source: v.source, sourceDigest: vectors.confirmed.sourceDigest, ...v.memory, visibility: 'private' }; const snapshot = { id: randomUUID(), bindingId: v.bindingId, grantId: randomUUID(), manifestDigest: manifestDigest(manifest), contextDigest: contextDigest([entry]), issuedAt: now, expiresAt: new Date(Date.now() + 20000).toISOString(), refs: [{ sourceId: entry.source.id, sequence: entry.source.sequence, sourceDigest: entry.sourceDigest, memoryId: entry.memoryId, revision: entry.revision }] }; const result = { protocol: MNEMO_PROTOCOL, entries: [entry], snapshot }; assert.equal(checkRecall(result, v.bindingId, manifest), result); for (const change of [r => { r.entries[0].content += 'corrupt'; }, r => { r.snapshot.contextDigest = 'a'.repeat(64); }, r => { r.entries[0].projectId = 'another'; }, r => { r.snapshot.expiresAt = new Date(Date.now() + 60000).toISOString(); }]) { const bad = structuredClone(result); change(bad); assert.throws(() => checkRecall(bad, v.bindingId, manifest)); } });