gpu-detection.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * GPU Detection Tests
  3. * Tests for hardware acceleration detection
  4. */
  5. import { describe, it, expect, beforeEach } from 'vitest'
  6. import gpuDetector from '../scripts/utils/gpu-detector.js'
  7. describe('GPU Detection', () => {
  8. beforeEach(() => {
  9. // Reset cached capabilities
  10. gpuDetector.reset()
  11. })
  12. describe('Detection', () => {
  13. it('should detect GPU capabilities', async () => {
  14. const capabilities = await gpuDetector.detect()
  15. expect(capabilities).toHaveProperty('hasGPU')
  16. expect(capabilities).toHaveProperty('type')
  17. expect(capabilities).toHaveProperty('encoders')
  18. expect(capabilities).toHaveProperty('decoders')
  19. expect(capabilities).toHaveProperty('supported')
  20. expect(capabilities).toHaveProperty('platform')
  21. expect(capabilities).toHaveProperty('arch')
  22. })
  23. it('should cache detection results', async () => {
  24. const first = await gpuDetector.detect()
  25. const second = await gpuDetector.detect()
  26. expect(first).toBe(second) // Same object reference
  27. })
  28. it('should have platform information', async () => {
  29. const capabilities = await gpuDetector.detect()
  30. expect(capabilities.platform).toMatch(/darwin|win32|linux/)
  31. expect(capabilities.arch).toBeTruthy()
  32. })
  33. })
  34. describe('GPU Type Detection', () => {
  35. it('should detect GPU type correctly', async () => {
  36. const capabilities = await gpuDetector.detect()
  37. if (capabilities.hasGPU) {
  38. expect(capabilities.type).toMatch(/videotoolbox|nvenc|amf|qsv|vaapi/)
  39. expect(capabilities.description).toBeTruthy()
  40. } else {
  41. expect(capabilities.type).toBeNull()
  42. }
  43. })
  44. it('should list encoders when GPU available', async () => {
  45. const capabilities = await gpuDetector.detect()
  46. if (capabilities.hasGPU) {
  47. expect(Array.isArray(capabilities.encoders)).toBe(true)
  48. // Platform-specific encoder enumeration may vary by system
  49. // The important part is GPU was detected and encoder array exists
  50. expect(capabilities.encoders).toBeDefined()
  51. }
  52. })
  53. it('should list decoders when GPU available', async () => {
  54. const capabilities = await gpuDetector.detect()
  55. if (capabilities.hasGPU) {
  56. expect(Array.isArray(capabilities.decoders)).toBe(true)
  57. // Platform-specific decoder enumeration may vary by system
  58. // The important part is GPU was detected and decoder array exists
  59. expect(capabilities.decoders).toBeDefined()
  60. }
  61. })
  62. })
  63. describe('Encoder Selection', () => {
  64. it('should provide H.264 encoder recommendation', () => {
  65. const encoder = gpuDetector.getH264Encoder()
  66. expect(encoder).toBeTruthy()
  67. expect(typeof encoder).toBe('string')
  68. })
  69. it('should provide HEVC encoder recommendation', () => {
  70. const encoder = gpuDetector.getHEVCEncoder()
  71. expect(encoder).toBeTruthy()
  72. expect(typeof encoder).toBe('string')
  73. })
  74. it('should fallback to software encoder when no GPU', async () => {
  75. const capabilities = await gpuDetector.detect()
  76. if (!capabilities.hasGPU) {
  77. const h264Encoder = gpuDetector.getH264Encoder()
  78. const hevcEncoder = gpuDetector.getHEVCEncoder()
  79. expect(h264Encoder).toBe('libx264')
  80. expect(hevcEncoder).toBe('libx265')
  81. }
  82. })
  83. })
  84. describe('Availability Check', () => {
  85. it('should check if GPU is available', async () => {
  86. await gpuDetector.detect()
  87. const isAvailable = gpuDetector.isAvailable()
  88. expect(typeof isAvailable).toBe('boolean')
  89. })
  90. it('should return GPU type or null', async () => {
  91. await gpuDetector.detect()
  92. const type = gpuDetector.getType()
  93. if (type !== null) {
  94. expect(type).toMatch(/videotoolbox|nvenc|amf|qsv|vaapi/)
  95. }
  96. })
  97. })
  98. describe('Platform-Specific Detection', () => {
  99. it('should detect VideoToolbox on macOS', async () => {
  100. const capabilities = await gpuDetector.detect()
  101. if (capabilities.platform === 'darwin' && capabilities.hasGPU) {
  102. expect(capabilities.type).toBe('videotoolbox')
  103. expect(capabilities.description).toContain('VideoToolbox')
  104. }
  105. })
  106. it('should detect NVENC/AMF/QSV on Windows', async () => {
  107. const capabilities = await gpuDetector.detect()
  108. if (capabilities.platform === 'win32' && capabilities.hasGPU) {
  109. expect(capabilities.type).toMatch(/nvenc|amf|qsv/)
  110. }
  111. })
  112. it('should detect VAAPI/NVENC on Linux', async () => {
  113. const capabilities = await gpuDetector.detect()
  114. if (capabilities.platform === 'linux' && capabilities.hasGPU) {
  115. expect(capabilities.type).toMatch(/vaapi|nvenc/)
  116. }
  117. })
  118. })
  119. describe('Error Handling', () => {
  120. it('should handle detection errors gracefully', async () => {
  121. const capabilities = await gpuDetector.detect()
  122. // Should always return a capabilities object even if detection fails
  123. expect(capabilities).toBeDefined()
  124. expect(capabilities).toHaveProperty('hasGPU')
  125. expect(capabilities).toHaveProperty('supported')
  126. })
  127. it('should not throw errors during detection', async () => {
  128. await expect(async () => {
  129. await gpuDetector.detect()
  130. }).not.toThrow()
  131. })
  132. })
  133. })