mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-10-27 16:01:54 +00:00
Merge pull request #270 from PR0M3TH3AN/chore/remove-dead-code-cmd-check-6250725036284174249
🧹 Remove dead code: src/cmd-check.mjs
This commit is contained in:
3 files changed
+72
-251
No files matched your search
@@ -117,7 +117,7 @@
|
||||
"importance": 0.25,
|
||||
"embedding_id": null,
|
||||
"created_at": 1771370050385,
|
||||
"last_seen": 1771385274968,
|
||||
"last_seen": 1771384958445,
|
||||
"source": "ingest",
|
||||
"ttl_days": null,
|
||||
"merged_into": null,
|
||||
@@ -140,7 +140,7 @@
|
||||
"importance": 0.25,
|
||||
"embedding_id": null,
|
||||
"created_at": 1771370084740,
|
||||
"last_seen": 1771385274968,
|
||||
"last_seen": 1771384958445,
|
||||
"source": "ingest",
|
||||
"ttl_days": null,
|
||||
"merged_into": null,
|
||||
@@ -163,7 +163,7 @@
|
||||
"importance": 0.25,
|
||||
"embedding_id": null,
|
||||
"created_at": 1771370148079,
|
||||
"last_seen": 1771385274968,
|
||||
"last_seen": 1771384958445,
|
||||
"source": "ingest",
|
||||
"ttl_days": null,
|
||||
"merged_into": null,
|
||||
@@ -245,5 +245,74 @@
|
||||
"merged_into": null,
|
||||
"pinned": false
|
||||
}
|
||||
],
|
||||
[
|
||||
"71791fc3-49a7-40f2-a8e5-c93e8b19f32b",
|
||||
{
|
||||
"schema_version": 1,
|
||||
"id": "71791fc3-49a7-40f2-a8e5-c93e8b19f32b",
|
||||
"agent_id": "agent-telemetry",
|
||||
"session_id": "s-telemetry",
|
||||
"type": "batch_window",
|
||||
"content": "customer token ABC123 secret",
|
||||
"summary": "customer token ABC123 secret",
|
||||
"tags": [
|
||||
"incident"
|
||||
],
|
||||
"importance": 0.25,
|
||||
"embedding_id": null,
|
||||
"created_at": 1771384842151,
|
||||
"last_seen": 1771384958445,
|
||||
"source": "ingest",
|
||||
"ttl_days": null,
|
||||
"merged_into": null,
|
||||
"pinned": false
|
||||
}
|
||||
],
|
||||
[
|
||||
"ca60066d-1d01-43de-a597-573d9826d101",
|
||||
{
|
||||
"schema_version": 1,
|
||||
"id": "ca60066d-1d01-43de-a597-573d9826d101",
|
||||
"agent_id": "agent-telemetry",
|
||||
"session_id": "s-telemetry",
|
||||
"type": "batch_window",
|
||||
"content": "customer token ABC123 secret",
|
||||
"summary": "customer token ABC123 secret",
|
||||
"tags": [
|
||||
"incident"
|
||||
],
|
||||
"importance": 0.25,
|
||||
"embedding_id": null,
|
||||
"created_at": 1771384870483,
|
||||
"last_seen": 1771384958445,
|
||||
"source": "ingest",
|
||||
"ttl_days": null,
|
||||
"merged_into": null,
|
||||
"pinned": false
|
||||
}
|
||||
],
|
||||
[
|
||||
"b95b047e-6bc6-4342-a535-8d311ebc7f1a",
|
||||
{
|
||||
"schema_version": 1,
|
||||
"id": "b95b047e-6bc6-4342-a535-8d311ebc7f1a",
|
||||
"agent_id": "agent-telemetry",
|
||||
"session_id": "s-telemetry",
|
||||
"type": "batch_window",
|
||||
"content": "customer token ABC123 secret",
|
||||
"summary": "customer token ABC123 secret",
|
||||
"tags": [
|
||||
"incident"
|
||||
],
|
||||
"importance": 0.25,
|
||||
"embedding_id": null,
|
||||
"created_at": 1771384958439,
|
||||
"last_seen": 1771384958445,
|
||||
"source": "ingest",
|
||||
"ttl_days": null,
|
||||
"merged_into": null,
|
||||
"pinned": false
|
||||
}
|
||||
]
|
||||
]
|
||||
@@ -1,61 +0,0 @@
|
||||
import {
|
||||
loadTorchConfig,
|
||||
getRelays,
|
||||
getNamespace,
|
||||
} from './torch-config.mjs';
|
||||
import { getRoster } from './roster.mjs';
|
||||
import { queryLocks } from './lock-ops.mjs';
|
||||
import { todayDateStr } from './utils.mjs';
|
||||
|
||||
export async function cmdCheck(cadence, deps = {}) {
|
||||
const {
|
||||
loadTorchConfigFn = loadTorchConfig,
|
||||
getRelaysFn = getRelays,
|
||||
getNamespaceFn = getNamespace,
|
||||
getRosterFn = getRoster,
|
||||
queryLocksFn = queryLocks,
|
||||
todayDateStrFn = todayDateStr,
|
||||
} = deps;
|
||||
|
||||
const relays = await getRelaysFn();
|
||||
const namespace = await getNamespaceFn();
|
||||
const dateStr = todayDateStrFn();
|
||||
const config = await loadTorchConfigFn();
|
||||
const pausedAgents = (cadence === 'daily' ? config.scheduler.paused.daily : config.scheduler.paused.weekly) || [];
|
||||
|
||||
console.error(`Checking locks: namespace=${namespace}, cadence=${cadence}, date=${dateStr}`);
|
||||
console.error(`Relays: ${relays.join(', ')}`);
|
||||
if (pausedAgents.length > 0) {
|
||||
console.error(`Paused agents: ${pausedAgents.join(', ')}`);
|
||||
}
|
||||
|
||||
const locks = await queryLocksFn(relays, cadence, dateStr, namespace);
|
||||
const lockedAgents = [...new Set(locks.map((l) => l.agent).filter(Boolean))];
|
||||
const roster = await getRosterFn(cadence);
|
||||
|
||||
const excludedAgents = [...new Set([...lockedAgents, ...pausedAgents])];
|
||||
const unknownLockedAgents = lockedAgents.filter((agent) => !roster.includes(agent));
|
||||
const available = roster.filter((a) => !excludedAgents.includes(a));
|
||||
|
||||
const result = {
|
||||
namespace,
|
||||
cadence,
|
||||
date: dateStr,
|
||||
locked: lockedAgents.sort(),
|
||||
paused: pausedAgents.sort(),
|
||||
excluded: excludedAgents.sort(),
|
||||
available: available.sort(),
|
||||
lockCount: locks.length,
|
||||
unknownLockedAgents: unknownLockedAgents.sort(),
|
||||
locks: locks.map((l) => ({
|
||||
agent: l.agent,
|
||||
eventId: l.eventId,
|
||||
createdAt: l.createdAtIso,
|
||||
expiresAt: l.expiresAtIso,
|
||||
platform: l.platform,
|
||||
})),
|
||||
};
|
||||
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
return result;
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
import { describe, it, mock, afterEach, after } from 'node:test';
|
||||
import assert from 'node:assert';
|
||||
import { cmdCheck } from '../src/cmd-check.mjs';
|
||||
|
||||
// Mocks
|
||||
const mockLoadTorchConfig = mock.fn(() => ({
|
||||
scheduler: {
|
||||
paused: {
|
||||
daily: [],
|
||||
weekly: [],
|
||||
},
|
||||
},
|
||||
}));
|
||||
const mockGetRelays = mock.fn(() => ['wss://mock-relay']);
|
||||
const mockGetNamespace = mock.fn(() => 'mock-namespace');
|
||||
const mockGetRoster = mock.fn(() => []);
|
||||
const mockQueryLocks = mock.fn(async () => []);
|
||||
const mockTodayDateStr = mock.fn(() => '2023-10-27');
|
||||
|
||||
const deps = {
|
||||
loadTorchConfigFn: mockLoadTorchConfig,
|
||||
getRelaysFn: mockGetRelays,
|
||||
getNamespaceFn: mockGetNamespace,
|
||||
getRosterFn: mockGetRoster,
|
||||
queryLocksFn: mockQueryLocks,
|
||||
todayDateStrFn: mockTodayDateStr,
|
||||
};
|
||||
|
||||
describe('cmdCheck', () => {
|
||||
// Capture console output
|
||||
const originalConsoleLog = console.log;
|
||||
const originalConsoleError = console.error;
|
||||
let consoleLogOutput = [];
|
||||
let consoleErrorOutput = [];
|
||||
|
||||
const mockConsoleLog = mock.fn((msg) => {
|
||||
consoleLogOutput.push(msg);
|
||||
});
|
||||
const mockConsoleError = mock.fn((msg) => {
|
||||
consoleErrorOutput.push(msg);
|
||||
});
|
||||
|
||||
// Setup console mocks
|
||||
console.log = mockConsoleLog;
|
||||
console.error = mockConsoleError;
|
||||
|
||||
afterEach(() => {
|
||||
// Reset mocks
|
||||
mockLoadTorchConfig.mock.resetCalls();
|
||||
mockGetRelays.mock.resetCalls();
|
||||
mockGetNamespace.mock.resetCalls();
|
||||
mockGetRoster.mock.resetCalls();
|
||||
mockQueryLocks.mock.resetCalls();
|
||||
mockTodayDateStr.mock.resetCalls();
|
||||
|
||||
// Clear captured output
|
||||
consoleLogOutput = [];
|
||||
consoleErrorOutput = [];
|
||||
});
|
||||
|
||||
// Restore console after all tests
|
||||
after(() => {
|
||||
console.log = originalConsoleLog;
|
||||
console.error = originalConsoleError;
|
||||
});
|
||||
|
||||
it('correctly reports available agents when no locks exist', async () => {
|
||||
mockGetRoster.mock.mockImplementation(() => ['agent1', 'agent2']);
|
||||
mockQueryLocks.mock.mockImplementation(async () => []);
|
||||
mockLoadTorchConfig.mock.mockImplementation(() => ({
|
||||
scheduler: { paused: { daily: [] } }
|
||||
}));
|
||||
|
||||
const result = await cmdCheck('daily', deps);
|
||||
|
||||
assert.strictEqual(result.cadence, 'daily');
|
||||
assert.strictEqual(result.namespace, 'mock-namespace');
|
||||
assert.strictEqual(result.date, '2023-10-27');
|
||||
assert.deepStrictEqual(result.available, ['agent1', 'agent2']);
|
||||
assert.deepStrictEqual(result.locked, []);
|
||||
assert.deepStrictEqual(result.paused, []);
|
||||
assert.deepStrictEqual(result.excluded, []);
|
||||
assert.strictEqual(result.lockCount, 0);
|
||||
|
||||
// Verify console output was JSON
|
||||
assert.strictEqual(consoleLogOutput.length, 1);
|
||||
const logOutput = JSON.parse(consoleLogOutput[0]);
|
||||
assert.deepStrictEqual(logOutput.available, ['agent1', 'agent2']);
|
||||
});
|
||||
|
||||
it('correctly identifies locked agents', async () => {
|
||||
mockGetRoster.mock.mockImplementation(() => ['agent1', 'agent2', 'agent3']);
|
||||
mockQueryLocks.mock.mockImplementation(async () => [
|
||||
{ agent: 'agent1', eventId: 'evt1', createdAtIso: '2023-10-27T00:00:00Z', expiresAtIso: '2023-10-27T01:00:00Z', platform: 'linux' }
|
||||
]);
|
||||
mockLoadTorchConfig.mock.mockImplementation(() => ({
|
||||
scheduler: { paused: { daily: [] } }
|
||||
}));
|
||||
|
||||
const result = await cmdCheck('daily', deps);
|
||||
|
||||
assert.deepStrictEqual(result.available, ['agent2', 'agent3']);
|
||||
assert.deepStrictEqual(result.locked, ['agent1']);
|
||||
assert.deepStrictEqual(result.excluded, ['agent1']);
|
||||
assert.strictEqual(result.lockCount, 1);
|
||||
assert.strictEqual(result.locks[0].agent, 'agent1');
|
||||
});
|
||||
|
||||
it('correctly identifies paused agents', async () => {
|
||||
mockGetRoster.mock.mockImplementation(() => ['agent1', 'agent2', 'agent3']);
|
||||
mockQueryLocks.mock.mockImplementation(async () => []);
|
||||
mockLoadTorchConfig.mock.mockImplementation(() => ({
|
||||
scheduler: { paused: { daily: ['agent2'] } }
|
||||
}));
|
||||
|
||||
const result = await cmdCheck('daily', deps);
|
||||
|
||||
assert.deepStrictEqual(result.available, ['agent1', 'agent3']);
|
||||
assert.deepStrictEqual(result.locked, []);
|
||||
assert.deepStrictEqual(result.paused, ['agent2']);
|
||||
assert.deepStrictEqual(result.excluded, ['agent2']);
|
||||
});
|
||||
|
||||
it('handles agents that are both locked and paused', async () => {
|
||||
mockGetRoster.mock.mockImplementation(() => ['agent1', 'agent2']);
|
||||
mockQueryLocks.mock.mockImplementation(async () => [
|
||||
{ agent: 'agent1', eventId: 'evt1' }
|
||||
]);
|
||||
mockLoadTorchConfig.mock.mockImplementation(() => ({
|
||||
scheduler: { paused: { daily: ['agent1'] } }
|
||||
}));
|
||||
|
||||
const result = await cmdCheck('daily', deps);
|
||||
|
||||
assert.deepStrictEqual(result.available, ['agent2']);
|
||||
assert.deepStrictEqual(result.locked, ['agent1']);
|
||||
assert.deepStrictEqual(result.paused, ['agent1']);
|
||||
// excluded should contain unique agents
|
||||
assert.deepStrictEqual(result.excluded, ['agent1']);
|
||||
});
|
||||
|
||||
it('identifies unknown locked agents (not in roster)', async () => {
|
||||
mockGetRoster.mock.mockImplementation(() => ['agent1']);
|
||||
mockQueryLocks.mock.mockImplementation(async () => [
|
||||
{ agent: 'agent1', eventId: 'evt1' },
|
||||
{ agent: 'unknown-agent', eventId: 'evt2' }
|
||||
]);
|
||||
mockLoadTorchConfig.mock.mockImplementation(() => ({
|
||||
scheduler: { paused: { daily: [] } }
|
||||
}));
|
||||
|
||||
const result = await cmdCheck('daily', deps);
|
||||
|
||||
assert.deepStrictEqual(result.available, []);
|
||||
assert.deepStrictEqual(result.locked, ['agent1', 'unknown-agent']);
|
||||
assert.deepStrictEqual(result.unknownLockedAgents, ['unknown-agent']);
|
||||
assert.deepStrictEqual(result.excluded, ['agent1', 'unknown-agent']);
|
||||
});
|
||||
|
||||
it('handles weekly cadence', async () => {
|
||||
mockGetRoster.mock.mockImplementation(() => ['weekly1', 'weekly2']);
|
||||
mockQueryLocks.mock.mockImplementation(async () => []);
|
||||
mockLoadTorchConfig.mock.mockImplementation(() => ({
|
||||
scheduler: { paused: { weekly: ['weekly2'] } }
|
||||
}));
|
||||
|
||||
const result = await cmdCheck('weekly', deps);
|
||||
|
||||
assert.strictEqual(result.cadence, 'weekly');
|
||||
assert.deepStrictEqual(result.available, ['weekly1']);
|
||||
assert.deepStrictEqual(result.paused, ['weekly2']);
|
||||
});
|
||||
|
||||
it('handles empty roster', async () => {
|
||||
mockGetRoster.mock.mockImplementation(() => []);
|
||||
mockQueryLocks.mock.mockImplementation(async () => []);
|
||||
mockLoadTorchConfig.mock.mockImplementation(() => ({
|
||||
scheduler: { paused: { daily: [] } }
|
||||
}));
|
||||
|
||||
const result = await cmdCheck('daily', deps);
|
||||
|
||||
assert.deepStrictEqual(result.available, []);
|
||||
assert.deepStrictEqual(result.locked, []);
|
||||
assert.deepStrictEqual(result.paused, []);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user