CodecTests.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import XCTest
  2. @testable import MixBoard
  3. final class CodecTests: XCTestCase {
  4. // MARK: - OGGDecoder Error Descriptions
  5. func testOGGFailedToOpenErrorDescription() {
  6. let error = OGGDecoder.OGGError.failedToOpen("test.ogg")
  7. XCTAssertEqual(error.errorDescription, "Failed to open OGG file: test.ogg")
  8. }
  9. func testOGGFailedToCreateBufferErrorDescription() {
  10. let error = OGGDecoder.OGGError.failedToCreateBuffer
  11. XCTAssertEqual(error.errorDescription, "Failed to create audio buffer")
  12. }
  13. func testOGGDecodingFailedErrorDescription() {
  14. let error = OGGDecoder.OGGError.decodingFailed
  15. XCTAssertEqual(error.errorDescription, "OGG decoding failed")
  16. }
  17. // MARK: - OpusDecoder Error Descriptions
  18. #if !DISABLE_OPUS
  19. func testOpusFailedToOpenErrorDescription() {
  20. let error = OpusDecoder.OpusError.failedToOpen("test.opus", -1)
  21. XCTAssertTrue(error.errorDescription!.contains("test.opus"))
  22. XCTAssertTrue(error.errorDescription!.contains("-1"))
  23. }
  24. func testOpusFailedToCreateBufferErrorDescription() {
  25. let error = OpusDecoder.OpusError.failedToCreateBuffer
  26. XCTAssertEqual(error.errorDescription, "Failed to create audio buffer")
  27. }
  28. func testOpusDecodingFailedErrorDescription() {
  29. let error = OpusDecoder.OpusError.decodingFailed
  30. XCTAssertEqual(error.errorDescription, "Opus decoding failed")
  31. }
  32. #endif
  33. // MARK: - isOGGFile
  34. func testIsOGGFileTrue() {
  35. XCTAssertTrue(OGGDecoder.isOGGFile(URL(fileURLWithPath: "/path/to/file.ogg")))
  36. }
  37. func testIsOGGFileFalse() {
  38. XCTAssertFalse(OGGDecoder.isOGGFile(URL(fileURLWithPath: "/path/to/file.mp3")))
  39. XCTAssertFalse(OGGDecoder.isOGGFile(URL(fileURLWithPath: "/path/to/file.flac")))
  40. XCTAssertFalse(OGGDecoder.isOGGFile(URL(fileURLWithPath: "/path/to/file.opus")))
  41. }
  42. func testIsOGGFileCaseInsensitive() {
  43. XCTAssertTrue(OGGDecoder.isOGGFile(URL(fileURLWithPath: "/path/to/file.OGG")))
  44. }
  45. // MARK: - isOpusFile
  46. #if !DISABLE_OPUS
  47. func testIsOpusFileTrue() {
  48. XCTAssertTrue(OpusDecoder.isOpusFile(URL(fileURLWithPath: "/path/to/file.opus")))
  49. }
  50. func testIsOpusFileFalse() {
  51. XCTAssertFalse(OpusDecoder.isOpusFile(URL(fileURLWithPath: "/path/to/file.ogg")))
  52. XCTAssertFalse(OpusDecoder.isOpusFile(URL(fileURLWithPath: "/path/to/file.mp3")))
  53. XCTAssertFalse(OpusDecoder.isOpusFile(URL(fileURLWithPath: "/path/to/file.wav")))
  54. }
  55. func testIsOpusFileCaseInsensitive() {
  56. XCTAssertTrue(OpusDecoder.isOpusFile(URL(fileURLWithPath: "/path/to/file.OPUS")))
  57. }
  58. #endif
  59. // MARK: - OGGDecoder with non-existent files
  60. func testOGGDecodeNonExistentFile() {
  61. let url = URL(fileURLWithPath: "/nonexistent/path/test.ogg")
  62. XCTAssertThrowsError(try OGGDecoder.decode(url: url)) { error in
  63. guard case OGGDecoder.OGGError.failedToOpen = error else {
  64. XCTFail("Expected failedToOpen error, got \(error)")
  65. return
  66. }
  67. }
  68. }
  69. func testOGGDurationNonExistentFile() {
  70. let url = URL(fileURLWithPath: "/nonexistent/path/test.ogg")
  71. let duration = OGGDecoder.duration(url: url)
  72. XCTAssertEqual(duration, 0)
  73. }
  74. func testOGGFileInfoNonExistentFile() {
  75. let url = URL(fileURLWithPath: "/nonexistent/path/test.ogg")
  76. let info = OGGDecoder.fileInfo(url: url)
  77. XCTAssertNil(info)
  78. }
  79. // MARK: - OpusDecoder with non-existent files
  80. #if !DISABLE_OPUS
  81. func testOpusDecodeNonExistentFile() {
  82. let url = URL(fileURLWithPath: "/nonexistent/path/test.opus")
  83. XCTAssertThrowsError(try OpusDecoder.decode(url: url)) { error in
  84. guard case OpusDecoder.OpusError.failedToOpen = error else {
  85. XCTFail("Expected failedToOpen error, got \(error)")
  86. return
  87. }
  88. }
  89. }
  90. func testOpusDurationNonExistentFile() {
  91. let url = URL(fileURLWithPath: "/nonexistent/path/test.opus")
  92. let duration = OpusDecoder.duration(url: url)
  93. XCTAssertEqual(duration, 0)
  94. }
  95. func testOpusFileInfoNonExistentFile() {
  96. let url = URL(fileURLWithPath: "/nonexistent/path/test.opus")
  97. let info = OpusDecoder.fileInfo(url: url)
  98. XCTAssertNil(info)
  99. }
  100. #endif
  101. // MARK: - OGG convertToCAF error path
  102. func testOGGConvertToCAFNonExistentFile() {
  103. let url = URL(fileURLWithPath: "/nonexistent/path/test.ogg")
  104. XCTAssertThrowsError(try OGGDecoder.convertToCAF(url: url))
  105. }
  106. // MARK: - Metadata Service format detection with codecs
  107. func testOpusIsSupportedAudioFile() {
  108. XCTAssertTrue(MetadataService.isSupportedAudioFile(URL(fileURLWithPath: "/test.opus")))
  109. }
  110. func testOGGIsSupportedAudioFile() {
  111. XCTAssertTrue(MetadataService.isSupportedAudioFile(URL(fileURLWithPath: "/test.ogg")))
  112. }
  113. }