Files
plugins/packages/agent-office/test/webkit-csp.mjs
T
QingJ 0e3562b25f agent-office: frame only fragments unfinished at EOF in cold image scans (#559)
The incremental session scanner frames only fragments still unfinished at
EOF, instead of passing every 64 KiB fragment of a complete cross-chunk
record through the character framer. A framed-part index stops a later
tick from walking text it already framed. Adds regressions for the four
mutation gaps named on #551 and a WebKit CSP diagnostic script.

Reviewed at 5f564b034df26e12b60c40f683922ce0389abdd1 on main 7714b61.
Run on the Mac mini test box (mote, Bun 1.4.2):
- packages/agent-office: HOME=$(mktemp -d) bun test: 167 pass, 0 fail;
  bun scripts/stamp-page.mjs --check: exit 0.
- Full repo: HOME=$(mktemp -d) bun test: 4321 pass, 9 skip, 0 fail
  (338 files); bun scripts/check.mjs: exit 0.
- Broken by hand: eager framing restored -> both cold-scan tests red;
  framed index not advanced -> 2 red (cold-scan EOF, unfinished quoted
  brace never reparses); ExtendedTail disabled -> extended-EOF test red.
  Each restored -> 167 pass.
- bench-scan.mjs 100 1 1, initial scan: ~29-32 ms at this head vs
  ~294-319 ms at main.
- Host probe: office.hooks.mjs imported without
  MAGPIE_AGENT_OFFICE_PAGE in a sandbox HOME with one Claude session;
  snapshot() ok and page/live.js written.
Not tried: a real magpie install of the page.
2026-10-31 19:09:38 +08:00

41 lines
3.1 KiB
JavaScript

// Minimal local-only diagnostic for the platform-specific WebKit CSP report.
// OFFICE_PLAYWRIGHT=/path/to/playwright/index.mjs bun test/webkit-csp.mjs
// Reports both outcomes: a working scheme-less source is not a failure.
import { pathToFileURL } from "node:url"
const modulePath=process.env.OFFICE_PLAYWRIGHT
if(!modulePath)throw new Error("Set OFFICE_PLAYWRIGHT to an installed Playwright module")
const {webkit}=await import(modulePath)
const playwrightInfo=await Bun.file(new URL("./package.json",modulePath.startsWith("file:")?modulePath:pathToFileURL(modulePath))).json().catch(()=>null)
const hits=[]
let origin
const server=Bun.serve({hostname:"127.0.0.1",port:0,fetch(request){
const url=new URL(request.url),policy=url.searchParams.get("policy")||"scheme-less"
if(url.pathname==="/")return new Response(`<iframe sandbox="allow-scripts" src="/_magpie/plugin/tok/index.html?policy=${policy}"></iframe>`,{headers:{"content-type":"text/html"}})
if(url.pathname.endsWith("/index.html")) {
const source=policy==="explicit"?`${origin}/_magpie/plugin/tok/`:`127.0.0.1:${server.port}/_magpie/plugin/tok/`
return new Response(`<p id="result">not executed</p><script src="./app.js?policy=${policy}"></script>`,{headers:{"content-type":"text/html","content-security-policy":`default-src 'none'; script-src ${source};`}})
}
if(url.pathname.endsWith("/app.js")){hits.push(policy);return new Response('globalThis.APP_EXECUTED=true;document.querySelector("#result").textContent="executed";console.log("APP_EXECUTED")',{headers:{"content-type":"text/javascript"}})}
return new Response("not found",{status:404})
}})
origin=`http://127.0.0.1:${server.port}`
let browser
try {
browser=await webkit.launch()
const results=[]
for(const policy of ["scheme-less","explicit"]) {
const page=await browser.newPage(),consoleMessages=[],pageErrors=[]
page.on("console",message=>consoleMessages.push({type:message.type(),text:message.text()}))
page.on("pageerror",error=>pageErrors.push(error.message))
const responsePromise=page.waitForResponse(response=>response.url().includes("/index.html"))
await page.goto(`${origin}/?policy=${policy}`)
const response=await responsePromise,frame=page.frames().find(frame=>frame.url().includes("/index.html"))
const observed=await frame.evaluate(()=>({executed:globalThis.APP_EXECUTED===true,text:document.querySelector("#result").textContent,documentOrigin:globalThis.origin,urlOrigin:location.origin}))
results.push({policy,status:response.status(),csp:response.headers()["content-security-policy"],...observed,scriptRequested:hits.includes(policy),consoleMessages,pageErrors})
await page.close()
}
const mac=process.platform==="darwin"?new TextDecoder().decode(Bun.spawnSync({cmd:["/usr/bin/sw_vers"]}).stdout).trim():null
console.log(JSON.stringify({platform:process.platform,arch:process.arch,macOS:mac,bun:Bun.version,playwrightVersion:playwrightInfo?.version||null,webkitVersion:browser.version(),webkitBuild:webkit.executablePath().match(/webkit-(\d+)/)?.[1]||null,results},null,2))
if(!results.find(r=>r.policy==="explicit").executed)throw new Error("Explicit source control did not execute")
}finally{if(browser)await browser.close();server.stop(true)}