Files
plugins/packages/pig/pig.test.mjs
T
yetoneandnetube 43ffb46018 agent: PiG (Pi in Go), with Magpie's endpoint and model catalog (#264)
New package @magpie-community/agent-pig 0.1.0, a connect-only Agent:
it manages the whole providers.magpie object in ~/.pig/agent/models.json
(name, baseUrl, apiKey, api openai-completions, models) and nothing else.
settings.json and auth.json are not touched; disconnect restores a prior
same-name provider or removes the provider whole. Models keep their ids,
names, image input, positive limits (output capped at the window), and
map Pi's seven thinking levels onto the model's efforts (off -> none,
unsupported -> null).

Re-review of head 7134b1038: both points of the 10-21 review are fixed.
The package README no longer names e2e scripts that aren't shipped, and
installs with `magpie plugin add @magpie-community/agent-pig`. Code is
unchanged from the reviewed 43fb5d927.

Run at this tree (PR squashed onto origin/main 397a890), sandbox HOME:
- bun test: 2093 pass, 0 fail.
- bun scripts/check.mjs: ok.
- Broken by hand, pig.test.mjs red each time: unsupported level as
  undefined (2 fail), no output cap (1), zero context written (1), off
  not mapped to none (1), hardcoded apiKey (1), ids rewritten (3).
- Read every line: no imports, no file or network access, no install
  hooks; the icon is a URL (pi-in-go.dev) as aider's is.
- npm has no @magpie-community/agent-pig yet, so 0.1.0 stays.

Not tried at this head: a real PiG binary or the magpie host. The 10-21
review ran both at 43fb5d927 (PiG v0.5.1 darwin-arm64 offline parse,
fake-upstream request, host connect/disconnect); the runtime file is
byte-identical since.

Co-authored-by: netube <61469467+netube99@users.noreply.github.com>
2026-10-22 02:24:08 +08:00

85 lines
4.0 KiB
JavaScript

import { expect, test } from "bun:test"
import { readFileSync } from "node:fs"
import { agent, connect } from "./pig.agent.js"
const gateway = { url: "http://127.0.0.1:3499", v1: "http://127.0.0.1:3499/v1", key: "magpie-pig" }
const model = { id: "relay/vendor/model:variant", name: "Example", context: 262144, output: 32768, images: true, efforts: ["none", "low", "high", "max"] }
const output = (models = [model]) => connect({ gateway, models, agent: { id: "pig", config: "/synthetic/.pig/agent/models.json" } })
const provider = (models = [model]) => output(models)["providers.magpie"]
test("registers PiG as a connect-only agent with no model or Library writes", () => {
expect(agent).toMatchObject({ id: "pig", name: "PiG", bin: "pig", config: "~/.pig/agent/models.json", format: "json" })
expect(agent.icon).toBe("https://pi-in-go.dev/pig-favicon.svg")
expect(agent.model).toBeUndefined()
expect(agent.library).toBeUndefined()
expect(agent.notice).toContain("/model")
})
test("declares the standard community Agent entry and no host plugin", () => {
const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8"))
expect(pkg.name).toBe("@magpie-community/agent-pig")
expect(pkg.magpie.agent).toBe("./pig.agent.js")
expect(pkg.magpie.icon).toBe(agent.icon)
expect(pkg.main).toBeUndefined()
expect(pkg.dependencies).toBeUndefined()
expect(pkg.scripts.preinstall).toBeUndefined()
expect(pkg.scripts.postinstall).toBeUndefined()
})
test("gives the host one complete provider restore unit without touching other providers or settings", () => {
const out = output()
expect(Object.keys(out)).toEqual(["providers.magpie"])
expect(out["providers.magpie"].baseUrl).toBe(gateway.v1)
expect(out["providers.magpie"].apiKey).toBe("magpie-pig")
expect(out["providers.magpie"].api).toBe("openai-completions")
})
test("preserves gateway model IDs including slashes and maps capability metadata", () => {
expect(provider().models).toEqual([{
id: model.id, name: "Example", input: ["text", "image"], reasoning: true,
contextWindow: 262144, maxTokens: 32768,
thinkingLevelMap: { off: "none", minimal: null, low: "low", medium: null, high: "high", xhigh: null, max: "max" },
}])
})
test("does not advertise thinking off for a model that always thinks", () => {
const [entry] = provider([{ ...model, efforts: ["high"] }]).models
expect(entry.thinkingLevelMap.off).toBeNull()
expect(entry.thinkingLevelMap.high).toBe("high")
expect(entry.thinkingLevelMap.medium).toBeNull()
})
test("leaves missing or nonpositive limits to PiG and handles text-only models", () => {
for (const limits of [{}, { context: 0, output: 0 }, { context: -1, output: -1 }]) {
expect(provider([{ id: "relay/plain", ...limits }]).models).toEqual([
{ id: "relay/plain", name: "relay/plain", input: ["text"], reasoning: false },
])
}
})
test("keeps a published output limit within its known context window", () => {
const [bounded] = provider([{ id: "relay/oversized-output", context: 8192, output: 32768 }]).models
expect(bounded.maxTokens).toBe(8192)
const [unknownWindow] = provider([{ id: "relay/unknown-window", output: 32768 }]).models
expect(unknownWindow.maxTokens).toBe(32768)
expect(unknownWindow.contextWindow).toBeUndefined()
})
test("replaces catalog contents on the next call, including an empty catalog", () => {
expect(provider([{ id: "new/only", efforts: [] }]).models.map(m => m.id)).toEqual(["new/only"])
expect(provider([]).models).toEqual([])
})
test("follows host gateway changes without caching an endpoint or key", () => {
const next = connect({ gateway: { v1: "http://127.0.0.1:3500/v1", key: "synthetic-next-key" }, models: [] })["providers.magpie"]
expect(next.baseUrl).toBe("http://127.0.0.1:3500/v1")
expect(next.apiKey).toBe("synthetic-next-key")
})
test("does not mutate host inputs", () => {
const input = { gateway: { ...gateway }, models: [{ ...model, efforts: [...model.efforts] }] }
const before = JSON.stringify(input)
connect(input)
expect(JSON.stringify(input)).toBe(before)
})