Files
plugins/packages/orcaterm/chunks.test.mjs
T
狂気のMad Scientist鳳凰院胸針 fdc5002319 orcaterm 0.1.0: Tencent Cloud OrcaTerm's assistant as a provider, with WeChat QR sign-in (#460)
New package @magpie-community/opencode-orcaterm-auth 0.1.0: Tencent Cloud
OrcaTerm's own AI assistant (its monthly question allowance) as provider
`orcaterm`, with WeChat QR sign-in (the same sign-in OrcaTerm's clients
use) or a pasted console cookie. Credentials go only to Tencent's hosts
(orcaterm.com, *.cloud.tencent.com) and WeChat's widget (open.weixin.qq.com,
res.wx.qq.com). The nine models OrcaTerm's picker offers are listed;
others resolve only as aliases. Per-account pacing at 10 chats/min,
maxConcurrency 1; a JSON or SSE refusal inside a 200 becomes 429/401/502.
registry.json gains its entry.

Author: hg1048596-pixel (#460, replaces #422)

The last review's three points are fixed at this head: the index.mjs
header, toUpstreamBody and plugin-entry comments describe the code; the
README's License links the package's own LICENSE; /cb accepts a bridge
error without a code and shows it, and a later scan still finishes.

Run at a75a2ae0fb554b3cb6ae879d5571b5dd7b2300a7 on mote (macOS, Bun 1.4.2),
HOME=$(mktemp -d) as CI:
- bun test packages/orcaterm: 88 pass, 0 fail
- bun test (whole repo): 3624 pass, 8 skip, 0 fail
- bun scripts/check.mjs: every package ok, orcaterm included
- npm pack: 5 files (index.mjs, wechat.mjs, README.md, LICENSE,
  package.json); the extracted index.mjs imports with bun and its plugin
  returns provider orcaterm; node scripts/dist.mjs packs
  opencode-orcaterm-auth@0.1.0
- The earlier review's fake-upstream script against the packed package:
  27 of 27 (JSON 429 with and without rate-limit words, SSE quota, rate
  and sign-in errors, envelopes in stream and JSON mode, per-account
  pacing, bearer headers, nine listed models, credentials only to
  lightai.cloud.tencent.com)
- The /cb script against the packed wechat.mjs: foreign, missing and
  rebinding origins 403, wrong state refused, right origin and state
  finish the sign-in
Broken by hand: requiring a string code again turns "callback preserves
bridge errors without a code" red (87 pass, 1 fail).
Not tried: a real OrcaTerm account, so neither the QR sign-in nor the real
upstream.
2026-10-30 00:44:34 +08:00

122 lines
4.5 KiB
JavaScript

// Frames that look cumulative but aren't, a frame split across network
// chunks, and a connection that dies or is cancelled: the three cases that
// decide whether the client sees the answer it was given, and whether a
// failure is reported as one.
import { expect, test } from "bun:test"
import { _internal } from "./index.mjs"
// The suite fires far more than the account's ten-a-minute budget; lift the
// pacer so these tests measure the plugin, not the limiter.
process.env.ORCATERM_NO_PACE = "1"
const frame = (o) => `data: ${JSON.stringify(o)}\n\n`
const collect = async (res) => {
const text = await res.text()
const out = []
for (const line of text.split("\n")) {
const t = line.trim()
if (!t.startsWith("data:")) continue
const p = t.slice(5).trim()
if (p === "[DONE]") continue
const d = JSON.parse(p).choices?.[0]?.delta
if (d?.content) out.push(d.content)
}
return out.join("")
}
const bodyOf = (frames, close = true) =>
new ReadableStream({
start(c) {
for (const f of frames) c.enqueue(new TextEncoder().encode(f))
if (close) c.close()
},
})
// The old "cumulative content" check cut real per-frame text whenever a frame
// happened to start with everything sent so far: "ha" then "haha" streamed as
// "haha" instead of "hahaha". `content` is each frame's addition, so it must
// be passed on untouched.
test("a frame that starts with what was already sent is still an addition", async () => {
const res = _internal.upstreamToOpenAI(bodyOf([frame({ content: "ha" }), frame({ content: "haha" })]), { model: "m" })
expect(await collect(res)).toBe("hahaha")
})
test("repeated and overlapping text is kept, not trimmed", async () => {
const res = _internal.upstreamToOpenAI(
bodyOf([frame({ content: "ab" }), frame({ content: "ab" }), frame({ content: "abc" })]),
{ model: "m" },
)
expect(await collect(res)).toBe("abababc")
})
// fullContent is the whole answer so far: its difference is the addition.
test("fullContent still becomes a delta", async () => {
const res = _internal.upstreamToOpenAI(
bodyOf([frame({ fullContent: "你" }), frame({ fullContent: "你好" }), frame({ fullContent: "你好,世界" })]),
{ model: "m" },
)
expect(await collect(res)).toBe("你好,世界")
})
// A fullContent that rewrites earlier text has no meaningful difference; the
// whole new text is sent rather than a slice from the wrong offset, which
// used to produce "abcw" from "abc" then "xyzw".
test("a fullContent that rewrites earlier text is sent whole", async () => {
const res = _internal.upstreamToOpenAI(bodyOf([frame({ fullContent: "abc" }), frame({ fullContent: "xyzw" })]), {
model: "m",
})
expect(await collect(res)).toBe("abcxyzw")
})
// A frame can arrive in pieces; the split may fall inside a multi-byte
// character. Neither may lose or mangle text.
test("a frame split across chunks keeps every character", async () => {
const whole = frame({ content: "你好,世界" })
const cut = Math.floor(whole.length / 2)
const body = new ReadableStream({
start(c) {
const enc = new TextEncoder()
c.enqueue(enc.encode(whole.slice(0, cut)))
c.enqueue(enc.encode(whole.slice(cut)))
c.close()
},
})
const res = _internal.upstreamToOpenAI(body, { model: "m" })
expect(await collect(res)).toBe("你好,世界")
})
// A connection that dies mid-stream must error the stream. As an ordinary
// end it would read as a complete answer, and magpie would not fail over.
test("a reset mid-stream errors the stream", async () => {
const body = new ReadableStream({
start(c) {
c.enqueue(new TextEncoder().encode(frame({ content: "par" })))
c.error(new Error("socket hang up"))
},
})
await expect(_internal.upstreamToOpenAI(body, { model: "m" }).text()).rejects.toThrow(/socket hang up/)
})
// Cancelling must reach the upstream reader: the connection is not left
// open when the agent gives up. Removing releaseUpstream() turns this red.
test("cancelling the answer cancels the upstream", async () => {
let cancelled = false
const body = new ReadableStream({
pull(c) {
c.enqueue(new TextEncoder().encode(frame({ content: "a" })))
},
cancel() {
cancelled = true
},
})
const res = _internal.upstreamToOpenAI(body, { model: "m" })
const reader = res.body.getReader()
await reader.read()
await reader.cancel()
// the cancel propagates asynchronously
for (let i = 0; i < 20 && !cancelled; i++) await new Promise((r) => setTimeout(r, 10))
expect(cancelled).toBe(true)
})