switched to yarn, and started adding tests
Added tests for process job service so far... its a start..
This commit is contained in:
@@ -67,5 +67,18 @@
|
||||
"objectLiteralTypeAssertions": "never"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"files": [
|
||||
"*.test.ts"
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/consistent-type-assertions": "off",
|
||||
"@typescript-eslint/explicit-function-return-type": "off",
|
||||
"@typescript-eslint/no-explicit-any": "off",
|
||||
"@typescript-eslint/no-unused-vars": "off"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
1
src/node/consumer/.nvmrc
Normal file
1
src/node/consumer/.nvmrc
Normal file
@@ -0,0 +1 @@
|
||||
v21.6.1
|
||||
11
src/node/consumer/jest.config.cjs
Normal file
11
src/node/consumer/jest.config.cjs
Normal file
@@ -0,0 +1,11 @@
|
||||
const { pathsToModuleNameMapper } = require('ts-jest');
|
||||
const { compilerOptions } = require('./tsconfig.json');
|
||||
|
||||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' }),
|
||||
modulePaths: [
|
||||
'<rootDir>'
|
||||
],
|
||||
};
|
||||
@@ -8,7 +8,9 @@
|
||||
"dev": "tsx watch --ignore node_modules src/main.ts | pino-pretty",
|
||||
"start": "node dist/main.cjs",
|
||||
"lint": "yarn eslint ./src --ext .ts,.js",
|
||||
"lint-fix": "yarn run lint --fix"
|
||||
"lint-fix": "yarn run lint --fix",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
@@ -33,6 +35,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/amqplib": "^0.10.4",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/magnet-uri": "^5.1.5",
|
||||
"@types/node": "^20.11.16",
|
||||
"@types/torrent-stream": "^0.0.9",
|
||||
@@ -43,11 +46,13 @@
|
||||
"eslint": "^8.56.0",
|
||||
"eslint-plugin-import": "^2.29.1",
|
||||
"eslint-plugin-import-helpers": "^1.3.1",
|
||||
"jest": "^29.7.0",
|
||||
"node-gyp": "^10.0.1",
|
||||
"pino-pretty": "^10.3.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"tsx": "^4.7.0",
|
||||
"typescript": "^5.3.3"
|
||||
"typescript": "^5.3.3",
|
||||
"ts-jest": "^29.1.2"
|
||||
}
|
||||
}
|
||||
|
||||
129
src/node/consumer/test/process_torrent_job.test.ts
Normal file
129
src/node/consumer/test/process_torrent_job.test.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import "reflect-metadata"; // required
|
||||
import { ILoggingService } from '@interfaces/logging_service';
|
||||
import { ITorrentProcessingService } from '@interfaces/torrent_processing_service';
|
||||
import { ProcessTorrentsJob } from '@jobs/process_torrents_job';
|
||||
import { configurationService } from '@services/configuration_service';
|
||||
import client, {ConsumeMessage} from 'amqplib';
|
||||
|
||||
jest.mock('@services/configuration_service', () => {
|
||||
return {
|
||||
configurationService: {
|
||||
rabbitConfig: {
|
||||
RABBIT_URI: 'amqp://localhost',
|
||||
QUEUE_NAME: 'test_queue',
|
||||
},
|
||||
jobConfig: {
|
||||
JOBS_ENABLED: true,
|
||||
JOB_CONCURRENCY: 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
jest.mock('amqplib', () => {
|
||||
return {
|
||||
connect: jest.fn().mockResolvedValue({
|
||||
createChannel: jest.fn().mockResolvedValue({
|
||||
assertQueue: jest.fn(),
|
||||
prefetch: jest.fn(),
|
||||
consume: jest.fn(),
|
||||
ack: jest.fn(),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
jest.mock('@services/logging_service', () => {
|
||||
return {
|
||||
error: jest.fn(),
|
||||
info: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
jest.mock('@services/torrent_processing_service', () => {
|
||||
return {
|
||||
processTorrentRecord: jest.fn().mockResolvedValue(undefined),
|
||||
}
|
||||
})
|
||||
|
||||
describe('ProcessTorrentsJob', () => {
|
||||
let processTorrentsJob: ProcessTorrentsJob,
|
||||
loggingService: ILoggingService,
|
||||
torrentProcessingService: ITorrentProcessingService;
|
||||
|
||||
beforeEach(() => {
|
||||
loggingService = jest.requireMock<ILoggingService>('@services/logging_service');
|
||||
torrentProcessingService = jest.requireMock('@services/torrent_processing_service');
|
||||
processTorrentsJob = new ProcessTorrentsJob(torrentProcessingService, loggingService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('listenToQueue', () => {
|
||||
test('should connect to the rabbitmq server and create a channel', async () => {
|
||||
await processTorrentsJob.listenToQueue();
|
||||
expect(client.connect).toHaveBeenCalledWith(configurationService.rabbitConfig.RABBIT_URI);
|
||||
});
|
||||
|
||||
test('should log an error if connection or channel setup fails', async () => {
|
||||
(client.connect as any).mockImplementationOnce(() => {
|
||||
throw new Error('Connection error');
|
||||
});
|
||||
|
||||
await processTorrentsJob.listenToQueue();
|
||||
expect(loggingService.error).toHaveBeenCalledWith('Failed to connect and setup channel', expect.any(Error));
|
||||
});
|
||||
|
||||
test('should process messages from the queue', async () => {
|
||||
const mockMessage = {
|
||||
content: Buffer.from(JSON.stringify({
|
||||
message: {
|
||||
name: 'test_name',
|
||||
source: 'test_source',
|
||||
category: 'test_category',
|
||||
infoHash: 'test_hash',
|
||||
size: 'test_size',
|
||||
seeders: 0,
|
||||
leechers: 0,
|
||||
imdb: 'test_imdb',
|
||||
processed: false,
|
||||
}
|
||||
})),
|
||||
} as ConsumeMessage;
|
||||
|
||||
(client.connect as any).mockResolvedValue({
|
||||
createChannel: jest.fn().mockResolvedValue({
|
||||
assertQueue: jest.fn().mockResolvedValue({
|
||||
consumerCount: 1,
|
||||
}),
|
||||
prefetch: jest.fn(),
|
||||
consume: jest.fn().mockImplementation((_, callback) => {
|
||||
callback(mockMessage);
|
||||
}),
|
||||
ack: jest.fn(),
|
||||
}),
|
||||
});
|
||||
|
||||
await processTorrentsJob.listenToQueue();
|
||||
expect(loggingService.info).toHaveBeenCalledWith('Worker is running! Waiting for new torrents...');
|
||||
expect(client.connect).toHaveBeenCalledWith(configurationService.rabbitConfig.RABBIT_URI);
|
||||
expect(loggingService.error).toBeCalledTimes(0);
|
||||
|
||||
expect(torrentProcessingService.processTorrentRecord).toHaveBeenCalledWith({
|
||||
name: 'test_name',
|
||||
source: 'test_source',
|
||||
category: 'test_category',
|
||||
infoHash: 'test_hash',
|
||||
size: 'test_size',
|
||||
seeders: 0,
|
||||
leechers: 0,
|
||||
imdb: 'test_imdb',
|
||||
processed: false,
|
||||
info_hash: 'test_hash'
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -31,6 +31,6 @@
|
||||
"@jobs/*": ["lib/jobs/*"]
|
||||
}
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"include": ["src", "test"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user