Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: afterEach cleanup, registeredAt assertion, empty-array check, frozen time, init before event handler, move vi.mock before imports

Adam Eivy93f608a633aa
diff --git a/server/services/agents.js b/server/services/agents.js
index d4830edcd9..7322eb8e8d 100644
--- a/server/services/agents.js
+++ b/server/services/agents.js
@@ -63,6 +63,7 @@ export async function getRunningAgents() {
model: spawnedData.model,
workspacePath: spawnedData.workspacePath,
prompt: spawnedData.prompt,
+ registeredAt: spawnedData.registeredAt,
source: 'cos'
})
});
diff --git a/server/services/agents.test.js b/server/services/agents.test.js
index 11503d991a..938a62ecb4 100644
--- a/server/services/agents.test.js
+++ b/server/services/agents.test.js
@@ -55,6 +55,13 @@ describe('agents.js', () => {
// registerSpawnedAgent / unregisterSpawnedAgent — module-level Map
// ===========================================================================
describe('registerSpawnedAgent / unregisterSpawnedAgent', () => {
+ // Clean up any PIDs registered by tests in this describe so they don't
+ // leak into subsequent tests via the module-level spawnedAgentCommands Map.
+ afterEach(() => {
+ unregisterSpawnedAgent(12345);
+ unregisterSpawnedAgent(99999);
+ });
+
it('registers metadata and surfaces it in getRunningAgents', async () => {
// Provide ps output that matches the claude pattern
mockExecWith('12345 1001 0.5 0.3 01:00 /usr/bin/claude --print\n');
@@ -101,8 +108,8 @@ describe('agents.js', () => {
}
});
- it('stores registeredAt timestamp on registration', async () => {
- const before = Date.now();
+ it('killProcess delegates to CoS killAgent for a registered PID', async () => {
+ // Register PID 99999 so killProcess detects it as a CoS-spawned agent
registerSpawnedAgent(99999, {
agentId: 'agent-ts',
taskId: 'task-ts',
@@ -112,16 +119,13 @@ describe('agents.js', () => {
prompt: 'hello'
});
- // getRunningAgents merges data; check the entry surfaced in results
- mockExecWith('99999 1001 0.0 0.1 00:01 /usr/bin/claude\n');
- const agents = await getRunningAgents();
- const found = agents.find(a => a.pid === 99999);
- expect(found).toBeDefined();
- // Cleanup so it doesn't leak into next test
- unregisterSpawnedAgent(99999);
- const after = Date.now();
- // registeredAt comes from Date.now() inside registerSpawnedAgent — just verify the call succeeded
- expect(before).toBeLessThanOrEqual(after);
+ // killProcess for a registered PID should delegate to killAgent (mocked above)
+ await killProcess(99999);
+
+ // Verify the CoS killAgent mock was invoked with the registered agentId —
+ // this proves the registry entry is live and used by killProcess.
+ const { killAgent } = await import('./subAgentSpawner.js');
+ expect(killAgent).toHaveBeenCalledWith('agent-ts');
});
});
@@ -146,9 +150,7 @@ describe('agents.js', () => {
mockExecWith('');
const agents = await getRunningAgents();
- expect(Array.isArray(agents)).toBe(true);
- // May still have entries from other patterns; no assertion on exact length
- // But with blank output for every pattern, all should be empty
+ expect(agents).toEqual([]);
});
it('sorts agents newest-first by startTime', async () => {
diff --git a/server/services/cosRunnerClient.test.js b/server/services/cosRunnerClient.test.js
index 3febb2f445..82b18fae43 100644
--- a/server/services/cosRunnerClient.test.js
+++ b/server/services/cosRunnerClient.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { describe, it, expect, vi, beforeEach, beforeAll } from 'vitest';
// Capture the mock socket at creation time so clearAllMocks() doesn't lose it.
// The socket's `on()` stores listener references so we can invoke them later.
@@ -74,6 +74,12 @@ describe('cosRunnerClient', () => {
// onCosRunnerEvent
// ===========================================================================
describe('onCosRunnerEvent', () => {
+ // Ensure initCosRunnerConnection has been called so capturedSocketListeners is populated,
+ // even when this describe block runs in isolation without the initCosRunnerConnection tests.
+ beforeAll(() => {
+ initCosRunnerConnection();
+ });
+
it('handler is invoked with payload when the socket emits agent:output', () => {
// capturedSocketListeners stores dispatch fns registered during initCosRunnerConnection.
// These are plain function references that survive vi.clearAllMocks().
diff --git a/server/services/subAgentSpawner.test.js b/server/services/subAgentSpawner.test.js
index 91333c13f3..c1d89437e3 100644
--- a/server/services/subAgentSpawner.test.js
+++ b/server/services/subAgentSpawner.test.js
@@ -1,6 +1,5 @@
-import { describe, it, expect, vi } from 'vitest';
-import { isTruthyMeta, isFalsyMeta, selectModelForTask } from './subAgentSpawner.js';
-import { applyAppWorktreeDefault } from './cos.js';
+// vi.mock() calls are hoisted by Vitest before import evaluation, but are placed
+// at the top for clarity and to match the convention in other test files.
// Mock taskLearning so suggestModelTier returns null → pattern-matching path runs.
// This matches the behavior of the old inline selectModelForTask copy.
@@ -24,6 +23,10 @@ vi.mock('./thinkingLevels.js', async (importOriginal) => {
};
});
+import { describe, it, expect, vi } from 'vitest';
+import { isTruthyMeta, isFalsyMeta, selectModelForTask } from './subAgentSpawner.js';
+import { applyAppWorktreeDefault } from './cos.js';
+
/**
* Tests for the subAgentSpawner service
*
diff --git a/server/services/usage.test.js b/server/services/usage.test.js
index 90b59e2584..6121cb3eab 100644
--- a/server/services/usage.test.js
+++ b/server/services/usage.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
/**
* Tests for usage.js — streak calculation logic and getUsageSummary shape.
@@ -42,11 +42,22 @@ function makeUsage(dailyActivity = {}, extras = {}) {
};
}
+// Fixed reference date: noon UTC on a Wednesday to avoid midnight edge cases.
+const FIXED_DATE = new Date('2025-06-11T12:00:00.000Z');
+
describe('usage.js — streak calculations', () => {
beforeEach(async () => {
+ // Freeze time so daysAgo() and usage.js internal new Date() agree,
+ // preventing flakiness when a test run crosses UTC midnight.
+ vi.useFakeTimers();
+ vi.setSystemTime(FIXED_DATE);
vi.clearAllMocks();
});
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
describe('currentStreak', () => {
it('returns 0 when dailyActivity is empty', async () => {
readJSONFile.mockResolvedValueOnce(makeUsage({}));