The new README described the directory as mostly Node ESM and called the pre-existing Deno policies "legacy", even though ldap-policy.ts is a shared module reused by the new member bridge and strfry-policy.ts is the members relay's current write policy. It also baked in internal deployment details (Chef, strfry-1, /etc/*.conf paths, the staging relay's ZeroTier IP). Document every file with its correct runtime (Deno vs Node 24 LTS), drop the "legacy" label, and genericize the deployment examples. Cross-link the README from docs/dev/nostr.md, and replace the remaining internal host/IP references in sync-policy.mjs and its test with neutral placeholders.
136 lines
4.6 KiB
JavaScript
136 lines
4.6 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { Readable, Writable } from 'node:stream';
|
|
|
|
import { createState, decide, run } from '../sync-policy.mjs';
|
|
|
|
const STAGING = 'ws://staging-relay:7779';
|
|
|
|
function syncMsg(overrides = {}) {
|
|
const { event: eventOverrides, ...rest } = overrides;
|
|
|
|
return {
|
|
type: 'new',
|
|
sourceType: 'Sync',
|
|
sourceInfo: STAGING,
|
|
event: {
|
|
id: 'evt1',
|
|
pubkey: 'pk1',
|
|
kind: 360,
|
|
tags: [],
|
|
content: 'hello world',
|
|
...(eventOverrides || {}),
|
|
},
|
|
...rest,
|
|
};
|
|
}
|
|
|
|
async function runLines(lines, { env, state } = {}) {
|
|
const input = Readable.from(lines.map((line) => `${line}\n`));
|
|
let stdout = '';
|
|
let stderr = '';
|
|
|
|
const output = new Writable({ write(chunk, _enc, cb) { stdout += chunk.toString(); cb(); } });
|
|
const error = new Writable({ write(chunk, _enc, cb) { stderr += chunk.toString(); cb(); } });
|
|
|
|
await run({ input, output, error, env, state });
|
|
|
|
const results = stdout.split('\n').filter((line) => line !== '').map((line) => JSON.parse(line));
|
|
return { results, stdout, stderr };
|
|
}
|
|
|
|
test('accepts Sync events from the configured staging relay', () => {
|
|
const state = createState({ STAGING_RELAY_URL: STAGING });
|
|
assert.deepEqual(decide(syncMsg(), state), { id: 'evt1', action: 'accept', msg: '' });
|
|
});
|
|
|
|
test('accepts any kind from the configured staging relay', () => {
|
|
const state = createState({ STAGING_RELAY_URL: STAGING });
|
|
|
|
for (const kind of [0, 1, 3, 7, 360, 30360, 24133, 9735]) {
|
|
const result = decide(syncMsg({ event: { id: `evt-${kind}`, kind } }), state);
|
|
assert.equal(result.action, 'accept', `kind ${kind} should be accepted`);
|
|
}
|
|
});
|
|
|
|
test('rejects events that did not arrive via Sync', () => {
|
|
const state = createState({ STAGING_RELAY_URL: STAGING });
|
|
|
|
for (const sourceType of ['IP4', 'IP6', 'Import', 'Stream', 'Stored']) {
|
|
const result = decide(syncMsg({ sourceType }), state);
|
|
assert.equal(result.action, 'reject', `${sourceType} should be rejected`);
|
|
assert.match(result.msg, /expected sourceType/);
|
|
}
|
|
});
|
|
|
|
test('rejects Sync events from the wrong source URL', () => {
|
|
const state = createState({ STAGING_RELAY_URL: STAGING });
|
|
const result = decide(syncMsg({ sourceInfo: 'ws://evil.example:7779' }), state);
|
|
|
|
assert.equal(result.action, 'reject');
|
|
assert.match(result.msg, /expected sourceInfo/);
|
|
});
|
|
|
|
test('compares sourceInfo byte-exactly', () => {
|
|
const state = createState({ STAGING_RELAY_URL: STAGING });
|
|
|
|
assert.equal(decide(syncMsg({ sourceInfo: `${STAGING} ` }), state).action, 'reject');
|
|
assert.equal(decide(syncMsg({ sourceInfo: `${STAGING}/` }), state).action, 'reject');
|
|
assert.equal(decide(syncMsg({ sourceInfo: STAGING.toUpperCase() }), state).action, 'reject');
|
|
});
|
|
|
|
test('trims whitespace around the configured STAGING_RELAY_URL', () => {
|
|
const state = createState({ STAGING_RELAY_URL: ` ${STAGING}\n` });
|
|
assert.equal(decide(syncMsg(), state).action, 'accept');
|
|
});
|
|
|
|
test('fails closed when STAGING_RELAY_URL is not configured', () => {
|
|
const state = createState({});
|
|
const result = decide(syncMsg(), state);
|
|
|
|
assert.equal(result.action, 'reject');
|
|
assert.match(result.msg, /not configured/);
|
|
});
|
|
|
|
test('rejects non-new message types', () => {
|
|
const state = createState({ STAGING_RELAY_URL: STAGING });
|
|
const result = decide(syncMsg({ type: 'lookback' }), state);
|
|
|
|
assert.equal(result.action, 'reject');
|
|
assert.match(result.msg, /unexpected request type/);
|
|
});
|
|
|
|
test('rejects malformed input', () => {
|
|
const state = createState({ STAGING_RELAY_URL: STAGING });
|
|
|
|
assert.equal(decide(null, state).action, 'reject');
|
|
assert.match(decide(null, state).msg, /malformed input/);
|
|
});
|
|
|
|
test('run() accepts an expected event and rejects the rest', async () => {
|
|
const { results, stderr } = await runLines(
|
|
[
|
|
JSON.stringify(syncMsg({ event: { id: 'a' } })),
|
|
JSON.stringify(syncMsg({ event: { id: 'b' }, sourceType: 'IP4' })),
|
|
JSON.stringify(syncMsg({ event: { id: 'c' }, sourceInfo: 'ws://evil.example:7779' })),
|
|
],
|
|
{ env: { STAGING_RELAY_URL: STAGING } },
|
|
);
|
|
|
|
assert.deepEqual(results.map((r) => r.action), ['accept', 'reject', 'reject']);
|
|
assert.match(stderr, /expected sourceType/);
|
|
assert.match(stderr, /expected sourceInfo/);
|
|
});
|
|
|
|
test('run() rejects malformed JSON without crashing and keeps going', async () => {
|
|
const { results, stderr } = await runLines(
|
|
['{not json', JSON.stringify(syncMsg({ event: { id: 'ok' } }))],
|
|
{ env: { STAGING_RELAY_URL: STAGING } },
|
|
);
|
|
|
|
assert.equal(results.length, 2);
|
|
assert.equal(results[0].action, 'reject');
|
|
assert.equal(results[1].action, 'accept');
|
|
assert.match(stderr, /malformed JSON/);
|
|
});
|