Files
plugins/packages/kimi-work/tests/body-abort.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

51 lines
2.9 KiB
JavaScript

import {test,expect} from 'bun:test';
import {_internal as I} from '../index.mjs';
const session={uid:'synthetic-account',workToken:'synthetic-work',webToken:'synthetic-web'};
const tick=()=>new Promise(resolve=>setImmediate(resolve));
for(const kind of ['stream','request'])test('aborting a buffered '+kind+' ends the read without sending the request',async()=>{
let calls=0,producer,cancelled;
const p=I.createPlugin({readSession:()=>session,fetchImpl:async()=>{calls++;return new Response('{}');}});
const loader=await p.auth.loader(async()=>({source:'kimi-work-desktop',uid:session.uid}));
const abort=new AbortController();
const body=new ReadableStream({start(c){producer=c;c.enqueue(new TextEncoder().encode('{"messages":['));},
cancel(reason){cancelled=reason;}});
const init={method:'POST',body,signal:abort.signal,duplex:'half'};
const pending=(kind==='request'?loader.fetch(new Request(I.BASE+'/chat/completions',init))
:loader.fetch(I.BASE+'/chat/completions',init)).then(value=>({value}),error=>({error}));
// Let the initial chunk be consumed, leaving the next read waiting.
await tick();await tick();
const reason=kind==='stream'?Error('synthetic custom cancellation'):new DOMException('synthetic cancellation','AbortError');
abort.abort(reason);
let timer;
const result=await Promise.race([pending,new Promise(resolve=>{timer=setTimeout(()=>resolve({timedOut:true}),250);})]);
clearTimeout(timer);
if(result.timedOut){producer.close();await pending;}
expect(result.timedOut).toBeUndefined();
expect(result.error).toBe(reason);
expect(calls).toBe(0);
// Request's stream wrapper may defer source cancellation across runtimes.
if(kind==='stream')expect(cancelled).toBe(reason);
});
test('a previously cancelled body retains the abort reason and is not sent',async()=>{
let calls=0;
const p=I.createPlugin({readSession:()=>session,fetchImpl:async()=>{calls++;return new Response('{}');}});
const loader=await p.auth.loader(async()=>({source:'kimi-work-desktop',uid:session.uid}));
const abort=new AbortController(),reason=Error('synthetic early cancellation');
abort.abort(reason);
await expect(loader.fetch(I.BASE+'/chat/completions',{method:'POST',body:'{"messages":[]}',signal:abort.signal})).rejects.toBe(reason);
expect(calls).toBe(0);
});
test('a buffered UTF-8 body keeps characters split between chunks',async()=>{
const original=JSON.stringify({messages:[{role:'user',content:'中文 🐦'}]});
const bytes=new TextEncoder().encode(original);let sent;
const body=new ReadableStream({start(c){for(const byte of bytes)c.enqueue(Uint8Array.of(byte));c.close();}});
const p=I.createPlugin({readSession:()=>session,fetchImpl:async(url,init)=>{sent=init.body;return new Response('{}');}});
const loader=await p.auth.loader(async()=>({source:'kimi-work-desktop',uid:session.uid}));
await loader.fetch(I.BASE+'/chat/completions',{method:'POST',body});
expect(sent).toBe(original);
});