Files
rea/scripts/lib/browser-network-assertions.mjs
Edgar Arakelyan 69f99c5198 feat(environment): time browser scenario records so scenario environments learn a write clock (#6967)
* feat(browser): record when scenario requests were observed and how long they took

Request events carry observed_at, the wall-clock time the provider observed
the request start, and finished or failed events carry the browser's
elapsed_ms when it reports one. Both are optional, so earlier captures still
parse, and scenario comparisons leave them out as a committed built-in rule.
The real-Chrome network lane checks every request and finished response is
timed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(environment): time browser scenario records so their state learns a write clock

Scenario records take startedDateTime from when each request was observed and
time from the browser's duration, as HAR entries are timed. A timed scenario
of the book session learns the same clock and serves the same writes as its
HAR; an untimed one keeps record order and learns no clock, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): document scenario request times and timed scenario records

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): start scenario records at the browser's own request start

The provider observes a request up to tens of milliseconds after the browser
starts it while it reads other responses, so a record starting then and
ending after the browser's duration mixed two clocks. Finished and failed
events now carry the browser's started_at, records prefer it and fall back to
observed_at, and comparisons drop it too. Tests cover the fallback for a
request that never ended or ended twice, a partly untimed capture keeping
record order, comparing an untimed capture with a timed one, and the
real-Chrome lane checks every finished request carries the browser's start
and duration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): document the browser start time, its fallback lag and older readers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the scenario timing verification

Gates, the real-Chrome lane, prompt-recorded and re-recorded published
scenario captures built with and without their times, the stale published
environments left unrepublished, and the review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(browser): compare timed completion events in the request-time comparison test

The comparison test's captures had no finished or failed events, so dropping
started_at and elapsed_ms from them went untested. The captures now end one
request and fail the other, the test asserts both carry times, compares them
shifted by seven seconds and against an untimed capture, and a control shows
a changed failure on a timed completion is still reported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-10-24 22:48:50 +08:00

173 lines
5.9 KiB
JavaScript

import assert from "node:assert/strict";
import {
networkFixtureBinary,
networkFixtureDecoded,
networkFixtureSecret,
} from "../fixtures/browser-network-site.mjs";
const retained = (event) => {
assert.equal(event?.kind, "network-content");
assert.equal(event.body.state, "captured", JSON.stringify(event.body));
return Buffer.from(event.body.content, "base64");
};
/** Verify association against server-authored markers rather than provider-derived expectations. */
export function assertBrowserNetworkEvidence(
capture,
origin,
expectedCleanup = "terminated-owned-process",
) {
assert.ok(capture.steps.every((step) => step.status === "completed"));
assert.equal(capture.browser.cleanup, expectedCleanup);
assert.deepEqual(capture.scenario.network_content, {
request_body: true,
response_body: true,
header_values: true,
});
assert.equal(capture.completeness.equality_eligible, false);
assert.ok(!JSON.stringify(capture).includes(networkFixtureSecret));
const events = capture.events.items;
const content = events.filter((event) => event.kind === "network-content");
const requestEvents = events.filter((event) => event.kind === "request");
const sameUrlRequests = requestEvents.filter(
(event) => event.url.url === `${origin}/same`,
);
assert.equal(sameUrlRequests.length, 2);
assert.equal(
new Set(sameUrlRequests.map((event) => event.transaction_id)).size,
2,
);
const markers = [];
for (const request of sameUrlRequests) {
const requestContent = content.find(
(event) =>
event.phase === "request" &&
event.transaction_id === request.transaction_id,
);
const responseContent = content.find(
(event) =>
event.phase === "response" &&
event.transaction_id === request.transaction_id,
);
const input = JSON.parse(retained(requestContent).toString());
const output = JSON.parse(retained(responseContent).toString());
assert.equal(
output.marker,
input.marker,
"Response was associated with a different same-URL request",
);
assert.equal(output.password, "ordinary-selected-value");
assert.equal(input.declared, "[REDACTED:network_secret]");
assert.equal(output.declared, input.declared);
assert.equal(requestContent.body.redacted, true);
assert.equal(responseContent.headers.state, "captured");
assert.ok(
responseContent.headers.items.some(
(item) =>
item.name.toLowerCase() === "x-declared" &&
item.value === "[REDACTED:network_secret]" &&
item.redacted,
),
);
assert.deepEqual(
responseContent.headers.items
.filter((item) => item.name.toLowerCase() === "x-repeat")
.map((item) => item.value),
["first", "second"],
);
assert.ok(
responseContent.headers.items.some(
(item) =>
item.name.toLowerCase() === "set-cookie" &&
item.value === null &&
item.redacted,
),
);
assert.ok(
requestContent.headers.items.some(
(item) =>
item.name.toLowerCase() === "authorization" &&
item.value === null &&
item.redacted,
),
);
assert.ok(
events.some(
(event) =>
event.kind === "request-finished" &&
event.transaction_id === request.transaction_id,
),
);
markers.push(input.marker);
}
assert.deepEqual(markers.sort(), ["fast", "slow"]);
const responseFor = (path) => {
const response = events.find(
(event) =>
event.kind === "response" && event.url.url === `${origin}${path}`,
);
assert.ok(response, `Missing response ${path}`);
return content.find(
(event) =>
event.phase === "response" &&
event.source_event_sequence === response.sequence,
);
};
assert.deepEqual(retained(responseFor("/binary")), networkFixtureBinary);
assert.deepEqual(retained(responseFor("/compressed")), networkFixtureDecoded);
assert.equal(
responseFor("/compressed").body.representation,
"playwright-response-body-bytes",
);
const formRequest = requestEvents.find(
(event) => event.url.url === `${origin}/form`,
);
assert.ok(formRequest);
const formContent = content.find(
(event) =>
event.phase === "request" &&
event.transaction_id === formRequest.transaction_id,
);
assert.equal(
retained(formContent).toString(),
"declared=[REDACTED:network_secret]",
);
assert.equal(
retained(responseFor("/form")).toString(),
"[REDACTED:network_secret]",
);
assert.equal(responseFor("/stream").body.state, "unavailable");
assert.equal(responseFor("/stream").body.reason, "response-unfinished");
const first = requestEvents.find(
(event) => event.url.url === `${origin}/redirect`,
);
const next = requestEvents.find(
(event) => event.url.url === `${origin}/landing`,
);
assert.ok(first && next);
assert.equal(next.redirected_from_transaction_id, first.transaction_id);
// Every request is stamped when it was observed; finished ones carry the browser's duration.
for (const request of requestEvents)
assert.ok(
Number.isFinite(Date.parse(request.observed_at)),
`Request ${request.url.url} has no observation time`,
);
const timed = events.filter((event) => event.kind === "request-finished");
for (const finished of timed)
assert.ok(
typeof finished.elapsed_ms === "number" &&
Number.isFinite(Date.parse(finished.started_at)),
`Finished request ${finished.url.url} has no browser start or duration`,
);
assert.ok(timed.length > 0, "No request finished");
return {
timed_requests: requestEvents.length,
timed_completions: timed.length,
same_url_markers: markers,
binary_bytes: networkFixtureBinary.length,
compressed_representation: "playwright-response-body-bytes",
unfinished_stream: "response-unfinished",
empty_response_state: responseFor("/empty").body.state,
};
}