mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 22:11:12 +00:00
* refactor(api-contract): share parser heap admission * refactor(errors): centralize measured heap refusals * test(api-contract): isolate parser heap regression
61 lines
2.5 KiB
TypeScript
61 lines
2.5 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { getHeapStatistics } from "node:v8";
|
|
import { expect, it, vi } from "vitest";
|
|
import { inspectApiContractEvidence } from "../../src/application/apiContract/ApiContractService.js";
|
|
import { nativeJsonParseRequiredBytes } from "../../src/application/JsonInputFile.js";
|
|
import { graphqlContractSchema } from "../../src/domain/apiContract/graphqlContract.js";
|
|
import { parseEvidence } from "../../src/domain/evidence.js";
|
|
import { ArtifactResourceScope } from "../../src/resources/ArtifactResourceScope.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
|
|
vi.mock("node:v8", { spy: true });
|
|
|
|
it("rechecks parser heap headroom for independently read GraphQL operations", async () => {
|
|
const schema = "type Query { greeting: String }";
|
|
const ops = "{ greeting }";
|
|
const root = await createTestTempDirectory("rea-graphql-heap-");
|
|
const path = join(root, "schema.graphql");
|
|
const operations_path = join(root, "operations.graphql");
|
|
await writeFile(path, schema);
|
|
await writeFile(operations_path, ops);
|
|
const sample = getHeapStatistics();
|
|
const required = nativeJsonParseRequiredBytes(Buffer.byteLength(ops));
|
|
vi.mocked(getHeapStatistics)
|
|
.mockReturnValueOnce({
|
|
...sample,
|
|
total_available_size: nativeJsonParseRequiredBytes(
|
|
Buffer.byteLength(schema),
|
|
),
|
|
})
|
|
.mockReturnValueOnce({ ...sample, total_available_size: required - 1 });
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
const result = await inspectApiContractEvidence(
|
|
{ path, operations_path, format: "graphql-sdl" },
|
|
resources,
|
|
);
|
|
if (result.ok) throw new Error("Expected an operations heap refusal");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisResourceConstraintError",
|
|
resource: "memory",
|
|
reportedLimits: {
|
|
required_bytes: required,
|
|
available_heap_bytes: required - 1,
|
|
},
|
|
recovery: { measuredExhaustion: true },
|
|
});
|
|
const evidence = parseEvidence(result.error.partialObservation);
|
|
const partial = graphqlContractSchema.parse(evidence.normalized_result);
|
|
expect(partial.artifact.path).toBe(path);
|
|
expect(partial.operations_artifact?.path).toBe(operations_path);
|
|
expect(partial.operations_artifact?.sha256).toBe(
|
|
createHash("sha256").update(ops).digest("hex"),
|
|
);
|
|
} finally {
|
|
vi.mocked(getHeapStatistics).mockReset();
|
|
await resources.close();
|
|
}
|
|
});
|