Files
plugins/packages/inkstone/testdata/fake-browser.mjs
T
我是一条没有名字的区 087c36123d inkstone 0.1.3: image-caps preflight, a zero-prompt browser sign-in, key calls on the session cookie (#832)
inkstone 0.1.3 (#832, yunwang2442-creator):

- Image requests are prechecked before they leave: 8 images, 10MB each, 40MB in all.
  - A request over a cap gets a local 400 shape refusal, so magpie asks the next account and rests none.
  - The plan's invalid_media / media_fetch_failed / unsupported_media_type errors become the same kind of refusal.
- The body rewrite strips the reasoning fields and sets image detail to auto.
- A zero-prompt browser sign-in comes first, and a second browser method reuses a pasted key.
  - Neither browser method is offered when no Chromium-family browser is found (#825).
- The /keys calls ride the session cookie, without the pagination query. Key creation retries the console's -15100 "refresh page and retry".
- The #826 metadata token read is kept, and its test is back.
- The fake browser is built once per user and source hash (#824), and the title wait reads only on Windows.
- maxConcurrency is 4.
- The doubled comment line is dropped from every opencode2.mjs copy, agy-cli's included.

Reviewed at 88e66bde, on mote (macOS, Bun 1.4.2) with a temp HOME:
- full bun test: 6853 pass / 0 fail; both checks exit 0
- bun scripts/check.mjs and bun scripts/check.mjs inkstone: exit 0
- 21 hand breaks: 20 red, 1 not caught (the /keys pagination test calls protocol.listKeys directly; follow-up filed)

Not tried: a real InkStone account or sign-in, Windows and Linux runs.
2026-11-07 22:24:56 +08:00

102 lines
4.2 KiB
JavaScript

// A fake Chromium for tests: serves the DevTools HTTP + WebSocket endpoints
// and writes the DevToolsActivePort file, so the plugin's real openSession /
// waitForToken / CDP cookie read run against it. Modes come from argv:
// --headless=new → serve CDP (the renewal / post-sign-in read)
// (no headless) → the login window: wait for the plugin's close
import { writeFileSync, mkdirSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
const args = process.argv.slice(2)
const profile = args.find((a) => a.startsWith("--user-data-dir="))?.slice(17) ?? "."
// bun build --compile's Windows launcher mangles argument values: the drive
// letter and part of the path are eaten. The test always makes the profile
// under tmpdir(), so the safe repair is: take the last path segment and
// re-seat it under tmpdir().
const profilePath = /^[A-Za-z]:/.test(profile)
? profile
: join(tmpdir(), profile.split(/[\\/]/).filter(Boolean).pop() ?? "fake-profile")
const headless = args.includes("--headless=new")
const url = args.find((a) => a.startsWith("http")) ?? ""
let pageTargetUrl = ""
try {
if (!headless) {
// login window. On Unix it waits for the plugin's SIGTERM and records
// the signal (the test asserts no SIGKILL was needed). On Windows
// TerminateProcess is uncatchable and the plugin's WM_CLOSE cannot reach
// a windowless exe, so exit on our own — otherwise the close path waits
// its full 15s grace period per sign-in.
const exit = () => {
try {
writeFileSync(join(profilePath, "close-signal.txt"), "SIGTERM")
} catch {}
process.exit(0)
}
if (process.platform === "win32") {
setTimeout(() => process.exit(0), 500)
} else {
process.on("SIGTERM", exit)
process.on("SIGINT", exit)
setInterval(() => {}, 10_000)
}
} else {
mkdirSync(profilePath, { recursive: true })
const server = Bun.serve({
port: 0,
// 仅本机回环:绑 0.0.0.0(不指定 hostname 的默认)会触发 Windows 防火墙
// 对每次编译出来的新 exe 弹"允许访问",路径随机所以每次都弹
hostname: "127.0.0.1",
fetch(req, server) {
const u = new URL(req.url)
if (u.pathname === "/json/version") return Response.json({ Browser: "FakeChrome/1.0" })
if (u.pathname === "/json/list") {
// the plugin's Cdp.connect picks a type:"page" target whose URL
// matches the console host and whose webSocketDebuggerUrl it can
// open — hand it exactly that, then serve the socket on upgrade
pageTargetUrl = `ws://127.0.0.1:${server.port}/devtools/page/1`
return Response.json([
{
type: "page",
url: url || "https://discovery.intern-ai.org.cn/token-plan/home",
webSocketDebuggerUrl: pageTargetUrl,
},
])
}
// any /devtools/* request is the WebSocket the plugin opened
const up = server.upgrade(req, { data: { page: u.pathname } })
if (up) return undefined
return new Response("not found", { status: 404 })
},
websocket: {
open() {},
message(ws, raw) {
const msg = JSON.parse(String(raw))
if (msg.method === "Network.getAllCookies") {
ws.send(
JSON.stringify({
id: msg.id,
result: {
cookies: [{ name: "uaa-token", value: "fake-uaa-token-for-tests", domain: ".intern-ai.org.cn" }],
},
}),
)
} else {
ws.send(JSON.stringify({ id: msg.id, result: {} }))
}
},
},
})
// the browser-level target: /devtools/browser/<id> on the same port
writeFileSync(join(profilePath, "DevToolsActivePort"), `${server.port}\n/devtools/browser/fake\n`)
console.error("[fake-chrome] serving on", server.port)
const exit = () => process.exit(0)
process.on("SIGTERM", exit)
process.on("SIGINT", exit)
// keep alive; the plugin closes us when it has the cookie
}
} catch (e) {
console.error("[fake-chrome] FAILED:", e?.message ?? e, e?.stack?.slice(0, 400))
process.exit(1)
}