|
| 1 | +import StacApi, { SearchMode } from './index'; |
| 2 | +import type { SearchRequestPayload } from '../types/stac'; |
| 3 | + |
| 4 | +// Mock fetch globally |
| 5 | +global.fetch = jest.fn(); |
| 6 | +const mockFetch = fetch as jest.MockedFunction<typeof fetch>; |
| 7 | + |
| 8 | +describe('StacApi', () => { |
| 9 | + let stacApi: StacApi; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + stacApi = new StacApi('https://api.example.com', SearchMode.POST); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('makeDatetimePayload', () => { |
| 17 | + it('should return undefined for undefined dateRange', () => { |
| 18 | + const result = stacApi.makeDatetimePayload(undefined); |
| 19 | + expect(result).toBeUndefined(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return undefined for empty dateRange', () => { |
| 23 | + const result = stacApi.makeDatetimePayload({}); |
| 24 | + expect(result).toBeUndefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should format date range with from and to', () => { |
| 28 | + const dateRange = { from: '2025-12-01', to: '2025-12-31' }; |
| 29 | + const result = stacApi.makeDatetimePayload(dateRange); |
| 30 | + // Simple date format for STAC API compatibility |
| 31 | + expect(result).toBe('2025-12-01/2025-12-31'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should format date range with only from', () => { |
| 35 | + const dateRange = { from: '2025-12-01' }; |
| 36 | + const result = stacApi.makeDatetimePayload(dateRange); |
| 37 | + expect(result).toBe('2025-12-01/..'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should format date range with only to', () => { |
| 41 | + const dateRange = { to: '2025-12-31' }; |
| 42 | + const result = stacApi.makeDatetimePayload(dateRange); |
| 43 | + expect(result).toBe('../2025-12-31'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle full datetime strings', () => { |
| 47 | + const dateRange = { |
| 48 | + from: '2025-12-01T00:00:00Z', |
| 49 | + to: '2025-12-31T23:59:59Z', |
| 50 | + }; |
| 51 | + const result = stacApi.makeDatetimePayload(dateRange); |
| 52 | + expect(result).toBe('2025-12-01T00:00:00Z/2025-12-31T23:59:59Z'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('search payload transformation', () => { |
| 57 | + beforeEach(() => { |
| 58 | + // Mock successful response |
| 59 | + mockFetch.mockResolvedValue({ |
| 60 | + ok: true, |
| 61 | + json: jest.fn().mockResolvedValue({ features: [] }), |
| 62 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 63 | + } as any); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should transform dateRange to datetime string in POST mode', async () => { |
| 67 | + const searchPayload: SearchRequestPayload = { |
| 68 | + collections: ['sentinel-2-l2a'], |
| 69 | + dateRange: { from: '2025-12-01', to: '2025-12-31' }, |
| 70 | + }; |
| 71 | + |
| 72 | + await stacApi.search(searchPayload); |
| 73 | + |
| 74 | + expect(mockFetch).toHaveBeenCalledWith( |
| 75 | + 'https://api.example.com/search', |
| 76 | + expect.objectContaining({ |
| 77 | + method: 'POST', |
| 78 | + headers: expect.objectContaining({ |
| 79 | + 'Content-Type': 'application/json', |
| 80 | + }), |
| 81 | + body: JSON.stringify({ |
| 82 | + collections: ['sentinel-2-l2a'], |
| 83 | + datetime: '2025-12-01/2025-12-31', |
| 84 | + ids: undefined, |
| 85 | + bbox: undefined, |
| 86 | + }), |
| 87 | + }) |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should transform dateRange to datetime string in GET mode', async () => { |
| 92 | + const getStacApi = new StacApi('https://api.example.com', SearchMode.GET); |
| 93 | + const searchPayload: SearchRequestPayload = { |
| 94 | + collections: ['sentinel-2-l2a'], |
| 95 | + dateRange: { from: '2025-12-01', to: '2025-12-31' }, |
| 96 | + }; |
| 97 | + |
| 98 | + await getStacApi.search(searchPayload); |
| 99 | + |
| 100 | + expect(mockFetch).toHaveBeenCalledWith( |
| 101 | + 'https://api.example.com/search?collections=sentinel-2-l2a&datetime=2025-12-01%2F2025-12-31', |
| 102 | + expect.objectContaining({ |
| 103 | + method: 'GET', |
| 104 | + headers: expect.objectContaining({ |
| 105 | + 'Content-Type': 'application/json', |
| 106 | + }), |
| 107 | + }) |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should not include undefined values in POST payload', async () => { |
| 112 | + const searchPayload: SearchRequestPayload = { |
| 113 | + collections: ['sentinel-2-l2a'], |
| 114 | + }; |
| 115 | + |
| 116 | + await stacApi.search(searchPayload); |
| 117 | + |
| 118 | + expect(mockFetch).toHaveBeenCalledWith( |
| 119 | + 'https://api.example.com/search', |
| 120 | + expect.objectContaining({ |
| 121 | + body: JSON.stringify({ |
| 122 | + collections: ['sentinel-2-l2a'], |
| 123 | + ids: undefined, |
| 124 | + bbox: undefined, |
| 125 | + datetime: undefined, |
| 126 | + }), |
| 127 | + }) |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should not include undefined values in GET query string', async () => { |
| 132 | + const getStacApi = new StacApi('https://api.example.com', SearchMode.GET); |
| 133 | + const searchPayload: SearchRequestPayload = { |
| 134 | + collections: ['sentinel-2-l2a'], |
| 135 | + }; |
| 136 | + |
| 137 | + await getStacApi.search(searchPayload); |
| 138 | + |
| 139 | + const expectedUrl = 'https://api.example.com/search?collections=sentinel-2-l2a'; |
| 140 | + expect(mockFetch).toHaveBeenCalledWith( |
| 141 | + expectedUrl, |
| 142 | + expect.objectContaining({ |
| 143 | + method: 'GET', |
| 144 | + }) |
| 145 | + ); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('payloadToQuery', () => { |
| 150 | + it('should convert arrays to comma-separated strings', () => { |
| 151 | + const payload = { |
| 152 | + collections: ['col1', 'col2'], |
| 153 | + ids: ['id1', 'id2', 'id3'], |
| 154 | + }; |
| 155 | + const result = stacApi.payloadToQuery(payload); |
| 156 | + expect(result).toBe('collections=col1%2Ccol2&ids=id1%2Cid2%2Cid3'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should convert primitive values to strings', () => { |
| 160 | + const payload = { |
| 161 | + collections: ['col1'], |
| 162 | + datetime: '2025-12-01T00:00:00Z/2025-12-31T23:59:59Z', |
| 163 | + }; |
| 164 | + const result = stacApi.payloadToQuery(payload); |
| 165 | + expect(result).toBe( |
| 166 | + 'collections=col1&datetime=2025-12-01T00%3A00%3A00Z%2F2025-12-31T23%3A59%3A59Z' |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should skip undefined values', () => { |
| 171 | + const payload = { |
| 172 | + collections: ['col1'], |
| 173 | + ids: undefined, |
| 174 | + datetime: '2025-12-01T00:00:00Z/2025-12-31T23:59:59Z', |
| 175 | + }; |
| 176 | + const result = stacApi.payloadToQuery(payload); |
| 177 | + expect(result).toBe( |
| 178 | + 'collections=col1&datetime=2025-12-01T00%3A00%3A00Z%2F2025-12-31T23%3A59%3A59Z' |
| 179 | + ); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should handle sortby parameter', () => { |
| 183 | + const payload = { |
| 184 | + collections: ['col1'], |
| 185 | + sortby: [ |
| 186 | + { field: 'datetime', direction: 'desc' as const }, |
| 187 | + { field: 'id', direction: 'asc' as const }, |
| 188 | + ], |
| 189 | + }; |
| 190 | + const result = stacApi.payloadToQuery(payload); |
| 191 | + expect(result).toBe('collections=col1&sortby=-datetime%2C%2Bid'); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments