From 59e96ae21afd9f7422004916c26d553217ffce04 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 18:12:58 +0000 Subject: [PATCH] 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 <53631862+PR0M3TH3AN@users.noreply.github.com> --- src/lock-ops.mjs | 6 +----- src/lock-publisher.mjs | 5 +---- src/lock-utils.mjs | 6 ++---- src/utils.mjs | 4 ++++ test/utils.test.mjs | 21 ++++++++++++++++++++- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/lock-ops.mjs b/src/lock-ops.mjs index 32891b6..ad8fef3 100644 --- a/src/lock-ops.mjs +++ b/src/lock-ops.mjs @@ -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])]; } diff --git a/src/lock-publisher.mjs b/src/lock-publisher.mjs index b506f1b..c55ed5b 100644 --- a/src/lock-publisher.mjs +++ b/src/lock-publisher.mjs @@ -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])]; } diff --git a/src/lock-utils.mjs b/src/lock-utils.mjs index cac3fdc..3cd93f8 100644 --- a/src/lock-utils.mjs +++ b/src/lock-utils.mjs @@ -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])]; } diff --git a/src/utils.mjs b/src/utils.mjs index af844e2..fde05f0 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -47,3 +47,7 @@ export function ensureDir(dir) { fs.mkdirSync(dir, { recursive: true }); } } + +export function relayListLabel(relays) { + return relays.join(', '); +} diff --git a/test/utils.test.mjs b/test/utils.test.mjs index b2d2785..6229cb5 100644 --- a/test/utils.test.mjs +++ b/test/utils.test.mjs @@ -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), ''); + }); + }); +});