Files
我是一条没有名字的区 e7004d9354 inkstone 0.1.0: Intern InkStone (书生·端砚) Token Plan, browser sign-in, credit card and ink-* key chat (#775)
New package @magpie-community/opencode-inkstone-auth 0.1.0 for Intern
InkStone (书生·端砚, Shanghai AI Lab) Token Plan, provider id
`intern-inkstone`. Community package by yunwang2442-creator.

What it does:
- Browser sign-in (Edge/Chrome, its own profile under
  $XDG_CONFIG_HOME or ~/.config/magpie/inkstone-browser, or
  %LOCALAPPDATA%\magpie\inkstone-browser). The login window has no
  debugging port. A headless CDP read takes the uaa-token cookie, and the
  same read renews it silently later.
- Usage card: ink-credit balance, 5-hour and 7-day windows, credit packs.
- Chat: a pasted ink-* API key, or one the sign-in creates, named
  ink-<host4>-<acct4>-<rand4> (18 of the console's 20 characters). An
  idempotency clash (-15109) retries with a fresh name, up to three times.
  Pruning is on by default. It only touches this machine's and account's
  prefix, keeps the newest two and the key this sign-in created, and never
  deletes manual keys or keys of unknown age.
- Closing on Unix: SIGTERM, then SIGKILL only after a 15s grace. Leftover
  browsers are matched by --user-data-dir=<escaped profile>, and Windows
  uses .Contains().
- The login wait comes from an injected waitForLogin in tests. No env
  hook is left in production code.
- Hosts: discovery.intern-ai.org.cn, discovery-api.intern-ai.org.cn,
  127.0.0.1 (CDP).

Outside the package: scripts/opencode2.mjs lets a prompt with
`required: false` stay optional (`p.required !== false`). Every package's
copy is synced byte for byte, so all 39 copies in the merged tree are blob
afe074cb. No other package sets `required: false` on a prompt, so this
changes nothing for them. Also a root README row and a registry entry.

Review at 36412a15, on mote, HOME=$(mktemp -d), Bun 1.4.2:
- bun test packages/inkstone: 68 pass, 0 fail in Asia/Shanghai, UTC,
  America/Los_Angeles and Pacific/Kiritimati.
- bun scripts/check.mjs inkstone and bun scripts/check.mjs: ok.
- bun test (repo), on 36412a15 merged with origin/main e7cb9dc5:
  6743 pass, 20 skip, 0 fail. main has since moved by cline 0.1.14 and
  zcode 0.1.55 only, and none of the adapter copies changed.
- Broken by hand, each red: no protectName protection; prune_keys default
  off; no post-sign-in prune; no SIGTERM on close; the -15109 retry keeping
  its name.

Not tried: a real InkStone account sign-in on macOS, Linux or Windows (no
account here); a live chat request; magpie end to end with the plugin.
2026-11-07 15:07:06 +08:00

127 lines
5.3 KiB
JavaScript

// The card's small helpers in isolation: numbers and timestamps from the
// console are loosely typed, so every shape has to degrade to "unknown" rather
// than to a fake zero.
import "./nonet.mjs"
import { expect, test } from "bun:test"
import { cardOf, compact, flexNum, isoOf, localDay, optionalNum, packageRows, windowsOf } from "./lib/quota.mjs"
test("flexNum reads loose numbers and turns junk into 0", () => {
expect(flexNum(600000)).toBe(600000)
expect(flexNum("600000")).toBe(600000)
expect(flexNum("591300.25")).toBe(591300.25)
expect(flexNum(null)).toBe(0)
expect(flexNum("")).toBe(0)
expect(flexNum("abc")).toBe(0)
expect(flexNum(-5)).toBe(0)
expect(flexNum(Infinity)).toBe(0)
})
test("optionalNum keeps 'missing' distinct from 0", () => {
expect(optionalNum(0)).toBe(0)
expect(optionalNum("0")).toBe(0)
expect(optionalNum(null)).toBeUndefined()
expect(optionalNum(undefined)).toBeUndefined()
expect(optionalNum("")).toBeUndefined()
expect(optionalNum("abc")).toBeUndefined()
})
test("compact keeps at most two decimals and no thousands separators", () => {
expect(compact(1000)).toBe("1000")
expect(compact(9831.7)).toBe("9831.7")
expect(compact(0.333)).toBe("0.33")
expect(compact(0)).toBe("0")
})
test("isoOf accepts ISO strings and second/millisecond stamps, and rejects nonsense", () => {
expect(isoOf("2026-11-05T08:00:00Z")).toBe("2026-11-05T08:00:00.000Z")
// a local wall-clock time: compare against the same parse, not a fixed UTC
// string, so the test does not depend on the runner's timezone
expect(isoOf("2026-11-05 08:00:00")).toBe(new Date("2026-11-05T08:00:00").toISOString())
// 0 is not "1970" — it is a missing timestamp, as a number or a string
expect(isoOf(0)).toBeUndefined()
expect(isoOf("0")).toBeUndefined()
// second- and millisecond-scale stamps arrive as strings too
expect(isoOf("1762320000")).toBe(new Date(1762320000 * 1000).toISOString())
expect(isoOf("1762320000000")).toBe(new Date(1762320000000).toISOString())
expect(isoOf(null)).toBeUndefined()
expect(isoOf("")).toBeUndefined()
expect(isoOf("not a date")).toBeUndefined()
})
test("localDay reads a local calendar day and ignores junk", () => {
// build the expected day from the same local-time parse, so the assertion
// holds in every timezone (a fixed UTC string fails west of UTC)
const stamp = "2026-11-28T00:00:00.000Z"
const d = new Date(stamp)
const expected = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`
expect(localDay(stamp)).toBe(expected)
expect(localDay("not a date")).toBeUndefined()
})
test("windowsOf skips disabled, limitless and unmeasurable windows", () => {
const out = windowsOf({
usage_windows: {
"5h": { enabled: false, limit_credits: 10, remaining_credits: 1 },
"7d": { enabled: true, limit_credits: 0, remaining_credits: 0 },
},
})
expect(out).toEqual([])
})
test("windowsOf clamps an over-spent window to 100% and prefers used_credits", () => {
const [five] = windowsOf({
usage_windows: {
"5h": { enabled: true, limit_credits: 10, used_credits: 25, remaining_credits: 0, next_recover_at: 0 },
},
})
expect(five.used).toBe(100)
expect(five.display).toBe("25 / 10")
expect(five.resetsAt).toBeUndefined()
})
test("packageRows tolerates every envelope and filters empty or dead packs", () => {
const packs = [
{ remark: "每月免费基础包", remaining_credits: 6.5, total_credits: 10, status: "available" },
{ remark_en: "Top-up", remaining_credits: 3, total_credits: 3, status: "unused" },
{ remark: "empty", remaining_credits: 0, total_credits: 10, status: "available" },
{ remark: "expired", remaining_credits: 5, total_credits: 5, status: "expired" },
]
const rows = packageRows({ packages: packs })
expect(rows.map((r) => r.name)).toEqual(["每月免费基础包", "Top-up"])
// amount is the *used* share: 10 - 3.5, used is the percentage, limit is the total
expect(rows[0]).toEqual({
name: "每月免费基础包",
used: 35,
display: "剩 6.5 / 10",
amount: 3.5,
limit: 10,
unit: "墨点",
aside: true,
})
expect(packageRows({ items: packs }).length).toBe(2)
expect(packageRows(packs).length).toBe(2)
expect(packageRows(null)).toEqual([])
// a pack with no name still gets a readable label
expect(packageRows([{ remaining_credits: 1, total_credits: 2 }])[0].name).toBe("墨点包1")
// a pack with no total shows only the remainder, never "剩 1 / 0"
const noTotal = packageRows([{ remaining_credits: 1, status: "available" }])[0]
expect(noTotal.display).toBe("剩 1")
expect(noTotal.limit).toBeUndefined()
expect(noTotal.amount).toBeUndefined()
// a pack with no expiry loses runsOutAt and never mutates resetsAt
expect(packageRows([{ remaining_credits: 1, total_credits: 1 }])[0].resetsAt).toBeUndefined()
})
test("cardOf with nothing at all still produces a card, not a crash", () => {
const card = cardOf()
expect(card.plan).toBe("墨点计划 (Token Plan)")
expect(card.windows).toEqual([])
expect(card.balance).toBeUndefined()
expect(card.signIn).toBe("kept")
})
test("cardOf shows a pack count even when the balance number is missing", () => {
const card = cardOf({ balance: { active_package_count: 2 } })
expect(card.balance).toBe("2 个可用包")
})