Prechádzať zdrojové kódy

fix(elasticsearch): refactor ElasticsearchDelegator tests for improved socket handling and reindexing options

Ryu Sato 2 týždňov pred
rodič
commit
4ae42bbc03

+ 10 - 15
apps/app/src/server/service/search-delegator/elasticsearch.integ.ts

@@ -1,28 +1,33 @@
 import { vi } from 'vitest';
 
 import { configManager } from '~/server/service/config-manager';
+import { SocketIoService } from '~/server/service/socket-io/socket-io';
 
 import { getInstance } from '../../../../test/setup/crowi';
+import type { RebuildIndexOption } from '../interfaces/search';
 import ElasticsearchDelegator from './elasticsearch';
 
 describe('ElasticsearchDelegator#init() with ELASTICSEARCH_REINDEX_ON_BOOT', () => {
   describe('when ELASTICSEARCH_REINDEX_ON_BOOT=true', () => {
     beforeAll(async () => {
+      process.env.ELASTICSEARCH_VERSION = '8';
       process.env.ELASTICSEARCH_REINDEX_ON_BOOT = 'true';
       await configManager.loadConfigs();
     });
     afterAll(() => {
+      delete process.env.ELASTICSEARCH_VERSION;
       delete process.env.ELASTICSEARCH_REINDEX_ON_BOOT;
     });
 
     it('should invoke rebuildIndex and complete without error', async () => {
       // arrange
       const crowi = await getInstance();
-      const delegator = new ElasticsearchDelegator(crowi.socketIoService);
+      // Use a SocketIoService without an attached HTTP server, as in actual boot.
+      // If rebuildIndex incorrectly emits progress, getAdminSocket() throws.
+      const socketIoService = new SocketIoService(crowi);
+      const delegator = new ElasticsearchDelegator(socketIoService);
       type WithRebuildIndex = {
-        rebuildIndex: (option?: {
-          shouldEmitProgress?: boolean;
-        }) => Promise<void>;
+        rebuildIndex: (option?: RebuildIndexOption) => Promise<void>;
       };
       const rebuildSpy = vi.spyOn(
         delegator as unknown as WithRebuildIndex,
@@ -34,19 +39,9 @@ describe('ElasticsearchDelegator#init() with ELASTICSEARCH_REINDEX_ON_BOOT', ()
 
       // assert
       expect(rebuildSpy).toHaveBeenCalledOnce();
-    }, 60_000);
-
-    it('should leave indices in normalized state', async () => {
-      // arrange
-      const crowi = await getInstance();
-      const delegator = new ElasticsearchDelegator(crowi.socketIoService);
-
-      // act
-      await delegator.init();
-
-      // assert
       const { isNormalized } = await delegator.getInfoForAdmin();
       expect(isNormalized).toBe(true);
+      await expect(rebuildSpy.mock.results[0].value).resolves.toBeUndefined();
     }, 60_000);
   });
 });