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

simplify: share loopback-server test helpers between ask.test.js and openclaw.test.js

[._.]/ Adam Eivy1842c6a4b470
diff --git a/server/lib/README.md b/server/lib/README.md
index 042b58fbbd..bebc35bdf6 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -372,4 +372,4 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `mirrorParity.js` | Source-comparison primitives for the `*.mirror.test.js` server↔client parity tests: `stripCommentsAndNormalize` (so per-side commentary may diverge but logic may not), `extractDeclaration(src, name)` (balanced `{}`/`()`/`[]` walk over `function` / `async function` / `const`), and `compareDeclaration(serverSrc, clientSrc, name)`. Use these instead of hand-rolling a brace-walker per mirror. Pure — no `vitest` import — so callers own the assertions. |
| `mockPathsDataRoot.js` | Shared Vitest helpers for `PATHS.data → temp dir` and no-peer record creation guards. |
| `settingsTestUtil.js` | `bindSettingsFile(dataRoot)` → `writeSettingsFile`/`mergeSettingsFile`: direct settings.json disk writes that also drop the `getSettings()` read cache (dynamic-import reset) so a stale cache can't survive a bypass-`save()` write. |
-| `testHelper.js` | Test helpers: `request()` (supertest-style HTTP) + `mockJsonResponse`/`mockTextResponse` (fetch `Response` mocks read via `.text()`), plus the source-scan pair `collectServerSources()` / `readServerSource(rel)` (and `SERVER_DIR`) used by the whole-tree guard suites — `spawnCwd.test.js` (#3193) and `cliChildEnv.test.js` (#3194). Those guards overlap deliberately, so they share one definition of "a source file"; change the ignore rules here and both move together. Cross-platform trio: `posixPath(v)` normalizes a RECEIVED path before comparing it to a POSIX-spelled literal (no-op on POSIX — never normalize the expectation, which would hide a genuinely wrong path), and `resolveTestPython()` returns an interpreter that actually runs, probing by execution because Windows ships a `python` Store-alias stub that exists but fails; `null` when there is none, for `describe.skipIf`; `pinPlatform(value)` pins `process.platform` and returns a restore that reinstates the ORIGINAL descriptor (deleting the pin when there was none) — it carries the one hazard every hand-rolled pin had to rediscover: never pin above an import that loads a native addon, which picks its prebuilt binary off the platform at load time (#4085). |
+| `testHelper.js` | Test helpers: `request()` (supertest-style HTTP) + `mockJsonResponse`/`mockTextResponse` (fetch `Response` mocks read via `.text()`), `startLoopbackServer(app)`/`closeLoopbackServer(server)`/`waitForAbort(signal)` for tests that need a real socket (raw disconnects, SSE streaming) that `request()`'s run-to-completion fetch harness can't model, plus the source-scan pair `collectServerSources()` / `readServerSource(rel)` (and `SERVER_DIR`) used by the whole-tree guard suites — `spawnCwd.test.js` (#3193) and `cliChildEnv.test.js` (#3194). Those guards overlap deliberately, so they share one definition of "a source file"; change the ignore rules here and both move together. Cross-platform trio: `posixPath(v)` normalizes a RECEIVED path before comparing it to a POSIX-spelled literal (no-op on POSIX — never normalize the expectation, which would hide a genuinely wrong path), and `resolveTestPython()` returns an interpreter that actually runs, probing by execution because Windows ships a `python` Store-alias stub that exists but fails; `null` when there is none, for `describe.skipIf`; `pinPlatform(value)` pins `process.platform` and returns a restore that reinstates the ORIGINAL descriptor (deleting the pin when there was none) — it carries the one hazard every hand-rolled pin had to rediscover: never pin above an import that loads a native addon, which picks its prebuilt binary off the platform at load time (#4085). |
diff --git a/server/lib/testHelper.js b/server/lib/testHelper.js
index cbaa8c159f..7322e67d4e 100644
--- a/server/lib/testHelper.js
+++ b/server/lib/testHelper.js
@@ -31,6 +31,32 @@ function closeServer(server) {
return new Promise((resolve, reject) => server.close(err => err ? reject(err) : resolve()));
}
+/**
+ * Start a real loopback HTTP server for tests that need to drive an actual
+ * socket (raw disconnect via `httpRequest(...).destroy()`, SSE streaming) —
+ * scenarios `request()`'s fetch-based harness can't model because it always
+ * runs a request to completion. Reject on a listen error instead of hanging.
+ */
+export function startLoopbackServer(app) {
+ return new Promise((resolve, reject) => {
+ const server = createServer(app);
+ server.once('error', reject);
+ server.listen(0, '127.0.0.1', () => resolve(server));
+ });
+}
+
+/** Close a server started with `startLoopbackServer`. */
+export function closeLoopbackServer(server) {
+ return new Promise((resolve) => server.close(resolve));
+}
+
+/** Resolve once an `AbortSignal` fires (or immediately if already aborted). */
+export function waitForAbort(signal) {
+ return signal.aborted
+ ? Promise.resolve()
+ : new Promise((resolve) => signal.addEventListener('abort', resolve, { once: true }));
+}
+
class RequestBuilder {
constructor(app, method, path) {
this._app = app;
diff --git a/server/routes/ask.test.js b/server/routes/ask.test.js
index 3b828741d1..1bcdd3bc85 100644
--- a/server/routes/ask.test.js
+++ b/server/routes/ask.test.js
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import express from 'express';
-import { createServer, request as httpRequest } from 'node:http';
-import { request } from '../lib/testHelper.js';
+import { request as httpRequest } from 'node:http';
+import { request, startLoopbackServer, closeLoopbackServer, waitForAbort } from '../lib/testHelper.js';
import { errorMiddleware } from '../lib/errorHandler.js';
vi.mock('../services/askConversations.js', () => ({
@@ -51,18 +51,6 @@ const makeApp = () => {
return app;
};
-const startLoopbackServer = (app) => new Promise((resolve, reject) => {
- const server = createServer(app);
- server.once('error', reject);
- server.listen(0, '127.0.0.1', () => resolve(server));
-});
-
-const closeLoopbackServer = (server) => new Promise((resolve) => server.close(resolve));
-
-const waitForAbort = (signal) => (signal.aborted
- ? Promise.resolve()
- : new Promise((resolve) => signal.addEventListener('abort', resolve, { once: true })));
-
beforeEach(() => {
vi.clearAllMocks();
});
diff --git a/server/routes/openclaw.test.js b/server/routes/openclaw.test.js
index b5a834aaed..2d7dd5f1c1 100644
--- a/server/routes/openclaw.test.js
+++ b/server/routes/openclaw.test.js
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import express from 'express';
-import { createServer, request as httpRequest } from 'node:http';
-import { request } from '../lib/testHelper.js';
+import { request as httpRequest } from 'node:http';
+import { request, startLoopbackServer, closeLoopbackServer, waitForAbort } from '../lib/testHelper.js';
import openclawRoutes, { sendMessageSchema } from './openclaw.js';
vi.mock('../integrations/openclaw/api.js', () => ({
@@ -15,18 +15,6 @@ vi.mock('../integrations/openclaw/api.js', () => ({
import * as openclawApi from '../integrations/openclaw/api.js';
-const startLoopbackServer = (app) => new Promise((resolve, reject) => {
- const server = createServer(app);
- server.once('error', reject);
- server.listen(0, '127.0.0.1', () => resolve(server));
-});
-
-const closeLoopbackServer = (server) => new Promise((resolve) => server.close(resolve));
-
-const waitForAbort = (signal) => (signal.aborted
- ? Promise.resolve()
- : new Promise((resolve) => signal.addEventListener('abort', resolve, { once: true })));
-
const CONFIGURED_STATUS = {
configured: true,
enabled: true,