diff --git a/src/network/fetch.test.ts b/src/network/fetch.test.ts index 732ad1e8..5ce09896 100644 --- a/src/network/fetch.test.ts +++ b/src/network/fetch.test.ts @@ -141,7 +141,7 @@ describe('Fetch', () => { } ); - it.each(['500', '500ms'])( + it.each(['500', '500ms', '500s'])( 'should send limitToOneBlocked and wait if zimbraPrefMailPollingInterval is %s', async (pollingPref) => { useAccountStore.setState( @@ -186,10 +186,10 @@ describe('Fetch', () => { } ); - it('should not send limitToOneBlocked and wait if zimbraPrefMailPollingInterval is not 500ms', async () => { + it('should not send limitToOneBlocked and wait if zimbraPrefMailPollingInterval is not either 500, 500ms or 500s', async () => { useAccountStore.setState( produce((state) => { - state.settings.prefs.zimbraPrefMailPollingInterval = '500s'; + state.settings.prefs.zimbraPrefMailPollingInterval = '60s'; }) ); diff --git a/src/store/network/utils.test.ts b/src/store/network/utils.test.ts index c111e69d..46a53a6c 100644 --- a/src/store/network/utils.test.ts +++ b/src/store/network/utils.test.ts @@ -98,26 +98,72 @@ describe('Utils', () => { expect(result).toBe(30000); }); - it('should return 500 if zimbraPrefMailPollingInterval is "500" without a duration unit', () => { - useNetworkStore.setState({ pollingInterval: 123456789 }); - useAccountStore.setState((state) => ({ - ...state, - settings: { - ...state.settings, - prefs: { ...state.settings.prefs, zimbraPrefMailPollingInterval: '500' } - } - })); - const response = { - Header: { - context: {} - }, - Body: {} - } satisfies RawSoapResponse>; - const result = getPollingInterval(response); - expect(result).toBe(500); + describe('long polling cases', () => { + it('should return 500 if zimbraPrefMailPollingInterval is "500" without a duration unit', () => { + useNetworkStore.setState({ pollingInterval: 123456789 }); + useAccountStore.setState((state) => ({ + ...state, + settings: { + ...state.settings, + prefs: { ...state.settings.prefs, zimbraPrefMailPollingInterval: '500' } + } + })); + const response = { + Header: { + context: {} + }, + Body: {} + } satisfies RawSoapResponse>; + const result = getPollingInterval(response); + expect(result).toBe(500); + }); + + it('should return 500 if zimbraPrefMailPollingInterval is "500ms"', () => { + useNetworkStore.setState({ pollingInterval: 123456789 }); + useAccountStore.setState((state) => ({ + ...state, + settings: { + ...state.settings, + prefs: { + ...state.settings.prefs, + zimbraPrefMailPollingInterval: '500ms' satisfies Duration + } + } + })); + const response = { + Header: { + context: {} + }, + Body: {} + } satisfies RawSoapResponse>; + const result = getPollingInterval(response); + expect(result).toBe(500); + }); + + it('should return 500 if zimbraPrefMailPollingInterval is "500s"', () => { + useNetworkStore.setState({ pollingInterval: 123456789 }); + useAccountStore.setState((state) => ({ + ...state, + settings: { + ...state.settings, + prefs: { + ...state.settings.prefs, + zimbraPrefMailPollingInterval: '500s' satisfies Duration + } + } + })); + const response = { + Header: { + context: {} + }, + Body: {} + } satisfies RawSoapResponse>; + const result = getPollingInterval(response); + expect(result).toBe(500); + }); }); - it('should return the number if zimbraPrefMailPollingInterval is set without a duration unit', () => { + it('should return the number * 1000 if zimbraPrefMailPollingInterval is set without a duration unit(so are handled as seconds)', () => { useNetworkStore.setState({ pollingInterval: 123456789 }); useAccountStore.setState((state) => ({ ...state, @@ -136,10 +182,10 @@ describe('Utils', () => { Body: {} } satisfies RawSoapResponse>; const result = getPollingInterval(response); - expect(result).toBe(753); + expect(result).toBe(753_000); }); - it('should return the number * 60 * 1000 if zimbraPrefMailPollingInterval duration is set with the duration unit m (minutes)', () => { + it('should return the number if zimbraPrefMailPollingInterval is set with the duration unit ms (milliseconds)', () => { useNetworkStore.setState({ pollingInterval: 123456789 }); useAccountStore.setState((state) => ({ ...state, @@ -147,7 +193,7 @@ describe('Utils', () => { ...state.settings, prefs: { ...state.settings.prefs, - zimbraPrefMailPollingInterval: '50m' satisfies Duration + zimbraPrefMailPollingInterval: '284ms' satisfies Duration } } })); @@ -158,10 +204,10 @@ describe('Utils', () => { Body: {} } satisfies RawSoapResponse>; const result = getPollingInterval(response); - expect(result).toBe(50 * 60 * 1000); + expect(result).toBe(284); }); - it('should return the number if zimbraPrefMailPollingInterval is set with the duration unit ms (milliseconds)', () => { + it('should return the number * 1000 if zimbraPrefMailPollingInterval is set with the duration unit s (seconds)', () => { useNetworkStore.setState({ pollingInterval: 123456789 }); useAccountStore.setState((state) => ({ ...state, @@ -169,7 +215,7 @@ describe('Utils', () => { ...state.settings, prefs: { ...state.settings.prefs, - zimbraPrefMailPollingInterval: '284ms' satisfies Duration + zimbraPrefMailPollingInterval: '753s' satisfies Duration } } })); @@ -180,10 +226,10 @@ describe('Utils', () => { Body: {} } satisfies RawSoapResponse>; const result = getPollingInterval(response); - expect(result).toBe(284); + expect(result).toBe(753000); }); - it('should return the number * 1000 if zimbraPrefMailPollingInterval is set with the duration unit s (seconds)', () => { + it('should return the number * 60 * 1000 if zimbraPrefMailPollingInterval duration is set with the duration unit m (minutes)', () => { useNetworkStore.setState({ pollingInterval: 123456789 }); useAccountStore.setState((state) => ({ ...state, @@ -191,7 +237,7 @@ describe('Utils', () => { ...state.settings, prefs: { ...state.settings.prefs, - zimbraPrefMailPollingInterval: '753s' satisfies Duration + zimbraPrefMailPollingInterval: '50m' satisfies Duration } } })); @@ -202,7 +248,7 @@ describe('Utils', () => { Body: {} } satisfies RawSoapResponse>; const result = getPollingInterval(response); - expect(result).toBe(753000); + expect(result).toBe(50 * 60 * 1000); }); it('should return the number * 60 * 60 * 1000 if zimbraPrefMailPollingInterval is set with the duration unit h (hours)', () => { diff --git a/src/store/network/utils.ts b/src/store/network/utils.ts index fa4e1523..faa8088e 100644 --- a/src/store/network/utils.ts +++ b/src/store/network/utils.ts @@ -29,6 +29,8 @@ const POLLING_RETRY_INTERVAL = 60_000; const POLLING_INVALID_DURATION = 30_000; +const LONG_POLLING_MARKER_VALUE = 500; + export const parsePollingInterval = (settings: AccountSettings): number => { const pollingPref = settings.prefs?.zimbraPrefMailPollingInterval ?? ''; const [value, durationUnit] = pollingPref.split(/([a-z]+)/g); @@ -36,10 +38,17 @@ export const parsePollingInterval = (settings: AccountSettings): number => { if (Number.isNaN(pollingValue)) { return POLLING_INVALID_DURATION; } + + if ( + pollingValue === LONG_POLLING_MARKER_VALUE && + (durationUnit === undefined || durationUnit === 'ms' || durationUnit === 's') + ) { + return LONG_POLLING_MARKER_VALUE; + } switch (durationUnit) { - case undefined: case 'ms': return pollingValue; + case undefined: case 's': return pollingValue * 1000; case 'm':