11import { describe , it , expect , vi , beforeEach , Mock } from 'vitest' ;
22import { handleElasticsearch } from './elasticsearch' ;
3- import { fetchElasticsearchAction , fetchElasticsearchStatistics } from '../api/elasticsearch' ;
3+ import {
4+ fetchElasticsearchAction ,
5+ fetchElasticsearchStatistics ,
6+ fetchElasticsearchHealthcheck ,
7+ } from '../api/elasticsearch' ;
48
59vi . mock ( '../api/elasticsearch' ) ;
610vi . mock ( '../../../../assets/src/utils' ) ;
@@ -16,8 +20,10 @@ describe('Elasticsearch Functions', () => {
1620 document . body . innerHTML = `
1721 <button class="pmf-elasticsearch" data-action="reindex">Reindex</button>
1822 <div id="pmf-elasticsearch-stats"></div>
23+ <div id="pmf-elasticsearch-healthcheck-alert"><span class="alert-message"></span></div>
1924 ` ;
2025
26+ ( fetchElasticsearchHealthcheck as Mock ) . mockResolvedValue ( { available : true , status : 'healthy' } ) ;
2127 ( fetchElasticsearchAction as Mock ) . mockResolvedValue ( { success : 'Reindexing started' } ) ;
2228 ( fetchElasticsearchStatistics as Mock ) . mockResolvedValue ( {
2329 index : 'test-index' ,
@@ -41,12 +47,14 @@ describe('Elasticsearch Functions', () => {
4147 expect ( fetchElasticsearchAction ) . toHaveBeenCalledWith ( 'reindex' ) ;
4248 } ) ;
4349
44- it ( 'should handle Elasticsearch statistics update' , async ( ) => {
50+ it ( 'should handle Elasticsearch statistics update when healthy ' , async ( ) => {
4551 document . body . innerHTML = `
4652 <button class="pmf-elasticsearch" data-action="reindex">Reindex</button>
4753 <div id="pmf-elasticsearch-stats"></div>
54+ <div id="pmf-elasticsearch-healthcheck-alert"><span class="alert-message"></span></div>
4855 ` ;
4956
57+ ( fetchElasticsearchHealthcheck as Mock ) . mockResolvedValue ( { available : true , status : 'healthy' } ) ;
5058 ( fetchElasticsearchStatistics as Mock ) . mockResolvedValue ( {
5159 index : 'test-index' ,
5260 stats : {
@@ -67,5 +75,63 @@ describe('Elasticsearch Functions', () => {
6775 expect ( statsDiv . innerHTML ) . toContain ( 'Documents' ) ;
6876 expect ( statsDiv . innerHTML ) . toContain ( 'Storage size' ) ;
6977 } ) ;
78+
79+ it ( 'should display health check alert when Elasticsearch is unavailable' , async ( ) => {
80+ document . body . innerHTML = `
81+ <button class="pmf-elasticsearch" data-action="reindex">Reindex</button>
82+ <div id="pmf-elasticsearch-stats"></div>
83+ <div id="pmf-elasticsearch-healthcheck-alert" style="display: none;"><span class="alert-message"></span></div>
84+ ` ;
85+
86+ ( fetchElasticsearchHealthcheck as Mock ) . mockRejectedValue ( new Error ( 'Elasticsearch is unavailable' ) ) ;
87+
88+ await handleElasticsearch ( ) ;
89+
90+ const alertDiv = document . getElementById ( 'pmf-elasticsearch-healthcheck-alert' ) as HTMLElement ;
91+ expect ( alertDiv . style . display ) . toBe ( 'block' ) ;
92+ expect ( alertDiv . querySelector ( '.alert-message' ) ?. textContent ) . toBe ( 'Elasticsearch is unavailable' ) ;
93+ } ) ;
94+
95+ it ( 'should hide health check alert when Elasticsearch is available' , async ( ) => {
96+ document . body . innerHTML = `
97+ <button class="pmf-elasticsearch" data-action="reindex">Reindex</button>
98+ <div id="pmf-elasticsearch-stats"></div>
99+ <div id="pmf-elasticsearch-healthcheck-alert" style="display: block;"><span class="alert-message"></span></div>
100+ ` ;
101+
102+ ( fetchElasticsearchHealthcheck as Mock ) . mockResolvedValue ( { available : true , status : 'healthy' } ) ;
103+ ( fetchElasticsearchStatistics as Mock ) . mockResolvedValue ( {
104+ index : 'test-index' ,
105+ stats : {
106+ indices : {
107+ 'test-index' : {
108+ total : {
109+ docs : { count : 1000 } ,
110+ store : { size_in_bytes : 1024 } ,
111+ } ,
112+ } ,
113+ } ,
114+ } ,
115+ } ) ;
116+
117+ await handleElasticsearch ( ) ;
118+
119+ const alertDiv = document . getElementById ( 'pmf-elasticsearch-healthcheck-alert' ) as HTMLElement ;
120+ expect ( alertDiv . style . display ) . toBe ( 'none' ) ;
121+ } ) ;
122+
123+ it ( 'should not fetch statistics when Elasticsearch is unhealthy' , async ( ) => {
124+ document . body . innerHTML = `
125+ <button class="pmf-elasticsearch" data-action="reindex">Reindex</button>
126+ <div id="pmf-elasticsearch-stats"></div>
127+ <div id="pmf-elasticsearch-healthcheck-alert"><span class="alert-message"></span></div>
128+ ` ;
129+
130+ ( fetchElasticsearchHealthcheck as Mock ) . mockRejectedValue ( new Error ( 'Service unavailable' ) ) ;
131+
132+ await handleElasticsearch ( ) ;
133+
134+ expect ( fetchElasticsearchStatistics ) . not . toHaveBeenCalled ( ) ;
135+ } ) ;
70136 } ) ;
71137} ) ;
0 commit comments