dashboardSupervisor.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { describe, expect, it } from 'vitest'
  2. import { DashboardSupervisor } from '../../src/server/dashboardSupervisor.js'
  3. function supervisor(initiallyRunning = false) {
  4. let running = initiallyRunning
  5. const calls: string[][] = [], launches: string[][] = [], probes: string[] = []
  6. const instance = new DashboardSupervisor({
  7. run(args) {
  8. calls.push([...args])
  9. if (args[0] === 'dashboard' && args[1] === '--stop') running = false
  10. return ''
  11. },
  12. launch(args) { launches.push([...args]); running = true },
  13. probe: async (host, port) => { probes.push(host + ':' + port); return running },
  14. log: () => undefined,
  15. })
  16. return { instance, calls, launches, probes }
  17. }
  18. describe('DashboardSupervisor', () => {
  19. it('starts a missing service on loopback without reading or writing authentication config', async () => {
  20. const f = supervisor()
  21. await f.instance.checkNow()
  22. expect(f.calls).toEqual([])
  23. expect(f.launches).toEqual([['dashboard', '--host', '127.0.0.1', '--no-open']])
  24. await f.instance.checkNow()
  25. expect(f.launches).toHaveLength(1)
  26. })
  27. it('leaves an existing service and its authentication/binding untouched', async () => {
  28. const f = supervisor(true)
  29. await f.instance.checkNow(); await f.instance.checkNow()
  30. expect(f.calls).toEqual([])
  31. expect(f.launches).toEqual([])
  32. expect(new Set(f.probes)).toEqual(new Set(['127.0.0.1:9119']))
  33. })
  34. it('only restarts on an explicit request, without configuring any credentials', async () => {
  35. const f = supervisor(true)
  36. await f.instance.restart()
  37. expect(f.calls).toEqual([['dashboard', '--stop']])
  38. expect(f.launches).toEqual([['dashboard', '--host', '127.0.0.1', '--no-open']])
  39. })
  40. it('coalesces concurrent checks', async () => {
  41. const f = supervisor()
  42. await Promise.all([f.instance.checkNow(), f.instance.checkNow()])
  43. expect(f.launches).toHaveLength(1)
  44. })
  45. })