From 9901e8dfcd7684d2bb5b025fe668f32b4c941297 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:22:52 +0800 Subject: [PATCH] test(environment): bound create explanation by request-body reads instead of elapsed time (#11657) The wide-body case reads its request body through a counting proxy with a fail-fast budget; re-indexing the body per field fails it at once. --- .../environmentStateWriteExplain.test.ts | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/domain/analysisView/environmentStateWriteExplain.test.ts b/src/domain/analysisView/environmentStateWriteExplain.test.ts index ea6289b6b3..fe66060c15 100644 --- a/src/domain/analysisView/environmentStateWriteExplain.test.ts +++ b/src/domain/analysisView/environmentStateWriteExplain.test.ts @@ -74,6 +74,27 @@ const updated = ( ...context, }); +/** + * The body behind a counting proxy: each member read counts, and a read past + * `budget` fails at once, so searching the body again per field cannot finish. + */ +const countedBody = ( + body: JsonObject, + reads: { count: number; readonly budget: number }, +): JsonObject => { + const wrap = (value: JsonValue): JsonValue => + typeof value === "object" && value !== null + ? (new Proxy(value, { + get(target, key, receiver) { + if (typeof key === "string" && ++reads.count > reads.budget) + throw new Error("Request body read budget exceeded"); + return wrap(Reflect.get(target, key, receiver) as JsonValue); + }, + }) as JsonValue) + : value; + return wrap(body) as JsonObject; +}; + const ruleFor = ( rules: readonly FieldRule[], field: string, @@ -937,8 +958,12 @@ it("explains many copied and slugged fields of a wide body without searching it value(index), ]), ); - const started = performance.now(); - const copied = explainCreate(created({ id: 1, ...copies }, nested)); + // Indexing reads each member a few times (72,000 and 64,001 reads here); + // searching the body again for each field reads all of it per field. + const reads = { count: 0, budget: 8 * 2 * n }; + const copied = explainCreate( + created({ id: 1, ...copies }, countedBody(nested, reads)), + ); expect(ruleFor(copied.rules, `c${String(n - 1)}`)).toEqual({ field: `c${String(n - 1)}`, rule: "copy", @@ -957,16 +982,15 @@ it("explains many copied and slugged fields of a wide body without searching it `x${String(index)}`, ]), ); + reads.count = 0; const slugged = explainCreate( - created({ id: 1, ...others, s: "member-7" }, flat), + created({ id: 1, ...others, s: "member-7" }, countedBody(flat, reads)), ); expect(ruleFor(slugged.rules, "s")).toEqual({ field: "s", rule: "slug", from: "m7", }); - // Searching the body again for each field is quadratic in its members. - expect(performance.now() - started).toBeLessThan(5_000); }); it("searches a wide request body for a copied value in linear time", () => {