mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-11-02 02:41:54 +00:00
Refactor: consolidate mergeRelayList into utils.mjs
- Moved `mergeRelayList` from `src/lock-ops.mjs` and `src/lock-publisher.mjs` to `src/utils.mjs`. - Updated imports in `src/lock-ops.mjs` and `src/lock-publisher.mjs`. - Added unit tests for `mergeRelayList` in `test/utils.test.mjs`. - Restored `torch-config-test.json` which was deleted by the test suite. Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
2b8a0c3d0d
commit
f54c540070
4 files changed
+24
-10
No files matched your search
+1
-5
@@ -8,7 +8,7 @@ import {
|
||||
KIND_APP_DATA,
|
||||
MS_PER_SECOND,
|
||||
} from './constants.mjs';
|
||||
import { nowUnix } from './utils.mjs';
|
||||
import { nowUnix, mergeRelayList } from './utils.mjs';
|
||||
import { defaultHealthManager, buildRelayHealthConfig, RelayHealthManager } from './relay-health-manager.mjs';
|
||||
import { publishLock, LockPublisher, secureRandom } from './lock-publisher.mjs';
|
||||
|
||||
@@ -69,10 +69,6 @@ function relayListLabel(relays) {
|
||||
return relays.join(', ');
|
||||
}
|
||||
|
||||
function mergeRelayList(primaryRelays, fallbackRelays) {
|
||||
return [...new Set([...primaryRelays, ...fallbackRelays])];
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries relays for active lock events matching the criteria.
|
||||
* Uses a tiered approach: tries primary relays first, then falls back to others if needed.
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
getRelayFallbacks,
|
||||
getMinActiveRelayPool,
|
||||
} from './torch-config.mjs';
|
||||
import { mergeRelayList } from './utils.mjs';
|
||||
import { defaultHealthManager, buildRelayHealthConfig } from './relay-health-manager.mjs';
|
||||
|
||||
function withTimeout(promise, timeoutMs, timeoutMessage) {
|
||||
@@ -22,10 +23,6 @@ function relayListLabel(relays) {
|
||||
return relays.join(', ');
|
||||
}
|
||||
|
||||
function mergeRelayList(primaryRelays, fallbackRelays) {
|
||||
return [...new Set([...primaryRelays, ...fallbackRelays])];
|
||||
}
|
||||
|
||||
const PUBLISH_ERROR_CODES = {
|
||||
TIMEOUT: 'publish_timeout',
|
||||
DNS: 'dns_resolution',
|
||||
|
||||
@@ -42,6 +42,10 @@ export function detectPlatform() {
|
||||
return null;
|
||||
}
|
||||
|
||||
export function mergeRelayList(primaryRelays, fallbackRelays) {
|
||||
return [...new Set([...primaryRelays, ...fallbackRelays])];
|
||||
}
|
||||
|
||||
export function ensureDir(dir) {
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
|
||||
+18
-1
@@ -2,7 +2,7 @@ import { describe, it, after } from 'node:test';
|
||||
import assert from 'node:assert';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { getIsoWeekStr, todayDateStr, nowUnix, ensureDir, detectPlatform } from '../src/utils.mjs';
|
||||
import { getIsoWeekStr, todayDateStr, nowUnix, ensureDir, detectPlatform, mergeRelayList } from '../src/utils.mjs';
|
||||
|
||||
describe('Date Utilities', () => {
|
||||
describe('todayDateStr', () => {
|
||||
@@ -59,6 +59,23 @@ describe('Date Utilities', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Relay Utilities', () => {
|
||||
describe('mergeRelayList', () => {
|
||||
it('merges two lists of relays without duplicates', () => {
|
||||
const primary = ['wss://relay1.com', 'wss://relay2.com'];
|
||||
const fallback = ['wss://relay2.com', 'wss://relay3.com'];
|
||||
const merged = mergeRelayList(primary, fallback);
|
||||
assert.deepStrictEqual(merged, ['wss://relay1.com', 'wss://relay2.com', 'wss://relay3.com']);
|
||||
});
|
||||
|
||||
it('handles empty lists', () => {
|
||||
assert.deepStrictEqual(mergeRelayList([], []), []);
|
||||
assert.deepStrictEqual(mergeRelayList(['wss://relay1.com'], []), ['wss://relay1.com']);
|
||||
assert.deepStrictEqual(mergeRelayList([], ['wss://relay1.com']), ['wss://relay1.com']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Platform Utilities', () => {
|
||||
describe('detectPlatform', () => {
|
||||
const originalEnv = { ...process.env };
|
||||
|
||||
Reference in new issue
Block a user