Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isolomak committed Dec 6, 2022
1 parent 7320a38 commit a558adf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
20 changes: 9 additions & 11 deletions test/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as assert from 'assert';
import assert from 'assert';
import bencodec from 'bencodec';
import * as fs from 'fs';
import * as path from 'path';
import { create } from '../src';
import { BencodeDictionary } from 'bencodec/lib/types';
import fs from 'fs';
import path from 'path';
import { create } from '../src';

const deleteFolderRecursively = (filePath: string) => {
if (fs.existsSync(filePath)) {
Expand Down Expand Up @@ -37,16 +37,14 @@ describe('Create torrent file tests', () => {
describe('Validation tests', () => {

test('should throw error if outPath is invalid', async () => {
// tslint:disable-next-line: no-empty
let f = () => { };
try {
await create({
announceList: [],
source: './test/testSources/directorySource',
}, '/someRandom/directory/on/the/system');
} catch (err) {
await create({ announceList: [], source: './test/testSources/directorySource' }, '/someRandom/directory/on/the/system');
}
catch (err) {
f = () => { throw err; };
} finally {
}
finally {
assert.throws(f);
}
});
Expand Down
24 changes: 20 additions & 4 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as assert from 'assert';
import { parse } from '../src';
import assert from 'assert';
import bencodec from 'bencodec';
import crypto from 'crypto';
import { parse } from '../src';

describe('Parse tests', () => {

Expand Down Expand Up @@ -145,6 +145,7 @@ describe('Parse tests', () => {
const result = parse(bencodec.encode({
info: {
files: [
Buffer.from('test_string'),
{
length: 100500,
path: [ 'test_file_1.txt' ]
Expand All @@ -155,6 +156,10 @@ describe('Parse tests', () => {
{
path: [ 'test_file_2.txt' ]
},
{
path: [ 100500 ],
length: 100500,
}
]
}
}));
Expand All @@ -177,6 +182,17 @@ describe('Parse tests', () => {
assert.deepStrictEqual(result.files[0].path, 'test_file.txt');
});

test('should not add invalid file to files for single file torrent', () => {
const result = parse(bencodec.encode({
info: {
name: 100500,
length: 100500
}
}));

assert.deepStrictEqual(result.files.length, 0);
});

});

describe('InfoHash field tests', () => {
Expand Down Expand Up @@ -244,9 +260,9 @@ describe('Parse tests', () => {
assert.deepStrictEqual(result.private, false);
});

test('should return false no private field', () => {
test('should return null no private field', () => {
const result = parse(bencodec.encode({ info: { } }));
assert.deepStrictEqual(result.private, false);
assert.deepStrictEqual(result.private, null);
});

});
Expand Down
37 changes: 33 additions & 4 deletions test/parseFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import * as assert from 'assert';
import { parseFile } from '../src';
import assert from 'assert';
import { parseFromFileAsync, parseFromFileSync } from '../src';

describe('Parse file tests', () => {

test('should parse file', () => {
const result = parseFile('./test/test.torrent');
test('should parse file sync', () => {
const result = parseFromFileSync('./test/test.torrent');

assert.deepStrictEqual(result, {
announce: 'https://testtracker-1.net/testtopic.php?t=1111111',
announceList: [
'https://testtracker-1.net/testtopic.php?t=1111111',
'https://testtracker-2.net/testtopic.php?t=2222222',
'https://testtracker-3.net/testtopic.php?t=3333333'
],
comment: 'Test comment',
createdBy: 'Transmission/2.92 (14714)',
createdAt: 1585998070,
encoding: 'UTF-8',
files: [
{ length: 16, path: 'file_1.txt' },
{ length: 16, path: 'file_2.txt' }
],
infoHash: '46bf9dd414eae4cd74fa4d129d2f23553a21bca7',
name: 'test_torrent',
pieceLength: 32768,
pieces: [ Buffer.from('4d92fb3893cc6ed5fbd0392dda657459b4675605', 'hex') ],
private: true,
publisher: null,
publisherUrl: null,
totalLength: 32
});
});

test('should parse file async', async () => {
const result = await parseFromFileAsync('./test/test.torrent');

assert.deepStrictEqual(result, {
announce: 'https://testtracker-1.net/testtopic.php?t=1111111',
Expand Down

0 comments on commit a558adf

Please sign in to comment.