Files
Zhaoji Wang 133cd35107 agent-office: Settings pickers read their value, keep open on overscroll, and the page's scrollbars are magpie's thin thumb (#778)
The three Settings pickers' buttons are labelled by the label and their
own value (aria-labelledby="lblScene optScene"), so they read "Scene
Office" again as the <select> did. .picker-list gets
overscroll-behavior: contain, so wheeling past a list's end no longer
scrolls #settings and closes it, and a #settings scroll in the frame a
picker opens repositions the list instead of closing it. The page's
scrollbars take magpie's thin thumb (8px, --faint, --muted on hover,
the same as magpie's Mac rule for pages), with --faint read from the
host's --magpie-faint token. Fixes #773. By frederick-wang.

Run on mote at c7ecc7a6 (Bun 1.4.2, HOME=mktemp -d):
packages/agent-office bun test 183 pass, 0 fail; bun scripts/check.mjs
exit 0; packages/agent-office stamp-page.mjs --check exit 0.
A Playwright script of the reviewer's own against the standalone page
(bin/agent-office.mjs --no-open, 600x520, chromium and webkit, light
and dark), run 4 times: the three accessible names are "Language
Auto", "Scene Office", "Quality Medium"; focus+Enter on #optQuality
keeps its list open; four 600px wheels over #optSceneList keep it open;
setting #settings' scrollTop still closes the picker; the list's
scrollbar is 8px with a #b9b9c2 thumb, #56565f under data-theme=dark;
no page errors.
Broken by hand: the same script at the base 10a61c2e fails the three
names in every engine and theme, and focus+Enter closed the Quality
list in 2 of 4 runs (the #773 race); the thumb is transparent there.
Not tried: test/browser.mjs inside a real magpie host window (needs a
magpie web URL); the wheel check also passes at the base in headless
Chromium and WebKit, so it does not prove the overscroll fix.
2026-11-06 11:08:29 +08:00

390 lines
22 KiB
JavaScript

import { afterAll, expect, test } from "bun:test"
import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync, existsSync, utimesSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { createTracker, outcomeOf, replyOf } from "./lib/track.mjs"
import { readClaude, readCodex, readDsh, readGemini, readPi, toolLine } from "./lib/readers.mjs"
import { createScanner } from "./lib/scan.mjs"
import * as F from "./test/fixtures.mjs"
const dir = import.meta.dir
const page = join(dir, "page")
const read = (f) => readFileSync(join(page, f), "utf8")
const pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"))
// the page's classic scripts, as the page loads them
const scope = {}
new Function("globalThis", read("agents.js"))(scope)
new Function("globalThis", read("scene.js"))(scope)
new Function("globalThis", read("model.js"))(scope)
const M = scope.AgentOfficeModel
const A = scope.AgentOfficeAgents
const { NAV, BLOCKERS, DESKS, SOFA, ZONE_SPOTS } = scope.AgentOfficeScene
// every point of a way, sampled, is on a floor one may stand on
const walkable = (pts) => {
for (let i = 0; i < pts.length - 1; i++) {
const [ax, ay] = pts[i], [bx, by] = pts[i + 1]
const n = Math.ceil(Math.hypot(bx - ax, by - ay) / 0.1)
for (let k = 0; k <= n; k++) {
const x = ax + ((bx - ax) * k) / n, y = ay + ((by - ay) * k) / n
if (!NAV.freeAt(x, y)) return `blocked at ${x.toFixed(2)},${y.toFixed(2)}`
}
}
return ""
}
const inside = ([x, y], [bx, by, bw, bd]) => x >= bx && x <= bx + bw && y >= by && y <= by + bd
const tmps = []
const tmp = () => { const d = mkdtempSync(join(tmpdir(), "agent-office-")); tmps.push(d); return d }
afterAll(() => { for (const d of tmps) rmSync(d, { recursive: true, force: true }) })
// ---- the package --------------------------------------------------------
test("a page magpie serves, with a host part and no reads", () => {
expect(pkg.name).toBe("@magpie-community/page-agent-office")
expect(pkg.main).toBeUndefined()
expect(pkg.magpie.page.file).toBe("page/index.html")
expect(pkg.magpie.hooks).toBe("./office.hooks.mjs")
expect(pkg.magpie.permissions).toEqual([])
const html = read("index.html")
expect(html).toContain('<script src="_magpie/bridge.js"></script>')
for (const m of html.matchAll(/<script([^>]*)>([\s\S]*?)<\/script>/g)) {
expect(m[1]).toMatch(/\ssrc="/)
expect(m[2].trim()).toBe("")
}
for (const m of html.matchAll(/(?:src|href)="([^"]+)"/g)) {
if (m[1].startsWith("_magpie/")) continue
expect(readdirSync(page)).toContain(m[1].split("?")[0])
}
for (const f of ["index.html", "style.css", "agents.js", "model.js", "icons.js", "scene.js", "app.js", "icon.svg"]) expect(pkg.files).toContain(`page/${f}`)
for (const f of ["page/icons", "lib", "office.hooks.mjs", "page/scene3d.js", "page/three.min.js", "bin", "SKILL.md"]) expect(pkg.files).toContain(f)
expect(html).toMatch(/<script src="three\.min\.js\?v=[a-f0-9]+"><\/script>/)
expect(html).toMatch(/<script src="scene3d\.js\?v=[a-f0-9]+"><\/script>/)
// screenshots are linked from GitHub: an install needs none of them
expect(pkg.files).not.toContain("docs")
expect(new Set(pkg.files).size).toBe(pkg.files.length)
for (const m of readFileSync(join(dir, "README.md"), "utf8").matchAll(/!\[[^\]]*\]\(([^)]+)\)/g)) {
// badges and screenshots are remote: nothing relative ships in the install
expect(m[1]).toMatch(/^https:\/\//)
expect(m[1]).not.toMatch(/^docs\//)
}
})
test("the page hides text itself when the switch is off", () => {
const [a, b] = M.redact([
{ title: "SECRET-TITLE", project: "secret-project", query: "SECRET-QUERY", reply: "SECRET-REPLY", error: "SECRET-ERROR", step: "Bash · SECRET-CMD curl x", brand: { name: "Claude" }, agent: "claude" },
{ title: "SECRET-TITLE", project: "", query: "q", reply: "r", error: "e", step: "SECRET-CMD curl x", brand: { name: "Codex" }, agent: "codex" },
])
expect(a).toMatchObject({ title: "secret-project", query: "", reply: "", error: "", step: "Bash" })
expect(b).toMatchObject({ title: "Codex", query: "", reply: "", error: "", step: "" })
expect(read("app.js")).toContain("M.redact")
})
test("the page reaches no clipboard API it cannot have", () => {
const src = read("app.js")
expect(src).not.toContain("navigator.clipboard")
expect(src).not.toContain("execCommand")
})
test("the page reads nothing of magpie's and reaches no network", () => {
// three.min.js is a vendored renderer with dormant loader strings; the
// scene3d.js we wrote must itself never reach out. Both are checked apart.
for (const f of readdirSync(page).filter((f) => f.endsWith(".js") && !["live.js", "scene3d.js", "three.min.js"].includes(f))) {
const src = read(f)
expect(src).not.toMatch(/magpie\.read\(/)
expect(src).not.toMatch(/\bfetch\(|XMLHttpRequest|WebSocket|sendBeacon|EventSource|importScripts|new Worker/)
}
expect(read("scene3d.js")).not.toMatch(/\bfetch\(|XMLHttpRequest|WebSocket|sendBeacon|EventSource|importScripts|new Worker/)
})
test("every agent magpie knows, each with its own icon file", () => {
const ids = Object.keys(A)
expect(ids.length).toBeGreaterThanOrEqual(50)
for (const id of ["claude", "codex", "gemini", "dsh", "grok", "agy", "cursor", "copilot", "opencode", "pi", "qwen", "kimi", "cline", "droid", "qoder", "workbuddy", "codebuddy", "zed", "hermes", "goose", "crush", "devin"]) {
expect(A[id]).toBeDefined()
}
const icons = new Set(readdirSync(join(page, "icons")))
let withIcon = 0
for (const [id, a] of Object.entries(A)) if (a.icon) { expect(icons.has(a.icon)).toBe(true); withIcon++ }
expect(withIcon).toBeGreaterThanOrEqual(50)
expect(M.brand("claude")).toMatchObject({ name: "Claude Code", icon: "claudecode-color.svg" })
expect(M.brand("codex").icon).toBe("codex-color.svg")
expect(M.brand("dsh")).toMatchObject({ name: "DeepSeek Harness", icon: "deepseek-color.svg" })
expect(M.brand("someone-new")).toMatchObject({ name: "someone-new", icon: "" })
})
// ---- the agents' own files ---------------------------------------------
test("Claude Code: a tool out, then done; a question; an interruption", () => {
const r = readClaude(F.claudeRunning)
expect(r).toMatchObject({ phase: "tool", step: "Edit · /Users/me/app/a.ts", title: "Fix failing test", query: "fix the failing test", model: "claude-opus-5", project: "app", turns: 1 })
expect(r.pending.map((p) => p.id)).toEqual(["t2"])
const d = readClaude(F.claudeDone)
expect(d).toMatchObject({ phase: "done", reply: "Fixed: the date was parsed in UTC." })
expect(d.pending).toEqual([])
expect(readClaude(F.claudeAsks).phase).toBe("ask")
expect(readClaude(F.claudeInterrupted).phase).toBe("aborted")
})
test("Codex: a running tool, an approval, done, aborted; its own prompts only", () => {
const r = readCodex(F.codexRunning)
expect(r).toMatchObject({ id: "019a-x", phase: "tool", model: "gpt-6.1-terra", project: "blog", query: "export the doc as markdown" })
expect(r.step).toBe("exec · pandoc in.docx -o out.md")
expect(readCodex(F.codexApproval)).toMatchObject({ phase: "approval", step: "shell · rm -rf dist" })
expect(readCodex(F.codexDone)).toMatchObject({ phase: "done", reply: "Exported to out.md." })
expect(readCodex(F.codexAborted).phase).toBe("aborted")
})
test("DeepSeek Harness: an approval asked, then an error", () => {
const r = readDsh(F.dshApproval)
expect(r).toMatchObject({ id: "session-d1", phase: "approval", title: "Deploy the site", model: "deepseek-flash", query: "deploy to cloudflare", project: "site" })
expect(r.step).toBe("bash · wrangler pages deploy ./dist")
expect(readDsh(F.dshError)).toMatchObject({ phase: "error", error: "429: usage limit reached" })
})
test("Gemini CLI and Pi", () => {
expect(readGemini(F.geminiApproval)).toMatchObject({ phase: "approval", query: "align the chorus to 120 BPM", model: "gemini-3-pro" })
expect(readPi(F.piDone)).toMatchObject({ phase: "done", reply: "Done: 12 tools, 5 ideas.", project: "notes" })
})
test("a tool line reads like what it does", () => {
expect(toolLine("Bash", { command: "bun test" })).toBe("Bash · bun test")
expect(toolLine("exec", 'text(await tools.exec_command({cmd:"rg -n foo"}))')).toBe("exec · rg -n foo")
expect(toolLine("shell", { command: ["git", "status"] })).toBe("shell · git status")
})
test("the scanner reads each agent's folder, and only what changed", () => {
const h = tmp()
const cl = join(h, ".claude", "projects", "-Users-me-app")
const cx = join(h, ".codex", "sessions", "2026", "10", "28")
mkdirSync(cl, { recursive: true }); mkdirSync(cx, { recursive: true })
writeFileSync(join(cl, "c1.jsonl"), F.claudeRunning)
writeFileSync(join(cx, "rollout-2026-10-28T10-00-00-019a0000-0000-7000-8000-000000000001.jsonl"), F.codexApproval)
const src = [
{ agent: "claude", kind: "claude", root: join(h, ".claude", "projects"), depth: 1, match: (n) => n.endsWith(".jsonl") },
{ agent: "codex", kind: "codex", root: join(h, ".codex", "sessions"), depth: 3, match: (n) => n.startsWith("rollout-") },
]
const sc = createScanner({ sources: src, days: 2 })
const now = Date.now()
let got = sc.scan(now)
expect(got.map((s) => [s.agent, s.phase]).sort()).toEqual([["claude", "tool"], ["codex", "approval"]])
// the agent writes on: the scanner reads the file again
writeFileSync(join(cl, "c1.jsonl"), F.claudeDone)
got = sc.scan(now + 1000)
expect(got.find((s) => s.agent === "claude").phase).toBe("done")
})
test("an active dsh session is decompressed only once per interval", () => {
const h = tmp()
const sd = join(h, "sessions", "--Users-me-site--", "session-z")
mkdirSync(sd, { recursive: true })
const file = join(sd, "session.v4.jsonl.zstd")
const enc = (t) => new TextEncoder().encode(t)
writeFileSync(file, Bun.zstdCompressSync(enc(F.dshApproval)))
const sc = createScanner({
sources: [{ agent: "dsh", kind: "dsh", root: join(h, "sessions"), depth: 2, match: (n) => n.endsWith(".zstd") }],
days: 2,
zstdEvery: 60000,
})
const now = Date.now()
const orig = Bun.zstdDecompressSync
let decodes = 0
Bun.zstdDecompressSync = (...a) => { decodes++; return orig(...a) }
try {
let got = sc.scan(now)
expect(decodes).toBe(1)
expect(got[0].phase).toBe("approval")
// the agent writes on: within the interval the last facts stand
writeFileSync(file, Bun.zstdCompressSync(enc(F.dshError)))
utimesSync(file, (now + 20000) / 1000, (now + 20000) / 1000)
got = sc.scan(now + 1500)
expect(decodes).toBe(1)
expect(got[0].phase).toBe("approval")
expect(got[0].touched).toBeGreaterThan(now)
// once the interval has passed it reads the new state
utimesSync(file, (now + 30000) / 1000, (now + 30000) / 1000)
got = sc.scan(now + 62000)
expect(decodes).toBe(2)
expect(got[0].phase).toBe("error")
} finally { Bun.zstdDecompressSync = orig }
})
// ---- walking -------------------------------------------------------------
test("a worker walks around the desks, not through them", () => {
// from the airlock door to the far desk seat and to the sofa
const door = [0.6, 2.6]
for (const seat of [...DESKS.map((d) => d.seat), ...SOFA, ...ZONE_SPOTS]) {
const pts = NAV.path(door, [seat[0], seat[1]])
expect(pts.length).toBeGreaterThan(0)
const floorPath = pts.filter(p => p[2] === undefined)
const bad = walkable([[door[0], door[1]], ...floorPath])
expect(bad).toBe("")
// Boarding is tested separately: it approaches perpendicular to the seat.
expect(pts[pts.length - 1].slice(0, 2)).toEqual([seat[0], seat[1]])
}
// no way crosses a desk's body
const pts = NAV.path([0.6, 2.6], [11.4, 6.75])
for (const [x, y] of pts) for (const b of BLOCKERS) expect(inside([x, y], b)).toBe(false)
})
test("a way is no longer than it has to be, and a straight line stays straight", () => {
const direct = NAV.path([7.0, 8.6], [9.6, 8.6])
expect(direct.length).toBe(1)
const around = NAV.path([0.6, 2.6], [15.0, 11.0])
expect(around.length).toBeGreaterThan(1)
const len = (pts) => { let d = 0; let p = [0.6, 2.6]; for (const q of pts) { d += Math.hypot(q[0] - p[0], q[1] - p[1]); p = q } return d }
// no wild detour: the straight line is 18.6
expect(len(around)).toBeLessThan(18.6 * 1.9)
})
// ---- the page's model ---------------------------------------------------
const S = (phase, extra = {}) => ({ key: "claude|x", source: "files", agent: "claude", session: "x", phase, touched: 0, updated: 0, ...extra })
test("statuses come from the file's own phase", () => {
const now = 1_000_000
expect(M.state(S("tool", { touched: now - 5000 }), now)).toEqual({ status: "executing", sub: "" })
expect(M.state(S("approval", { touched: now - 600e3 }), now)).toEqual({ status: "waiting", sub: "approval" })
expect(M.state(S("ask"), now)).toEqual({ status: "waiting", sub: "ask" })
expect(M.state(S("done"), now)).toEqual({ status: "done", sub: "" })
expect(M.state(S("aborted"), now)).toEqual({ status: "done", sub: "stopped" })
expect(M.state(S("error"), now)).toEqual({ status: "error", sub: "" })
expect(M.state(S("thinking", { touched: now - 1000 }), now).status).toBe("thinking")
// the model writing its answer: the file moves and there is text
expect(M.state(S("thinking", { touched: now - 1000, reply: "So far…" }), now).status).toBe("replying")
// a turn whose file hasn't moved for ten minutes is stuck
expect(M.state(S("tool", { touched: now - 11 * 60e3 }), now)).toEqual({ status: "error", sub: "stuck" })
})
test("the office's agents, counts and resume commands", () => {
const now = 10_000_000
const list = M.agents(M.demo(now), now)
expect(list.length).toBe(7)
expect(M.counts(list)).toEqual({ needYou: 3, working: 2, done: 2 })
expect(list.find((a) => a.title === "站点部署")).toMatchObject({ status: "waiting", sub: "approval" })
expect(M.sorted(list)[0].status).toBe("waiting")
expect(M.agents(M.demo(now), now + 7 * 3600e3, { hideAfter: "6h" }).every((a) => a.status !== "done")).toBe(true)
expect(M.resume({ agent: "claude", session: "abc", cwd: "/a b" })).toBe("cd '/a b' && claude --resume abc")
expect(M.resume({ agent: "codex", session: "019a", cwd: "/x" })).toBe("cd /x && codex resume 019a")
expect(M.resume({ agent: "dsh", session: "session-1d68", cwd: "/x" })).toBe("cd /x && dsh --resume 1d68")
expect(M.resume({ agent: "unknown", session: "x" })).toBe("")
})
test("sample agents sit beside real ones, never instead of them", () => {
const now = 10_000_000
const real = { sessions: [S("tool", { key: "claude|real", title: "real work", touched: now - 1000 })] }
const both = M.merge(real, M.demo(now))
expect(both.sessions[0].key).toBe("claude|real")
expect(M.agents(both, now).some((a) => a.title === "real work")).toBe(true)
})
test("words, in English and Chinese", () => {
expect(M.text("zh-CN").title).toBe("我的 Agent 在干嘛")
expect(Object.keys(M.TEXT.zh).sort()).toEqual(Object.keys(M.TEXT.en).sort())
for (const e of read("index.html").matchAll(/data-t="([^"]+)"/g)) expect(M.TEXT.en[e[1]]).toBeDefined()
expect(M.ago(28 * 60e3, "zh")).toBe("28分钟前")
expect(M.duration(130e3, "en")).toBe("2m 10s")
})
test("the Settings chevron and Language, Scene and Quality pickers are SVG app menus, with no native <select>", () => {
const html = read("index.html")
const css = read("style.css")
const app = read("app.js")
expect(html).not.toContain("⌄")
expect(html).not.toContain("<select")
expect(html).toMatch(/id="settingsBtn"[^>]*><span data-t="settings">Settings<\/span><svg class="chev" viewBox="0 0 10 6"/)
for (const [lblId, btnId, listId] of [["lblLang", "optLang", "optLangList"], ["lblScene", "optScene", "optSceneList"], ["lblQuality", "optQuality", "optQualityList"]]) {
expect(html).toMatch(new RegExp(`<button[^>]*id="${btnId}"[^>]*aria-labelledby="${lblId} ${btnId}"[^>]*aria-haspopup="listbox"[^>]*aria-expanded="false"[^>]*aria-controls="${listId}"[^>]*>.*<svg class="chev" viewBox="0 0 10 6"`))
expect(html).toMatch(new RegExp(`<div[^>]*id="${listId}"[^>]*role="listbox"[^>]*aria-labelledby="${lblId}"`))
}
expect(css).toContain("--faint: var(--magpie-faint, #b9b9c2);")
expect(css).toMatch(/\[data-theme="dark"\][^{]*\{[^}]*--faint:\s*var\(--magpie-faint,\s*#56565f\);/)
expect(css).toMatch(/::-webkit-scrollbar\s*\{\s*width:\s*8px;\s*height:\s*8px;\s*background:\s*transparent;\s*\}/)
expect(css).toMatch(/::-webkit-scrollbar-thumb\s*\{\s*background:\s*var\(--faint\);\s*border:\s*2px solid transparent;\s*border-radius:\s*8px;\s*background-clip:\s*padding-box;\s*\}/)
expect(css).toMatch(/::-webkit-scrollbar-thumb:hover\s*\{\s*background-color:\s*var\(--muted\);\s*\}/)
expect(css).toMatch(/::-webkit-scrollbar-button,\s*::-webkit-scrollbar-corner\s*\{\s*display:\s*none;\s*background:\s*transparent;\s*\}/)
expect(css).not.toContain("scrollbar-width")
expect(css).toMatch(/\.picker-list\s*\{[^}]*overscroll-behavior:\s*contain;/)
expect(css).toMatch(/\.btn\s*\{[^}]*display:\s*inline-flex;[^}]*align-items:\s*center;[^}]*gap:\s*7px;/)
expect(css).toContain(".today-tokens:empty { display: none; }")
expect(css).toContain('#settingsBtn[aria-expanded="true"] .chev, .lang-select[aria-expanded="true"] .chev { transform: rotate(180deg); }')
expect(css).not.toContain("appearance: none")
expect(css).toMatch(/prefers-reduced-motion:\s*reduce[\s\S]*\.chev/)
expect(app).toContain('setAttribute("role", "option")')
expect(app).toContain('getAttribute("aria-controls")')
for (const btnId of ["optLang", "optScene", "optQuality"]) {
expect(app).toContain(`makePicker(\n "${btnId}"`)
}
})
// ---- the host part --------------------------------------------------------
test("the host part writes what it reads, and changes no request", async () => {
const out = tmp()
process.env.MAGPIE_AGENT_OFFICE_PAGE = out
const hook = await import("./office.hooks.mjs")
const h = tmp()
const cl = join(h, "projects", "-Users-me-app")
mkdirSync(cl, { recursive: true })
writeFileSync(join(cl, "c1.jsonl"), F.claudeRunning)
hook.start({ text: true, scanner: { sources: [{ agent: "claude", kind: "claude", root: join(h, "projects"), depth: 1, match: (n) => n.endsWith(".jsonl") }] } })
hook.stop()
const c = { origin: "local", protocol: "anthropic", agent: "cursor", session: "cu1", cwd: "/x", model: "m", query: "hi", options: {}, state: {}, report() {} }
expect(await hook.onRequest({ model: "m", tools: [{ name: "x" }], messages: [{ role: "user", content: "hi" }] }, c)).toBeUndefined()
// with no remote option set, a request from another computer is only counted
const far = { ...c, origin: "gateway-key", agent: "opencode", session: "far1", state: {} }
await hook.onRequest({ model: "m", tools: [{ name: "x" }], messages: [{ role: "user", content: "hi" }] }, far)
expect(far.state.office).toBeNull()
expect(hook.snapshot().sessions.some((s) => s.session === "far1")).toBe(false)
expect(hook.snapshot().dropped).toBeGreaterThanOrEqual(1)
hook.write()
const live = readFileSync(join(out, "live.js"), "utf8")
const snap = JSON.parse(live.slice("globalThis.AgentOfficeLive = ".length).replace(/;\s*$/, ""))
const claude = snap.sessions.find((s) => s.agent === "claude")
expect(claude).toMatchObject({ source: "files", phase: "tool", title: "Fix failing test" })
// an agent whose files aren't read shows from the gateway
expect(snap.sessions.find((s) => s.agent === "cursor")?.source).toBe("gateway")
expect(live).not.toMatch(/authorization|x-api-key/i)
})
test("the gateway part still reads replies in each API", () => {
expect(outcomeOf(replyOf({ role: "assistant", content: [{ type: "text", text: "Which one?" }] }, "anthropic"))).toBe("ask")
expect(outcomeOf(replyOf({ output: [{ type: "function_call", name: "shell", arguments: "{}" }] }, "responses"))).toBe("tools")
const t = createTracker()
expect(t.request({ model: "m", tools: [1], messages: [{ role: "user", content: "x" }] }, { origin: "remote", agent: "a", session: "s" }, 1)).toBeNull()
expect(t.snapshot(1).dropped).toBe(1)
const remoteOn = createTracker({ remote: true })
expect(remoteOn.request({ model: "m", tools: [1], messages: [{ role: "user", content: "x" }] }, { origin: "gateway-key", agent: "a", session: "s" }, 1)).toBe("a|s")
expect(remoteOn.snapshot(1).dropped).toBe(0)
})
test("a gateway install counts dropped non-local requests until remote is enabled", async () => {
const out = tmp()
process.env.MAGPIE_AGENT_OFFICE_PAGE = out
const hook = await import("./office.hooks.mjs")
const body = { model: "m", tools: [{ name: "x" }], messages: [{ role: "user", content: "hi" }] }
await hook.onRequest(body, { origin: "gateway-key", protocol: "anthropic", agent: "cursor", session: "r1", cwd: "/srv", model: "m", query: "hi", options: { remote: false, text: true }, state: {}, report() {} })
expect(hook.snapshot().dropped).toBeGreaterThanOrEqual(1)
// toggling text while remote stays false preserves the dropped count
await hook.onRequest(body, { origin: "local", protocol: "anthropic", agent: "cursor", session: "l1", cwd: "/srv", model: "m", query: "hi", options: { remote: false, text: false }, state: {}, report() {} })
expect(hook.snapshot().dropped).toBeGreaterThanOrEqual(1)
// enabling remote resets dropped to 0 and records the remote agent
await hook.onRequest(body, { origin: "gateway-key", protocol: "anthropic", agent: "cursor", session: "r1", cwd: "/srv", model: "m", query: "hi", options: { remote: true, text: true }, state: {}, report() {} })
expect(hook.snapshot().dropped).toBe(0)
expect(hook.snapshot().sessions.some((s) => s.session === "r1")).toBe(true)
expect(read("index.html")).toContain('id="optRemote"')
expect(read("app.js")).toContain("remote: view.remote")
expect(read("app.js")).toContain("state.snap.dropped > 0")
for (const lang of M.LANGS) {
expect(M.TEXT[lang].remote).toBeTruthy()
expect(M.TEXT[lang].emptyRemote).toBeTruthy()
}
})
test("live.js is no file of the package's", () => {
expect(pkg.files.some((f) => f.includes("live"))).toBe(false)
expect(readFileSync(join(dir, ".gitignore"), "utf8")).toContain("page/live.js")
})