fix(replay): share one seeded generator and present builtins with real identity (#539)

This commit is contained in:
morluto
2026-10-05 16:47:20 +08:00
committed by GitHub
parent 333d118c0f
commit 5b51fa01e2
5 changed files with 176 additions and 19 deletions
@@ -497,6 +497,60 @@ describe("deterministic replay Date compatibility", () => {
});
});
describe("replay determinism and builtin identity", () => {
const evaluate = async (
expression: string,
randomSeed: number,
): Promise<unknown> => {
const workerRequest = await request("parser.mjs", "esm", "default", []);
workerRequest.determinism.randomSeed = randomSeed;
workerRequest.left.modules[0] = {
alias: "entry",
format: "esm",
dependencies: {},
source: `export default function () { return ${expression}; }`,
};
const result = await runWorker(workerRequest);
expect(result.code).toBe(0);
return JSON.parse(result.stdout).left[0];
};
it("varies Math.random across draws when seeded with zero", async () => {
const outcome = (await evaluate(
"[Math.random(), Math.random(), Math.random()]",
0,
)) as { outcome: string; value?: number[] };
expect(outcome.outcome).toBe("return");
expect(new Set(outcome.value).size).toBe(3);
});
it("produces an identical stream for the same seed", async () => {
const first = await evaluate("[Math.random(), Math.random()]", 5);
const second = await evaluate("[Math.random(), Math.random()]", 5);
expect(first).toEqual(second);
});
it("presents Date with real builtin identity", async () => {
expect(await evaluate("[Date.name, Date.length]", 3)).toMatchObject({
outcome: "return",
value: ["Date", 7],
});
});
it("presents Math with real descriptors and unaffected members", async () => {
// Object identity is realm-specific inside the vm, so the meaningful checks
// are the descriptor and the untouched members.
const outcome = (await evaluate(
"[Object.getOwnPropertyDescriptor(Math, 'random').configurable, Object.getOwnPropertyDescriptor(Math, 'random').writable, Math.max(1, 2), Math.floor(1.7), Math.PI > 3]",
3,
)) as { outcome: string; value?: unknown[] };
expect(outcome).toMatchObject({
outcome: "return",
value: [true, true, 2, 1, true],
});
});
});
describe("replay array projection", () => {
it.each([
["Array(3)", [null, null, null]],