Files
plugins/packages/inkstone/testdata/fake-browser.mjs
T
我是一条没有名字的区andnonamelane f4ff29c9b7 inkstone 0.1.5: sign-in window closes in ~30s, refused keys removed, unchanged bodies byte-for-byte (#847)
inkstone 0.1.5 changes three things:
- The sign-in window closes within about half a minute.
- A refused key is removed, so it no longer leaves a dead end.
- An unchanged request body is sent byte-for-byte.

Runs at head d90df26 merged onto main 4a063646, in WSL (Ubuntu 24.04):
- inkstone package tests: 120 pass, 0 fail.
- bun scripts/check.mjs inkstone: exit 0.
- Root bun test: 8024 pass, 19 skip, 0 fail.

Broken by hand:
- Without the per-attempt removed reset, "a stale removed does not spare the last orphan" goes red.
- Changed to upstream(input, init), "the fetch wiring is the request path" goes red.

Earlier on mote (macOS), at main 1aab125: 120 pass, and root 7579 pass, 0 fail.

Not tried: a real inkstone account, and Windows.

Co-authored-by: nonamelane <nonamelane@users.noreply.github.com>
2026-11-11 01:00:30 +08:00

126 lines
5.4 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)
}
// B1/B4: after "the user signed in", write the console session into the
// profile's real Cookies sqlite — the plugin watches it off Windows to
// close the window itself. FAKE_BROWSER_HOLD_COOKIE keeps it out
// (an old session with no new cookie: the wait must not read that as done).
if (!process.env.FAKE_BROWSER_HOLD_COOKIE) {
setTimeout(() => {
try {
mkdirSync(join(profilePath, "Default"), { recursive: true })
const { Database } = require("bun:sqlite")
const db = new Database(join(profilePath, "Default", "Cookies"))
db.query(
"CREATE TABLE IF NOT EXISTS cookies (host_key TEXT, name TEXT, value TEXT, creation_utc INTEGER, last_update_utc INTEGER, expires_utc INTEGER)",
).run()
const now = Date.now()
const micros = (ms) => (ms + 11644473600000) * 1000
db.query(
"INSERT INTO cookies (host_key, name, value, creation_utc, last_update_utc, expires_utc) VALUES ('.intern-ai.org.cn', 'uaa-token', 'fake-uaa-token', ?, ?, ?)",
).run(micros(now), micros(now), micros(now + 86400_000))
db.close()
} catch (e) {
console.error("[fake-chrome] cookie write failed:", e?.message ?? e)
}
}, 300)
}
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)
}