diff --git a/containers/api-proxy/guards/effective-token-guard.test.js b/containers/api-proxy/guards/effective-token-guard.test.js index 9b66490dd..dc55df8ae 100644 --- a/containers/api-proxy/guards/effective-token-guard.test.js +++ b/containers/api-proxy/guards/effective-token-guard.test.js @@ -4,19 +4,7 @@ const { getEffectiveTokenReflectState, resetEffectiveTokenGuardForTests, } = require('./effective-token-guard'); - -function collectLogOutput() { - const lines = []; - const spy = jest.spyOn(process.stdout, 'write').mockImplementation((data) => { - try { - lines.push(JSON.parse(data.toString())); - } catch { - // ignore non-JSON writes - } - return true; - }); - return { lines, spy }; -} +const { collectLogOutput } = require('../test-helpers/log-test-helpers'); describe('effective-token-guard reflect state', () => { beforeEach(() => { diff --git a/containers/api-proxy/server.billing.test.js b/containers/api-proxy/server.billing.test.js index f8d6e59a2..ead48a1e5 100644 --- a/containers/api-proxy/server.billing.test.js +++ b/containers/api-proxy/server.billing.test.js @@ -8,6 +8,7 @@ const https = require('https'); const { EventEmitter } = require('events'); const { validateApiKeys, keyValidationResults, resetKeyValidationState, extractBillingHeaders } = require('./server'); +const { collectLogOutput } = require('./test-helpers/log-test-helpers'); // ── Helpers for validateApiKeys tests ────────────────────────────────────────── @@ -32,22 +33,6 @@ function mockHttpsRequestWithStatus(statusCode) { }); } -/** - * Collect structured log lines emitted by logRequest() (written to process.stdout). - */ -function collectLogOutput() { - const lines = []; - const spy = jest.spyOn(process.stdout, 'write').mockImplementation((data) => { - try { - lines.push(JSON.parse(data.toString())); - } catch { - // ignore non-JSON writes - } - return true; - }); - return { lines, spy }; -} - function createValidationAdapter(name, probe) { return { name, diff --git a/containers/api-proxy/server.lifecycle.test.js b/containers/api-proxy/server.lifecycle.test.js index 8ad667eb9..8c195efbf 100644 --- a/containers/api-proxy/server.lifecycle.test.js +++ b/containers/api-proxy/server.lifecycle.test.js @@ -10,19 +10,7 @@ const { EventEmitter } = require('events'); const { fetchStartupModels, healthResponse, createProviderServer, resetModelCacheState } = require('./server'); const { createCopilotAdapter } = require('./providers/copilot'); - -function collectLogOutput() { - const lines = []; - const spy = jest.spyOn(process.stdout, 'write').mockImplementation((data) => { - try { - lines.push(JSON.parse(data.toString())); - } catch { - // ignore non-JSON writes - } - return true; - }); - return { lines, spy }; -} +const { collectLogOutput } = require('./test-helpers/log-test-helpers'); describe('healthResponse', () => { afterEach(() => { diff --git a/containers/api-proxy/test-helpers/log-test-helpers.js b/containers/api-proxy/test-helpers/log-test-helpers.js new file mode 100644 index 000000000..f45a58887 --- /dev/null +++ b/containers/api-proxy/test-helpers/log-test-helpers.js @@ -0,0 +1,23 @@ +'use strict'; + +/** + * Spy on process.stdout.write and collect any structured JSON log lines emitted + * during a test. Call spy.mockRestore() (or jest.restoreAllMocks()) in afterEach + * to clean up. + * + * @returns {{ lines: object[], spy: jest.SpyInstance }} + */ +function collectLogOutput() { + const lines = []; + const spy = jest.spyOn(process.stdout, 'write').mockImplementation((data) => { + try { + lines.push(JSON.parse(data.toString())); + } catch { + // ignore non-JSON writes + } + return true; + }); + return { lines, spy }; +} + +module.exports = { collectLogOutput };