Files
plugins/packages/mclaw/models.test.mjs
T
Joey Shao 367e879cab mclaw 0.5.2: China Mobile Cloud's MClaw agent (ai.yun.139.com), with a browser sign-in (#746)
New package @magpie-community/opencode-mclaw-auth 0.5.2 (joey-41): China
Mobile Cloud's MClaw agent (ai.yun.139.com) as a provider. A browser
sign-in in a dedicated Chrome profile per sign-in harvests the yun.139.com
ticket and fetches the llm key and the model multipliers by itself; the
models come from the account's own lists; the usage card reads the
aitokens pool and, through the vip SSO chain (toSourceId 001005), the
records' own endTime as the reset; the ticket is renewed headless from the
account's own profile, and a ticket for another phone number is refused.
Every host is China Mobile's own. registry.json and the README table list
it.

Reviewed at 905e2a6b, 7ed7d0aa and 7304ac15 (0.5.2, edcd9549 plus a
merge of main). The last review's two must-fixes are in the code:
magpie's cancel (callback(undefined, { signal }) and authorize's signal
both call harvesting.abort(); startBrowserHarvest records an abort while
Chrome is still starting and closes it once open), and a failed,
cancelled or timed-out sign-in deletes its signin-* profile dir after
releasing the lock. Re-signing an account leaving its old dir behind is
in the package README as a known limit.

Run at 7304ac15 (origin/main 10a61c2e already in it), sandbox HOME:
- mote, Bun 1.4.2: bun test in packages/mclaw 30 pass, 0 fail;
  bun scripts/check.mjs mclaw and bun scripts/check.mjs (all) ok.
- WSL Ubuntu 24.04, Bun 1.4.2: whole-repo bun test (see the PR).
- The real startBrowserHarvest with Chrome for Testing on mote, aborted
  before Chrome was up and 4s after: both reject "登录已取消", no Chrome
  left on the profile, no signin-* dir left.
- Review test: authorize and callback given separate signals, as
  magpie's host does, only the callback's aborted: closed and released.
  With callback's listener taken out it goes red (5s timeout).
Not tried: a real MClaw sign-in (no account here). The package's own
magpie-style test hands both calls one signal, so it stays green
without callback's listener; that is the only gap seen.
npm: the name is new, so trusted publishing can't create it; magpie
serves it from the dist branch until an owner publishes its first
version.
2026-11-06 10:35:49 +08:00

58 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 账号模型目录:agent/list × apikey/get v1 的合并(倍率、名字)、票过期向外抛、
// 无凭证时静态目录原样返回。全部走本地 fake。
import './nonet.mjs'
import { beforeEach, afterEach, expect, test } from 'bun:test'
import { MclawAuthPlugin, _internal } from './index.mjs'
import { fakeUpstream, json, TICKET_A, TICKET_B } from './fake.mjs'
let f
afterEach(() => f?.close())
beforeEach(() => _internal.resetModelCache())
const authOf = (ticket) => ({
type: 'oauth', refresh: 'browser', access: ticket, expires: Date.now() + 86400e3,
metadata: { apiKey: 'na-test-key', email: '131****9239' },
})
test('agent/list × apikey/get 合并出账号目录与倍率', async () => {
f = fakeUpstream()
const p = await MclawAuthPlugin({}, {})
const models = await p.provider.models({ models: {} }, { auth: authOf(TICKET_A) })
expect(Object.keys(models).sort()).toEqual(['blian_qwen38_flash', 'huoshan_glm_5_3'])
expect(models.blian_qwen38_flash.rate).toBe(0.18)
expect(models.huoshan_glm_5_3.rate).toBe(3.5)
expect(models.blian_qwen38_flash.api.url).toContain('/api/llm/102001/v1')
expect(models.blian_qwen38_flash.tool_call).toBe(true)
expect(models.huoshan_glm_5_3.reasoning).toBe(true)
})
test('票过期(01000002)向外抛 expired', async () => {
f = fakeUpstream()
f.route('POST /api/adapter/assistant/agent/list', () => json({ success: false, code: '01000002', message: '无效的凭证' }))
const p = await MclawAuthPlugin({}, {})
let caught
try {
await p.provider.models({ models: {} }, { auth: authOf(TICKET_B) })
} catch (error) {
caught = error
}
expect(caught?.signIn).toBe('expired')
})
test('倍率接口挂了、config/get 兜底也不可用 → 仍有目录(无倍率)', async () => {
f = fakeUpstream()
f.route('POST /api/llm/apikey/get', () => new Response('boom', { status: 500 }))
f.route('POST /api/openclaw/config/get', () => new Response('boom', { status: 500 }))
const p = await MclawAuthPlugin({}, {})
const models = await p.provider.models({ models: {} }, { auth: authOf(TICKET_B) })
expect(models.blian_qwen38_flash).toBeTruthy()
expect(models.blian_qwen38_flash.rate).toBeUndefined()
})
test('无凭证 → 静态目录原样返回,不发请求', async () => {
const p = await MclawAuthPlugin({}, {})
const staticModels = { static_model: { name: 'S' } }
const models = await p.provider.models({ models: staticModels }, { auth: undefined })
expect(models).toBe(staticModels)
})