|
| 1 | +import { defaultOmProtocolSettings } from '../om-protocol'; |
| 2 | +import { RequestParameters } from 'maplibre-gl'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { DimensionRange, Domain, TileJSON } from '../types'; |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + // Reset module cache so module-level singletons are recreated for each test |
| 9 | + vi.resetModules(); |
| 10 | + vi.clearAllMocks(); |
| 11 | +}); |
| 12 | + |
| 13 | +describe('om-protocol unit tests', () => { |
| 14 | + it('parseOmUrl returns expected fields and computes ranges for partial=true', async () => { |
| 15 | + const { parseOmUrl } = await import('../om-protocol'); |
| 16 | + |
| 17 | + const domainOptions: Domain[] = [ |
| 18 | + { |
| 19 | + value: 'domain1', |
| 20 | + label: 'Domain 1', |
| 21 | + grid: { type: 'regular', nx: 10, ny: 20, lonMin: 0, latMin: 0, dx: 1, dy: 1 }, |
| 22 | + time_interval: 1, |
| 23 | + model_interval: 3, |
| 24 | + windUVComponents: false |
| 25 | + } |
| 26 | + ]; |
| 27 | + const variableOptions = [{ value: 'temperature', label: 'Temperature' }]; |
| 28 | + |
| 29 | + const url = |
| 30 | + 'om://https://map-tiles.open-meteo.com/data_spatial?variable=temperature&bounds=0,0,10,10&dark=true&partial=true&interval=2'; |
| 31 | + const parsed = parseOmUrl(url, domainOptions, variableOptions); |
| 32 | + |
| 33 | + console.log(parsed); |
| 34 | + expect(parsed.dark).toBe(true); |
| 35 | + expect(parsed.variable.value).toBe('temperature'); |
| 36 | + expect(parsed.interval).toBe(2); |
| 37 | + expect(parsed.mapBounds).toEqual([0, 0, 10, 10]); |
| 38 | + // If partial is true, the ranges are the overlap of the domain grid and the requested mapBounds |
| 39 | + expect(parsed.partial).toBe(true); |
| 40 | + expect(parsed.ranges).toEqual([ |
| 41 | + { start: 0, end: 12 }, |
| 42 | + { start: 0, end: 10 } |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('omProtocol with json returns tilejson with tiles url', async () => { |
| 47 | + const { omProtocol } = await import('../om-protocol'); |
| 48 | + |
| 49 | + const params: RequestParameters = { |
| 50 | + url: 'om://https://map-tiles.open-meteo.com/data_spatial/dwd_icon/2025/10/27/1200Z/2025-10-27T1200.om?variable=temperature_2m', |
| 51 | + type: 'json' |
| 52 | + }; |
| 53 | + const result = await omProtocol(params, undefined, defaultOmProtocolSettings); |
| 54 | + const resultData = result.data as TileJSON; |
| 55 | + |
| 56 | + // tiles url uses the full request URL + '/{z}/{x}/{y}' |
| 57 | + expect(resultData.tiles[0]).toBe( |
| 58 | + 'om://https://map-tiles.open-meteo.com/data_spatial/dwd_icon/2025/10/27/1200Z/2025-10-27T1200.om?variable=temperature_2m/{z}/{x}/{y}' |
| 59 | + ); |
| 60 | + expect(resultData.tilejson).toBe('2.2.0'); |
| 61 | + expect(resultData.bounds).toEqual([-180, -90, 179.875, 90.125]); // bounds of icon global |
| 62 | + }); |
| 63 | + |
| 64 | + it('omProtocol arrayBuffer path calls workerPool and returns ArrayBuffer', async () => { |
| 65 | + // Mock OMapsFileReader so ensureData will put values in state.data |
| 66 | + vi.mock('../om-file-reader', () => { |
| 67 | + return { |
| 68 | + OMapsFileReader: class { |
| 69 | + async setToOmFile() {} |
| 70 | + async readVariable(variable: string, ranges: DimensionRange[]) { |
| 71 | + const totalValues = |
| 72 | + ranges?.reduce((acc, range) => acc * (range.end - range.start + 1), 1) || 0; |
| 73 | + const values = new Float32Array(totalValues); // initialize with zeros |
| 74 | + return { values, directions: undefined }; |
| 75 | + } |
| 76 | + } |
| 77 | + }; |
| 78 | + }); |
| 79 | + |
| 80 | + // Mock WorkerPool so requestTile returns a predictable ArrayBuffer and "works" on Node |
| 81 | + vi.mock('../worker-pool', () => { |
| 82 | + const fakeBuf = new ArrayBuffer(16); |
| 83 | + return { |
| 84 | + WorkerPool: class { |
| 85 | + requestTile = vi.fn(() => Promise.resolve(fakeBuf)); |
| 86 | + } |
| 87 | + }; |
| 88 | + }); |
| 89 | + |
| 90 | + const { omProtocol, getValueFromLatLong } = await import('../om-protocol'); |
| 91 | + |
| 92 | + const params: RequestParameters = { |
| 93 | + url: 'om://https://map-tiles.open-meteo.com/data_spatial/dwd_icon/2025/10/27/1200Z/2025-10-27T1200.om?variable=temperature_2m/0/0/0', |
| 94 | + type: 'arrayBuffer' |
| 95 | + }; |
| 96 | + const res = await omProtocol(params, undefined, defaultOmProtocolSettings); |
| 97 | + expect(res.data).toEqual(new ArrayBuffer(16)); |
| 98 | + |
| 99 | + // test getValueFromLatLong uses the same stored state and returns the interpolated value |
| 100 | + const valueResult = getValueFromLatLong(0, 0, { value: 'temperature_2m' }, params.url); |
| 101 | + expect(valueResult.value).toEqual(0); |
| 102 | + }); |
| 103 | +}); |
0 commit comments