refactor(server): event emits (#10648)

* refactor(server): event emits

* refactor: change default priority to 0
This commit is contained in:
Jason Rasmussen 2024-06-27 15:54:20 -04:00 committed by GitHub
parent 7e99394c70
commit 72bf9439b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 222 additions and 171 deletions

View file

@ -69,11 +69,11 @@ describe(LibraryService.name, () => {
expect(sut).toBeDefined();
});
describe('init', () => {
describe('onBootstrapEvent', () => {
it('should init cron job and subscribe to config changes', async () => {
systemMock.get.mockResolvedValue(systemConfigStub.libraryScan);
await sut.init();
await sut.onBootstrapEvent();
expect(systemMock.get).toHaveBeenCalled();
expect(jobMock.addCronJob).toHaveBeenCalled();
@ -105,7 +105,7 @@ describe(LibraryService.name, () => {
),
);
await sut.init();
await sut.onBootstrapEvent();
expect(storageMock.watch.mock.calls).toEqual(
expect.arrayContaining([
@ -118,7 +118,7 @@ describe(LibraryService.name, () => {
it('should not initialize watcher when watching is disabled', async () => {
systemMock.get.mockResolvedValue(systemConfigStub.libraryWatchDisabled);
await sut.init();
await sut.onBootstrapEvent();
expect(storageMock.watch).not.toHaveBeenCalled();
});
@ -127,16 +127,16 @@ describe(LibraryService.name, () => {
systemMock.get.mockResolvedValue(systemConfigStub.libraryWatchEnabled);
databaseMock.tryLock.mockResolvedValue(false);
await sut.init();
await sut.onBootstrapEvent();
expect(storageMock.watch).not.toHaveBeenCalled();
});
});
describe('onValidateConfig', () => {
describe('onConfigValidateEvent', () => {
it('should allow a valid cron expression', () => {
expect(() =>
sut.onValidateConfig({
sut.onConfigValidateEvent({
newConfig: { library: { scan: { cronExpression: '0 0 * * *' } } } as SystemConfig,
oldConfig: {} as SystemConfig,
}),
@ -145,7 +145,7 @@ describe(LibraryService.name, () => {
it('should fail for an invalid cron expression', () => {
expect(() =>
sut.onValidateConfig({
sut.onConfigValidateEvent({
newConfig: { library: { scan: { cronExpression: 'foo' } } } as SystemConfig,
oldConfig: {} as SystemConfig,
}),
@ -730,7 +730,7 @@ describe(LibraryService.name, () => {
const mockClose = vitest.fn();
storageMock.watch.mockImplementation(makeMockWatcher({ close: mockClose }));
await sut.init();
await sut.onBootstrapEvent();
await sut.delete(libraryStub.externalLibraryWithImportPaths1.id);
expect(mockClose).toHaveBeenCalled();
@ -861,7 +861,7 @@ describe(LibraryService.name, () => {
libraryMock.get.mockResolvedValue(libraryStub.externalLibraryWithImportPaths1);
libraryMock.getAll.mockResolvedValue([]);
await sut.init();
await sut.onBootstrapEvent();
await sut.create({
ownerId: authStub.admin.user.id,
importPaths: libraryStub.externalLibraryWithImportPaths1.importPaths,
@ -917,7 +917,7 @@ describe(LibraryService.name, () => {
systemMock.get.mockResolvedValue(systemConfigStub.libraryWatchEnabled);
libraryMock.getAll.mockResolvedValue([]);
await sut.init();
await sut.onBootstrapEvent();
});
it('should update library', async () => {
@ -933,7 +933,7 @@ describe(LibraryService.name, () => {
beforeEach(async () => {
systemMock.get.mockResolvedValue(systemConfigStub.libraryWatchDisabled);
await sut.init();
await sut.onBootstrapEvent();
});
it('should not watch library', async () => {
@ -949,7 +949,7 @@ describe(LibraryService.name, () => {
beforeEach(async () => {
systemMock.get.mockResolvedValue(systemConfigStub.libraryWatchEnabled);
libraryMock.getAll.mockResolvedValue([]);
await sut.init();
await sut.onBootstrapEvent();
});
it('should watch library', async () => {
@ -1107,8 +1107,8 @@ describe(LibraryService.name, () => {
const mockClose = vitest.fn();
storageMock.watch.mockImplementation(makeMockWatcher({ close: mockClose }));
await sut.init();
await sut.teardown();
await sut.onBootstrapEvent();
await sut.onShutdownEvent();
expect(mockClose).toHaveBeenCalledTimes(2);
});