Skip to content

Commit b699bc3

Browse files
committed
add initial tests
1 parent 4db9f22 commit b699bc3

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

src/om-protocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export const clearOmUrlData = (url: string) => {
105105
state.dataPromise = null;
106106
};
107107

108+
const URL_REGEX = /^om:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)$/;
109+
108110
const getStateKeyFromUrl = (url: string): string => {
109111
const match = url.match(URL_REGEX);
110112
// FIXME: removing arrows=true avoids duplicate decoding for raster and vector layers (for windspeeds)
@@ -128,7 +130,7 @@ const getOrCreateUrlState = (
128130

129131
console.warn('Creating new state for URL:', url);
130132

131-
const parsed = settings.parseUrlCallback(url, protocol.domainOptions, protocol.variableOptions);
133+
const parsed = settings.parseUrlCallback(key, protocol.domainOptions, protocol.variableOptions);
132134

133135
const { omUrl, variable, ranges, dark, partial, interval, domain, mapBounds } = parsed;
134136

@@ -238,8 +240,6 @@ const getTile = async (
238240
});
239241
};
240242

241-
const URL_REGEX = /^om:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)$/;
242-
243243
const renderTile = async (
244244
url: string,
245245
type: 'image' | 'arrayBuffer',

src/tests/om-protocol.test.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)