refactor(api-proxy tests): extract shared upstream mock cycle helpers#5028
Conversation
…teUpstreamResponse helpers
Reduces ~70 lines of duplicated upstream request/response scaffolding
across server.token-guards.test.js and server.token-steering.test.js.
- Add createMockUpstreamCycle(https) to server-mock-factories.js: sets up
the https.request spy, captures the upstream response handler, and
returns a pre-built mock upstream request via makeProxyReq().
- Add completeUpstreamResponse(responseHandler, { statusCode, body }) to
server-mock-factories.js: calls the captured handler with a mocked
proxy response, optionally emits a JSON data chunk, then emits 'end'.
- Refactor 6 tests in server.token-guards.test.js to use these helpers.
- Refactor 2 tests in server.token-steering.test.js to use these helpers.
- Replace all remaining inline `new EventEmitter() + jest.fn()` upstream
request patterns with the existing makeProxyReq() factory.
- Remove the now-unused `const { EventEmitter } = require('events')`
import from server.token-guards.test.js.
Closes #5003
✅ Coverage Check PassedOverall Coverage
📁 Per-file Coverage Changes (1 files)
Coverage comparison generated by |
There was a problem hiding this comment.
Pull request overview
Refactors the api-proxy Jest tests to reduce duplicated upstream HTTPS request/response mock scaffolding by introducing reusable helper functions, improving readability and consistency across token guard/steering test suites.
Changes:
- Added
createMockUpstreamCycle()andcompleteUpstreamResponse()helpers to standardize upstream mock setup and response completion. - Replaced repeated inline
EventEmitterupstream request stubs with the existingmakeProxyReq()factory across token steering/guard tests. - Simplified token guard tests by using the new helpers and removing an unused
EventEmitterimport.
Show a summary per file
| File | Description |
|---|---|
| containers/api-proxy/test-helpers/server-mock-factories.js | Adds shared upstream-cycle mock helpers for HTTPS request spying and response completion. |
| containers/api-proxy/server.token-steering.test.js | Refactors upstream mock setup to use makeProxyReq() and completeUpstreamResponse() instead of duplicated scaffolding. |
| containers/api-proxy/server.token-guards.test.js | Refactors multiple guard tests to use createMockUpstreamCycle() / completeUpstreamResponse() and makeProxyReq(). |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 1
| function completeUpstreamResponse(responseHandler, { statusCode = 200, body = null } = {}) { | ||
| const proxyRes = makeProxyRes(statusCode); | ||
| responseHandler(proxyRes); | ||
| if (body !== null) { |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Smoke test results:
Running in direct BYOK mode (AWF_AUTH_TYPE=github-oidc + AWF_AUTH_AZURE_* + COPILOT_PROVIDER_BASE_URL) via api-proxy → Azure OpenAI (Foundry, o4-mini-aw) authenticated via Microsoft Entra Overall: PASS
|
This comment has been minimized.
This comment has been minimized.
Throws a descriptive error if called before the https.request callback is captured, rather than the generic 'is not a function' TypeError. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
🔬 Smoke Test ResultsPR: refactor(api-proxy tests): extract shared upstream mock cycle helpers
Overall: FAIL — Pre-step template variables were not substituted at runtime.
|
|
PR: refactor(api-proxy tests): extract shared upstream mock cycle helpers Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "registry.npmjs.org"See Network Configuration for more information.
|
Chroot Version Comparison Results
Overall: ❌ Not all tests passed — Python and Node.js versions differ between host and chroot environments.
|
Smoke Test: API Proxy OpenTelemetry Tracing
All scenarios pass. OTEL tracing integration is complete and working correctly.
|
🔥 Smoke Test: PAT Auth — PASS
Overall: PASS · Auth mode: PAT (COPILOT_GITHUB_TOKEN) cc
|
✅ Smoke Test: Copilot BYOK (Direct) Mode — PASSTest Results:
Status: All smoke tests passed. Agent running with BYOK credentials routed through api-proxy sidecar. CC:
|
🏗️ Build Test Suite Results
Overall: 8/8 ecosystems passed — ✅ PASS
|
Smoke Test: GitHub Actions Services Connectivity
Overall: FAIL —
|
Gemini Smoke Test Results
Overall Status: FAIL Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "localhost"See Network Configuration for more information.
|
~70 lines of identical upstream HTTP mock scaffolding were duplicated across 8 test cases in
server.token-guards.test.jsandserver.token-steering.test.js. Each test hand-built the sameEventEmitterupstream request,https.requestspy, proxy response, and event emission sequence.New helpers in
test-helpers/server-mock-factories.jscreateMockUpstreamCycle(https)— spies onhttps.request, captures the upstream response handler, and returns{ spy, upstreamRequest, responseHandler }(handler exposed via getter, populated afterflushPromises())completeUpstreamResponse(responseHandler, { statusCode, body })— calls the handler with amakeProxyRes(), optionally emits a JSON data chunk, then emits'end'Refactoring
Before:
After:
new EventEmitter() + jest.fn()upstream request constructions in both files replaced with the existingmakeProxyReq()factoryconst { EventEmitter } = require('events')import fromserver.token-guards.test.js