-
-
Notifications
You must be signed in to change notification settings - Fork 739
/
query.test.ts
257 lines (218 loc) · 8.12 KB
/
query.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import {Browser, chromium, Page} from 'playwright';
import {deepEqual} from '../lib/json-diff';
import st from 'st';
import http from 'node:http';
import type {Server} from 'node:http';
import path from 'node:path/posix';
import fs from 'node:fs';
import type {AddressInfo} from 'node:net';
import localizeURLs from '../lib/localize-urls';
import {glob} from 'glob';
function performQueryOnFixture(fixture) {
return new Promise((resolve, _reject) => {
function handleOperation(map, operations, opIndex, done) {
const operation = operations[opIndex];
const opName = operation[0];
//Delegate to special handler if one is available
if (opName in operationHandlers) {
operationHandlers[opName](map, operation.slice(1), () => {
done(opIndex);
});
} else {
map[opName](...operation.slice(1));
done(opIndex);
}
}
const operationHandlers = {
wait(map, params, done) {
const wait = function() {
if (map.loaded()) {
done();
} else {
map.once('render', wait);
}
};
wait();
},
idle(map, params, done) {
const idle = function() {
if (!map.isMoving()) {
done();
} else {
map.once('render', idle);
}
};
idle();
}
};
function applyOperations(map, operations, done) {
// No operations specified, end immediately and invoke done.
if (!operations || operations.length === 0) {
done();
return;
}
// Start recursive chain
const scheduleNextOperation = (lastOpIndex) => {
if (lastOpIndex === operations.length - 1) {
// Stop recusive chain when at the end of the operations
done();
return;
}
handleOperation(map, operations, ++lastOpIndex, scheduleNextOperation);
};
scheduleNextOperation(-1);
}
const style = fixture.style;
const options = style.metadata.test;
const skipLayerDelete = style.metadata.skipLayerDelete;
// @ts-ignore
const map = new maplibregl.Map({
container: 'map',
style,
// @ts-ignore
classes: options.classes,
interactive: false,
attributionControl: false,
pixelRatio: options.pixelRatio,
preserveDrawingBuffer: true,
axonometric: options.axonometric || false,
skew: options.skew || [0, 0],
fadeDuration: options.fadeDuration || 0,
localIdeographFontFamily: options.localIdeographFontFamily || false,
crossSourceCollisions: typeof options.crossSourceCollisions === 'undefined' ? true : options.crossSourceCollisions
});
map.repaint = true;
map.once('load', () => {
console.log('load', map);
// Run the operations on the map
applyOperations(map, options.operations, () => {
console.log('operation', map.queryRenderedFeatures);
// Perform query operation and compare results from expected values
const results = options.queryGeometry ?
map.queryRenderedFeatures(options.queryGeometry, options.queryOptions || {}) :
[];
console.log('results', results);
const actual = results.map((feature) => {
const featureJson = JSON.parse(JSON.stringify(feature.toJSON()));
if (!skipLayerDelete) delete featureJson.layer;
return featureJson;
});
resolve(actual);
});
});
});
}
describe('query tests', () => {
let browser: Browser;
let server: Server;
beforeAll(async () => {
server = http.createServer(
st({
path: 'test/integration/assets',
cors: true,
})
);
browser = await chromium.launch();
await new Promise<void>((resolve) => server.listen(resolve));
});
afterAll(async () => {
await browser.close();
await new Promise(resolve => server.close(resolve));
});
let page: Page;
beforeEach(async () => {
page = await browser.newPage();
});
afterEach(async() => {
await page.close();
});
const allTestsRoot = 'test/integration/query/tests';
const testStyles = glob.sync(path.join(allTestsRoot, '**/style.json'));
for (const [testindex, styleJson] of testStyles.entries()) {
const testCaseRoot = path.dirname(styleJson);
const caseName = path.relative(allTestsRoot, testCaseRoot);
// eslint-disable-next-line no-loop-func
test(caseName, async () => {
const port = (server.address() as AddressInfo).port;
const fixture = await dirToJson(testCaseRoot, port);
console.log(`${testindex + 1} / ${testStyles.length}: ${caseName}`);
const style = fixture.style;
const options = style.metadata.test;
await page.setContent(`
<!DOCTYPE html>
<html lang="en">
<head>
<title>Query Test Page</title>
<meta charset='utf-8'>
<link rel="icon" href="about:blank">
<style>#map {
box-sizing:content-box;
width:${options.width}px;
height:${options.height}px;
}</style>
</head>
<body id='map'></body>
</html>`);
await page.addScriptTag({path: 'dist/maplibre-gl.js'});
await page.addStyleTag({path: 'dist/maplibre-gl.css'});
const actual = await page.evaluate(performQueryOnFixture, fixture);
const isEqual = deepEqual(actual, fixture.expected);
// update expected.json if UPDATE=true is passed and the test fails
if (process.env.UPDATE && !isEqual) {
const expectedPath = path.join(testCaseRoot, 'expected.json');
console.log('updating', expectedPath);
fs.writeFileSync(expectedPath, JSON.stringify(actual, null, 2));
}
expect(isEqual).toBeTruthy();
}, 20000);
}
});
/**
* Analyzes the contents of the specified directory, and converts it to a JSON-serializable
* object, suitable for sending to the browser
* @param basePath - The root directory
*/
async function dirToJson(dir: string, port: number) {
const files = await fs.promises.readdir(dir);
// Extract the filedata into a flat dictionary
const result = {} as {
style?: any;
expected?: any;
actual?: any;
[s:string]:unknown;
};
for (const file of files) {
const fullname = path.join(dir, file);
const pp = path.parse(file);
try {
if (pp.ext === '.json') {
let json = JSON.parse(await fs.promises.readFile(fullname, {encoding: 'utf8'}));
// Special case for style json which needs some preprocessing
if (file === 'style.json') {
json = processStyle(dir, json, port);
}
result[pp.name] = json;
} else {
console.warn(`Ignoring file with unexpected extension. ${pp.ext}`);
}
} catch (e) {
console.warn(`Error parsing file: ${file} ${e.message}`);
throw e;
}
}
return result;
}
function processStyle(testName:string, style: unknown, port:number) {
const clone = JSON.parse(JSON.stringify(style));
localizeURLs(clone, port, 'test/integration');
clone.metadata = clone.metadata || {};
clone.metadata.test = Object.assign({
testName,
width: 512,
height: 512,
pixelRatio: 1,
recycleMap: false,
allowed: 0.00015
}, clone.metadata.test);
return clone;
}