forked from b00tc4mp/isdi-parttime-202403
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renamed streamTrack to stream and updated its references b00tc4mp#175
- Loading branch information
1 parent
27e9db2
commit 7a75b0b
Showing
6 changed files
with
32 additions
and
32 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
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
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
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
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
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 |
---|---|---|
|
@@ -7,15 +7,15 @@ import { mkdir, rm, writeFile } from 'fs/promises'; | |
import path from 'path'; | ||
import { Track, User, Log } from '../../../data/index.js'; | ||
import { CredentialError, InvalidArgumentError, NotFoundError, SystemError } from 'com/errors.js'; | ||
import streamTrack from '../../../services/streamTrack.js'; | ||
import stream from '../../../services/stream.js'; | ||
import constants from 'com/constants.js'; | ||
|
||
// WARN: Change TRACK_DIR to test with the test track directory to not work with "production" data | ||
const { MONGO_TEST_URI, TRACK_DIR } = process.env; | ||
chai.use(chaiAsPromised); | ||
const expect = chai.expect; | ||
|
||
describe('streamTrack', () => { | ||
describe('stream', () => { | ||
const testTrackId = new mongoose.Types.ObjectId().toString(); | ||
const testTrackPath = path.join(TRACK_DIR, testTrackId); | ||
|
||
|
@@ -39,7 +39,7 @@ describe('streamTrack', () => { | |
|
||
await writeFile(testTrackPath, 'a'.repeat(1000), 'utf8'); | ||
|
||
const streamInfo = await expect(streamTrack(user.id, track.id)).to.be.fulfilled.and.to.eventually.be.a('object'); | ||
const streamInfo = await expect(stream(user.id, track.id)).to.be.fulfilled.and.to.eventually.be.a('object'); | ||
expect(streamInfo).to.have.property('contentRange').that.equals('bytes 0-999/1000'); | ||
expect(streamInfo).to.have.property('contentLength').that.equals(1000); | ||
expect(streamInfo).to.have.property('contentType').that.equals('audio/mpeg'); | ||
|
@@ -58,7 +58,7 @@ describe('streamTrack', () => { | |
|
||
await writeFile(testTrackPath, 'a'.repeat(1500), 'utf8'); | ||
|
||
const streamInfo = await expect(streamTrack(user.id, track.id, 'bytes=0-500')).to.be.fulfilled.and.to.eventually.be.a('object'); | ||
const streamInfo = await expect(stream(user.id, track.id, 'bytes=0-500')).to.be.fulfilled.and.to.eventually.be.a('object'); | ||
expect(streamInfo).to.have.property('contentRange').that.equals('bytes 0-500/1500'); | ||
expect(streamInfo).to.have.property('contentLength').that.equals(501); | ||
expect(streamInfo).to.have.property('contentType').that.equals('audio/mpeg'); | ||
|
@@ -77,7 +77,7 @@ describe('streamTrack', () => { | |
|
||
await writeFile(testTrackPath, 'a'.repeat(750), 'utf8'); | ||
|
||
const streamInfo = await expect(streamTrack(user.id, track.id, 'bytes=200-')).to.be.fulfilled.and.to.eventually.be.a('object'); | ||
const streamInfo = await expect(stream(user.id, track.id, 'bytes=200-')).to.be.fulfilled.and.to.eventually.be.a('object'); | ||
expect(streamInfo).to.have.property('contentRange').that.equals('bytes 200-749/750'); | ||
expect(streamInfo).to.have.property('contentLength').that.equals(550); | ||
expect(streamInfo).to.have.property('contentType').that.equals('audio/mpeg'); | ||
|
@@ -96,7 +96,7 @@ describe('streamTrack', () => { | |
|
||
await writeFile(testTrackPath, 'a'.repeat(921), 'utf8'); | ||
|
||
const streamInfo = await expect(streamTrack(user.id, track.id, 'bytes=-211')).to.be.fulfilled.and.to.eventually.be.a('object'); | ||
const streamInfo = await expect(stream(user.id, track.id, 'bytes=-211')).to.be.fulfilled.and.to.eventually.be.a('object'); | ||
expect(streamInfo).to.have.property('contentRange').that.equals('bytes 710-920/921'); | ||
expect(streamInfo).to.have.property('contentLength').that.equals(211); | ||
expect(streamInfo).to.have.property('contentType').that.equals('audio/mpeg'); | ||
|
@@ -113,22 +113,22 @@ describe('streamTrack', () => { | |
const user = await User.create({ username: 'eva02', email: '[email protected]', passwordHash: hash }); | ||
const track = await Track.create({ _id: testTrackId, addedBy: user.id, name: 'test_track_2024', duration: 60 }); | ||
|
||
await expect(streamTrack('66b2cebc5621e4111875102c', track.id)).to.be.rejectedWith(CredentialError, "User doesn't exist"); | ||
await expect(stream('66b2cebc5621e4111875102c', track.id)).to.be.rejectedWith(CredentialError, "User doesn't exist"); | ||
}); | ||
|
||
it('fails when the track is not found', async () => { | ||
const hash = await bcrypt.hash('Neon-Genesis02', 8); | ||
const user = await User.create({ username: 'eva02', email: '[email protected]', passwordHash: hash }); | ||
|
||
await expect(streamTrack(user.id, '66b2cebc5621e4111875102c')).to.be.rejectedWith(NotFoundError, 'Track not found'); | ||
await expect(stream(user.id, '66b2cebc5621e4111875102c')).to.be.rejectedWith(NotFoundError, 'Track not found'); | ||
}); | ||
|
||
it('fails when the track file is not found', async () => { | ||
const hash = await bcrypt.hash('Neon-Genesis02', 8); | ||
const user = await User.create({ username: 'eva02', email: '[email protected]', passwordHash: hash }); | ||
const track = await Track.create({ _id: testTrackId, addedBy: user.id, name: 'test_track_2024', duration: 60 }); | ||
|
||
await expect(streamTrack(user.id, track.id)).to.be.rejectedWith(NotFoundError, 'Track file not found'); | ||
await expect(stream(user.id, track.id)).to.be.rejectedWith(NotFoundError, 'Track file not found'); | ||
}); | ||
|
||
it('fails when the range exceeds the file size', async () => { | ||
|
@@ -138,7 +138,7 @@ describe('streamTrack', () => { | |
|
||
await writeFile(testTrackPath, 'a'.repeat(900), 'utf8'); | ||
|
||
await expect(streamTrack(user.id, track.id, 'bytes=0-1000')).to.be.rejectedWith(InvalidArgumentError, 'Range exceeds file size'); | ||
await expect(stream(user.id, track.id, 'bytes=0-1000')).to.be.rejectedWith(InvalidArgumentError, 'Range exceeds file size'); | ||
}); | ||
|
||
it('fails with SystemError on database error during user search', async () => { | ||
|
@@ -150,7 +150,7 @@ describe('streamTrack', () => { | |
throw new Error('Database connection error'); | ||
}; | ||
|
||
await expect(streamTrack(user.id, '66b2cebc5621e4111875102c')).to.be.rejectedWith(SystemError, 'Streaming the track failed: Database connection error'); | ||
await expect(stream(user.id, '66b2cebc5621e4111875102c')).to.be.rejectedWith(SystemError, 'Streaming the track failed: Database connection error'); | ||
|
||
User.findById = findById; | ||
}); | ||
|
@@ -165,49 +165,49 @@ describe('streamTrack', () => { | |
throw new Error('Database connection error'); | ||
}; | ||
|
||
await expect(streamTrack(user.id, track.id)).to.be.rejectedWith(SystemError, 'Streaming the track failed: Database connection error'); | ||
await expect(stream(user.id, track.id)).to.be.rejectedWith(SystemError, 'Streaming the track failed: Database connection error'); | ||
|
||
Track.findById = findById; | ||
}); | ||
|
||
it('fails when all fields are empty', () => { | ||
expect(() => streamTrack('', '')).to.throw(InvalidArgumentError, 'All inputs are required'); | ||
expect(() => stream('', '')).to.throw(InvalidArgumentError, 'All inputs are required'); | ||
}); | ||
|
||
it('fails when not provided with a user id', () => { | ||
expect(() => streamTrack('', '66b2cebc5621e4111875102c')).to.throw(InvalidArgumentError, ''); | ||
expect(() => stream('', '66b2cebc5621e4111875102c')).to.throw(InvalidArgumentError, ''); | ||
}); | ||
|
||
it('fails when not provided with a track id', () => { | ||
expect(() => streamTrack('66b2cebc5621e4111875102c', '')).to.throw(InvalidArgumentError, ''); | ||
expect(() => stream('66b2cebc5621e4111875102c', '')).to.throw(InvalidArgumentError, ''); | ||
}); | ||
|
||
it('fails when the user id is invalid', () => { | ||
expect(() => streamTrack('66b2cebc5621e4111875102X', '66b2cebc5621e4111875102c')).to.throw(InvalidArgumentError, 'Invalid ObjectId'); | ||
expect(() => stream('66b2cebc5621e4111875102X', '66b2cebc5621e4111875102c')).to.throw(InvalidArgumentError, 'Invalid ObjectId'); | ||
}); | ||
|
||
it('fails when the track id is invalid', () => { | ||
expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102X')).to.throw(InvalidArgumentError, 'Invalid ObjectId'); | ||
expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102X')).to.throw(InvalidArgumentError, 'Invalid ObjectId'); | ||
}); | ||
|
||
it('fails when the range is provided but incorrect (number)', () => { | ||
expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 200)).to.throw(InvalidArgumentError, 'Invalid range format'); | ||
expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 200)).to.throw(InvalidArgumentError, 'Invalid range format'); | ||
}); | ||
|
||
it('fails when the range is provided but incorrect (Range start value)', () => { | ||
expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=undefined-200')).to.throw(InvalidArgumentError, 'Invalid range start value'); | ||
expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=undefined-200')).to.throw(InvalidArgumentError, 'Invalid range start value'); | ||
}); | ||
|
||
it('fails when the range is provided but incorrect (Range end value)', () => { | ||
expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=0-undefined')).to.throw(InvalidArgumentError, 'Invalid range end value'); | ||
expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=0-undefined')).to.throw(InvalidArgumentError, 'Invalid range end value'); | ||
}); | ||
|
||
it('fails when the range is provided but incorrect !(Range >= 0)', () => { | ||
expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=-1-100')).to.throw(InvalidArgumentError, 'Invalid range format'); | ||
expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=-1-100')).to.throw(InvalidArgumentError, 'Invalid range format'); | ||
}); | ||
|
||
it('fails when the range is provided but incorrect (Range start value > Range end value)', () => { | ||
expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=200-100')).to.throw(InvalidArgumentError, 'Invalid range. End value must be >= start value'); | ||
expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=200-100')).to.throw(InvalidArgumentError, 'Invalid range. End value must be >= start value'); | ||
}); | ||
|
||
after(async () => { | ||
|