workerResolution.test.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // @vitest-environment node
  2. import {afterEach,beforeEach,expect,it} from 'vitest'
  3. import {mkdtemp,mkdir,writeFile,rm} from 'node:fs/promises'
  4. import {tmpdir,homedir} from 'node:os'
  5. import {join,resolve} from 'node:path'
  6. import {HermesWorkerProcess} from '../../src/runner/worker/process'
  7. let root:string,source:string,home:string
  8. const workers:HermesWorkerProcess[]=[]
  9. beforeEach(async()=>{
  10. root=await mkdtemp(join(tmpdir(),'yaoyao-resolver-'));source=join(root,'source');home=join(root,'profile')
  11. await mkdir(join(source,'hermes_cli'),{recursive:true});await mkdir(home)
  12. // Only replace the installed Hermes config/SDK boundary. Exercise the real
  13. // Python reader and Node protocol, including failures and secret filtering.
  14. await writeFile(join(source,'dotenv.py'),'def dotenv_values(path): return {}\n')
  15. await writeFile(join(source,'hermes_constants.py'),'def set_hermes_home_override(path): pass\n')
  16. await writeFile(join(source,'hermes_cli','config.py'),'import os,json\nfrom pathlib import Path\ndef load_config_readonly(): return json.loads((Path(os.environ["HERMES_HOME"])/"config.yaml").read_text())\n')
  17. await writeFile(join(source,'hermes_cli','runtime_provider.py'),'raise RuntimeError("provider secret must never escape")\n')
  18. })
  19. afterEach(async()=>{await Promise.all(workers.splice(0).map(w=>w.close()));await rm(root,{recursive:true,force:true})})
  20. function read(mode='resolve-workspace',profile='default'){
  21. const worker=new HermesWorkerProcess('python3',resolve('src/runner/worker/hermes_worker.py'),{mode,profile,hermesSource:source,hermesHome:home})
  22. workers.push(worker);return worker.wait('resolved')
  23. }
  24. it.each([
  25. ['.','/home/cua/workspace'],['auto','/home/cua/workspace'],['cwd','/home/cua/workspace'],
  26. ['projects/demo','/home/cua/workspace/projects/demo'],['/projects/demo','/projects/demo'],['~/projects',join(homedir(),'projects')],
  27. ])('resolves %s without importing the model SDK',async(configured,cwd)=>{
  28. await writeFile(join(home,'config.yaml'),JSON.stringify({terminal:{cwd:configured}}))
  29. expect(await read()).toMatchObject({cwd,configuredCwd:configured})
  30. })
  31. it('handles default/missing cwd and named Profiles without mixing Profile settings',async()=>{
  32. await writeFile(join(home,'config.yaml'),'{}')
  33. expect(await read()).toMatchObject({cwd:'/home/cua/workspace',configuredCwd:'.'})
  34. await mkdir(join(home,'profiles','writer'),{recursive:true})
  35. await writeFile(join(home,'profiles','writer','config.yaml'),JSON.stringify({terminal:{cwd:'/projects/writer'}}))
  36. expect(await read('resolve-workspace','writer')).toMatchObject({cwd:'/projects/writer'})
  37. })
  38. it('reports directory and Profile errors without mislabelling them as model errors',async()=>{
  39. for(const cwd of ['../../escape','\u0000',32]){
  40. await writeFile(join(home,'config.yaml'),JSON.stringify({terminal:{cwd}}))
  41. await expect(read()).rejects.toMatchObject({code:'computer_cwd_invalid'})
  42. }
  43. await expect(read('resolve-workspace','missing')).rejects.toMatchObject({code:'computer_profile_missing'})
  44. await expect(read('resolve-workspace','../escape')).rejects.toMatchObject({code:'computer_profile_invalid'})
  45. await writeFile(join(home,'config.yaml'),'not valid config')
  46. await expect(read()).rejects.toMatchObject({code:'computer_profile_config_invalid'})
  47. })
  48. it('still requires a usable model for an Agent run and does not expose SDK details',async()=>{
  49. await writeFile(join(home,'config.yaml'),'{}')
  50. await expect(read('resolve')).rejects.toMatchObject({code:'computer_model_missing'})
  51. await writeFile(join(home,'config.yaml'),JSON.stringify({model:{default:'fixture',provider:'fixture'}}))
  52. await expect(read('resolve')).rejects.toMatchObject({code:'computer_model_unavailable'})
  53. try{await read('resolve')}catch(error){expect(String(error)).not.toContain('provider secret')}
  54. })