Skip to content

Commit 2d95781

Browse files
test(service-automation): stub the host lookup in the http-nodes failed-call suite (#38147)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DDypKu7B9nwbu5WuV7AEio8)_ Fixes #38126 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户无感:只改了自动化服务里一个测试文件,产品代码与线上行为都不变。 - 为什么做:消除一个偶发失败的测试,它依赖真实域名解析,CI 上解析慢时会超时变红。 - 出错会怎样:最坏是这个测试本身再变红,不影响任何用户,可以直接撤回。 - 要你做什么:无。 ## What changes `packages/services/service-automation/src/builtin/http-nodes.test.ts` only (+25 / -3). In the describe `http (canonical node) — a failed call names its host and cause`, `run()` no longer falls back to `defaultEgressGuard()` (whose resolver is the real `dns.promises.lookup`). It hands the node the **real** egress guard, `createEgressGuard({ policy: createEnvEgressPolicy({ allowedTargets: '127.0.0.1' }), resolve })`, through the existing `registerHttpNodes(..., { egress })` seam and the guard's own `resolve` option (documented "DNS override (tests)"; same pattern as `http-node-egress-guard.test.ts`). The stub resolver answers an ENOTFOUND-shaped error for `no-such-host.invalid` and throws on any other name; it records every name asked. - The refusal text assertion and the "nothing of the lookup" assertion (`not.toContain('ENOTFOUND')`) are unchanged. - The two cases that name `no-such-host.invalid` (unknown host; path/query/userinfo secrets) now also assert the stub answered the lookup (`lookups` equals `['no-such-host.invalid']`). - ECONNREFUSED and timeout cases stay real undici against a real loopback socket: an IP literal never reaches the resolver. - No product change: `http-nodes.ts` and the egress guard are untouched. Every other case in the file was checked for a host lookup: `example.test` / `api.test` cases use `stubbedFetchEgress()` or the durable outbox (no fetch); `example.invalid` in the missing-url case never sends. The DNS-off run below records zero lookups for the whole file. ## Tests - `http-nodes.test.ts`: 19/19 passed, normally and with DNS disabled. - Control on `origin/main`'s version with every name lookup made to hang (the CI shape): the two cases time out at 5000 ms, two lookups of `no-such-host.invalid` recorded. - Ablation: stub resolving `no-such-host.invalid` to a public address (93.184.215.14) turns both cases red; restored byte-identical. - Package: `@objectstack/service-automation` typecheck (incl. `check:test-typecheck`) green; full vitest 297/297 files, 4342/4342 tests. <details><summary>DNS-off evidence (a preload hook records and then hangs or fails every non-IP lookup)</summary> ``` # origin/main version, DNS_HOOK=hang × an unknown host reads the refusal ... 5003ms Error: Test timed out in 5000ms. × never names the path, query secret or userinfo of the url 5002ms Tests 2 failed | 17 passed (19) lookups: dns.promises.lookup no-such-host.invalid (x2) # this branch, DNS_HOOK=hang and DNS_HOOK=fail Test Files 1 passed (1) Tests 19 passed (19) lookups recorded: 0 ``` </details> <details><summary>Ablation (scripts/ablation-replace.mjs, anchor x1 -> x0, restored blob == HEAD blob)</summary> ``` × an unknown host reads the refusal ... 23ms AssertionError: expected 'Node \'http\' failed: HTTP 403' to match /http: EGRESS_REFUSED: / × never names the path, query secret or userinfo of the url 14ms AssertionError: expected 'Node \'http\' failed: HTTP 403' to contain 'no-such-host.invalid' Tests 2 failed | 17 passed (19) restore: blob a5bbfa6 == HEAD ``` </details> ## Acceptance notes - `skip-changeset`: test-only diff. `files[]` is `dist`, `README.md`, `CHANGELOG.md`; after a build, the new test symbols have zero hits in `dist/`, and the positive control (`defaultEgressGuard`) has hits. ## Verification Read first: `http-nodes.test.ts` around the `stubbedLookupEgress()` helper (the only risky hunk: the default egress for `run()` changed). Two-minute check: `pnpm --filter @objectstack/service-automation exec vitest run --maxWorkers=2 src/builtin/http-nodes.test.ts` with networking off; expect 19 passed. <details><summary>Local gates at fc05d4e (derived list, dispatch-gates --ran: 53 derived, 52 run, 1 NOT MEASURED)</summary> - 52 derived families exit 0 (incl. `check:nul-bytes`, `check:cross-package-test-inputs`, `check:test-source-alias`, `check:type-check-coverage`, `check:type-check-debt`, `check:issue-citations`). - NOT MEASURED: `check:dual-build-cjs-loads`, exit 3 PREREQUISITE NOT MET (needs a whole-workspace `dist/`; this diff changes no built output). CI runs it. - `overlap-check.mjs`: VERDICT clean exit 0 (origin/main merges cleanly, 0 overlapping PR heads). - Everything else (repo-wide `check:*`, Test Core, Build Core, type-check lanes): CI's. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_016WV4cxRDKprCtjpEdtiPFW)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016WV4cxRDKprCtjpEdtiPFW Co-authored-by: Claude <noreply@anthropic.com>
1 parent e3bdc74 commit 2d95781

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

‎packages/services/service-automation/src/builtin/http-nodes.test.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AutomationEngine } from '../engine.js';
55
import { registerHttpNodes } from './http-nodes.js';
66
import { registerTryCatchNode } from './index.js';
77
import { stubbedFetchEgress } from './stubbed-fetch-egress.testkit.js';
8-
import { egressRefusalText } from '@objectstack/core/egress';
8+
import { createEgressGuard, createEnvEgressPolicy, egressRefusalText, type EgressGuard } from '@objectstack/core/egress';
99

