From 4ef77f4bf440fdc651d4dc05ce644e069a3a2127 Mon Sep 17 00:00:00 2001 From: Felipe Young <121059628+fyoungqualabs@users.noreply.github.com> Date: Thu, 22 Feb 2024 14:45:53 -0300 Subject: [PATCH] test: add more ID3 tests see: #74 --------- Signed-off-by: Felipe Young Co-authored-by: Fernando Cuadro Signed-off-by: Casey Occhialini <1508707+littlespex@users.noreply.github.com> --- CHANGELOG.md | 3 +++ lib/src/id3/getId3Frames.ts | 9 +++++--- lib/test/id3/decodeId3TextFrame.test.ts | 23 +++++++++++++++++++ lib/test/id3/isId3Footer.test.ts | 30 +++++++++++++++++++++++++ lib/test/id3/isId3Header.test.ts | 30 +++++++++++++++++++++++++ lib/test/id3/readId3Size.test.ts | 10 +++++++++ 6 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 lib/test/id3/decodeId3TextFrame.test.ts create mode 100644 lib/test/id3/isId3Footer.test.ts create mode 100644 lib/test/id3/isId3Header.test.ts create mode 100644 lib/test/id3/readId3Size.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e9b3b1d..7d2435de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Unit tests for id3 feature helper functions [#74](https://github.com/streaming-video-technology-alliance/common-media-library/pull/74) + ## [0.6.2] - 2023-01-18 diff --git a/lib/src/id3/getId3Frames.ts b/lib/src/id3/getId3Frames.ts index abdc7f5f..f3f7f05f 100644 --- a/lib/src/id3/getId3Frames.ts +++ b/lib/src/id3/getId3Frames.ts @@ -6,6 +6,9 @@ import { isId3Footer } from './util/isId3Footer.js'; import { isId3Header } from './util/isId3Header.js'; import { readId3Size } from './util/readId3Size.js'; +const HEADER_FOOTER_SIZE = 10; +const FRAME_SIZE = 10; + /** * Returns an array of ID3 frames found in all the ID3 tags in the id3Data * @@ -24,10 +27,10 @@ export function getId3Frames(id3Data: Uint8Array): Id3Frame[] { while (isId3Header(id3Data, offset)) { const size = readId3Size(id3Data, offset + 6); // skip past ID3 header - offset += 10; + offset += HEADER_FOOTER_SIZE; const end = offset + size; // loop through frames in the ID3 tag - while (offset + 8 < end) { + while (offset + FRAME_SIZE < end) { const frameData: RawId3Frame = getId3FrameData(id3Data.subarray(offset)); const frame: Id3Frame | undefined = decodeId3Frame(frameData); if (frame) { @@ -39,7 +42,7 @@ export function getId3Frames(id3Data: Uint8Array): Id3Frame[] { } if (isId3Footer(id3Data, offset)) { - offset += 10; + offset += HEADER_FOOTER_SIZE; } } diff --git a/lib/test/id3/decodeId3TextFrame.test.ts b/lib/test/id3/decodeId3TextFrame.test.ts new file mode 100644 index 00000000..a8db4b2f --- /dev/null +++ b/lib/test/id3/decodeId3TextFrame.test.ts @@ -0,0 +1,23 @@ +import { equal } from 'node:assert'; +import { describe, it } from 'node:test'; +import { decodeId3TextFrame } from '../../src/id3/util/decodeId3TextFrame.js'; +import { Id3Frame } from '../../src/id3'; + +describe('decodeId3TextFrame', () => { + it('should decode a TXXX frame', () => { + const frame = { + type: 'TXXX', + data: new Uint8Array([0, 102, 111, 111, 0, 97, 98, 99]), + size: 2, // required by the decodeTextFrame function + }; + + const testables = { + decodeId3TextFrame: decodeId3TextFrame, + }; + + const result: Id3Frame | undefined = testables.decodeId3TextFrame(frame); + equal(result!.key, 'TXXX'); + equal(result!.info, 'foo'); + equal(result!.data, 'abc'); + }); +}); diff --git a/lib/test/id3/isId3Footer.test.ts b/lib/test/id3/isId3Footer.test.ts new file mode 100644 index 00000000..83805a78 --- /dev/null +++ b/lib/test/id3/isId3Footer.test.ts @@ -0,0 +1,30 @@ +import { equal } from 'node:assert'; +import { describe, it } from 'node:test'; +import { isId3Footer } from '../../src/id3/util/isId3Footer.js'; + +const LEADING_BYTE_SIZE = 8; +const TRAILING_BYTE_SIZE = 8; + +describe('isId3Footer', () => { + const mockID3Footer = Uint8Array.from([ + 0x33, 0x44, 0x49, 4, 0, 0, 0, 0, 0, 63, 80, 82, 73, 86, 0, 0, 0, 53, 0, 0, + 99, 111, 109, 46, 97, 112, 112, 108, 101, 46, 115, 116, 114, 101, 97, 109, + 105, 110, 103, 46, 116, 114, 97, 110, 115, 112, 111, 114, 116, 83, 116, 114, + 101, 97, 109, 84, 105, 109, 101, 115, 116, 97, 109, 112, 0, 0, 0, 0, 0, 0, + 13, 198, 135, + ]); + const mockID3FooterMissingLeadingByte = mockID3Footer.slice( + LEADING_BYTE_SIZE, + mockID3Footer.length + ); + const mockID3FooterMissingTrailingByte = mockID3Footer.slice( + 0, + mockID3Footer.length - TRAILING_BYTE_SIZE + ); + + it('Properly parses ID3 Footers', () => { + equal(isId3Footer(mockID3Footer, 0), true); + equal(isId3Footer(mockID3FooterMissingLeadingByte, 0), false); + equal(isId3Footer(mockID3FooterMissingTrailingByte, 0), true); + }); +}); diff --git a/lib/test/id3/isId3Header.test.ts b/lib/test/id3/isId3Header.test.ts new file mode 100644 index 00000000..09b1d063 --- /dev/null +++ b/lib/test/id3/isId3Header.test.ts @@ -0,0 +1,30 @@ +import { equal } from 'node:assert'; +import { describe, it } from 'node:test'; +import { isId3Header } from '../../src/id3/util/isId3Header.js'; + +const LEADING_BYTE_SIZE = 8; +const TRAILING_BYTE_SIZE = 8; + +describe('isId3Header', () => { + const mockID3Header = Uint8Array.from([ + 73, 68, 51, 4, 0, 0, 0, 0, 0, 63, 80, 82, 73, 86, 0, 0, 0, 53, 0, 0, 99, + 111, 109, 46, 97, 112, 112, 108, 101, 46, 115, 116, 114, 101, 97, 109, 105, + 110, 103, 46, 116, 114, 97, 110, 115, 112, 111, 114, 116, 83, 116, 114, 101, + 97, 109, 84, 105, 109, 101, 115, 116, 97, 109, 112, 0, 0, 0, 0, 0, 0, 13, + 198, 135, + ]); + const mockID3HeaderMissingLeadingByte = mockID3Header.slice( + LEADING_BYTE_SIZE, + mockID3Header.length + ); + const mockID3HeaderMissingTrailingByte = mockID3Header.slice( + 0, + mockID3Header.length - TRAILING_BYTE_SIZE + ); + + it('Properly parses ID3 Headers', () => { + equal(isId3Header(mockID3Header, 0), true); + equal(isId3Header(mockID3HeaderMissingLeadingByte, 0), false); + equal(isId3Header(mockID3HeaderMissingTrailingByte, 0), true); + }); +}); diff --git a/lib/test/id3/readId3Size.test.ts b/lib/test/id3/readId3Size.test.ts new file mode 100644 index 00000000..0d7bfc0e --- /dev/null +++ b/lib/test/id3/readId3Size.test.ts @@ -0,0 +1,10 @@ +import { equal } from 'node:assert'; +import { describe, it } from 'node:test'; +import { readId3Size } from '../../src/id3/util/readId3Size.js'; + +describe('readId3Size', () => { + const mockID3SizeData = Uint8Array.from([73, 68, 51, 4, 0, 0, 0, 7, 1, 0]); + it('returns Id3 size', () => { + equal(readId3Size(mockID3SizeData, 6), 114816); + }); +});