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.
145 lines
6.0 KiB
JavaScript
145 lines
6.0 KiB
JavaScript
// The browser helpers that can be tested without launching a browser: the
|
|
// cookie parse, JWT expiry, argv, and the profile location.
|
|
import "./nonet.mjs"
|
|
import { join } from "node:path"
|
|
import { expect, test } from "bun:test"
|
|
import {
|
|
browserArgs,
|
|
browserCandidates,
|
|
jwtExp,
|
|
profileDir,
|
|
readUaaToken,
|
|
requireBrowser,
|
|
tokenFresh,
|
|
waitForConsoleWindow,
|
|
waitForToken,
|
|
} from "./lib/browser.mjs"
|
|
|
|
test("uaa-token is read out of document.cookie and URL-decoded", () => {
|
|
expect(readUaaToken("a=b; uaa-token=abc.def.ghi; c=d")).toBe("abc.def.ghi")
|
|
const encoded = encodeURIComponent("hello world")
|
|
expect(readUaaToken(`uaa-token=${encoded}`)).toBe("hello world")
|
|
expect(readUaaToken("")).toBe("")
|
|
expect(readUaaToken("nothing=here")).toBe("")
|
|
})
|
|
|
|
test("a JWT's exp is read, an opaque token is left to the 401", () => {
|
|
const exp = Math.floor(Date.now() / 1000) + 3600
|
|
const jwt = ["e30", Buffer.from(JSON.stringify({ exp })).toString("base64url"), "sig"].join(".")
|
|
expect(jwtExp(jwt)).toBe(exp * 1000)
|
|
expect(jwtExp("opaque-token")).toBe(0)
|
|
expect(tokenFresh(jwt, 10 * 60_000)).toBe(true)
|
|
const stale = ["e30", Buffer.from(JSON.stringify({ exp: Math.floor(Date.now() / 1000) - 1 })).toString("base64url"), "sig"].join(".")
|
|
expect(tokenFresh(stale, 10 * 60_000)).toBe(false)
|
|
expect(tokenFresh("opaque-token", 10 * 60_000)).toBe(true)
|
|
})
|
|
|
|
test("browser candidates reject a path that is not there", () => {
|
|
expect(() => browserCandidates("C:\\definitely\\not\\here.exe")).toThrow()
|
|
// an existing executable is taken as the explicit browser
|
|
expect(browserCandidates(process.execPath)).toEqual([process.execPath])
|
|
})
|
|
|
|
test("requireBrowser gives the first browser, and with none says what to use instead (#825)", () => {
|
|
expect(requireBrowser(undefined, { candidates: () => ["/a/chrome", "/b/edge"] })).toBe("/a/chrome")
|
|
const none = { candidates: () => [] }
|
|
for (const platform of ["darwin", "linux", "win32"]) {
|
|
let msg = ""
|
|
try {
|
|
requireBrowser(undefined, { ...none, platform })
|
|
} catch (e) {
|
|
msg = e.message
|
|
}
|
|
expect(msg).toContain('"API key only"')
|
|
expect(msg).toContain('"API key + console token"')
|
|
expect(msg).toContain("MAGPIE_INKSTONE_BROWSER")
|
|
// Safari is named on macOS only, where it is the browser the user has
|
|
if (platform === "darwin") expect(msg).toContain("Safari can't be used")
|
|
else expect(msg).not.toContain("Safari")
|
|
}
|
|
// a missing explicit path still says which path
|
|
expect(() => requireBrowser("/definitely/not/here")).toThrow("/definitely/not/here")
|
|
})
|
|
|
|
test("waitForToken's single read has its own timeout, the account never waits 8 minutes", async () => {
|
|
let calls = 0
|
|
const session = {
|
|
token: async () => {
|
|
calls += 1
|
|
return calls === 1 ? "" : "read-token"
|
|
},
|
|
}
|
|
expect(await waitForToken(session, { timeoutMs: 5000, pollMs: 50 })).toBe("read-token")
|
|
expect(calls).toBe(2)
|
|
|
|
const gridlock = { token: async () => new Promise(() => {}) }
|
|
const start = Date.now()
|
|
await expect(waitForToken(gridlock, { timeoutMs: 50, pollMs: 25 })).rejects.toThrow(/登录超时/)
|
|
expect(Date.now() - start).toBeLessThan(2000)
|
|
})
|
|
|
|
test("headless renewal gets its own argv and the profile is per platform", () => {
|
|
const headless = browserArgs("C:\\profile", 0, "https://discovery.intern-ai.org.cn/token-plan/home", {
|
|
headless: true,
|
|
})
|
|
expect(headless[0]).toBe("--headless=new")
|
|
expect(headless).toContain("--user-data-dir=C:\\profile")
|
|
expect(headless).toContain("--remote-debugging-port=0")
|
|
// the visible login window carries no debugging port: the console's
|
|
// CAPTCHA must see an ordinary browser, not an automation session
|
|
const visible = browserArgs("C:\\profile", 0, "https://discovery.intern-ai.org.cn/token-plan/home", {
|
|
cdp: false,
|
|
login: true,
|
|
})
|
|
expect(visible.some((a) => a.startsWith("--remote-debugging-port"))).toBe(false)
|
|
expect(visible).toContain("--window-size=1100,800")
|
|
// join() on the expected side too: the separator is the test host's, not win32's
|
|
expect(profileDir({ LOCALAPPDATA: "C:\\Users\\u\\AppData\\Local" }, "win32")).toBe(
|
|
join("C:\\Users\\u\\AppData\\Local", "magpie", "inkstone-browser"),
|
|
)
|
|
expect(profileDir({ HOME: "/home/u" }, "linux").endsWith(join("magpie", "inkstone-browser"))).toBe(true)
|
|
})
|
|
|
|
test('off Windows the login wait ends on process exit, never on a title', async () => {
|
|
const { spawn } = await import('node:child_process')
|
|
// a child that stays alive in a non-Windows run must never be treated as
|
|
// 'console': only its exit ends the wait
|
|
const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 10000)'], { stdio: 'ignore' })
|
|
try {
|
|
const early = await waitForConsoleWindow(child, { platform: 'linux', timeoutMs: 120, pollMs: 40 })
|
|
expect(early).toBe('timeout')
|
|
child.kill()
|
|
const how = await waitForConsoleWindow(child, { platform: 'linux', timeoutMs: 2000, pollMs: 50 })
|
|
expect(how).toBe('closed')
|
|
} finally {
|
|
try { child.kill() } catch {}
|
|
}
|
|
})
|
|
|
|
test('the window title is read on Windows only, even when it already shows the console (#824)', async () => {
|
|
const { spawn } = await import('node:child_process')
|
|
const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 10000)'], { stdio: 'ignore' })
|
|
try {
|
|
// a title that would end the wait at once, if anything read it
|
|
let reads = 0
|
|
const titleOf = () => {
|
|
reads += 1
|
|
return '书生·端砚 Intern InkStone'
|
|
}
|
|
for (const platform of ['darwin', 'linux']) {
|
|
reads = 0
|
|
expect(await waitForConsoleWindow(child, { platform, titleOf, settleMs: 0, timeoutMs: 200, pollMs: 20 })).toBe('timeout')
|
|
expect(reads).toBe(0)
|
|
}
|
|
// the same title does end it on Windows, so the check above is not empty
|
|
// — on the second hit exactly (a build that counts three reads the
|
|
// window a third time before it answers, and fails here)
|
|
reads = 0
|
|
expect(await waitForConsoleWindow(child, { platform: 'win32', titleOf, settleMs: 0, timeoutMs: 2000, pollMs: 20 })).toBe('console')
|
|
expect(reads).toBe(2)
|
|
} finally {
|
|
try { child.kill() } catch {}
|
|
}
|
|
})
|
|
|