remove NoOpCaches in favor of CacheManagementService

This commit is contained in:
Hazelnoot 2025-10-01 11:48:56 -04:00
parent c8976fef94
commit 3f950e398b
10 changed files with 240 additions and 554 deletions

View file

@ -8,8 +8,9 @@ process.env.NODE_ENV = 'test';
import { jest } from '@jest/globals';
import { ModuleMocker } from 'jest-mock';
import { Test } from '@nestjs/testing';
import { NoOpCacheService } from '../misc/noOpCaches.js';
import { FakeInternalEventService } from '../misc/FakeInternalEventService.js';
import { FakeCacheManagementService } from '../misc/FakeCacheManagementService.js';
import { MockInternalEventService } from '../misc/MockInternalEventService.js';
import { CacheManagementService } from '@/core/CacheManagementService.js';
import { GlobalModule } from '@/GlobalModule.js';
import { AnnouncementService } from '@/core/AnnouncementService.js';
import { AnnouncementEntityService } from '@/core/entities/AnnouncementEntityService.js';
@ -23,11 +24,11 @@ import type {
} from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { genAidx } from '@/misc/id/aidx.js';
import { CacheService } from '@/core/CacheService.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
import { RoleService } from '@/core/RoleService.js';
import { CoreModule } from '@/core/CoreModule.js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import type { TestingModule } from '@nestjs/testing';
import type { MockFunctionMetadata } from 'jest-mock';
@ -42,7 +43,7 @@ describe('AnnouncementService', () => {
let announcementReadsRepository: AnnouncementReadsRepository;
let globalEventService: jest.Mocked<GlobalEventService>;
let moderationLogService: jest.Mocked<ModerationLogService>;
let roleService: jest.Mocked<RoleService>;
let cacheManagementService: CacheManagementService;
function createUser(data: Partial<MiUser> = {}) {
const un = secureRndstr(16);
@ -66,20 +67,11 @@ describe('AnnouncementService', () => {
.then(x => announcementsRepository.findOneByOrFail(x.identifiers[0]));
}
beforeEach(async () => {
beforeAll(async () => {
app = await Test.createTestingModule({
imports: [
GlobalModule,
],
providers: [
AnnouncementService,
AnnouncementEntityService,
CacheService,
IdService,
InternalEventService,
GlobalEventService,
ModerationLogService,
RoleService,
CoreModule,
],
})
.useMocker((token) => {
@ -99,19 +91,27 @@ describe('AnnouncementService', () => {
.overrideProvider(RoleService).useValue({
getUserRoles: jest.fn((_) => []),
})
.overrideProvider(InternalEventService).useClass(FakeInternalEventService)
.overrideProvider(CacheService).useClass(NoOpCacheService)
// TODO should we remove this now that cache is cleared?
.overrideProvider(InternalEventService).useClass(MockInternalEventService)
.overrideProvider(CacheManagementService).useClass(FakeCacheManagementService)
.compile();
await app.init();
app.enableShutdownHooks();
});
afterAll(async () => {
await app.close();
});
beforeEach(() => {
announcementService = app.get<AnnouncementService>(AnnouncementService);
usersRepository = app.get<UsersRepository>(DI.usersRepository);
announcementsRepository = app.get<AnnouncementsRepository>(DI.announcementsRepository);
announcementReadsRepository = app.get<AnnouncementReadsRepository>(DI.announcementReadsRepository);
globalEventService = app.get<GlobalEventService>(GlobalEventService) as jest.Mocked<GlobalEventService>;
moderationLogService = app.get<ModerationLogService>(ModerationLogService) as jest.Mocked<ModerationLogService>;
roleService = app.get<RoleService>(RoleService) as jest.Mocked<RoleService>;
cacheManagementService = app.get(CacheManagementService);
});
afterEach(async () => {
@ -121,8 +121,10 @@ describe('AnnouncementService', () => {
announcementsRepository.delete({}),
announcementReadsRepository.delete({}),
]);
await app.close();
moderationLogService.log.mockReset();
globalEventService.publishMainStream.mockReset();
globalEventService.publishBroadcastStream.mockReset();
cacheManagementService.clear();
});
describe('getUnreadAnnouncements', () => {