From 5c23e89e369e08a413bcba9deba4451a5d764a97 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 4 Dec 2026 03:15:57 +0800 Subject: [PATCH] test(javascript): share response consumer fixtures (#16445) --- ...nticAnalysis.response_body_readers.test.ts | 19 ++++----------- ...emanticAnalysis.response_certainty.test.ts | 5 ++-- ...emanticAnalysis.response_endpoints.test.ts | 19 ++++----------- ...ascriptSemanticResponseAssignments.test.ts | 23 ++++--------------- ...scriptSemanticResponseConsumers.fixture.ts | 11 +++++++++ 5 files changed, 28 insertions(+), 49 deletions(-) create mode 100644 src/domain/javascript/javascriptSemanticResponseConsumers.fixture.ts diff --git a/src/domain/javascript/javascriptSemanticAnalysis.response_body_readers.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.response_body_readers.test.ts index b505c53cb6b..56030ee8c8c 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.response_body_readers.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.response_body_readers.test.ts @@ -1,6 +1,6 @@ import { expect, it } from "vitest"; -import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; +import { responseReaderFacts } from "./javascriptSemanticResponseConsumers.fixture.js"; /** * Only `fetch` resolves to a `Response`, and only once its promise settles. @@ -9,15 +9,6 @@ import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; * `fetch` returns a `Promise`. In every one of those the call throws at the call * site instead of reading a body, so it is not a response consumer. */ -const consumersFor = (source: string) => - analyzeJavaScriptSemantics(source) - .requestOperations.filter(({ kind }) => kind === "response-consumer") - .map(({ method, endpoint, resolution }) => ({ - method, - endpoint, - resolution, - })); - it.each([ [ "an un-awaited fetch", @@ -38,7 +29,7 @@ it.each([ `function f(){ const ws = new WebSocket("wss://api.test/x"); return ws.json(); }`, ], ])("emits no response consumer for %s", (_name, source) => { - expect(consumersFor(source)).toEqual([]); + expect(responseReaderFacts(source)).toEqual([]); }); it.each([ @@ -47,7 +38,7 @@ it.each([ ["arrayBuffer", "arrayBuffer"], ] as const)("keeps the settled fetch body reader for %s", (_name, method) => { expect( - consumersFor( + responseReaderFacts( `async function f(){ const res = await fetch("https://api.test/x"); return res.${method}(); }`, ), ).toEqual([ @@ -61,7 +52,7 @@ it.each([ it("keeps a settled fetch reader through a wrapper expression", () => { expect( - consumersFor( + responseReaderFacts( `async function f(){ const res = await fetch("https://api.test/x"); return (res).json(); }`, ), ).toHaveLength(1); @@ -70,7 +61,7 @@ it("keeps a settled fetch reader through a wrapper expression", () => { it("keeps the fetch candidate when the binding also holds another request", () => { // The mixed binding keeps its fetch link, so the consumer stays a candidate // rather than losing the endpoint entirely. - const consumers = consumersFor(` + const consumers = responseReaderFacts(` import http from "node:http"; async function f(){ let res = await fetch("https://api.test/x"); diff --git a/src/domain/javascript/javascriptSemanticAnalysis.response_certainty.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.response_certainty.test.ts index ad597f4f3bd..ac3df429e62 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.response_certainty.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.response_certainty.test.ts @@ -1,10 +1,9 @@ import { expect, it } from "vitest"; import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; +import { responseConsumers } from "./javascriptSemanticResponseConsumers.fixture.js"; const consumers = (source: string) => - analyzeJavaScriptSemantics(source).requestOperations.filter( - ({ kind }) => kind === "response-consumer", - ); + responseConsumers(analyzeJavaScriptSemantics(source)); it.each([ 'let response = await fetch("data:text/plain,original"); response = {text: () => "replacement"}; response.text();', diff --git a/src/domain/javascript/javascriptSemanticAnalysis.response_endpoints.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.response_endpoints.test.ts index 016666db1c0..699e1eb4739 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.response_endpoints.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.response_endpoints.test.ts @@ -1,19 +1,10 @@ import { expect, it } from "vitest"; -import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; - -const responses = (source: string) => - analyzeJavaScriptSemantics(source) - .requestOperations.filter(({ kind }) => kind === "response-consumer") - .map(({ method, endpoint, resolution }) => ({ - method, - endpoint, - resolution, - })); +import { responseReaderFacts } from "./javascriptSemanticResponseConsumers.fixture.js"; it("keeps the endpoint of a uniquely consumed request response", () => { expect( - responses(` + responseReaderFacts(` async function load() { const a = await fetch("https://example.test/a"); const b = await fetch("https://example.test/b"); @@ -36,7 +27,7 @@ it("keeps the endpoint of a uniquely consumed request response", () => { it("leaves an ambiguous response consumer without an endpoint", () => { expect( - responses(` + responseReaderFacts(` async function load(flag) { let response = await fetch("https://example.test/a"); if (flag) response = await fetch("https://example.test/b"); @@ -48,7 +39,7 @@ it("leaves an ambiguous response consumer without an endpoint", () => { it("leaves a response replaced by a non-request value without an endpoint", () => { expect( - responses(` + responseReaderFacts(` async function load(cachedResponse) { let response = await fetch("https://example.test/a"); response = cachedResponse; @@ -87,7 +78,7 @@ it.each([ ] as const)( "keeps a response endpoint only when its request reaches the read: %s", (_name, source, endpoint) => { - expect(responses(source)).toEqual([ + expect(responseReaderFacts(source)).toEqual([ expect.objectContaining({ method: "json", endpoint }), ]); }, diff --git a/src/domain/javascript/javascriptSemanticResponseAssignments.test.ts b/src/domain/javascript/javascriptSemanticResponseAssignments.test.ts index d89a76eb203..4db54fadf46 100644 --- a/src/domain/javascript/javascriptSemanticResponseAssignments.test.ts +++ b/src/domain/javascript/javascriptSemanticResponseAssignments.test.ts @@ -1,6 +1,7 @@ import { expect, it } from "vitest"; import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; +import { responseConsumers } from "./javascriptSemanticResponseConsumers.fixture.js"; it.each([ ["response.json();", "/original"], @@ -25,9 +26,7 @@ it.each([ (operation) => operation.kind === "request", ); expect(request?.endpoint).toBe("/original"); - const consumers = result.requestOperations.filter( - (operation) => operation.kind === "response-consumer", - ); + const consumers = responseConsumers(result); expect(consumers).toHaveLength(1); expect(consumers[0]).toMatchObject({ endpoint, @@ -41,11 +40,7 @@ it("does not prove an incoming response was initialized by a conditional request const result = analyzeJavaScriptSemantics( "let response;if(flag)response=await fetch('/original');response.json();", ); - expect( - result.requestOperations.filter( - (operation) => operation.kind === "response-consumer", - ), - ).toEqual([ + expect(responseConsumers(result)).toEqual([ expect.objectContaining({ endpoint: null, resolution: "partial" }), ]); }); @@ -62,11 +57,7 @@ it.each([ (operation) => operation.kind === "request", ), ).toEqual([expect.objectContaining({ endpoint: "/original" })]); - expect( - result.requestOperations.filter( - (operation) => operation.kind === "response-consumer", - ), - ).toEqual([]); + expect(responseConsumers(result)).toEqual([]); }, ); @@ -74,11 +65,7 @@ it("keeps the existing contract for a response linked to multiple requests", () const result = analyzeJavaScriptSemantics( "let response=await fetch('/original');response=await fetch('/other');response.json();", ); - expect( - result.requestOperations.filter( - (operation) => operation.kind === "response-consumer", - ), - ).toEqual([ + expect(responseConsumers(result)).toEqual([ expect.objectContaining({ endpoint: null, resolution: "partial" }), ]); }); diff --git a/src/domain/javascript/javascriptSemanticResponseConsumers.fixture.ts b/src/domain/javascript/javascriptSemanticResponseConsumers.fixture.ts new file mode 100644 index 00000000000..96f1195df57 --- /dev/null +++ b/src/domain/javascript/javascriptSemanticResponseConsumers.fixture.ts @@ -0,0 +1,11 @@ +import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; + +export const responseConsumers = ( + result: ReturnType, +) => + result.requestOperations.filter(({ kind }) => kind === "response-consumer"); + +export const responseReaderFacts = (source: string) => + responseConsumers(analyzeJavaScriptSemantics(source)).map( + ({ method, endpoint, resolution }) => ({ method, endpoint, resolution }), + );