1010
function createTestLogger() {
1111
return {
@@ -261,15 +261,36 @@ describe('http (canonical node)', () => {
261261

262262
describe('http (canonical node) — a failed call names its host and cause', () => {
263263
// Real fetches against real failures: the cause shape is undici's, not a mock's.
264-
async function run(config: Record<string, unknown>, egress?: ReturnType<typeof stubbedFetchEgress>) {
264+
// Only the host lookup is stubbed. `.invalid` never resolves (RFC 6761), but a
265+
// real lookup of it is still a DNS round-trip, and on a slow resolver that
266+
// outlasts the test's timeout — so the guard is the real one, handed a resolver
267+
// that answers ENOTFOUND for the one unknown host this suite names. An IP
268+
// literal never reaches the resolver; any other name is a mistake here.
269+
const lookups: string[] = [];
270+
beforeEach(() => { lookups.length = 0; });
271+
function stubbedLookupEgress(): EgressGuard {
272+
return createEgressGuard({
273+
policy: createEnvEgressPolicy({ allowedTargets: '127.0.0.1' }),
274+
resolve: async (hostname) => {
275+
lookups.push(hostname);
276+
if (hostname === 'no-such-host.invalid') {
277+
throw Object.assign(new Error(`getaddrinfo ENOTFOUND ${hostname}`), { code: 'ENOTFOUND', hostname });
278+
}
279+
throw new Error(`unexpected lookup of ${hostname}: this suite resolves no real host name`);
280+
},
281+
});
282+
}
283+
284+
async function run(config: Record<string, unknown>, egress: EgressGuard = stubbedLookupEgress()) {
265285
const engine = new AutomationEngine(createTestLogger());
266-
registerHttpNodes(engine, createCtx(), egress ? { egress } : {});
286+
registerHttpNodes(engine, createCtx(), { egress });
267287
engine.registerFlow('http_flow', httpFlow('http', config));
268288
return engine.execute('http_flow');
269289
}
270290

271291
it('an unknown host reads the refusal a non-public target reads, naming the host and nothing of the lookup', async () => {
272292
const result = await run({ url: 'http://no-such-host.invalid/path' });
293+
expect(lookups, 'the stubbed resolver answered the lookup').toEqual(['no-such-host.invalid']);
273294
expect(result.success).toBe(false);
274295
expect(result.error).toMatch(/http: EGRESS_REFUSED: /);
275296
expect(result.error!.endsWith(`http: ${egressRefusalText('no-such-host.invalid')}`)).toBe(true);
@@ -291,6 +312,7 @@ describe('http (canonical node) — a failed call names its host and cause', ()
291312
it('never names the path, query secret or userinfo of the url', async () => {
292313
// A query secret on a url that fails to connect.
293314
const q = await run({ url: 'http://no-such-host.invalid/hook/PATHSECRET?token=QUERYSECRET' });
315+
expect(lookups, 'the stubbed resolver answered the lookup').toEqual(['no-such-host.invalid']);
294316
expect(q.error).toContain('no-such-host.invalid');
295317
expect(q.error).not.toContain('QUERYSECRET');
296318
expect(q.error).not.toContain('PATHSECRET');

0 commit comments

Comments
 (0)