Verification: npx --yes bun test packages/kimi-work (123 pass, 0 fail, 608 assertions) and npx --yes bun scripts/check.mjs kimi-work passed. The new plan/cache tests failed on the old implementation with 'I.planName is not a function' and 'p.auth.accountPlan is not a function'. Real NAS account, GUI and SSE verification are not yet run.
87 lines
4.4 KiB
JavaScript
87 lines
4.4 KiB
JavaScript
import {test,expect} from 'bun:test';
|
|
import {_internal as I} from '../index.mjs';
|
|
|
|
const auth=(uid,token='web-'+uid)=>({source:'kimi-work-desktop',uid,access:token,refresh:'refresh-'+uid,expires:0});
|
|
const reply=plan=>Response.json({subscription:{plan}});
|
|
|
|
test('every current and legacy Kimi Work plan maps to its Agent task concurrency',()=>{
|
|
const cases=[
|
|
[{name:'Free'},1],[{name:'Andante'},1],[{name:'Go'},1],
|
|
[{name:'Moderato'},2],[{name:'Plus'},2],
|
|
[{name:'Allegretto'},2],[{name:'Pro'},2],
|
|
[{name:'Allegro'},4],[{name:'Max'},4],[{name:'Ultra'},4],
|
|
[{level:'LEVEL_BASIC'},2],[{level:'LEVEL_INTERMEDIATE'},2],[{level:'LEVEL_ADVANCED'},4],
|
|
[{level:'LEVEL_ADVANCED',name:'Moderato'},2],
|
|
[{level:'LEVEL_FUTURE',name:'Unannounced'},1],[{},1],[null,1],
|
|
];
|
|
for(const [plan,want] of cases)expect(I.planConcurrency(plan)).toBe(want);
|
|
expect(I.planName({name:' Moderato '})).toBe('Moderato');
|
|
expect(I.planName({level:'LEVEL_BASIC'})).toBe('Basic');
|
|
expect(I.planName({level:'LEVEL_INTERMEDIATE'})).toBe('Intermediate');
|
|
expect(I.planName({level:'LEVEL_ADVANCED'})).toBe('Advanced');
|
|
expect(I.planName({level:'LEVEL_FUTURE'})).toBe('');
|
|
});
|
|
|
|
test('each account reads its own subscription and caches a successful result for five minutes',async()=>{
|
|
let clock=1000;const calls=[];
|
|
const p=I.createPlugin({now:()=>clock,fetchImpl:async(url,init)=>{
|
|
calls.push({url,token:new Headers(init.headers).get('authorization')});
|
|
const token=new Headers(init.headers).get('authorization');
|
|
return reply(token==='Bearer web-a'?{name:'Moderato'}:{name:'Allegro'});
|
|
}});
|
|
const [a,b,aPlan,bPlan]=await Promise.all([
|
|
p.auth.accountConcurrency(async()=>auth('a')),
|
|
p.auth.accountConcurrency(async()=>auth('b')),
|
|
p.auth.accountPlan(async()=>auth('a')),
|
|
p.auth.accountPlan(async()=>auth('b')),
|
|
]);
|
|
expect([a,b,aPlan,bPlan]).toEqual([2,4,'Moderato','Allegro']);
|
|
expect(calls).toHaveLength(2);
|
|
expect(calls.every(c=>c.url===I.MEMBER+'GetSubscription')).toBe(true);
|
|
clock+=4*60*1000;
|
|
expect(await p.auth.accountConcurrency(async()=>auth('a'))).toBe(2);
|
|
expect(calls).toHaveLength(2);
|
|
clock+=60*1000;
|
|
expect(await p.auth.accountConcurrency(async()=>auth('a'))).toBe(2);
|
|
expect(calls).toHaveLength(3);
|
|
});
|
|
|
|
test('concurrent reads for one account share one subscription request',async()=>{
|
|
let calls=0,release;const held=new Promise(resolve=>{release=resolve;});
|
|
const p=I.createPlugin({fetchImpl:async()=>{calls++;await held;return reply({name:'Plus'});}});
|
|
const reads=Promise.all(Array.from({length:8},()=>p.auth.accountConcurrency(async()=>auth('same'))));
|
|
await new Promise(resolve=>setTimeout(resolve,0));release();
|
|
expect(await reads).toEqual(Array(8).fill(2));expect(calls).toBe(1);
|
|
});
|
|
|
|
test('a failed refresh keeps the last known limit, and an unknown account stays at one',async()=>{
|
|
let clock=0,fail=false,calls=0;
|
|
const p=I.createPlugin({now:()=>clock,fetchImpl:async()=>{calls++;if(fail)return new Response('{}',{status:503});return reply({name:'Pro'});}});
|
|
const getAuth=async()=>auth('known');
|
|
expect(await p.auth.accountConcurrency(getAuth)).toBe(2);
|
|
expect(await p.auth.accountPlan(getAuth)).toBe('Pro');
|
|
clock+=5*60*1000;fail=true;
|
|
expect(await p.auth.accountConcurrency(getAuth)).toBe(2);
|
|
expect(await p.auth.accountPlan(getAuth)).toBe('Pro');
|
|
expect(await p.auth.accountConcurrency(getAuth)).toBe(2);
|
|
expect(calls).toBe(2,'a failure backs off for one minute');
|
|
expect(await p.auth.accountConcurrency(async()=>auth('unknown'))).toBe(1);
|
|
expect(await p.auth.accountPlan(async()=>auth('unknown'))).toBe('');
|
|
});
|
|
|
|
test('an away desktop account never borrows the current user web token',async()=>{
|
|
let calls=0;
|
|
const p=I.createPlugin({readSession:()=>({uid:'current',workToken:'work-current',webToken:'web-current',keys:{away:'work-away'}}),fetchImpl:async()=>{calls++;return reply({name:'Max'});}});
|
|
const getAuth=async()=>({source:'kimi-work-desktop',uid:'away'});
|
|
expect(await p.auth.accountConcurrency(getAuth)).toBe(1);
|
|
expect(await p.auth.accountPlan(getAuth)).toBe('');
|
|
expect(calls).toBe(0);
|
|
});
|
|
|
|
test('camelCase and snake_case subscription replies are both accepted',async()=>{
|
|
for(const body of [{subscription:{plan:{name:'Moderato'}}},{subscription:{plan:{level:'LEVEL_ADVANCED'}}}]) {
|
|
const p=I.createPlugin({fetchImpl:async()=>Response.json(body)});
|
|
expect(await p.auth.accountConcurrency(async()=>auth('shape'))).toBe(body.subscription.plan.name?2:4);
|
|
}
|
|
});
|