Files
plugins/packages/doubao-work/doubao-work.test.mjs
T
yetone f379bf907c doubao-work 0.1.2: say at once that an open Doubao must be quit
The owner's sign-in spun with no end. Doubao runs one copy at a time: with
the user's own Doubao open, the plugin's copy (its own profile, debugging
port) quits within a second of starting, so the port never opens.
waitSignedIn took every error but app_missing as "not signed in yet" and
started it again for five minutes, saying nothing.

- App.start looks for a running copy of the binary on another profile
  before it starts one, and fails with app_busy. The user's copy can't be
  driven instead: Chromium allows debugging only off the default profile.
- waitSignedIn ends on any app_* error (busy, missing, start timeout) and
  on abort; only a window still loading is waited for.
- Sign-in and requests say: quit Doubao (⌘Q) and try again. A request
  gets 503.

Run: bun test packages/doubao-work on mote (+ check.mjs), Linux (WSL) and
Windows: 28 pass, 0 fail. With waitSignedIn's old catch put back, the new
waitSignedIn test is red (60s timeout instead of app_busy). Live on this
Mac, with a Doubao running from another profile: waitSignedIn threw
app_busy in 92ms, nothing spawned, no profile made. That a second copy
quits was tried first: Doubao with a fresh profile and port exited in ~1s,
port never up.
Not tried: the owner's Mac (10.0.0.175) itself.
2026-11-09 06:53:05 +08:00

419 lines
21 KiB
JavaScript

