Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(environment): retain conflicting scenario MIME uncertainty
  • Loading branch information
ShuaixinHuang committed Oct 28, 2026
commit a709e6287269ce000523b973309fec86064b79f3
85 changes: 85 additions & 0 deletions src/domain/analysisView/scenarioCaptureRecords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {
admitScenarioCaptureParent,
scenarioCaptureRecords,
scenarioContentMediaType,
} from "./scenarioCaptureRecords.js";
import { EnvironmentManifestAccumulator } from "./EnvironmentManifestAccumulator.js";

Expand All @@ -28,6 +29,90 @@ const manifestLimitations = (
return result.value.limitations;
};

describe("scenario Content-Type evidence", () => {
it.each(
[
["application/json", "application/octet-stream"],
["application/octet-stream", "application/json"],
["application/json", null],
[null, "application/json"],
["application/json", "application/octet-stream", "application/json"],
].map((values) => ({ values })),
)(
"keeps conflicting or excluded Content-Type declarations unknown: $values",
({ values }) => {
const capture = scenarioCaptureFixture([
{
method: "GET",
url: "/download",
status: 200,
responseHeaders: values.map((value, index) => ({
name: index % 2 === 0 ? "Content-Type" : "content-type",
value,
})),
},
]);
const original = JSON.stringify(capture);
const content = capture.events.items.find(
(event) =>
event.kind === "network-content" && event.phase === "response",
);
if (content?.kind !== "network-content")
throw new Error("Missing content");
expect(scenarioContentMediaType(content)).toBeUndefined();
for (const purpose of ["manifest", "replay"] as const) {
const projected = scenarioCaptureRecords(capture, purpose);
expect(projected.records[0]?.reported).toMatchObject({
response: { status: 200, content: { mimeType: "" } },
});
const accumulator = new EnvironmentManifestAccumulator();
for (const record of projected.records) {
const added = accumulator.add(
record,
projected.responseBodies.get(record.ordinal),
);
if (!added.ok) throw added.error;
}
const manifest = accumulator.finish();
if (!manifest.ok) throw manifest.error;
expect(manifest.value.tools).toEqual([]);
}
expect(JSON.stringify(capture)).toBe(original);
},
);

it("retains agreeing repeated Content-Type values and independently captured body metadata", () => {
const capture = scenarioCaptureFixture([
{
method: "GET",
url: "/headers",
status: 200,
responseHeaders: [
{ name: "Content-Type", value: "application/json" },
{ name: "content-type", value: "application/json" },
],
},
{
method: "GET",
url: "/body",
status: 200,
body: "{}",
mediaType: "application/json",
responseHeaders: [
{ name: "Content-Type", value: "application/json" },
{ name: "content-type", value: null },
],
},
]);
const projected = scenarioCaptureRecords(capture, "manifest");
expect(projected.records).toHaveLength(2);
for (const record of projected.records)
expect(record.reported).toMatchObject({
response: { content: { mimeType: "application/json" } },
});
});
});

describe("scenarioCaptureRecords", () => {
it("projects each transaction, in request order, as a HAR-shaped record with its retained bytes", () => {
const projected = scenarioCaptureRecords(
Expand Down
14 changes: 10 additions & 4 deletions src/domain/analysisView/scenarioCaptureRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ const headerValue = (
name: string,
): string | undefined => {
if (headers?.state !== "captured") return undefined;
const item = headers.items.find(
(candidate) => candidate.name.toLowerCase() === name,
);
return item?.value ?? undefined;
let value: string | undefined;
for (const item of headers.items) {
if (item.name.toLowerCase() !== name) continue;
// A singleton field has no certain value if declarations disagree or one
// was excluded. Never let ordering choose an interpretation of Evidence.
if (item.value === null || (value !== undefined && value !== item.value))
return undefined;
value = item.value;
}
return value;
};

const captured = (
Expand Down
56 changes: 56 additions & 0 deletions tests/boundary/cli/scenarioManifestCoverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,59 @@ it.each(["absent", "redacted"] as const)(
]);
},
);

it("does not derive a tool from conflicting header-only MIME evidence through the CLI handler", async () => {
for (const values of [
["application/json", "application/octet-stream"],
["application/octet-stream", "application/json"],
["application/json", null],
["application/json", "application/json"],
]) {
const capture = scenarioCaptureFixture([
{
method: "GET",
url: "/download",
status: 200,
responseHeaders: values.map((value) => ({
name: "Content-Type",
value,
})),
},
]);
const cli = Cli.create("scenario-mime", { sync: false });
registerAnalysisViewCommands(cli, silentLogger);
let stdout = "";
let exitCode = 0;
await cli.serve(
[
"inspect-analysis-view",
JSON.stringify({
source: {
kind: "inline",
evidence: scenarioCaptureEvidence(capture),
},
view: { kind: "environment-manifest" },
}),
"--json",
],
{
env: {},
stdout: (text) => {
stdout += text;
},
exit: (code) => {
exitCode = code;
},
},
);
expect(exitCode, stdout).toBe(0);
const result = (
JSON.parse(stdout) as {
normalized_result: { tools: unknown[] };
}
).normalized_result;
expect(result.tools, JSON.stringify(values)).toHaveLength(
values.every((value) => value === "application/json") ? 1 : 0,
);
}
});
Loading