mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-10-27 07:51:52 +00:00
- Extracted `LockPublisher` and `publishLock` to `src/lock-publisher.mjs` (already existed, but cleaned up). - Refactored `src/lock-publisher.mjs` to import error constants and classification logic from `src/lock-error-classifier.mjs` instead of duplicating them. - Refactored `src/lock-publisher.mjs` and `src/lock-utils.mjs` to use `mergeRelayList` from `src/utils.mjs`. - Consolidated and moved publisher tests from `test/lock-ops.test.mjs` and `test/lock-ops-publish-retry.test.mjs` to `test/lock-publisher.test.mjs`. - Cleaned up `src/lock-utils.mjs` to remove duplicate `mergeRelayList`. - Verified changes with tests. Co-authored-by: PR0M3TH3AN <[email protected]>
114 lines
3.1 KiB
JavaScript
114 lines
3.1 KiB
JavaScript
import { describe, it, beforeEach } from 'node:test';
|
|
import assert from 'node:assert';
|
|
import {
|
|
parseLockEvent,
|
|
queryLocks,
|
|
_resetRelayHealthState,
|
|
} from '../src/lock-ops.mjs';
|
|
|
|
describe('lock-ops', () => {
|
|
beforeEach(() => {
|
|
_resetRelayHealthState();
|
|
});
|
|
|
|
describe('parseLockEvent', () => {
|
|
it('parses a valid lock event correctly', () => {
|
|
const event = {
|
|
id: 'mock-event-id',
|
|
pubkey: 'mock-pubkey',
|
|
created_at: 1678886400,
|
|
tags: [['d', 'mock-d-tag'], ['expiration', '1678890000']],
|
|
content: JSON.stringify({
|
|
agent: 'test-agent',
|
|
cadence: 'daily',
|
|
status: 'started',
|
|
date: '2023-03-15',
|
|
platform: 'test-platform',
|
|
}),
|
|
};
|
|
|
|
const result = parseLockEvent(event);
|
|
|
|
assert.strictEqual(result.dTag, 'mock-d-tag');
|
|
assert.strictEqual(result.expiresAt, 1678890000);
|
|
assert.strictEqual(result.agent, 'test-agent');
|
|
assert.strictEqual(result.platform, 'test-platform');
|
|
});
|
|
|
|
it('returns nulls when content is invalid JSON', () => {
|
|
const event = {
|
|
id: 'mock-id',
|
|
pubkey: 'mock-pubkey',
|
|
created_at: 1678886400,
|
|
tags: [],
|
|
content: 'invalid json',
|
|
};
|
|
const result = parseLockEvent(event);
|
|
assert.strictEqual(result.agent, null);
|
|
assert.strictEqual(result.cadence, null);
|
|
});
|
|
|
|
it('returns nulls when required content fields are missing', () => {
|
|
const event = {
|
|
id: 'mock-id',
|
|
pubkey: 'mock-pubkey',
|
|
created_at: 1678886400,
|
|
tags: [],
|
|
content: JSON.stringify({}),
|
|
};
|
|
const result = parseLockEvent(event);
|
|
assert.strictEqual(result.agent, null);
|
|
assert.strictEqual(result.cadence, null);
|
|
});
|
|
|
|
it('returns nulls when content is an array (not object)', () => {
|
|
const event = {
|
|
id: 'mock-id',
|
|
pubkey: 'mock-pubkey',
|
|
created_at: 1678886400,
|
|
tags: [],
|
|
content: JSON.stringify(['not', 'an', 'object']),
|
|
};
|
|
const result = parseLockEvent(event);
|
|
assert.strictEqual(result.agent, null);
|
|
});
|
|
});
|
|
|
|
describe('queryLocks', () => {
|
|
it('falls back to fallback relays when primary query fails', async () => {
|
|
const mockPool = {
|
|
querySync: async (relays) => {
|
|
if (relays.includes('wss://primary')) {
|
|
throw new Error('primary unavailable');
|
|
}
|
|
return [{
|
|
id: 'id-1',
|
|
pubkey: 'pk',
|
|
created_at: Math.floor(Date.now() / 1000),
|
|
tags: [['d', 't']],
|
|
content: JSON.stringify({ agent: 'agent1' }),
|
|
}];
|
|
},
|
|
close: () => {},
|
|
};
|
|
|
|
const locks = await queryLocks(
|
|
['wss://primary'],
|
|
'daily',
|
|
'2026-02-14',
|
|
'torch',
|
|
{
|
|
poolFactory: () => mockPool,
|
|
getQueryTimeoutMsFn: () => 200,
|
|
getRelayFallbacksFn: () => ['wss://fallback'],
|
|
errorLogger: () => {},
|
|
healthLogger: () => {},
|
|
},
|
|
);
|
|
|
|
assert.strictEqual(locks.length, 1);
|
|
assert.strictEqual(locks[0].agent, 'agent1');
|
|
});
|
|
});
|
|
});
|