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('')
for (const m of html.matchAll(/