Skip to content

Commit d51efef

Browse files
authored
refactor(api-proxy): extract duplicate collectLogOutput() into shared test helper (#4207)
* Initial plan * refactor: extract collectLogOutput into shared log-test-helpers.js Resolves duplicate helper copied across three api-proxy test files. Extract to containers/api-proxy/test-helpers/log-test-helpers.js and import from there in the three consumers. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 231ce27 commit d51efef

4 files changed

Lines changed: 26 additions & 42 deletions

File tree

containers/api-proxy/guards/effective-token-guard.test.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,7 @@ const {
44
getEffectiveTokenReflectState,
55
resetEffectiveTokenGuardForTests,
66
} = require('./effective-token-guard');
7-
8-
function collectLogOutput() {
9-
const lines = [];
10-
const spy = jest.spyOn(process.stdout, 'write').mockImplementation((data) => {
11-
try {
12-
lines.push(JSON.parse(data.toString()));
13-
} catch {
14-
// ignore non-JSON writes
15-
}
16-
return true;
17-
});
18-
return { lines, spy };
19-
}
7+
const { collectLogOutput } = require('../test-helpers/log-test-helpers');
208

219
describe('effective-token-guard reflect state', () => {
2210
beforeEach(() => {

containers/api-proxy/server.billing.test.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const https = require('https');
88
const { EventEmitter } = require('events');
99

1010
const { validateApiKeys, keyValidationResults, resetKeyValidationState, extractBillingHeaders } = require('./server');
11+
const { collectLogOutput } = require('./test-helpers/log-test-helpers');
1112

1213
// ── Helpers for validateApiKeys tests ──────────────────────────────────────────
1314

@@ -32,22 +33,6 @@ function mockHttpsRequestWithStatus(statusCode) {
3233
});
3334
}
3435

35-
/**
36-
* Collect structured log lines emitted by logRequest() (written to process.stdout).
37-
*/
38-
function collectLogOutput() {
39-
const lines = [];
40-
const spy = jest.spyOn(process.stdout, 'write').mockImplementation((data) => {
41-
try {
42-
lines.push(JSON.parse(data.toString()));
43-
} catch {
44-
// ignore non-JSON writes
45-
}
46-
return true;
47-
});
48-
return { lines, spy };
49-
}
50-
5136
function createValidationAdapter(name, probe) {
5237
return {
5338
name,

containers/api-proxy/server.lifecycle.test.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,7 @@ const { EventEmitter } = require('events');
1010

1111
const { fetchStartupModels, healthResponse, createProviderServer, resetModelCacheState } = require('./server');
1212
const { createCopilotAdapter } = require('./providers/copilot');
13-
14-
function collectLogOutput() {
15-
const lines = [];
16-
const spy = jest.spyOn(process.stdout, 'write').mockImplementation((data) => {
17-
try {
18-
lines.push(JSON.parse(data.toString()));
19-
} catch {
20-
// ignore non-JSON writes
21-
}
22-
return true;
23-
});
24-
return { lines, spy };
25-
}
13+
const { collectLogOutput } = require('./test-helpers/log-test-helpers');
2614

2715
describe('healthResponse', () => {
2816
afterEach(() => {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
/**
4+
* Spy on process.stdout.write and collect any structured JSON log lines emitted
5+
* during a test. Call spy.mockRestore() (or jest.restoreAllMocks()) in afterEach
6+
* to clean up.
7+
*
8+
* @returns {{ lines: object[], spy: jest.SpyInstance }}
9+
*/
10+
function collectLogOutput() {
11+
const lines = [];
12+
const spy = jest.spyOn(process.stdout, 'write').mockImplementation((data) => {
13+
try {
14+
lines.push(JSON.parse(data.toString()));
15+
} catch {
16+
// ignore non-JSON writes
17+
}
18+
return true;
19+
});
20+
return { lines, spy };
21+
}
22+
23+
module.exports = { collectLogOutput };

0 commit comments

Comments
 (0)