Files
plugins/packages/tabbit/tests/checkin.test.mjs
T
hclhcl 7ea611975a tabbit: Tabbit (web.tabbit.com) as a provider, with live catalog, check-in and cycle usage (#673)
Adds @magpie-community/opencode-tabbit-auth 0.1.0 (packages/tabbit) and its
registry.json entry. Tabbit (web.tabbit.com) becomes a provider:
- A live model catalog.
- Turns run as room -> run -> SSE join. Each turn has its own stream
  timeout (TABBIT_STREAM_TIMEOUT_MS, default 300 s), and the 30 s request
  timeout no longer covers join.
- A run is stopped through /proxy/v0/chat/stop/ when the client aborts or
  the stream times out.
- A cut turn ends as an error event (streaming) or a 502, never as a
  "stop" finish, and its room is not reused.
- Rooms are keyed by account and session, with a 30 min TTL and a cap of 256.
- 400/401/403/404/429 pass through with retry-after.
- The daily desktop_pet check-in is free.
- Cycle usage.
- macOS desktop sign-in reads the Tabbit Browser cookie store from a temp
  copy that is removed in a finally. Windows and Linux paste the token.

Run at 5be6e90 on mote (macOS):
- bun test: 58 pass. node --test tests/*.test.mjs: 58 pass.
  bun scripts/check.mjs: ok.
- Broken by hand, and each change turned its test red:
  - no rmSync of the cookie copy;
  - 429 not passed through;
  - room keyed by session only;
  - stop sent on an already-aborted signal;
  - no abort listener on the request;
  - streaming truncation ignored;
  - the old truncation rule;
  - totalTimeout back on join;
  - no wake of a quiet reader;
  - onTimeout not called;
  - no room pruning.
- Probes against a local SSE server:
  - A 35 s turn gives 200 under Bun (streaming and non-streaming) and under
    Node.
  - A silent stream with TABBIT_STREAM_TIMEOUT_MS=3000 ends at 3 s under
    Bun and Node (an error event, or a 502 non-streaming), and the server
    sees the stop at 3 s.
- tabbit-* temp dirs in TMPDIR: the same count before and after the bun and
  node runs.

Not tried: a real Tabbit account.
2026-11-03 21:39:56 +08:00

127 lines
4.6 KiB
JavaScript

// The daily sign-in: what Tabbit's own answers come to.
import test from "node:test"
import assert from "node:assert/strict"
import { checkinOf, _internal } from "../src/checkin.mjs"
const creds = { header: "token=t; user_id=u", token: "t", uid: "u" }
// stub answers the status and the sign-in with what the test wants.
function stub({ status, claim } = {}) {
const calls = []
const original = globalThis.fetch
globalThis.fetch = async (input, init = {}) => {
const url = new URL(typeof input === "string" ? input : input.url)
calls.push({ path: url.pathname, query: url.search, method: init.method ?? "GET", body: init.body })
if (url.pathname.endsWith("/sign-in/status")) return Response.json(status ?? {})
if (url.pathname.endsWith("/sign-in")) return Response.json(claim ?? {})
return new Response("nope", { status: 404 })
}
return { calls, restore: () => (globalThis.fetch = original) }
}
const open = (extra) => ({
sign_in_date: "2026-11-03",
results: [{ scene_code: "desktop_pet", activity_open: true, signed_today: false, signed_days: 0, total_signed_days: 0, required_days: 3, usage_reward_result: null, pet_entitlement_result: "not_eligible", ...extra }],
})
test("a day already signed is 'done', and nothing is claimed", async () => {
const { calls, restore } = stub({ status: open({ signed_today: true, signed_days: 1 }) })
try {
const out = await checkinOf(creds)
assert.equal(out.outcome, "done")
assert.equal(out.streak, 1)
assert.equal(calls.filter((c) => c.method === "POST").length, 0)
} finally {
restore()
}
})
test("a fresh day is claimed, with the request number Tabbit wants", async () => {
const { calls, restore } = stub({
status: open(),
claim: { sign_in_date: "2026-11-03", results: [{ scene_code: "desktop_pet", activity_open: true, sign_in_result: "signed", signed_days: 1, required_days: 3, usage_reward_result: { amount: 5 } }] },
})
try {
const out = await checkinOf(creds)
assert.equal(out.outcome, "claimed")
assert.equal(out.credit, 5)
assert.equal(out.streak, 1)
const post = calls.find((c) => c.method === "POST")
const body = JSON.parse(post.body)
assert.deepEqual(body.scene_codes, ["desktop_pet"])
assert.match(body.request_no, /^[0-9a-f-]{36}$/)
} finally {
restore()
}
})
test("a race that lands on 'already_signed' is 'done', not a failure", async () => {
const { restore } = stub({
status: open(),
claim: { results: [{ scene_code: "desktop_pet", activity_open: true, sign_in_result: "already_signed", signed_days: 1 }] },
})
try {
assert.equal((await checkinOf(creds)).outcome, "done")
} finally {
restore()
}
})
test("a closed activity is 'inactive'", async () => {
const { restore } = stub({ status: { results: [{ scene_code: "desktop_pet", activity_open: false, signed_today: false }] } })
try {
assert.equal((await checkinOf(creds)).outcome, "inactive")
} finally {
restore()
}
})
test("no result for the scene is 'ineligible'", async () => {
const { restore } = stub({ status: { sign_in_date: "2026-11-03", results: [] } })
try {
assert.equal((await checkinOf(creds)).outcome, "ineligible")
} finally {
restore()
}
})
test("a word this file hasn't seen is reported, not guessed as a success", async () => {
const { restore } = stub({ status: open(), claim: { results: [{ scene_code: "desktop_pet", sign_in_result: "throttled" }] } })
try {
const out = await checkinOf(creds)
assert.equal(out.outcome, "failed")
assert.match(out.message, /throttled/)
} finally {
restore()
}
})
test("a refused sign-in throws with magpie's expired mark", async () => {
const original = globalThis.fetch
globalThis.fetch = async () => new Response(JSON.stringify({ detail: "invalid bearer token" }), { status: 401 })
try {
await assert.rejects(() => checkinOf(creds), (error) => error.signIn === "expired")
} finally {
globalThis.fetch = original
}
})
test("the status call asks for the scene the service takes", async () => {
const { calls, restore } = stub({ status: open({ signed_today: true }) })
try {
await checkinOf(creds)
assert.match(calls[0].query, /scene_codes=desktop_pet/)
} finally {
restore()
}
})
test("a reward's number is read out of whatever shape it arrives in", () => {
assert.equal(_internal.rewardOf({ usage_reward_result: { amount: 3 } }), 3)
assert.equal(_internal.rewardOf({ usage_reward_result: { credit: "7" } }), 7)
assert.equal(_internal.rewardOf({ usage_reward_result: 12 }), 12)
assert.equal(_internal.rewardOf({ usage_reward_result: null }), undefined)
assert.equal(_internal.rewardOf({}), undefined)
})