platform.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { readFileSync } from 'fs'
  2. import { join } from 'path'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. const execSync = vi.fn<(command: string) => string>()
  5. vi.mock('child_process', () => ({ execSync: (command: string) => execSync(command) }))
  6. const {
  7. getConnectedScreens,
  8. hasStatusNotifierHost,
  9. parseHyprctlMonitors,
  10. parseNameHasOwner,
  11. parseNiriOutputs,
  12. parseSwayOutputs,
  13. parseXrandrGeometries
  14. } = await import('./platform')
  15. function answerCommands(answers: Record<string, string>): void {
  16. execSync.mockImplementation((command: string) => {
  17. const known = Object.keys(answers).find((name) => command.includes(name))
  18. if (!known) throw new Error(`${command}: not found`)
  19. return answers[known]
  20. })
  21. }
  22. function waylandSession(markers: Record<string, string>): void {
  23. vi.stubEnv('WAYLAND_DISPLAY', 'wayland-1')
  24. for (const marker of ['HYPRLAND_INSTANCE_SIGNATURE', 'SWAYSOCK', 'NIRI_SOCKET']) {
  25. vi.stubEnv(marker, markers[marker] ?? '')
  26. }
  27. }
  28. afterEach(() => {
  29. vi.unstubAllEnvs()
  30. execSync.mockReset()
  31. })
  32. describe('parseHyprctlMonitors', () => {
  33. it('lists the monitor names and skips disabled ones', () => {
  34. const output = JSON.stringify([
  35. { id: 0, name: 'DP-1', description: 'Dell U2720Q', width: 3840, height: 2160, disabled: false },
  36. { id: 1, name: 'HDMI-A-1', width: 1920, height: 1080 },
  37. { id: 2, name: 'eDP-1', disabled: true }
  38. ])
  39. expect(parseHyprctlMonitors(output)).toEqual(['DP-1', 'HDMI-A-1'])
  40. })
  41. it('returns nothing for output that is not a monitor list', () => {
  42. expect(parseHyprctlMonitors('')).toEqual([])
  43. expect(parseHyprctlMonitors('Couldn\'t connect to the socket')).toEqual([])
  44. expect(parseHyprctlMonitors('{"name":"DP-1"}')).toEqual([])
  45. expect(parseHyprctlMonitors('[null, {"id": 3}]')).toEqual([])
  46. })
  47. })
  48. describe('parseSwayOutputs', () => {
  49. it('lists the output names and skips inactive ones', () => {
  50. const output = JSON.stringify([
  51. { name: 'DP-2', active: true, make: 'Dell', rect: { x: 0, y: 0, width: 2560, height: 1440 } },
  52. { name: 'HDMI-A-1', active: false },
  53. { name: 'eDP-1', active: true }
  54. ])
  55. expect(parseSwayOutputs(output)).toEqual(['DP-2', 'eDP-1'])
  56. })
  57. it('returns nothing for output that is not an output list', () => {
  58. expect(parseSwayOutputs('')).toEqual([])
  59. expect(parseSwayOutputs('{"success":false}')).toEqual([])
  60. })
  61. })
  62. describe('parseNiriOutputs', () => {
  63. it('takes the names from the keys and skips outputs without a current mode', () => {
  64. const output = JSON.stringify({
  65. 'eDP-1': { name: 'eDP-1', make: 'BOE', current_mode: 0, logical: { x: 0, y: 0, width: 1920, height: 1080 } },
  66. 'DP-3': { name: 'DP-3', current_mode: 2, logical: { x: 1920, y: 0, width: 2560, height: 1440 } },
  67. 'HDMI-A-1': { name: 'HDMI-A-1', current_mode: null, logical: null }
  68. })
  69. expect(parseNiriOutputs(output)).toEqual(['eDP-1', 'DP-3'])
  70. })
  71. it('returns nothing for output that is not an output map', () => {
  72. expect(parseNiriOutputs('')).toEqual([])
  73. expect(parseNiriOutputs('[]')).toEqual([])
  74. expect(parseNiriOutputs('null')).toEqual([])
  75. expect(parseNiriOutputs('{}')).toEqual([])
  76. })
  77. })
  78. // captured from headless sessions: sway 1.12, Hyprland 0.56.2 and niri 26.04
  79. function captured(name: string): string {
  80. return readFileSync(join(__dirname, 'fixtures', 'compositors', name), 'utf8')
  81. }
  82. describe('output lists the compositors really print', () => {
  83. it('sway lists every output, an inactive one is switched off', () => {
  84. expect(parseSwayOutputs(captured('sway-outputs.json')).sort()).toEqual(['HEADLESS-1', 'HEADLESS-2'])
  85. expect(parseSwayOutputs(captured('sway-outputs-one-disabled.json'))).toEqual(['HEADLESS-1'])
  86. })
  87. it('hyprctl leaves a disabled monitor out of `monitors` and marks it in `monitors all`', () => {
  88. expect(parseHyprctlMonitors(captured('hypr-monitors.json'))).toEqual(['HEADLESS-1', 'HEADLESS-2'])
  89. expect(parseHyprctlMonitors(captured('hypr-monitors-one-disabled.json'))).toEqual(['HEADLESS-1'])
  90. expect(parseHyprctlMonitors(captured('hypr-monitors-all-one-disabled.json'))).toEqual(['HEADLESS-1'])
  91. })
  92. it('niri keys its outputs by name', () => {
  93. expect(parseNiriOutputs(captured('niri-outputs.json'))).toEqual(['winit'])
  94. })
  95. })
  96. describe('parseNameHasOwner', () => {
  97. it('reads busctl and gdbus answers', () => {
  98. expect(parseNameHasOwner('b true\n')).toBe(true)
  99. expect(parseNameHasOwner('b false\n')).toBe(false)
  100. expect(parseNameHasOwner('(true,)\n')).toBe(true)
  101. expect(parseNameHasOwner('(false,)\n')).toBe(false)
  102. })
  103. it('gives null for anything else', () => {
  104. expect(parseNameHasOwner('')).toBeNull()
  105. expect(parseNameHasOwner('Failed to connect to bus')).toBeNull()
  106. })
  107. })
  108. describe.skipIf(process.platform !== 'linux')('getConnectedScreens', () => {
  109. it('asks hyprctl on Hyprland, before wlr-randr', () => {
  110. waylandSession({ HYPRLAND_INSTANCE_SIGNATURE: 'abc_123' })
  111. answerCommands({
  112. 'hyprctl monitors -j': '[{"name":"DP-1"},{"name":"HDMI-A-1","disabled":true}]',
  113. 'wlr-randr': 'eDP-1 "Some panel"\n Enabled: yes\n'
  114. })
  115. expect(getConnectedScreens()).toEqual(['DP-1'])
  116. })
  117. it('asks swaymsg on sway and niri msg on niri', () => {
  118. waylandSession({ SWAYSOCK: '/run/user/1000/sway-ipc.sock' })
  119. answerCommands({ 'swaymsg -t get_outputs -r': '[{"name":"DP-2","active":true}]' })
  120. expect(getConnectedScreens()).toEqual(['DP-2'])
  121. waylandSession({ NIRI_SOCKET: '/run/user/1000/niri.sock' })
  122. answerCommands({ 'niri msg --json outputs': '{"eDP-1":{"name":"eDP-1","current_mode":0}}' })
  123. expect(getConnectedScreens()).toEqual(['eDP-1'])
  124. })
  125. it('falls back to wlr-randr when the compositor query fails or lists nothing', () => {
  126. waylandSession({ HYPRLAND_INSTANCE_SIGNATURE: 'abc_123' })
  127. answerCommands({ 'wlr-randr': 'DP-1 "Dell"\n Enabled: yes\nHDMI-A-1 "LG"\n Enabled: yes\n' })
  128. expect(getConnectedScreens()).toEqual(['DP-1', 'HDMI-A-1'])
  129. answerCommands({ 'hyprctl monitors -j': '[]', 'wlr-randr': 'DP-1 "Dell"\n' })
  130. expect(getConnectedScreens()).toEqual(['DP-1'])
  131. })
  132. it('leaves the compositor tools alone without their marker', () => {
  133. waylandSession({})
  134. answerCommands({ 'wlr-randr': 'DP-1 "Dell"\n' })
  135. expect(getConnectedScreens()).toEqual(['DP-1'])
  136. expect(execSync).toHaveBeenCalledTimes(1)
  137. })
  138. })
  139. describe.skipIf(process.platform !== 'linux')('hasStatusNotifierHost', () => {
  140. it('follows the owner of the watcher name', () => {
  141. answerCommands({ busctl: 'b true\n' })
  142. expect(hasStatusNotifierHost(0)).toBe(true)
  143. answerCommands({ busctl: 'b false\n' })
  144. expect(hasStatusNotifierHost(0)).toBe(false)
  145. })
  146. it('tries gdbus when busctl is missing', () => {
  147. answerCommands({ gdbus: '(false,)\n' })
  148. expect(hasStatusNotifierHost(0)).toBe(false)
  149. })
  150. it('assumes a host when neither tool can answer', () => {
  151. answerCommands({})
  152. expect(hasStatusNotifierHost(0)).toBe(true)
  153. answerCommands({ busctl: 'garbled' })
  154. expect(hasStatusNotifierHost(0)).toBe(true)
  155. })
  156. it('reuses an answer for a short while', () => {
  157. answerCommands({ busctl: 'b false\n' })
  158. expect(hasStatusNotifierHost(0)).toBe(false)
  159. answerCommands({ busctl: 'b true\n' })
  160. expect(hasStatusNotifierHost()).toBe(false)
  161. expect(hasStatusNotifierHost(0)).toBe(true)
  162. })
  163. })
  164. describe('parseXrandrGeometries', () => {
  165. it('reads connected outputs as XxYxWxH', () => {
  166. const out = [
  167. 'Screen 0: minimum 320 x 200, current 4480 x 1440, maximum 16384 x 16384',
  168. 'DP-1 connected primary 2560x1440+1920+0 (normal left inverted right x axis y axis) 600mm x 340mm',
  169. 'HDMI-1 connected 1920x1080+0+0 (normal left inverted right) 510mm x 290mm',
  170. 'HDMI-2 disconnected (normal left inverted right x axis y axis)',
  171. 'DP-2 connected (normal left inverted right x axis y axis)'
  172. ].join('\n')
  173. expect([...parseXrandrGeometries(out)]).toEqual([
  174. ['DP-1', '1920x0x2560x1440'],
  175. ['HDMI-1', '0x0x1920x1080']
  176. ])
  177. })
  178. })