import { test, expect } from "bun:test"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { App, Page, copies } from "./cdp.mjs"
import { Doubao, NEW_TASK, CLOUD, READ, STATE, binary, owns, usageOf } from "./doubao.mjs"
import { Streamer, sse, completion } from "./openai.mjs"
import { flatten, parse, heldFrom } from "./prompt.mjs"
import { _internal as I } from "./index.mjs"
let n = 0
const ids = () => "call_" + ++n
// ---- prompt ----
test("flatten writes system, tools and every turn, calls and results by name", () => {
const p = flatten({
messages: [
{ role: "system", content: "Be brief." },
{ role: "user", content: [{ type: "text", text: "How long is main.go?" }, { type: "image_url", image_url: { url: "data:," } }] },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "Bash", arguments: '{"command":"wc -l main.go"}' } }] },
{ role: "tool", tool_call_id: "c1", content: "412 main.go" },
],
tools: [{ type: "function", function: { name: "Bash", description: "Run a command", parameters: { type: "object", properties: { command: { type: "string" } } } } }],
})
expect(p).toContain("Be brief.")
expect(p).toContain("## Bash\nRun a command")
expect(p).toContain('<tool_call>{"name":"Bash","arguments":{"command":"wc -l main.go"}}</tool_call>')
expect(p).toContain("[tool result of Bash]\n412 main.go")
expect(p).toContain("[image omitted")
expect(p.indexOf("[user]")).toBeLessThan(p.indexOf("[assistant]"))
})
test("tool_choice none leaves the tools out; a named one is required", () => {
const tools = [{ type: "function", function: { name: "Read", parameters: {} } }]
const msgs = [{ role: "user", content: "hi" }]
expect(flatten({ messages: msgs, tools, tool_choice: "none" })).not.toContain("<tool_call>")
expect(flatten({ messages: msgs, tools, tool_choice: { type: "function", function: { name: "Read" } } })).toContain("You must call the tool Read now.")
expect(flatten({ messages: msgs })).not.toContain("# Tools")
})
test("parse reads calls, keeps a block it can't read as text, and reads an unclosed last call", () => {
const r = parse('Looking.\n<tool_call>{"name":"Read","arguments":{"path":"a"}}</tool_call>\n<tool_call>```json\n{"name":"Bash","parameters":{"command":"ls"}}\n```</tool_call>', ids)
expect(r.calls.map((c) => [c.function.name, JSON.parse(c.function.arguments)])).toEqual([["Read", { path: "a" }], ["Bash", { command: "ls" }]])
expect(r.text).toBe("Looking.")
expect(parse("<tool_call>not json</tool_call>", ids)).toMatchObject({ calls: [], text: "<tool_call>not json</tool_call>" })
expect(parse('<tool_call>{"name":"Read","arguments":{"path":"b"}}', ids).calls[0].function.arguments).toBe('{"path":"b"}')
expect(parse("plain answer", ids)).toMatchObject({ calls: [], text: "plain answer" })
})
test("heldFrom holds a tail that could start a call", () => {
expect(heldFrom("abc")).toBe(3)
expect(heldFrom("abc <tool")).toBe(4)
expect(heldFrom("abc <tool_call>{")).toBe(4)
expect(heldFrom("a < b")).toBe(5)
})
// ---- openai ----
test("Streamer shows text up to a call and gives calls at the end", () => {
const s = new Streamer()
expect(s.update({ text: "Let me ", reasoning: "think" })).toEqual({ reasoning: "think", content: "Let me " })
expect(s.update({ text: "Let me look.<tool", reasoning: "think more" })).toEqual({ reasoning: " more", content: "look." })
expect(s.update({ text: 'Let me look.<tool_call>{"name":"Read","arguments":{}}</tool_call>' })).toEqual({})
const f = s.finish({ text: 'Let me look.<tool_call>{"name":"Read","arguments":{}}</tool_call>\n' }, ids)
expect(f.content).toBe("")
expect(f.calls[0].function.name).toBe("Read")
})
test("Streamer: a '<' that isn't a call is shown at the end", () => {
const s = new Streamer()
expect(s.update({ text: "1 <" })).toEqual({ content: "1 " })
expect(s.finish({ text: "1 <" }, ids)).toEqual({ content: "<", calls: [] })
})
const events = async (res) =>
(await res.text())
.split("\n\n")
.filter(Boolean)
.map((l) => l.replace(/^data: /, ""))
.map((l) => (l === "[DONE]" ? l : JSON.parse(l)))
test("sse streams reasoning, content, tool_calls, finish and usage", async () => {
const res = await sse({
id: "x",
model: "auto",
newId: ids,
promptTokens: () => 100,
ask: async (on) => {
on({ text: "", reasoning: "hmm" })
on({ text: "Reading.", reasoning: "hmm" })
return { text: 'Reading.<tool_call>{"name":"Read","arguments":{"path":"x"}}</tool_call>', reasoning: "hmm" }
},
})
expect(res.headers.get("content-type")).toBe("text/event-stream")
const ev = await events(res)
const deltas = ev.filter((e) => e !== "[DONE]" && e.choices?.length).map((e) => e.choices[0])
expect(deltas.map((d) => d.delta.reasoning_content).filter(Boolean).join("")).toBe("hmm")
expect(deltas.map((d) => d.delta.content).filter(Boolean).join("")).toBe("Reading.")
expect(deltas.find((d) => d.delta.tool_calls).delta.tool_calls[0]).toMatchObject({ index: 0, type: "function", function: { name: "Read", arguments: '{"path":"x"}' } })
expect(deltas.at(-1).finish_reason).toBe("tool_calls")
expect(ev.find((e) => e.usage).usage.prompt_tokens).toBe(100)
expect(ev.at(-1)).toBe("[DONE]")
})
test("sse throws a failure that comes before the reply, and streams one after it", async () => {
const boom = Object.assign(Error("not signed in"), { code: "doubao_signed_out" })
await expect(sse({ id: "x", model: "auto", ask: async () => { throw boom } })).rejects.toBe(boom)
const res = await sse({ id: "x", model: "auto", ask: async (on) => { on({ text: "par" }); throw Error("cut off") } })
const ev = await events(res)
expect(ev.at(-1).error.message).toBe("cut off")
})
test("sse cancel aborts the ask", async () => {
let seen
const res = await sse({
id: "x",
model: "auto",
ask: (on, signal) =>
new Promise((_, reject) => {
seen = signal
on({ text: "a" })
signal.addEventListener("abort", () => reject(signal.reason))
}),
})
await res.body.cancel()
expect(seen.aborted).toBe(true)
})
test("completion gives a whole message", () => {
const c = completion({ id: "x", model: "auto", final: { text: '<tool_call>{"name":"Read","arguments":{"path":"y"}}</tool_call>', reasoning: "r" }, promptTokens: 5, newId: ids })
expect(c.choices[0]).toMatchObject({ finish_reason: "tool_calls", message: { content: null, reasoning_content: "r" } })
expect(c.usage.prompt_tokens).toBe(5)
})
// ---- doubao adapter ----
test("usageOf adds Doubao's prompt counts", () => {
expect(usageOf('{"system_prompt":6423,"messages":111175,"skills":0,"tools":0,"total_window_size":256000}')).toBe(117598)
expect(usageOf("")).toBe(0)
expect(usageOf("nope")).toBe(0)
})
test("the app is found on macOS only, or where DOUBAO_WORK_APP says", () => {
const was = process.env.DOUBAO_WORK_APP
delete process.env.DOUBAO_WORK_APP
try {
expect(binary("darwin")).toBe("/Applications/Doubao.app/Contents/MacOS/Doubao")
expect(binary("linux")).toBe("")
expect(binary("win32")).toBe("")
process.env.DOUBAO_WORK_APP = "/opt/doubao"
expect(binary("linux")).toBe("/opt/doubao")
} finally {
if (was === undefined) delete process.env.DOUBAO_WORK_APP
else process.env.DOUBAO_WORK_APP = was
}
})
test("owns knows Doubao's chat page", () => {
expect(owns([{ url: "doubao://doubao-chat/chat" }])).toBe(true)
expect(owns([{ url: "chrome://newtab" }, { url: "https://doubao-chat/chat" }])).toBe(false)
})
function fakePage(reads, { signedIn = true, steps = {} } = {}) {
const calls = []
const keys = []
return {
calls,
keys,
closed: false,
async eval(expr) {
calls.push(expr)
if (expr === STATE) return { signedIn, name: "yetone", sends: 0 }
if (expr === NEW_TASK) return steps.newTask ?? ""
if (expr === CLOUD) return steps.cloud ?? ""
if (expr === READ) return reads.length > 1 ? reads.shift() : reads[0]
if (expr.includes("chat_input_action_model")) return steps.model ?? ""
if (expr.includes("insertContent")) return 10
throw Error("unexpected eval")
},
async key(k) { keys.push(k) },
}
}
const fakeApp = { start: async () => 1 }
test("ask opens a task on 云电脑, sends, and follows the reply to its end", async () => {
const page = fakePage([
{ sends: 0, reply: false },
{ sends: 1, reply: false },
{ sends: 1, reply: true, text: "Hel", reasoning: "", finished: false },
{ sends: 1, reply: true, text: "Hello", reasoning: "", finished: true, status: "Success", reason: "succeed:completion", usage: '{"messages":12}' },
])
const d = new Doubao({ app: fakeApp, openPage: async () => page, poll: 1 })
const seen = []
const r = await d.ask("hi\nthere", { model: "doubao-2.1-lite", onUpdate: (x) => seen.push(x.text) })
expect(r.text).toBe("Hello")
expect(seen).toEqual(["Hel", "Hello"])
expect(page.keys).toEqual(["Enter"])
const order = page.calls.map((c) => (c === STATE ? "state" : c === NEW_TASK ? "new" : c === CLOUD ? "cloud" : c.includes("豆包 2.1 Lite") ? "model" : c.includes("insertContent") ? "fill" : "read"))
expect(order.slice(0, 5)).toEqual(["state", "new", "cloud", "model", "fill"])
expect(page.calls.find((c) => c.includes("insertContent"))).toContain('["hi","there"]')
})
test("ask refuses when Doubao is signed out, and a paid model with 403", async () => {
const out = new Doubao({ app: fakeApp, openPage: async () => fakePage([], { signedIn: false }), poll: 1 })
await expect(out.ask("x")).rejects.toMatchObject({ signIn: "expired", code: "doubao_signed_out" })
const pro = new Doubao({ app: fakeApp, openPage: async () => fakePage([], { steps: { model: "upgrade" } }), poll: 1 })
await expect(pro.ask("x", { model: "doubao-2.1-pro" })).rejects.toMatchObject({ status: 403, code: "doubao_upgrade" })
const stuck = new Doubao({ app: fakeApp, openPage: async () => fakePage([], { steps: { cloud: "no 云电脑 in the mode menu" } }), poll: 1 })
await expect(stuck.ask("x")).rejects.toMatchObject({ code: "doubao_ui" })
})
test("ask fails a reply that ended empty, and one that stopped growing", async () => {
const empty = new Doubao({ app: fakeApp, openPage: async () => fakePage([{ sends: 1, reply: true, text: "", reasoning: "", finished: true, status: "Failed", reason: "fail:risk" }]), poll: 1 })
await expect(empty.ask("x")).rejects.toMatchObject({ code: "doubao_failed", status: 502 })
const idle = new Doubao({ app: fakeApp, openPage: async () => fakePage([{ sends: 1, reply: true, text: "a", reasoning: "", finished: false }]), poll: 1, idle: 20 })
await expect(idle.ask("x")).rejects.toMatchObject({ code: "doubao_timeout" })
})
test("requests run one at a time", async () => {
let open = 0
let most = 0
const page = fakePage([{ sends: 1, reply: true, text: "ok", reasoning: "", finished: true, reason: "succeed" }])
const ev = page.eval.bind(page)
page.eval = async (e) => {
if (e === NEW_TASK) { open++; most = Math.max(most, open) }
if (e === READ) { await new Promise((r) => setTimeout(r, 5)); open-- }
return ev(e)
}
const d = new Doubao({ app: fakeApp, openPage: async () => page, poll: 1 })
await Promise.all([d.ask("a"), d.ask("b"), d.ask("c")])
expect(most).toBe(1)
})
test("an aborted ask stops", async () => {
const d = new Doubao({ app: fakeApp, openPage: async () => fakePage([{ sends: 1, reply: true, text: "a", reasoning: "", finished: false }]), poll: 5 })
const ac = new AbortController()
const p = d.ask("x", { signal: ac.signal })
setTimeout(() => ac.abort(), 20)
await expect(p).rejects.toBeDefined()
})
// ---- cdp ----
test("App reuses its own running app and never drives a port someone else took", async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "dw-"))
try {
fs.writeFileSync(path.join(dir, "port"), "4567")
const bin = path.join(dir, "app")
fs.writeFileSync(bin, "")
const spawned = []
const mk = (pages) =>
new App({
binary: bin,
dataDir: dir,
owns,
spawn: (...a) => { spawned.push(a); return { unref() {} } },
others: () => [],
fetchImpl: async (url) => Response.json(String(url).includes(":4567/") ? pages : [{ type: "page", url: "doubao://doubao-chat/chat" }]),
})
expect(await mk([{ type: "page", url: "doubao://doubao-chat/chat" }]).start()).toBe(4567)
expect(spawned.length).toBe(0)
const port = await mk([{ type: "page", url: "https://example.com" }]).start({ timeout: 2000 })
expect(port).not.toBe(4567)
expect(spawned[0][1]).toEqual([`--remote-debugging-port=${port}`, `--user-data-dir=${path.join(dir, "profile")}`])
expect(fs.readFileSync(path.join(dir, "port"), "utf8")).toBe(String(port))
} finally {
fs.rmSync(dir, { recursive: true, force: true })
}
})
test("App says at once that a copy the user opened keeps it from starting", async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "dw-"))
try {
const bin = path.join(dir, "app")
fs.writeFileSync(bin, "")
const spawned = []
const mk = (others) =>
new App({ binary: bin, dataDir: dir, owns, others: () => others, spawn: (...a) => { spawned.push(a); return { unref() {} } }, fetchImpl: async () => { throw Error("refused") } })
await expect(mk([bin]).start({ timeout: 60000 })).rejects.toMatchObject({ code: "app_busy" })
await expect(mk([`${bin} --type=gpu --user-data-dir=/elsewhere`]).start({ timeout: 60000 })).rejects.toMatchObject({ code: "app_busy" })
expect(spawned.length).toBe(0)
// its own copy, still starting, is waited for
await expect(mk([`${bin} --user-data-dir=${path.join(dir, "profile")}`]).start({ timeout: 500 })).rejects.toMatchObject({ code: "app_start_timeout" })
expect(spawned.length).toBe(1)
} finally {
fs.rmSync(dir, { recursive: true, force: true })
}
})
test("copies lists the binary's own processes only", () => {
expect(copies("/no/such/binary-" + Date.now())).toEqual([])
})
test("waitSignedIn says the app not starting at once, and waits for a window still loading", async () => {
const busy = new Doubao({ app: { start: async () => { throw Object.assign(Error("busy"), { code: "app_busy" }) } }, poll: 1 })
await expect(busy.waitSignedIn({ timeout: 60000 })).rejects.toMatchObject({ code: "app_busy" })
let n = 0
const loading = new Doubao({ app: fakeApp, openPage: async () => (++n < 2 ? null : fakePage([])), poll: 1 })
expect(await loading.waitSignedIn({ timeout: 60000 })).toMatchObject({ signedIn: true })
})
test("App says when the app is not installed", async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "dw-"))
try {
const app = new App({ binary: path.join(dir, "nope"), dataDir: dir, owns, fetchImpl: async () => { throw Error("refused") } })
await expect(app.start()).rejects.toMatchObject({ code: "app_missing" })
} finally {
fs.rmSync(dir, { recursive: true, force: true })
}
})
class FakeWS {
constructor(url) {
this.url = url
this.sent = []
queueMicrotask(() => this.onopen?.())
}
send(s) {
const m = JSON.parse(s)
this.sent.push(m)
queueMicrotask(() => {
if (m.params?.expression === "throw") this.onmessage({ data: JSON.stringify({ id: m.id, result: { exceptionDetails: { exception: { description: "Error: boom\n at x" } } } }) })
else this.onmessage({ data: JSON.stringify({ id: m.id, result: { result: { value: m.params?.expression === "1+1" ? 2 : null } } }) })
})
}
close() { this.onclose?.() }
}
test("Page evaluates, turns page exceptions into errors, and fails calls once closed", async () => {
const page = await Page.open(1, (t) => t.url.includes("chat"), {
fetchImpl: async () => Response.json([{ type: "page", url: "doubao://doubao-chat/chat", webSocketDebuggerUrl: "ws://x" }]),
WebSocketImpl: FakeWS,
})
expect(await page.eval("1+1")).toBe(2)
await expect(page.eval("throw")).rejects.toThrow("page: Error: boom")
await page.key("Enter", { keyCode: 13, text: "\r" })
expect(page.ws.sent.filter((m) => m.method === "Input.dispatchKeyEvent").map((m) => m.params.type)).toEqual(["keyDown", "keyUp"])
page.ws.close()
await expect(page.eval("1+1")).rejects.toThrow("closed")
})
// ---- plugin ----
const req = (body) => new Request(I.BASE + "/chat/completions", { method: "POST", body: JSON.stringify(body), headers: { "Content-Type": "application/json" } })
test("the plugin serves chat completions through Doubao, whole and streamed", async () => {
const asked = []
const doubao = {
ask: async (prompt, { model, onUpdate }) => {
asked.push({ prompt, model })
onUpdate?.({ text: "Hi", reasoning: "" })
return { text: "Hi there", reasoning: "", usage: '{"messages":42}' }
},
}
const p = I.createPlugin({ doubao, newId: ids })
const cfg = {}
await p.config(cfg)
expect(Object.keys(cfg.provider["doubao-work"].models)).toEqual(["auto", "doubao-2.1-lite", "doubao-2.1-pro"])
for (const m of Object.values(cfg.provider["doubao-work"].models)) {
expect(m.name).not.toContain("豆包工作")
expect(m.modalities.input).toEqual(["text"])
}
const loader = await p.auth.loader(async () => ({}))
const whole = await (await loader.fetch(req({ model: "doubao-2.1-lite", messages: [{ role: "user", content: "hello" }] }))).json()
expect(whole.choices[0].message.content).toBe("Hi there")
expect(whole.usage.prompt_tokens).toBe(42)
expect(asked[0].model).toBe("doubao-2.1-lite")
expect(asked[0].prompt).toContain("[user]\nhello")
const streamed = await events(await loader.fetch(req({ model: "auto", stream: true, messages: [{ role: "user", content: "hello" }] })))
expect(streamed.filter((e) => e.choices?.[0]?.delta?.content).map((e) => e.choices[0].delta.content).join("")).toBe("Hi there")
expect((await loader.fetch(req({ model: "gpt-9", messages: [] }))).status).toBe(404)
expect((await loader.fetch(new Request("https://elsewhere.example/v1/chat/completions", { method: "POST", body: "{}" }))).status).toBe(404)
})
test("the plugin passes a sign-out on for re-sign-in and other failures as HTTP errors", async () => {
const fail = (e) => I.createPlugin({ doubao: { ask: async () => { throw e } } })
const out = await fail(Object.assign(Error("signed out"), { signIn: "expired" })).auth.loader(async () => ({}))
await expect(out.fetch(req({ model: "auto", messages: [] }))).rejects.toMatchObject({ signIn: "expired" })
const missing = await fail(Object.assign(Error("not installed"), { code: "app_missing" })).auth.loader(async () => ({}))
expect((await missing.fetch(req({ model: "auto", stream: true, messages: [] }))).status).toBe(503)
const busy = await fail(Object.assign(Error("busy"), { code: "app_busy" })).auth.loader(async () => ({}))
const rb = await busy.fetch(req({ model: "auto", messages: [] }))
expect(rb.status).toBe(503)
expect((await rb.json()).error.message).toContain("退出豆包")
const paid = await fail(Object.assign(Error("upgrade"), { code: "doubao_upgrade", status: 403 })).auth.loader(async () => ({}))
const r = await paid.fetch(req({ model: "auto", messages: [] }))
expect(r.status).toBe(403)
expect((await r.json()).error.type).toBe("doubao_upgrade")
})
test("sign-in waits for the Doubao window's account", async () => {
const mac = () => "/Applications/Doubao.app/Contents/MacOS/Doubao"
const ok = I.createPlugin({ appBinary: mac, doubao: { waitSignedIn: async () => ({ signedIn: true, name: "yetone" }) } })
const r = await (await ok.auth.methods[0].authorize()).callback()
expect(r).toMatchObject({ type: "success", accountId: "豆包工作 · yetone" })
const late = I.createPlugin({ appBinary: mac, doubao: { waitSignedIn: async () => null } })
expect(await (await late.auth.methods[0].authorize()).callback()).toMatchObject({ type: "failed", error: expect.stringContaining("五分钟") })
const busy = I.createPlugin({ appBinary: mac, doubao: { waitSignedIn: async () => { throw Object.assign(Error("busy"), { code: "app_busy" }) } } })
const b = await (await busy.auth.methods[0].authorize()).callback()
expect(b.type).toBe("failed")
expect(b.error).toContain("退出豆包")
const linux = I.createPlugin({ appBinary: () => "", doubao: { waitSignedIn: async () => ({ signedIn: true, name: "x" }) } })
expect(await (await linux.auth.methods[0].authorize()).callback()).toMatchObject({ type: "failed", error: expect.stringContaining("macOS") })
})
test("an abort between two polls stops the ask too", async () => {
const ac = new AbortController()
const page = fakePage([{ sends: 1, reply: true, text: "a", reasoning: "", finished: false }])
const ev = page.eval.bind(page)
page.eval = async (e) => {
if (e === READ) ac.abort()
return ev(e)
}
const d = new Doubao({ app: fakeApp, openPage: async () => page, poll: 1 })
await expect(d.ask("x", { signal: ac.signal })).rejects.toBeDefined()
})