From 61f7484d1ff6f7863d6a9e4b300b40902aa05ab0 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:40:58 +0800 Subject: [PATCH] test(environment): prove linked read retention budget (#11959) --- .../environmentStateWriteLinks.test.ts | 90 ++++++------------- 1 file changed, 29 insertions(+), 61 deletions(-) diff --git a/src/domain/analysisView/environmentStateWriteLinks.test.ts b/src/domain/analysisView/environmentStateWriteLinks.test.ts index 14759a4cce..90bf3de6c9 100644 --- a/src/domain/analysisView/environmentStateWriteLinks.test.ts +++ b/src/domain/analysisView/environmentStateWriteLinks.test.ts @@ -278,49 +278,43 @@ describe("the entity identities creation limits use", () => { describe("the ids creation limits keep", () => { it("keeps and charges only the shown ids a creation returned", () => { - let retained = 0; - const lists = 50; - const reads = Array.from({ length: lists }, (_, index) => - record(index < lists / 2 ? index : index + 1, "GET", "/api/projects"), - ); - const post = record(lists / 2, "POST", "/api/imports"); - const rows = (extra: number) => - Array.from({ length: 1000 }, (_, row) => ({ - id: `proj-${String(row + extra).padStart(5, "0")}`, - })); - const bodies = new Map( - reads.map((read) => [ - read.ordinal, - read.ordinal > post.ordinal - ? [...rows(0), { id: "proj-made" }] - : rows(0), - ]), - ); - expect( - projectsLimit(reads, post, bodies, { + const reads = [ + record(0, "GET", "/api/projects"), + record(2, "GET", "/api/projects"), + ]; + const post = record(1, "POST", "/api/imports"); + const chargeOf = (after: readonly string[]) => { + let retained = 0; + const bodies = new Map([ + [0, [{ id: "proj-existing" }]], + [2, after.map((id) => ({ id }))], + ]); + const limit = projectsLimit(reads, post, bodies, { created: ["proj-made"], retain: (bytes) => { retained += bytes; }, - }), - ).toBe(post); - // One kept id per later list, not a thousand per list. - expect(retained).toBeGreaterThan(0); - expect(retained).toBeLessThan(lists * 1000 * 16); + }); + return { limit, retained }; + }; + const narrow = chargeOf(["proj-made"]); + const broad = chargeOf(["proj-made", "proj-other", "proj-more"]); + const missing = chargeOf(["proj-new", "proj-other", "proj-more"]); + expect(narrow.limit).toBe(post); + expect(broad.limit).toBe(post); + expect(missing.limit).toBeUndefined(); + expect(broad.retained).toBe(narrow.retained); + expect(broad.retained).toBeGreaterThan(missing.retained); }); }); describe("the reads creation limits parse", () => { - /** - * The creation limit of `/api/projects` over list reads, a POST returning `proj-0002` that later - * lists show, and the write that ends the initial reads; with the reads parsed, and those whose - * body was read after its parse ended. - */ - const parsedLimit = ( - reads: readonly StateRecordFacts[], - post: StateRecordFacts, - until: StateRecordFacts, - ) => { + it("parses only reads before the cutoff and inspects each body while its parse is charged", () => { + const reads = [0, 2, 4, 5].map((ordinal) => + record(ordinal, "GET", "/api/projects"), + ); + const post = record(1, "POST", "/api/imports"); + const until = record(3, "PUT", "/api/projects/proj-0001"); const parsed: number[] = []; const outside = new Set(); let parsing = false; @@ -370,34 +364,8 @@ describe("the reads creation limits parse", () => { }, until, ); - return { limit, parsed, outside }; - }; - - it("parses no read at or after the write that ends the initial reads, since no id it shows can be fresh", () => { - const reads = [0, 2, 4, 5].map((ordinal) => - record(ordinal, "GET", "/api/projects"), - ); - const post = record(1, "POST", "/api/imports"); - const { limit, parsed } = parsedLimit( - reads, - post, - record(3, "PUT", "/api/projects/proj-0001"), - ); expect(limit).toBe(post); expect(parsed).toEqual([0, 2]); - }); - - it("reads the ids a body shows only while its parse is charged", () => { - const reads = [0, 2].map((ordinal) => - record(ordinal, "GET", "/api/projects"), - ); - const post = record(1, "POST", "/api/imports"); - const { limit, outside } = parsedLimit( - reads, - post, - record(3, "PUT", "/api/projects/proj-0001"), - ); - expect(limit).toBe(post); expect([...outside]).toEqual([]); }); });