workshop-account.test.js 2.8 KB

1234567891011121314151617181920212223242526272829
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import path from 'node:path';
  4. import {runAccountBinding} from '../skills/ksf-workshop-runtime/scripts/ksf-account.mjs';
  5. import {parseArgs} from '../skills/ksf-workshop-runtime/scripts/ksf-secure.mjs';
  6. import {redactIdentity} from '../skills/ksf-workshop-runtime/scripts/ksf-identity.mjs';
  7. const profile={key:'participant-registration:1ab680ff-4ad4-4377-9333-3b2f23e26bb7',account:{platform:'fmode',accountName:'guopeng',accountId:null}};
  8. const task={participant:{profileKey:profile.key},username:'guopeng',displayName:'果鹏'};
  9. const opts={requestId:'first-link',agentProfile:{role:'workshop'},participantProfile:profile};
  10. test('首次绑定命令只传当前档案和自报称呼账号,不收密码或冒充平台确认',async()=>{
  11. let called=0;const call=async(name,p)=>{called++;assert.equal(name,'ksfWorkshopAccount');assert.equal(p.action,'prepare');assert.equal(p.participantProfileKey,profile.key);assert.ok(!('password'in p));return{status:'awaiting_account_confirmation'};};
  12. const r=await runAccountBinding('account-prepare',task,{...opts,call});assert.equal(r.status,'awaiting_account_confirmation');assert.equal(called,1);
  13. await assert.rejects(()=>runAccountBinding('account-confirm',task,{...opts,call}),{code:'INVALID_INPUT'});assert.equal(called,1);
  14. });
  15. test('档案不匹配、冒用其他账号或通道字段写入前拒绝',async()=>{
  16. const call=()=>{assert.fail('must stop before network');};
  17. for(const t of [{...task,username:'liuna'},{...task,participant:{profileKey:'another'}},{...task,password:'forbidden'}])await assert.rejects(()=>runAccountBinding('account-prepare',t,{...opts,call}));
  18. const status=await runAccountBinding('account-status',{participant:task.participant},{...opts,call:async()=>({status:'bound',memberKey:'verified-key'})});assert.equal(status.memberKey,'verified-key');
  19. });
  20. test('新增受控命令沿用加密凭据入口,要求Agent及请求编号',()=>{
  21. const flags=['--input',path.resolve('work/task.json'),'--request-id','link-test','--agent-profile','agent-account:'+'a'.repeat(64)];
  22. assert.equal(parseArgs(['account-prepare',...flags]).command,'account-prepare');
  23. assert.equal(parseArgs(['account-status',...flags]).command,'account-status');
  24. assert.throws(()=>parseArgs(['account-prepare','--input',path.resolve('work/task.json'),'--request-id','link-test']),{code:'AGENT_PROFILE_REQUIRED'});
  25. });
  26. test('报告回读移除新增私有绑定快照和平台编号,只保留公开memberKey',()=>{
  27. const result=redactIdentity({memberKey:'public-member',meta:{attribution:{participantKey:'old-participant',accountBinding:{platformUserId:'private-user'},accountProfileKey:'private-profile',platformUserId:'private-user'}}});
  28. assert.deepEqual(result,{memberKey:'public-member',meta:{attribution:{participantKey:'old-participant'}}});
  29. });