test(core): shrink webfetch stress fixtures (#45375)

This commit is contained in:
Aiden Cline
2026-08-26 15:03:49 -05:00
committed by GitHub
parent 94bd9f6c8b
commit 009eca8b5b
+30 -39
View File
@@ -114,18 +114,24 @@ describe("WebFetchTool helpers", () => {
expect(WebFetchTool.convertHTMLToMarkdown(html)).toBe("before after")
})
test("is deterministic and bounded for malformed maximum-size input", () => {
const html = `<main><p>${"visible &amp; text ".repeat(250_000)}</main></p></unknown>`
test("is deterministic and bounded for malformed input across parser chunks", () => {
const html = `<main><p>${"visible &amp; text ".repeat(4_096)}</main></p></unknown>`
const first = WebFetchTool.convertHTMLToMarkdown(html)
expect(WebFetchTool.convertHTMLToMarkdown(html)).toBe(first)
expect(first.startsWith("visible & text visible & text")).toBe(true)
expect(first.length).toBeLessThanOrEqual(html.length)
})
test("defaults to the production byte budget with room for closing syntax", () => {
const output = WebFetchTool.convertHTMLToMarkdown("x".repeat(WebFetchTool.MAX_RESPONSE_BYTES))
expect(WebFetchTool.MAX_RESPONSE_BYTES).toBe(5 * 1024 * 1024)
expect(output).toHaveLength(WebFetchTool.MAX_RESPONSE_BYTES - 64 * 1024)
})
test("bounds deeply nested list output and fragmented code fences", () => {
const lists = `${"<ul><li>item".repeat(2_000)}${"</li></ul>".repeat(2_000)}`
const quotes = `${"<blockquote><p>item".repeat(2_000)}${"</p></blockquote>".repeat(2_000)}`
const code = `<pre>${"` x ".repeat(250_000)}</pre>`
const code = `<pre>${"` x ".repeat(4_096)}</pre>`
expect(WebFetchTool.convertHTMLToMarkdown(lists).length).toBeLessThan(lists.length * 4)
expect(WebFetchTool.convertHTMLToMarkdown(quotes).length).toBeLessThan(quotes.length * 4)
expect(() => WebFetchTool.convertHTMLToMarkdown(code)).not.toThrow()
@@ -250,46 +256,35 @@ describe("WebFetchTool helpers", () => {
expect(WebFetchTool.convertHTMLToMarkdown(html)).toBe(`| a\\|b next | \`x\\|y\` |\n| --- | --- |`)
})
test("keeps each near-boundary inline construct closed and UTF-8-safe", () => {
const payload = "😀".repeat(WebFetchTool.MAX_RESPONSE_BYTES / 4)
test("preserves Unicode in inline constructs", () => {
const payload = "😀".repeat(16)
const cases = [
[`<strong>${payload}</strong>`, /^\*\*[\s\S]*\*\*$/],
[`<a href="/docs">${payload}</a>`, /^\[[\s\S]*\]\(\/docs\)$/],
[`<img src="image.png" alt="${payload}">`, /^!\[[\s\S]*\]\(image\.png\)$/],
[`<code>${payload}</code>`, /^`[\s\S]*`$/],
[`<strong>${payload}</strong>`, `**${payload}**`],
[`<a href="/docs">${payload}</a>`, `[${payload}](/docs)`],
[`<img src="image.png" alt="${payload}">`, `![${payload}](image.png)`],
[`<code>${payload}</code>`, `\`${payload}\``],
] as const
for (const [html, pattern] of cases) {
const output = WebFetchTool.convertHTMLToMarkdown(html)
expect(Buffer.byteLength(output)).toBeLessThanOrEqual(WebFetchTool.MAX_RESPONSE_BYTES)
expect(output).not.toContain("")
expect(output).toMatch(pattern)
for (const [html, expected] of cases) {
expect(WebFetchTool.convertHTMLToMarkdown(html)).toBe(expected)
}
})
test("keeps near-boundary block constructs syntactically complete", () => {
const payload = "x".repeat(WebFetchTool.MAX_RESPONSE_BYTES)
test("preserves block content and following lists", () => {
const payload = "x".repeat(256)
const table = WebFetchTool.convertHTMLToMarkdown(
`<table><tr><th>Name</th></tr><tr><td>${payload}</td></tr></table>`,
)
const list = WebFetchTool.convertHTMLToMarkdown(`<ul><li>${payload}</li></ul><ul><li>nested</li></ul>`)
const list = WebFetchTool.convertHTMLToMarkdown(`<ul><li>${payload}</li></ul><ul><li>next</li></ul>`)
const code = WebFetchTool.convertHTMLToMarkdown(`<pre>${payload}</pre>`)
for (const output of [table, list, code]) {
expect(Buffer.byteLength(output)).toBeLessThanOrEqual(WebFetchTool.MAX_RESPONSE_BYTES)
expect(output).not.toContain("")
}
expect(table).toMatch(/^\| Name \|\n\| --- \|\n\| [\s\S]* \|$/)
expect(list).toMatch(/^- [\s\S]*$/)
expect(list.includes("nested")).toBe(false)
expect(code.match(/^(`{3,}|~{3,})$/gm)).toHaveLength(2)
expect(table).toBe(`| Name |\n| --- |\n| ${payload} |`)
expect(list).toBe(`- ${payload}\n\n- next`)
expect(code).toBe(`\`\`\`\n${payload}\n\`\`\``)
})
test("keeps quoted code within budget with a safe closed fence", () => {
const html = `<blockquote><pre>${"`".repeat(32)}${"~".repeat(32)}${"x".repeat(WebFetchTool.MAX_RESPONSE_BYTES)}</pre></blockquote>`
const output = WebFetchTool.convertHTMLToMarkdown(html)
expect(Buffer.byteLength(output)).toBeLessThanOrEqual(WebFetchTool.MAX_RESPONSE_BYTES)
const lines = output.split("\n")
expect(lines[0]).toMatch(/^> (`{33}|~{33})$/)
expect(lines.at(-1)).toBe(lines[0])
test("keeps quoted code with long delimiter runs inside a safe closed fence", () => {
const payload = `${"`".repeat(32)}${"~".repeat(32)}${"x".repeat(64)}`
const output = WebFetchTool.convertHTMLToMarkdown(`<blockquote><pre>${payload}</pre></blockquote>`)
expect(output).toBe(`> ${"`".repeat(33)}\n> ${payload}\n> ${"`".repeat(33)}`)
})
test("separates reconstructed tables from adjacent inline and quoted content", () => {
@@ -299,13 +294,9 @@ describe("WebFetchTool helpers", () => {
)
})
test("keeps multiline quoted code closed at the content budget", () => {
const html = `<blockquote><pre>${"x\n".repeat(WebFetchTool.MAX_RESPONSE_BYTES / 2)}</pre></blockquote><p>tail</p>`
const output = WebFetchTool.convertHTMLToMarkdown(html)
expect(Buffer.byteLength(output)).toBeLessThanOrEqual(WebFetchTool.MAX_RESPONSE_BYTES)
expect((output.match(/(`{3}|~{3})/g) ?? []).length).toBe(2)
expect(output.includes("\uFFFD")).toBe(false)
expect(output.endsWith("tail")).toBe(true)
test("keeps multiline quoted code closed before following prose", () => {
const html = `<blockquote><pre>${"x\n".repeat(16)}</pre></blockquote><p>tail</p>`
expect(WebFetchTool.convertHTMLToMarkdown(html)).toBe(`> \`\`\`\n${"> x\n".repeat(16)}> \`\`\`\n\ntail`)
})
test("keeps active content suppressed when depth fallback begins", () => {