Files
plugins/packages/kimi-work/no-magpie.test.mjs
T
Changtian Li 280d522830 kimi-work 0.1.15: renew membership tokens and keep existing accounts (#218)
Explicit sign-in now imports Kimi desktop's membership access/refresh pair
when the desktop has one, and magpie renews it through the host's
auth.refresh (conditional commit under the auth-store lock). Accounts saved
through 0.1.14 (reference-only, no pair) keep reading the desktop's web
token for usage, with no reconnect, no import and no refresh spent. The
usage card is one monthly meter, with Code's share as text. Request-body
reads follow the caller's abort signal.

Kept from main (0.1.11 to 0.1.14): callback errors split into no session /
Work 401 / HTTP-timeout-network (#425), per-uid account ids
`Kimi Work · <uid6>` (#440), share-folder-missing as kept (#444),
spent-quota 403 -> 429 (#465).

Reviewed head 96f7f52501082e4747ef649d836d955a8ece3f4c, merged onto
origin/main fdc5002 locally (clean), run on mote (macOS, Bun 1.4.2) with
HOME=$(mktemp -d):
- bun test packages/kimi-work: 99 pass, 0 fail
- bun test (whole repo): 3681 pass, 8 skip, 0 fail
- bun scripts/check.mjs: all packages pass

Broken by hand, each red in packages/kimi-work:
- legacy (no pair) usage throws expired: 7 fail
- callback 401 folded into the generic error: 3 fail
- callback network error folded into "no session": 3 fail
- accountId back to 'Kimi Work (local)': 2 fail
- AuthService 401 marked signIn expired: 2 fail
- refresh hook returning a pair for a legacy account: 2 fail

Version 0.1.15: main has 0.1.14; npm has no kimi-work package (served
from dist, highest 0.1.14), so 0.1.15 is free.

Not tried: a live Kimi sign-in or renewal at this head. The author's
live coexistence evidence is one cycle on 2026-10-24 at f67d742
(COMPATIBILITY.md). The rewritten COMPATIBILITY.md replaces main's 0.1.4
macOS record; that record stays in git history at fdc5002.
2026-10-30 01:34:05 +08:00

59 lines
3.3 KiB
JavaScript

// magpie-community/plugins#353: every request to Kimi went out with the
// User-Agent "Magpie Kimi Work provider/<version>". Nothing sent names the
// app now: agent-gw gets the headers Kimi's own Work client (the daimon)
// sends, and the membership read the same User-Agent.
import {test,expect} from 'bun:test';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {_internal as I} from './index.mjs';
const account={type:'oauth',source:'kimi-work-desktop',uid:'fixture-account',access:'synthetic-web-token',refresh:'synthetic-web-refresh',expires:0};
const session={uid:'fixture-account',workToken:'synthetic-work-token',webToken:account.access,webRefresh:account.refresh,deviceId:'0f8fad5b-d9cb-469f-a165-70867728950e'};
function record() {
const sent=[];
const fetchImpl=async(url,init={})=>{
const headers=Object.fromEntries(new Headers(init.headers));
sent.push({url:String(url),headers,body:typeof init.body==='string'?init.body:''});
if(String(url).endsWith('/models'))return Response.json({data:[{id:'k3-agent',context_length:262144}]});
// a menu with models: an empty one is asked again without the sign-in
if(String(url).includes('DescribeKimiWorkConfig'))return new Response(fs.readFileSync(new URL('./tests/fixtures/work-config.mac.json',import.meta.url)),{headers:{'Content-Type':'application/json'}});
if(String(url).includes('GetSubscriptionStats'))return Response.json({subscriptionBalance:{amountUsedRatio:0.25}});
return new Response('data: [DONE]\n\n',{headers:{'Content-Type':'text/event-stream'}});
};
return {sent,fetchImpl};
}
test('no request to Kimi names magpie: models, the Work menu, chat and membership',async()=>{
const {sent,fetchImpl}=record();
const p=I.createPlugin({readSession:()=>session,fetchImpl});
const loader=await p.auth.loader(async()=>account);
await p.provider.models({models:{}},{auth:account});
await (await loader.fetch(I.BASE+'/chat/completions',{method:'POST',
headers:{'User-Agent':'magpie/1.0','x-magpie-work-dir':'/w'},
body:JSON.stringify({model:'k3-agent',stream:true,messages:[{role:'user',content:'hi'}]})})).text();
await p.auth.usage(async()=>account);
expect(sent.length).toBe(4);
for(const s of sent)expect(JSON.stringify(s)).not.toMatch(/magpie/i);
for(const s of sent.filter(s=>s.url.startsWith(I.BASE))){
expect(s.headers['user-agent']).toBe('kimi-code-cli/0.36.0');
expect(s.headers['x-msh-platform']).toBe('kimi-code-cli');
expect(s.headers['x-msh-version']).toBe('0.36.0');
expect(s.headers['x-msh-device-id']).toBe(session.deviceId);
}
});
test("the device id is the daimon's own, read and never written",()=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'kimi-work-device-'));
try{
const file=path.join(dir,'config.json');
expect(I.readDeviceId(file)).toBeUndefined();
expect(fs.existsSync(path.join(dir,'runtime'))).toBe(false);
expect(I.workHeaders(undefined)).not.toHaveProperty('X-Msh-Device-Id');
fs.mkdirSync(path.join(dir,'runtime','kimi-code','home'),{recursive:true});
fs.writeFileSync(path.join(dir,'runtime','kimi-code','home','device_id'),'0f8fad5b-d9cb-469f-a165-70867728950e\n');
expect(I.readDeviceId(file)).toBe('0f8fad5b-d9cb-469f-a165-70867728950e');
}finally{fs.rmSync(dir,{recursive:true,force:true});}
});