Skip to content

Commit

Permalink
test: add more ID3 tests
Browse files Browse the repository at this point in the history
see: streaming-video-technology-alliance#74

---------

Signed-off-by: Felipe Young <[email protected]>
Co-authored-by: Fernando Cuadro <[email protected]>
Signed-off-by: Casey Occhialini <[email protected]>
  • Loading branch information
2 people authored and littlespex committed Apr 5, 2024
1 parent c73c029 commit 4ef77f4
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions lib/src/id3/getId3Frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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) {
Expand All @@ -39,7 +42,7 @@ export function getId3Frames(id3Data: Uint8Array): Id3Frame[] {
}

if (isId3Footer(id3Data, offset)) {
offset += 10;
offset += HEADER_FOOTER_SIZE;
}
}

Expand Down
23 changes: 23 additions & 0 deletions lib/test/id3/decodeId3TextFrame.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
30 changes: 30 additions & 0 deletions lib/test/id3/isId3Footer.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
30 changes: 30 additions & 0 deletions lib/test/id3/isId3Header.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 10 additions & 0 deletions lib/test/id3/readId3Size.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 4ef77f4

Please sign in to comment.