103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
|
|
/**
|
||
|
|
* Reply Format Compatibility Tests
|
||
|
|
*
|
||
|
|
* Verifies that existing handler reply formats are correctly
|
||
|
|
* normalized by the @open-matrix/federation package.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { describe, it } from 'node:test';
|
||
|
|
import { strict as assert } from 'node:assert';
|
||
|
|
import { normalizeInternalReply } from '@open-matrix/federation';
|
||
|
|
|
||
|
|
describe('Reply Format Compatibility', () => {
|
||
|
|
|
||
|
|
describe('RequestReplyHelper format', () => {
|
||
|
|
|
||
|
|
it('normalizes { ok: true, correlationId, result }', () => {
|
||
|
|
// This is what our RequestReplyHelper.executeWithReply publishes
|
||
|
|
const reply = JSON.stringify({
|
||
|
|
ok: true,
|
||
|
|
correlationId: 'abc-123',
|
||
|
|
result: { files: 10, directories: 3 },
|
||
|
|
});
|
||
|
|
|
||
|
|
const normalized = normalizeInternalReply(reply, 'fallback');
|
||
|
|
|
||
|
|
assert.strictEqual(normalized.ok, true);
|
||
|
|
assert.strictEqual(normalized.correlationId, 'abc-123');
|
||
|
|
assert.deepStrictEqual(normalized.body, { files: 10, directories: 3 });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('normalizes { ok: false, correlationId, error }', () => {
|
||
|
|
// This is what our RequestReplyHelper publishes on error
|
||
|
|
const reply = JSON.stringify({
|
||
|
|
ok: false,
|
||
|
|
correlationId: 'abc-123',
|
||
|
|
error: {
|
||
|
|
message: 'File not found',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const normalized = normalizeInternalReply(reply, 'fallback');
|
||
|
|
|
||
|
|
assert.strictEqual(normalized.ok, false);
|
||
|
|
assert.strictEqual(normalized.correlationId, 'abc-123');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('Alternative reply formats', () => {
|
||
|
|
|
||
|
|
it('normalizes { result: ... } shorthand', () => {
|
||
|
|
const reply = JSON.stringify({ result: { count: 42 } });
|
||
|
|
const normalized = normalizeInternalReply(reply, 'fallback');
|
||
|
|
|
||
|
|
assert.strictEqual(normalized.ok, true);
|
||
|
|
assert.deepStrictEqual(normalized.body, { count: 42 });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('normalizes plain JSON object', () => {
|
||
|
|
const reply = JSON.stringify({ foo: 'bar', count: 123 });
|
||
|
|
const normalized = normalizeInternalReply(reply, 'fallback');
|
||
|
|
|
||
|
|
assert.strictEqual(normalized.ok, true);
|
||
|
|
assert.deepStrictEqual(normalized.body, { foo: 'bar', count: 123 });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('normalizes raw string (non-JSON)', () => {
|
||
|
|
const normalized = normalizeInternalReply('hello world', 'fallback');
|
||
|
|
|
||
|
|
assert.strictEqual(normalized.ok, true);
|
||
|
|
assert.strictEqual(normalized.body, 'hello world');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('normalizes JSON array', () => {
|
||
|
|
const reply = JSON.stringify([1, 2, 3]);
|
||
|
|
const normalized = normalizeInternalReply(reply, 'fallback');
|
||
|
|
|
||
|
|
assert.strictEqual(normalized.ok, true);
|
||
|
|
assert.deepStrictEqual(normalized.body, [1, 2, 3]);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('Correlation ID handling', () => {
|
||
|
|
|
||
|
|
it('uses correlationId from payload if present', () => {
|
||
|
|
const reply = JSON.stringify({
|
||
|
|
ok: true,
|
||
|
|
correlationId: 'from-payload',
|
||
|
|
result: {},
|
||
|
|
});
|
||
|
|
|
||
|
|
const normalized = normalizeInternalReply(reply, 'fallback');
|
||
|
|
assert.strictEqual(normalized.correlationId, 'from-payload');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('uses fallback correlationId if not in payload', () => {
|
||
|
|
const reply = JSON.stringify({ result: {} });
|
||
|
|
|
||
|
|
const normalized = normalizeInternalReply(reply, 'fallback-id');
|
||
|
|
assert.strictEqual(normalized.correlationId, 'fallback-id');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|