Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(harness): bind ephemeral test ports on loopback in every package
Alfredo Lopezcbe09afef157
diff --git a/desktop-app/test/bindLoopbackSetup.test.ts b/desktop-app/test/bindLoopbackSetup.test.tsnew file mode 100644index 000000000..6fca61fc6--- /dev/null+++ b/desktop-app/test/bindLoopbackSetup.test.ts@@ -0,0 +1,65 @@+import { afterEach, expect, it } from 'vitest'+import dns from 'node:dns'+import { type Server, createServer } from 'node:http'+import type { AddressInfo } from 'node:net'++// Witness that `scripts/testing/bind-loopback-in-tests.mjs` is registered in+// this package's vitest `setupFiles`. A package that forgets to register it+// keeps the defect and says nothing: every test still passes while its ephemeral+// binds go back to the dual-stack wildcard, where another process holding the+// same port on `127.0.0.1` receives the test's own requests.+//+// The synchronous reads below are the point, not incidental. supertest calls+// `app.listen(0)` and reads `app.address().port` on the next line+// (`supertest/lib/test.js:63-67`), so a setup file that reaches loopback through+// an asynchronous host resolution satisfies a witness that awaits `listening`+// and still breaks every supertest suite. That revision shipped once; these+// assertions are what would have stopped it.+const servers: Server[] = []++afterEach(async () => {+ await Promise.all(+ servers.splice(0).map(server => new Promise<void>(resolve => server.close(() => resolve())))+ )+})++function track(server: Server): Server {+ servers.push(server)+ return server+}++function boundAddress(server: Server): AddressInfo {+ const address = server.address()+ if (address === null || typeof address === 'string') {+ throw new Error(`expected a bound TCP address, got ${JSON.stringify(address)}`)+ }+ return address+}++it('binds an ephemeral port on the IPv4 loopback, readably, before listen() returns', () => {+ const numeric = track(createServer())+ numeric.listen(0)+ const numericAddress = boundAddress(numeric)+ expect(numericAddress.address).toBe('127.0.0.1')+ expect(numericAddress.port).toBeGreaterThan(0)++ const options = track(createServer())+ options.listen({ port: 0 })+ const optionsAddress = boundAddress(options)+ expect(optionsAddress.address).toBe('127.0.0.1')+ expect(optionsAddress.port).toBeGreaterThan(0)+})++it('leaves a host the caller asked for alone', async () => {+ const explicit = track(createServer())+ await new Promise<void>(resolve => {+ explicit.listen(0, '0.0.0.0', () => resolve())+ })+ expect(boundAddress(explicit).address).toBe('0.0.0.0')+})++it('leaves node:dns as it found it once listen() has returned', () => {+ const beforeListen = dns.lookup+ track(createServer()).listen(0)+ expect(dns.lookup).toBe(beforeListen)+})diff --git a/desktop-app/vitest.config.ts b/desktop-app/vitest.config.tsindex bfd33353f..f4901843e 100644--- a/desktop-app/vitest.config.ts+++ b/desktop-app/vitest.config.ts@@ -2,6 +2,14 @@ import { defineConfig } from 'vitest/config'import path from 'node:path'export default defineConfig({+ server: {+ fs: {+ // The loopback setup file below lives at the repository root, outside+ // this package. The `ui/**` suites are served through Vite's module+ // server, which refuses an out-of-root file unless it is allowed here.+ allow: [path.resolve(__dirname, '..')],+ },+ },resolve: {alias: {'@': path.resolve(__dirname, 'ui/src'),@@ -17,6 +25,10 @@ export default defineConfig({},test: {testTimeout: 30_000,+ // Absolute, because the ui/** suites run through Vite's module server,+ // which serves an out-of-root setup file under `/@fs/` and cannot resolve+ // it from a path relative to this package.+ setupFiles: [path.resolve(__dirname, '../scripts/testing/bind-loopback-in-tests.mjs')],fileParallelism: false,teardownTimeout: 60_000,include: [diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.jsonindex 30bc257eb..7e1bfc9b1 100644--- a/host-context-controller/package-lock.json+++ b/host-context-controller/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-host-context-controller",- "version": "0.1.344",+ "version": "0.1.345","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-host-context-controller",- "version": "0.1.344",+ "version": "0.1.345","license": "SEE LICENSE IN LICENSE","dependencies": {"@clerum/codex-catalog-projection": "file:../packages/codex-catalog-projection",diff --git a/host-context-controller/package.json b/host-context-controller/package.jsonindex e308a7a4c..e106708f1 100644--- a/host-context-controller/package.json+++ b/host-context-controller/package.json@@ -1,6 +1,6 @@{"name": "clerum-host-context-controller",- "version": "0.1.344",+ "version": "0.1.345","description": "Host Context Controller - provides McpServer CRDs to mcp-host via REST API","main": "dist/main.js","engines": {diff --git a/host-context-controller/src/bindLoopbackSetup.test.ts b/host-context-controller/src/bindLoopbackSetup.test.tsnew file mode 100644index 000000000..6fca61fc6--- /dev/null+++ b/host-context-controller/src/bindLoopbackSetup.test.ts@@ -0,0 +1,65 @@+import { afterEach, expect, it } from 'vitest'+import dns from 'node:dns'+import { type Server, createServer } from 'node:http'+import type { AddressInfo } from 'node:net'++// Witness that `scripts/testing/bind-loopback-in-tests.mjs` is registered in+// this package's vitest `setupFiles`. A package that forgets to register it+// keeps the defect and says nothing: every test still passes while its ephemeral+// binds go back to the dual-stack wildcard, where another process holding the+// same port on `127.0.0.1` receives the test's own requests.+//+// The synchronous reads below are the point, not incidental. supertest calls+// `app.listen(0)` and reads `app.address().port` on the next line+// (`supertest/lib/test.js:63-67`), so a setup file that reaches loopback through+// an asynchronous host resolution satisfies a witness that awaits `listening`+// and still breaks every supertest suite. That revision shipped once; these+// assertions are what would have stopped it.+const servers: Server[] = []++afterEach(async () => {+ await Promise.all(+ servers.splice(0).map(server => new Promise<void>(resolve => server.close(() => resolve())))+ )+})++function track(server: Server): Server {+ servers.push(server)+ return server+}++function boundAddress(server: Server): AddressInfo {+ const address = server.address()+ if (address === null || typeof address === 'string') {+ throw new Error(`expected a bound TCP address, got ${JSON.stringify(address)}`)+ }+ return address+}++it('binds an ephemeral port on the IPv4 loopback, readably, before listen() returns', () => {+ const numeric = track(createServer())+ numeric.listen(0)+ const numericAddress = boundAddress(numeric)+ expect(numericAddress.address).toBe('127.0.0.1')+ expect(numericAddress.port).toBeGreaterThan(0)++ const options = track(createServer())+ options.listen({ port: 0 })+ const optionsAddress = boundAddress(options)+ expect(optionsAddress.address).toBe('127.0.0.1')+ expect(optionsAddress.port).toBeGreaterThan(0)+})++it('leaves a host the caller asked for alone', async () => {+ const explicit = track(createServer())+ await new Promise<void>(resolve => {+ explicit.listen(0, '0.0.0.0', () => resolve())+ })+ expect(boundAddress(explicit).address).toBe('0.0.0.0')+})++it('leaves node:dns as it found it once listen() has returned', () => {+ const beforeListen = dns.lookup+ track(createServer()).listen(0)+ expect(dns.lookup).toBe(beforeListen)+})diff --git a/host-context-controller/vitest.config.ts b/host-context-controller/vitest.config.tsindex b6b83c3bf..df9dd7264 100644--- a/host-context-controller/vitest.config.ts+++ b/host-context-controller/vitest.config.ts@@ -1,9 +1,10 @@-import { defineConfig } from "vitest/config";+import { defineConfig } from 'vitest/config'export default defineConfig({test: {globals: false,- environment: "node",- exclude: ["dist/**", "node_modules/**"],+ environment: 'node',+ setupFiles: ['../scripts/testing/bind-loopback-in-tests.mjs'],+ exclude: ['dist/**', 'node_modules/**'],},-});+})diff --git a/workflow-approval-request-reader/test/bindLoopbackSetup.test.ts b/workflow-approval-request-reader/test/bindLoopbackSetup.test.tsnew file mode 100644index 000000000..6fca61fc6--- /dev/null+++ b/workflow-approval-request-reader/test/bindLoopbackSetup.test.ts@@ -0,0 +1,65 @@+import { afterEach, expect, it } from 'vitest'+import dns from 'node:dns'+import { type Server, createServer } from 'node:http'+import type { AddressInfo } from 'node:net'++// Witness that `scripts/testing/bind-loopback-in-tests.mjs` is registered in+// this package's vitest `setupFiles`. A package that forgets to register it+// keeps the defect and says nothing: every test still passes while its ephemeral+// binds go back to the dual-stack wildcard, where another process holding the+// same port on `127.0.0.1` receives the test's own requests.+//+// The synchronous reads below are the point, not incidental. supertest calls+// `app.listen(0)` and reads `app.address().port` on the next line+// (`supertest/lib/test.js:63-67`), so a setup file that reaches loopback through+// an asynchronous host resolution satisfies a witness that awaits `listening`+// and still breaks every supertest suite. That revision shipped once; these+// assertions are what would have stopped it.+const servers: Server[] = []++afterEach(async () => {+ await Promise.all(+ servers.splice(0).map(server => new Promise<void>(resolve => server.close(() => resolve())))+ )+})++function track(server: Server): Server {+ servers.push(server)+ return server+}++function boundAddress(server: Server): AddressInfo {+ const address = server.address()+ if (address === null || typeof address === 'string') {+ throw new Error(`expected a bound TCP address, got ${JSON.stringify(address)}`)+ }+ return address+}++it('binds an ephemeral port on the IPv4 loopback, readably, before listen() returns', () => {+ const numeric = track(createServer())+ numeric.listen(0)+ const numericAddress = boundAddress(numeric)+ expect(numericAddress.address).toBe('127.0.0.1')+ expect(numericAddress.port).toBeGreaterThan(0)++ const options = track(createServer())+ options.listen({ port: 0 })+ const optionsAddress = boundAddress(options)+ expect(optionsAddress.address).toBe('127.0.0.1')+ expect(optionsAddress.port).toBeGreaterThan(0)+})++it('leaves a host the caller asked for alone', async () => {+ const explicit = track(createServer())+ await new Promise<void>(resolve => {+ explicit.listen(0, '0.0.0.0', () => resolve())+ })+ expect(boundAddress(explicit).address).toBe('0.0.0.0')+})++it('leaves node:dns as it found it once listen() has returned', () => {+ const beforeListen = dns.lookup+ track(createServer()).listen(0)+ expect(dns.lookup).toBe(beforeListen)+})diff --git a/workflow-approval-request-reader/vitest.config.ts b/workflow-approval-request-reader/vitest.config.tsnew file mode 100644index 000000000..df9dd7264--- /dev/null+++ b/workflow-approval-request-reader/vitest.config.ts@@ -0,0 +1,10 @@+import { defineConfig } from 'vitest/config'++export default defineConfig({+ test: {+ globals: false,+ environment: 'node',+ setupFiles: ['../scripts/testing/bind-loopback-in-tests.mjs'],+ exclude: ['dist/**', 'node_modules/**'],+ },+})diff --git a/workflow-recipes/package-lock.json b/workflow-recipes/package-lock.jsonindex 5ad6ef60d..2104b456b 100644--- a/workflow-recipes/package-lock.json+++ b/workflow-recipes/package-lock.json@@ -1,12 +1,12 @@{"name": "workload-recipes-controller",- "version": "0.1.363",+ "version": "0.1.364","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "workload-recipes-controller",- "version": "0.1.363",+ "version": "0.1.364","license": "SEE LICENSE IN LICENSE","dependencies": {"@aws-sdk/client-s3": "^3.1009.0",diff --git a/workflow-recipes/package.json b/workflow-recipes/package.jsonindex cc02b5d18..334b84f31 100644--- a/workflow-recipes/package.json+++ b/workflow-recipes/package.json@@ -1,6 +1,6 @@{"name": "workload-recipes-controller",- "version": "0.1.363",+ "version": "0.1.364","description": "Workload Recipes Controller (WRC) - WorkloadRecipe lifecycle management and MCP server interface","main": "dist/main.js","engines": {diff --git a/workflow-recipes/tests/unit/bindLoopbackSetup.test.ts b/workflow-recipes/tests/unit/bindLoopbackSetup.test.tsnew file mode 100644index 000000000..6fca61fc6--- /dev/null+++ b/workflow-recipes/tests/unit/bindLoopbackSetup.test.ts@@ -0,0 +1,65 @@+import { afterEach, expect, it } from 'vitest'+import dns from 'node:dns'+import { type Server, createServer } from 'node:http'+import type { AddressInfo } from 'node:net'++// Witness that `scripts/testing/bind-loopback-in-tests.mjs` is registered in+// this package's vitest `setupFiles`. A package that forgets to register it+// keeps the defect and says nothing: every test still passes while its ephemeral+// binds go back to the dual-stack wildcard, where another process holding the+// same port on `127.0.0.1` receives the test's own requests.+//+// The synchronous reads below are the point, not incidental. supertest calls+// `app.listen(0)` and reads `app.address().port` on the next line+// (`supertest/lib/test.js:63-67`), so a setup file that reaches loopback through+// an asynchronous host resolution satisfies a witness that awaits `listening`+// and still breaks every supertest suite. That revision shipped once; these+// assertions are what would have stopped it.+const servers: Server[] = []++afterEach(async () => {+ await Promise.all(+ servers.splice(0).map(server => new Promise<void>(resolve => server.close(() => resolve())))+ )+})++function track(server: Server): Server {+ servers.push(server)+ return server+}++function boundAddress(server: Server): AddressInfo {+ const address = server.address()+ if (address === null || typeof address === 'string') {+ throw new Error(`expected a bound TCP address, got ${JSON.stringify(address)}`)+ }+ return address+}++it('binds an ephemeral port on the IPv4 loopback, readably, before listen() returns', () => {+ const numeric = track(createServer())+ numeric.listen(0)+ const numericAddress = boundAddress(numeric)+ expect(numericAddress.address).toBe('127.0.0.1')+ expect(numericAddress.port).toBeGreaterThan(0)++ const options = track(createServer())+ options.listen({ port: 0 })+ const optionsAddress = boundAddress(options)+ expect(optionsAddress.address).toBe('127.0.0.1')+ expect(optionsAddress.port).toBeGreaterThan(0)+})++it('leaves a host the caller asked for alone', async () => {+ const explicit = track(createServer())+ await new Promise<void>(resolve => {+ explicit.listen(0, '0.0.0.0', () => resolve())+ })+ expect(boundAddress(explicit).address).toBe('0.0.0.0')+})++it('leaves node:dns as it found it once listen() has returned', () => {+ const beforeListen = dns.lookup+ track(createServer()).listen(0)+ expect(dns.lookup).toBe(beforeListen)+})diff --git a/workflow-recipes/vitest.config.ts b/workflow-recipes/vitest.config.tsindex 2be9810af..a7c4ea823 100644--- a/workflow-recipes/vitest.config.ts+++ b/workflow-recipes/vitest.config.ts@@ -4,6 +4,7 @@ export default defineConfig({test: {globals: false,environment: 'node',+ setupFiles: ['../scripts/testing/bind-loopback-in-tests.mjs'],testTimeout: 15_000,exclude: ['tests/e2e/**', 'node_modules/**', 'dist/**'],},