-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚚 Exchange unit and integration tests
- Loading branch information
Showing
2 changed files
with
93 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,82 @@ | ||
import { describe, it, expect, jest } from '@jest/globals' | ||
import { describe, it, expect } from '@jest/globals' | ||
|
||
import * as execa from 'execa' | ||
import * as fs from 'fs' | ||
import { resolve as resolvePath } from 'path' | ||
|
||
import getDuration from '../src' | ||
|
||
jest.mock('execa', () => | ||
jest.fn().mockResolvedValue({ | ||
stdout: 'duration="42.0"', | ||
} as never) | ||
) | ||
jest.mock('is-stream', () => jest.fn().mockReturnValue(false)) | ||
import getDuration, { getAudioDurationInSeconds } from '../src' | ||
|
||
const expectedOGGAudioDuration = 33 | ||
const expectedFLACAudioDuration = 97 | ||
const expectedWAVAudioDuration = 33 | ||
const expectedAudioDurationThreshold = -1 | ||
|
||
const sampleOGGFilePath = resolvePath(__dirname, './Rayman_2_music_sample.ogg') | ||
const sampleOGGWithSpacesFilePath = resolvePath( | ||
__dirname, | ||
'./Rayman 2 music sample.ogg' | ||
) | ||
const sampleFLACFilePath = resolvePath( | ||
__dirname, | ||
'./2L-125_stereo-44k-16b_04.flac' | ||
) | ||
const sampleWAVFilePath = resolvePath(__dirname, './file_example_WAV_1MG.wav') | ||
|
||
describe('get-audio-duration', () => { | ||
describe('when using a file path', () => { | ||
it('Should use overriden ffprobe when provided', async () => { | ||
const durationPromise = getDuration( | ||
'fake file', | ||
'the overriden path to ffprobe' | ||
) | ||
it('Should export function under named export, too', () => { | ||
expect(getDuration).toEqual(getAudioDurationInSeconds) | ||
}) | ||
|
||
expect(execa).toHaveBeenCalledWith( | ||
'the overriden path to ffprobe', | ||
expect.anything() | ||
) | ||
const getReadableStreamAsInput = (pathToFile: string) => | ||
fs.createReadStream(pathToFile) | ||
const getFilePathAsInput = (pathToFile: string) => pathToFile | ||
|
||
await expect(durationPromise).resolves.toBeCloseTo( | ||
42.0, | ||
describe.each` | ||
description | getInput | isOGGSupported | ||
${'readable stream'} | ${getReadableStreamAsInput} | ${false} | ||
${'file path'} | ${getFilePathAsInput} | ${true} | ||
`('when using a $description', ({ getInput, isOGGSupported }) => { | ||
it('Should return proper duration for flac files', async () => { | ||
const input = getInput(sampleFLACFilePath) | ||
const duration = await getDuration(input) | ||
expect(duration).toBeCloseTo( | ||
expectedFLACAudioDuration, | ||
expectedAudioDurationThreshold | ||
) | ||
}) | ||
}) | ||
|
||
describe('when input is invalid', () => { | ||
it('Should throw an error', async () => { | ||
await expect(getDuration(42 as unknown as string)).rejects.toThrowError( | ||
'Given input was neither a string nor a Stream' | ||
it.skip('Should return proper duration for wav files', async () => { | ||
const input = getInput(sampleWAVFilePath) | ||
const duration = await getDuration(input) | ||
expect(duration).toBeCloseTo( | ||
expectedWAVAudioDuration, | ||
expectedAudioDurationThreshold | ||
) | ||
}) | ||
|
||
it('Should throw an error if not an audio stream', async () => { | ||
const input = getInput(resolvePath(__dirname, __filename)) | ||
const durationPromise = getDuration(input) | ||
await expect(durationPromise).rejects.toThrowError() | ||
}) | ||
|
||
if (isOGGSupported) { | ||
it('Should return proper duration for OGG files', async () => { | ||
const input = getInput(sampleOGGFilePath) | ||
const duration = await getDuration(input) | ||
expect(duration).toBeCloseTo( | ||
expectedOGGAudioDuration, | ||
expectedAudioDurationThreshold | ||
) | ||
}) | ||
|
||
it('Should return proper duration if file contains spaces', async () => { | ||
const input = getInput(sampleOGGWithSpacesFilePath) | ||
const duration = await getDuration(input) | ||
expect(duration).toBeCloseTo( | ||
expectedOGGAudioDuration, | ||
expectedAudioDurationThreshold | ||
) | ||
}) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,43 @@ | ||
import { describe, it, expect } from '@jest/globals' | ||
import { describe, it, expect, jest } from '@jest/globals' | ||
|
||
import * as fs from 'fs' | ||
import { resolve as resolvePath } from 'path' | ||
import * as execa from 'execa' | ||
|
||
import getDuration, { getAudioDurationInSeconds } from '../src' | ||
import getDuration from '../src' | ||
|
||
const expectedOGGAudioDuration = 33 | ||
const expectedFLACAudioDuration = 97 | ||
const expectedWAVAudioDuration = 33 | ||
const expectedAudioDurationThreshold = -1 | ||
|
||
const sampleOGGFilePath = resolvePath(__dirname, './Rayman_2_music_sample.ogg') | ||
const sampleOGGWithSpacesFilePath = resolvePath( | ||
__dirname, | ||
'./Rayman 2 music sample.ogg' | ||
jest.mock('execa', () => | ||
jest.fn().mockResolvedValue({ | ||
stdout: 'duration="42.0"', | ||
} as never) | ||
) | ||
const sampleFLACFilePath = resolvePath( | ||
__dirname, | ||
'./2L-125_stereo-44k-16b_04.flac' | ||
) | ||
const sampleWAVFilePath = resolvePath(__dirname, './file_example_WAV_1MG.wav') | ||
jest.mock('is-stream', () => jest.fn().mockReturnValue(false)) | ||
|
||
describe('get-audio-duration', () => { | ||
it('Should export function under named export, too', () => { | ||
expect(getDuration).toEqual(getAudioDurationInSeconds) | ||
}) | ||
const expectedAudioDurationThreshold = -1 | ||
|
||
const getReadableStreamAsInput = (pathToFile: string) => | ||
fs.createReadStream(pathToFile) | ||
const getFilePathAsInput = (pathToFile: string) => pathToFile | ||
describe('get-audio-duration', () => { | ||
describe('when using a file path', () => { | ||
it('Should use overriden ffprobe when provided', async () => { | ||
const durationPromise = getDuration( | ||
'fake file', | ||
'the overriden path to ffprobe' | ||
) | ||
|
||
describe.each` | ||
description | getInput | isOGGSupported | ||
${'readable stream'} | ${getReadableStreamAsInput} | ${false} | ||
${'file path'} | ${getFilePathAsInput} | ${true} | ||
`('when using a $description', ({ getInput, isOGGSupported }) => { | ||
it('Should return proper duration for flac files', async () => { | ||
const input = getInput(sampleFLACFilePath) | ||
const duration = await getDuration(input) | ||
expect(duration).toBeCloseTo( | ||
expectedFLACAudioDuration, | ||
expectedAudioDurationThreshold | ||
expect(execa).toHaveBeenCalledWith( | ||
'the overriden path to ffprobe', | ||
expect.anything() | ||
) | ||
}) | ||
|
||
it.skip('Should return proper duration for wav files', async () => { | ||
const input = getInput(sampleWAVFilePath) | ||
const duration = await getDuration(input) | ||
expect(duration).toBeCloseTo( | ||
expectedWAVAudioDuration, | ||
await expect(durationPromise).resolves.toBeCloseTo( | ||
42.0, | ||
expectedAudioDurationThreshold | ||
) | ||
}) | ||
}) | ||
|
||
it('Should throw an error if not an audio stream', async () => { | ||
const input = getInput(resolvePath(__dirname, __filename)) | ||
const durationPromise = getDuration(input) | ||
await expect(durationPromise).rejects.toThrowError() | ||
describe('when input is invalid', () => { | ||
it('Should throw an error', async () => { | ||
await expect(getDuration(42 as unknown as string)).rejects.toThrowError( | ||
'Given input was neither a string nor a Stream' | ||
) | ||
}) | ||
|
||
if (isOGGSupported) { | ||
it('Should return proper duration for OGG files', async () => { | ||
const input = getInput(sampleOGGFilePath) | ||
const duration = await getDuration(input) | ||
expect(duration).toBeCloseTo( | ||
expectedOGGAudioDuration, | ||
expectedAudioDurationThreshold | ||
) | ||
}) | ||
|
||
it('Should return proper duration if file contains spaces', async () => { | ||
const input = getInput(sampleOGGWithSpacesFilePath) | ||
const duration = await getDuration(input) | ||
expect(duration).toBeCloseTo( | ||
expectedOGGAudioDuration, | ||
expectedAudioDurationThreshold | ||
) | ||
}) | ||
} | ||
}) | ||
}) |