static-wallpaper.service.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { EventEmitter } from 'events'
  2. import type { ChildProcess } from 'child_process'
  3. import { describe, expect, it, vi } from 'vitest'
  4. import {
  5. applyHyprpaper,
  6. applySwaybg,
  7. applySwww,
  8. prefersHyprpaper,
  9. swwwCommand,
  10. type CommandResult,
  11. type DaemonStarter
  12. } from './static-wallpaper.service'
  13. const IMAGE = '/home/user/wall paper.png'
  14. /** answers each command line with the first matching reply, records every call */
  15. function fakeRunner(replies: Array<[RegExp, CommandResult | (() => CommandResult)]>) {
  16. const calls: string[] = []
  17. const run = async (cmd: string, args: string[]) => {
  18. const line = [cmd, ...args].join(' ')
  19. calls.push(line)
  20. const reply = replies.find(([pattern]) => pattern.test(line))?.[1]
  21. if (!reply) return { ok: false, output: `unexpected: ${line}` }
  22. return typeof reply === 'function' ? reply() : reply
  23. }
  24. return { run, calls }
  25. }
  26. describe('applyHyprpaper', () => {
  27. it('sets every monitor in one request on hyprpaper 0.8', async () => {
  28. const { run, calls } = fakeRunner([[/wallpaper/, { ok: true, output: '' }]])
  29. await applyHyprpaper(IMAGE, run, vi.fn())
  30. expect(calls).toEqual([`hyprctl hyprpaper wallpaper ,${IMAGE},cover`])
  31. })
  32. it('starts hyprpaper when it is not running and tries again', async () => {
  33. let started = false
  34. const { run, calls } = fakeRunner([
  35. [
  36. /wallpaper/,
  37. () =>
  38. started
  39. ? { ok: true, output: '' }
  40. : { ok: false, output: "Error: can't send: failed to connect to hyprpaper (is it running?)" }
  41. ]
  42. ])
  43. const start = vi.fn<DaemonStarter>(() => {
  44. started = true
  45. })
  46. await applyHyprpaper(IMAGE, run, start)
  47. expect(start).toHaveBeenCalledWith('hyprpaper', [])
  48. expect(calls).toHaveLength(2)
  49. })
  50. it('preloads first for hyprpaper up to 0.7', async () => {
  51. const { run, calls } = fakeRunner([
  52. [/wallpaper ,.*,cover$/, { ok: true, output: 'wallpaper failed (not preloaded)' }],
  53. [/preload/, { ok: true, output: 'ok' }],
  54. [/wallpaper ,/, { ok: true, output: 'ok' }],
  55. [/unload unused/, { ok: true, output: 'ok' }]
  56. ])
  57. await applyHyprpaper(IMAGE, run, vi.fn())
  58. expect(calls.slice(1)).toEqual([
  59. `hyprctl hyprpaper preload ${IMAGE}`,
  60. `hyprctl hyprpaper wallpaper ,${IMAGE}`,
  61. 'hyprctl hyprpaper unload unused'
  62. ])
  63. })
  64. it('reports what hyprpaper said when nothing worked', async () => {
  65. const { run } = fakeRunner([
  66. [/wallpaper ,.*,cover$/, { ok: false, output: 'error: failed to set wallpaper: Invalid path' }],
  67. [/preload/, { ok: false, output: 'error: unknown request' }],
  68. [/wallpaper ,/, { ok: false, output: '' }]
  69. ])
  70. await expect(applyHyprpaper(IMAGE, run, vi.fn())).rejects.toThrow('unknown request')
  71. })
  72. })
  73. describe('applySwww', () => {
  74. it('prefers awww, the renamed swww', () => {
  75. expect(swwwCommand((cmd) => cmd === 'awww' || cmd === 'swww')).toBe('awww')
  76. expect(swwwCommand((cmd) => cmd === 'swww')).toBe('swww')
  77. expect(swwwCommand(() => false)).toBeUndefined()
  78. })
  79. it('starts the daemon when query fails', async () => {
  80. let running = false
  81. const { run, calls } = fakeRunner([
  82. [/query/, () => ({ ok: running, output: running ? '' : 'Socket file not found' })],
  83. [/img/, { ok: true, output: '' }]
  84. ])
  85. const start = vi.fn<DaemonStarter>(() => {
  86. running = true
  87. })
  88. await applySwww(IMAGE, 'awww', run, start)
  89. expect(start).toHaveBeenCalledWith('awww-daemon', [])
  90. expect(calls.at(-1)).toBe(`awww img ${IMAGE} --transition-type fade`)
  91. })
  92. })
  93. describe('applySwaybg', () => {
  94. function fakeChild() {
  95. const child = new EventEmitter() as ChildProcess & EventEmitter
  96. Object.assign(child, { exitCode: null, kill: vi.fn<() => boolean>() })
  97. return child
  98. }
  99. it('keeps the new swaybg running and stops the previous one', async () => {
  100. const first = Object.assign(fakeChild(), { pid: 4242 })
  101. const second = Object.assign(fakeChild(), { pid: 4243 })
  102. const kill = vi.spyOn(process, 'kill').mockImplementation(() => true)
  103. await applySwaybg(IMAGE, () => first)
  104. await applySwaybg(IMAGE, () => second)
  105. expect(kill).toHaveBeenCalledWith(-4242, 'SIGTERM')
  106. expect(kill).not.toHaveBeenCalledWith(-4243, 'SIGTERM')
  107. kill.mockRestore()
  108. })
  109. it('fails when swaybg exits at once', async () => {
  110. const child = fakeChild()
  111. const pending = applySwaybg(IMAGE, () => {
  112. setTimeout(() => child.emit('exit', 1), 10)
  113. return child
  114. })
  115. await expect(pending).rejects.toThrow('code 1')
  116. })
  117. })
  118. describe('prefersHyprpaper', () => {
  119. const installed = (...cmds: string[]) => (cmd: string) => cmds.includes(cmd)
  120. const runningOnly = (...names: string[]) => (name: string) => names.includes(name)
  121. it('takes hyprpaper unless a swww daemon already draws', () => {
  122. expect(prefersHyprpaper(installed('hyprpaper', 'hyprctl'), runningOnly())).toBe(true)
  123. expect(prefersHyprpaper(installed('hyprpaper', 'hyprctl', 'swww'), runningOnly())).toBe(true)
  124. expect(prefersHyprpaper(installed('hyprpaper', 'hyprctl', 'swww'), runningOnly('swww-daemon'))).toBe(false)
  125. expect(prefersHyprpaper(installed('hyprpaper', 'hyprctl', 'awww'), runningOnly('awww-daemon'))).toBe(false)
  126. })
  127. it('keeps a running hyprpaper', () => {
  128. expect(prefersHyprpaper(installed('hyprpaper', 'hyprctl', 'swww'), runningOnly('hyprpaper', 'swww-daemon'))).toBe(true)
  129. })
  130. it('needs hyprpaper and hyprctl', () => {
  131. expect(prefersHyprpaper(installed('hyprpaper'), runningOnly('hyprpaper'))).toBe(false)
  132. })
  133. })