personaSection.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import {it,expect} from 'vitest'
  2. import {personaSection} from '../../src/shared/workspace'
  3. const base={job:undefined,antiJobs:undefined,voice:undefined,voiceCustom:undefined,actBias:undefined}
  4. it('returns empty when no persona field is set',()=>{
  5. expect(personaSection(base)).toBe('')
  6. })
  7. it('composes job, anti-jobs, preset voice and act bias',()=>{
  8. const text=personaSection({...base,job:'盯家庭邮箱和日程',antiJobs:['绝不直接发送邮件','绝不提交表单'],voice:'concise',actBias:'ask_key_then_act'})
  9. expect(text).toContain('你的唯一职责:盯家庭邮箱和日程')
  10. expect(text).toContain('- 绝不直接发送邮件')
  11. expect(text).toContain('语气:简洁专业')
  12. expect(text).toContain('行动策略:先问关键问题即行动')
  13. expect(text.indexOf('唯一职责')).toBeLessThan(text.indexOf('明确不做'))
  14. expect(text.indexOf('明确不做')).toBeLessThan(text.indexOf('语气'))
  15. })
  16. it('uses the custom voice text only when provided',()=>{
  17. expect(personaSection({...base,voice:'custom'})).toBe('')
  18. expect(personaSection({...base,voice:'custom',voiceCustom:'像朋友一样随意'})).toContain('按以下要求把握语气:像朋友一样随意')
  19. })
  20. import {mkdtempSync,rmSync} from 'node:fs'
  21. import {tmpdir} from 'node:os'
  22. import {join} from 'node:path'
  23. import {WorkspaceStore} from '../../src/server/workspaceStore'
  24. it('persists persona fields and clears them via null patches',()=>{
  25. const home=mkdtempSync(join(tmpdir(),'persona-'))
  26. const store=new WorkspaceStore(home)
  27. try{
  28. const agent=store.createAgent('owner',{name:'管家',instructions:'',profile:'default',job:'盯家庭日程',antiJobs:['绝不直接发送邮件'],voice:'casual',actBias:'ask_first'})
  29. expect(agent.job).toBe('盯家庭日程');expect(agent.antiJobs).toEqual(['绝不直接发送邮件'])
  30. const patched=store.updateAgent('owner',agent.id,{job:null,antiJobs:null,voice:null,voiceCustom:null,actBias:'act_now'})
  31. expect(patched.job).toBeUndefined();expect(patched.antiJobs).toBeUndefined();expect(patched.voice).toBeUndefined();expect(patched.actBias).toBe('act_now')
  32. const reloaded=store.require<import('../../src/shared/workspace').WorkspaceAgent>('owner','agent',agent.id)
  33. expect(reloaded.job).toBeUndefined();expect(reloaded.antiJobs).toBeUndefined();expect(reloaded.actBias).toBe('act_now')
  34. }finally{store.close();rmSync(home,{recursive:true,force:true})}
  35. })