gpu-detection.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. expect(capabilities.encoders.length).toBeGreaterThan(0)
  49. }
  50. })
  51. it('should list decoders when GPU available', async () => {
  52. const capabilities = await gpuDetector.detect()
  53. if (capabilities.hasGPU) {
  54. expect(Array.isArray(capabilities.decoders)).toBe(true)
  55. expect(capabilities.decoders.length).toBeGreaterThan(0)
  56. }
  57. })
  58. })
  59. describe('Encoder Selection', () => {
  60. it('should provide H.264 encoder recommendation', () => {
  61. const encoder = gpuDetector.getH264Encoder()
  62. expect(encoder).toBeTruthy()
  63. expect(typeof encoder).toBe('string')
  64. })
  65. it('should provide HEVC encoder recommendation', () => {
  66. const encoder = gpuDetector.getHEVCEncoder()
  67. expect(encoder).toBeTruthy()
  68. expect(typeof encoder).toBe('string')
  69. })
  70. it('should fallback to software encoder when no GPU', async () => {
  71. const capabilities = await gpuDetector.detect()
  72. if (!capabilities.hasGPU) {
  73. const h264Encoder = gpuDetector.getH264Encoder()
  74. const hevcEncoder = gpuDetector.getHEVCEncoder()
  75. expect(h264Encoder).toBe('libx264')
  76. expect(hevcEncoder).toBe('libx265')
  77. }
  78. })
  79. })
  80. describe('Availability Check', () => {
  81. it('should check if GPU is available', async () => {
  82. await gpuDetector.detect()
  83. const isAvailable = gpuDetector.isAvailable()
  84. expect(typeof isAvailable).toBe('boolean')
  85. })
  86. it('should return GPU type or null', async () => {
  87. await gpuDetector.detect()
  88. const type = gpuDetector.getType()
  89. if (type !== null) {
  90. expect(type).toMatch(/videotoolbox|nvenc|amf|qsv|vaapi/)
  91. }
  92. })
  93. })
  94. describe('Platform-Specific Detection', () => {
  95. it('should detect VideoToolbox on macOS', async () => {
  96. const capabilities = await gpuDetector.detect()
  97. if (capabilities.platform === 'darwin' && capabilities.hasGPU) {
  98. expect(capabilities.type).toBe('videotoolbox')
  99. expect(capabilities.description).toContain('VideoToolbox')
  100. }
  101. })
  102. it('should detect NVENC/AMF/QSV on Windows', async () => {
  103. const capabilities = await gpuDetector.detect()
  104. if (capabilities.platform === 'win32' && capabilities.hasGPU) {
  105. expect(capabilities.type).toMatch(/nvenc|amf|qsv/)
  106. }
  107. })
  108. it('should detect VAAPI/NVENC on Linux', async () => {
  109. const capabilities = await gpuDetector.detect()
  110. if (capabilities.platform === 'linux' && capabilities.hasGPU) {
  111. expect(capabilities.type).toMatch(/vaapi|nvenc/)
  112. }
  113. })
  114. })
  115. describe('Error Handling', () => {
  116. it('should handle detection errors gracefully', async () => {
  117. const capabilities = await gpuDetector.detect()
  118. // Should always return a capabilities object even if detection fails
  119. expect(capabilities).toBeDefined()
  120. expect(capabilities).toHaveProperty('hasGPU')
  121. expect(capabilities).toHaveProperty('supported')
  122. })
  123. it('should not throw errors during detection', async () => {
  124. await expect(async () => {
  125. await gpuDetector.detect()
  126. }).not.toThrow()
  127. })
  128. })
  129. })