#444 (kongbai01203): when the folder daimon-storage.json points to was moved or deleted, 0.1.12 threw its "sign in again" error with signIn "expired", so magpie showed "登录已失效" and marked the account, and signing in again in a running Kimi didn't help. Kimi 3.2.21 makes <shareDir>/daimon only when it provisions at start, and never falls back to the default folder while a pointer stands (read from its main process), so the plugin doesn't fall back either. A missing share folder is now its own error, kimi_work_share_missing, with signIn "kept": it names the folder and says to put it back (no new sign-in) or to quit and reopen Kimi, then sign in to Work. The sign-in callback shows the same text. A share folder that exists but has no config.json is still the old missing-sign-in error. The plugin's fetch already fails before any network call; the ~30s 502 the reporter saw is outside the plugin and was not looked into here. #440 (kongbai01203): magpie already keeps a second Kimi user beside the first (settle tells them apart by uid), but the plugin served only the user Kimi is signed in to, so the first account failed once Kimi switched. Kimi 3.2.21 keeps one minted Work key per user in <daimon>/kimi-code-key.json ({"v":2,"keys":[{userId,apiKey,keyId?}]}, the current user first), keeps an entry when it switches users, reuses it when it switches back, and drops one only on a 401 (DaimonProvision's #readKeyStore/#upsertKeyStoreEntry/#resolvePersistedKimiCodeApiKey). An account Kimi has switched away from now uses its key from that file, read as Kimi reads it (v 2 only, first entry per user). Its allowance can't be read (Kimi keeps only the current user's web sign-in), so its usage card says to switch to it, signIn "kept". No key, or a key the gateway refuses, is kimi_work_account_not_here, marked signed out. Each account is named "Kimi Work · <first 6 of its hashed uid>" instead of the shared "Kimi Work (local)". Not done: rotating between accounts or falling back on quota, which is magpie's routing, not the plugin's. Run: bun test in packages/kimi-work (65 pass) and the whole repo with HOME and TMPDIR in a sandbox under magpie-wt (2879 pass, 3 skip, 3 fail: cursor upstream-words, commandcode no-magpie and layerfs no-magpie, which fail because the sandbox path itself contains "magpie"; none of those packages is touched). bun scripts/check.mjs ok. Broken by hand: index.mjs at origin/main fails the new share-folder, reporter's D:\KimiData\daimon-share and two-users tests. In the new code, dropping the share check reds the two #444 tests; ignoring the key store reds both #440 tests; taking the last entry per user reds the key-store test; reading usage for an away account reds the two-users test. Not tried: a real Kimi with two users (no Kimi install or Kimi account on this machine); that a minted key still works server-side after Kimi switches users is read from Kimi's code (it reuses it after a probe), not seen. Not tried on Windows.
79 lines
5.3 KiB
JavaScript
79 lines
5.3 KiB
JavaScript
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';
|
|
|
|
// Where Kimi's Work sign-in is looked for on each system, the way Kimi
|
|
// 3.2.21's main process finds daimon-share: <userData>/daimon-storage.json's
|
|
// shareDir, else <userData>/daimon-share (#425).
|
|
const files=m=>file=>{if(!(file in m))throw Object.assign(Error('ENOENT'),{code:'ENOENT'});return m[file];};
|
|
|
|
test('each system\'s default, with no pointer',()=>{
|
|
const none=files({});
|
|
expect(I.desktopFile({platform:'win32',home:'C:\\Users\\u',appdata:'C:\\Users\\u\\AppData\\Roaming',read:none}))
|
|
.toBe('C:\\Users\\u\\AppData\\Roaming\\kimi-desktop\\daimon-share\\daimon\\config.json');
|
|
expect(I.desktopFile({platform:'win32',home:'C:\\Users\\u',appdata:'',read:none}))
|
|
.toBe('C:\\Users\\u\\AppData\\Roaming\\kimi-desktop\\daimon-share\\daimon\\config.json');
|
|
expect(I.desktopFile({platform:'darwin',home:'/Users/u',read:none}))
|
|
.toBe('/Users/u/Library/Application Support/kimi-desktop/daimon-share/daimon/config.json');
|
|
expect(I.desktopFile({platform:'linux',home:'/home/u',read:none}))
|
|
.toBe('/home/u/.config/kimi-desktop/daimon-share/daimon/config.json');
|
|
});
|
|
|
|
test('Windows: Kimi installed on another drive keeps daimon-share where its pointer says',()=>{
|
|
// What Kimi 3.2.21 writes for a new user who installed it on D:.
|
|
const read=files({'C:\\Users\\u\\AppData\\Roaming\\kimi-desktop\\daimon-storage.json':
|
|
'\uFEFF'+JSON.stringify({version:1,shareDir:'D:\\KimiData\\daimon-share'})});
|
|
expect(I.desktopFile({platform:'win32',home:'C:\\Users\\u',appdata:'C:\\Users\\u\\AppData\\Roaming',read}))
|
|
.toBe('D:\\KimiData\\daimon-share\\daimon\\config.json');
|
|
});
|
|
|
|
test('macOS and Linux: storage the user moved is followed too',()=>{
|
|
const mac=files({'/Users/u/Library/Application Support/kimi-desktop/daimon-storage.json':JSON.stringify({version:1,shareDir:'/Volumes/X/KimiData/daimon-share'})});
|
|
expect(I.desktopFile({platform:'darwin',home:'/Users/u',read:mac})).toBe('/Volumes/X/KimiData/daimon-share/daimon/config.json');
|
|
const linux=files({'/home/u/.config/kimi-desktop/daimon-storage.json':JSON.stringify({version:1,shareDir:'/data/kimi/daimon-share/'})});
|
|
expect(I.desktopFile({platform:'linux',home:'/home/u',read:linux})).toBe('/data/kimi/daimon-share/daimon/config.json');
|
|
});
|
|
|
|
test('a pointer Kimi would not take leaves the default, as in Kimi',()=>{
|
|
const ptr='C:\\Users\\u\\AppData\\Roaming\\kimi-desktop\\daimon-storage.json',def='C:\\Users\\u\\AppData\\Roaming\\kimi-desktop\\daimon-share\\daimon\\config.json';
|
|
for(const body of ['not json','null','[]','{}',JSON.stringify({shareDir:''}),JSON.stringify({shareDir:' '}),
|
|
JSON.stringify({shareDir:'KimiData\\daimon-share'}),JSON.stringify({shareDir:42})])
|
|
expect(I.desktopFile({platform:'win32',home:'C:\\Users\\u',appdata:'C:\\Users\\u\\AppData\\Roaming',read:files({[ptr]:body})})).toBe(def);
|
|
});
|
|
|
|
test('the real reader follows a pointer on disk and reads the sign-in there',()=>{
|
|
const home=fs.mkdtempSync(path.join(os.tmpdir(),'kimi-work-ptr-'));
|
|
try{
|
|
const platform=process.platform==='win32'?'win32':process.platform==='darwin'?'darwin':'linux';
|
|
const data=I.userDataDir({home,platform,appdata:path.join(home,'AppData','Roaming')});
|
|
const share=path.join(home,'KimiData','daimon-share');
|
|
fs.mkdirSync(path.join(share,'daimon'),{recursive:true});fs.mkdirSync(data,{recursive:true});
|
|
fs.writeFileSync(path.join(share,'daimon','config.json'),JSON.stringify({credentials:{kimiCode:{apiKey:'synthetic-work',baseUrl:I.BASE},kimiWeb:{accessToken:'synthetic-web',userId:'fixture-user'}}}));
|
|
const opts={home,platform,appdata:path.join(home,'AppData','Roaming')};
|
|
// No pointer: the default folder, which has no sign-in.
|
|
expect(()=>I.readDesktop(I.desktopFile(opts))).toThrow();
|
|
fs.writeFileSync(path.join(data,'daimon-storage.json'),JSON.stringify({version:1,shareDir:share}));
|
|
expect(I.desktopFile(opts)).toBe(path.join(share,'daimon','config.json'));
|
|
expect(I.readDesktop(I.desktopFile(opts)).workToken).toBe('synthetic-work');
|
|
}finally{fs.rmSync(home,{recursive:true,force:true});}
|
|
});
|
|
|
|
test('a failed sign-in says which file it looked for, and a failed Work check is not called a missing sign-in',async()=>{
|
|
const missing=I.createPlugin({readSession:()=>I.readDesktop('/nowhere/kimi-desktop/daimon-share/daimon/config.json')});
|
|
const a=await (await missing.auth.methods[0].authorize()).callback();
|
|
expect(a.type).toBe('failed');
|
|
// the share folder itself is gone, so it is the folder that is named (#444)
|
|
expect(a.error).toContain('/nowhere/kimi-desktop/daimon-share');
|
|
const session={uid:'u',workToken:'synthetic-work-token',webToken:'w'};
|
|
const down=I.createPlugin({readSession:()=>session,fetchImpl:async()=>{throw Object.assign(TypeError('fetch failed'),{cause:{code:'ECONNRESET'}});}});
|
|
const b=await (await down.auth.methods[0].authorize()).callback();
|
|
expect(b.type).toBe('failed');
|
|
expect(b.error).toContain('ECONNRESET');
|
|
expect(b.error).not.toContain('没有可用');
|
|
expect(b.error).not.toContain(session.workToken);
|
|
const gone=I.createPlugin({readSession:()=>session,fetchImpl:async()=>new Response('{}',{status:401})});
|
|
expect((await (await gone.auth.methods[0].authorize()).callback()).error).toContain('过期');
|
|
});
|