Refactor: Consolidate duplicate relayListLabel utility

Consolidated the duplicate `relayListLabel` function from `src/lock-ops.mjs` and `src/lock-publisher.mjs` into `src/utils.mjs`.

- Moved `relayListLabel` to `src/utils.mjs`.
- Updated `src/lock-ops.mjs` and `src/lock-publisher.mjs` to import it.
- Updated `src/lock-utils.mjs` to re-export it from `src/utils.mjs` to maintain backward compatibility.
- Added unit tests in `test/utils.test.mjs`.
- Verified all tests pass.

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-20 18:12:58 +00:00
1 parent 2b8a0c3d0d
commit 59e96ae21a
5 files changed
+28 -14

No files matched your search

+1 -5
View File
@@ -8,7 +8,7 @@ import {
KIND_APP_DATA,
MS_PER_SECOND,
} from './constants.mjs';
import { nowUnix } from './utils.mjs';
import { nowUnix, relayListLabel } from './utils.mjs';
import { defaultHealthManager, buildRelayHealthConfig, RelayHealthManager } from './relay-health-manager.mjs';
import { publishLock, LockPublisher, secureRandom } from './lock-publisher.mjs';
@@ -65,10 +65,6 @@ function withTimeout(promise, timeoutMs, timeoutMessage) {
});
}
function relayListLabel(relays) {
return relays.join(', ');
}
function mergeRelayList(primaryRelays, fallbackRelays) {
return [...new Set([...primaryRelays, ...fallbackRelays])];
}
+1 -4
View File
@@ -7,6 +7,7 @@ import {
getMinActiveRelayPool,
} from './torch-config.mjs';
import { defaultHealthManager, buildRelayHealthConfig } from './relay-health-manager.mjs';
import { relayListLabel } from './utils.mjs';
function withTimeout(promise, timeoutMs, timeoutMessage) {
let timeoutHandle;
@@ -18,10 +19,6 @@ function withTimeout(promise, timeoutMs, timeoutMessage) {
});
}
function relayListLabel(relays) {
return relays.join(', ');
}
function mergeRelayList(primaryRelays, fallbackRelays) {
return [...new Set([...primaryRelays, ...fallbackRelays])];
}
+2 -4
View File
@@ -2,6 +2,8 @@ import fs from 'node:fs/promises';
import path from 'node:path';
import { todayDateStr, getIsoWeekStr } from './utils.mjs';
export { relayListLabel } from './utils.mjs';
/**
* Scans the local log directory to identify agents that have already completed their task
* for the current period (daily or weekly).
@@ -58,10 +60,6 @@ export function withTimeout(promise, timeoutMs, timeoutMessage) {
});
}
export function relayListLabel(relays) {
return relays.join(', ');
}
export function mergeRelayList(primaryRelays, fallbackRelays) {
return [...new Set([...primaryRelays, ...fallbackRelays])];
}
+4
View File
@@ -47,3 +47,7 @@ export function ensureDir(dir) {
fs.mkdirSync(dir, { recursive: true });
}
}
export function relayListLabel(relays) {
return relays.join(', ');
}
+20 -1
View File
@@ -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, relayListLabel } from '../src/utils.mjs';
describe('Date Utilities', () => {
describe('todayDateStr', () => {
@@ -184,3 +184,22 @@ describe('File Utilities', () => {
});
});
});
describe('String Utilities', () => {
describe('relayListLabel', () => {
it('joins relay URLs with comma and space', () => {
const relays = ['ws://relay1.com', 'ws://relay2.com'];
assert.strictEqual(relayListLabel(relays), 'ws://relay1.com, ws://relay2.com');
});
it('handles single relay', () => {
const relays = ['ws://relay1.com'];
assert.strictEqual(relayListLabel(relays), 'ws://relay1.com');
});
it('handles empty array', () => {
const relays = [];
assert.strictEqual(relayListLabel(relays), '');
});
});
});