@webship-js/meilisearch-steps 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +218 -2
- package/assets/logo.svg +1 -0
- package/demo/books.json +502 -0
- package/package.json +69 -4
- package/tests/features/dashboard.feature +16 -0
- package/tests/features/indexes.feature +38 -0
- package/tests/features/keys.feature +27 -0
- package/tests/step-definitions/meilisearch-dashboard.steps.js +163 -0
- package/tests/step-definitions/meilisearch-indexes.steps.js +213 -0
- package/tests/step-definitions/meilisearch-keys.steps.js +122 -0
- package/tests/step-definitions/meilisearch-support.js +159 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Shared helpers for the step files of this pack.
|
|
4
|
+
//
|
|
5
|
+
// Not a step file: it registers no step definitions (only hooks: one that notes every request a page sends, and one
|
|
6
|
+
// that deletes what a scenario made). It holds what the steps share, and what a project can require for steps of its
|
|
7
|
+
// own: the settings (worldParameters.meilisearch), requests to the search engine's REST API with its master key or a
|
|
8
|
+
// key a scenario made, its tasks waited for, the indexes and keys a scenario made (deleted at its end), and the browser
|
|
9
|
+
// errors its dashboard logs at known moments.
|
|
10
|
+
//
|
|
11
|
+
// A key is never shown: not in a message, not in a failure, not in a request a step reports. The master key comes
|
|
12
|
+
// from worldParameters.meilisearch.masterKey (the environment, MEILISEARCH_MASTER_KEY); a key a scenario makes lives in
|
|
13
|
+
// memory for that scenario and is deleted at its end.
|
|
14
|
+
|
|
15
|
+
const { Before, After } = require('@cucumber/cucumber');
|
|
16
|
+
const webship = require('webship-js/tests/step-definitions/webship');
|
|
17
|
+
|
|
18
|
+
const { friendly, failure } = webship;
|
|
19
|
+
|
|
20
|
+
// The budgets of the waits (milliseconds): each is a ceiling, not a pause.
|
|
21
|
+
const WAIT = { task: 60000, element: 15000 };
|
|
22
|
+
|
|
23
|
+
/** The settings of the pack, `worldParameters.meilisearch` in cucumber.js: `masterKey` (from the environment). */
|
|
24
|
+
function settings(world) {
|
|
25
|
+
const own = (world.parameters && world.parameters.meilisearch) || {};
|
|
26
|
+
return { masterKey: own.masterKey || '' };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function masterKey(world) {
|
|
30
|
+
const key = settings(world).masterKey;
|
|
31
|
+
if (!key) {
|
|
32
|
+
throw friendly({ message: 'No master key is set for the search engine.', hint: 'Set worldParameters.meilisearch.masterKey in cucumber.js, from the environment (MEILISEARCH_MASTER_KEY).' });
|
|
33
|
+
}
|
|
34
|
+
return key;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The search engine's address, the launch URL of cucumber.js, without a trailing slash. */
|
|
38
|
+
function base(world) {
|
|
39
|
+
const url = (world.parameters && world.parameters.launchUrl) || world.launchUrl || '';
|
|
40
|
+
if (!url) throw friendly({ message: 'No launch URL is set, so the search engine has no address.', hint: 'Set worldParameters.launchUrl in cucumber.js, or LAUNCH_URL in the environment.' });
|
|
41
|
+
return String(url).replace(/\/+$/, '');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Any key's value taken out of a text. */
|
|
45
|
+
function hide(world, text) {
|
|
46
|
+
let out = String(text === undefined || text === null ? '' : text);
|
|
47
|
+
const keys = [settings(world).masterKey, ...Object.values(world.meiliKeys || {}).map((k) => k.key)].filter(Boolean);
|
|
48
|
+
for (const k of keys) out = out.split(k).join('…');
|
|
49
|
+
return out.replace(/Bearer\s+\S+/g, 'Bearer …');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A request to the search engine's REST API (outside the page), with the master key, a key a scenario made (by its
|
|
54
|
+
* name), or none (`key: null`). Keeps the answer for the steps that read it: { status, body, method, path }.
|
|
55
|
+
*/
|
|
56
|
+
async function api(world, method, path, { body, key } = {}) {
|
|
57
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
58
|
+
const token = key === null ? null : key === undefined ? masterKey(world) : keyValue(world, key);
|
|
59
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
60
|
+
let res;
|
|
61
|
+
try {
|
|
62
|
+
res = await world.page.context().request.fetch(`${base(world)}${path}`, { method, headers, data: body === undefined ? undefined : JSON.stringify(body), failOnStatusCode: false });
|
|
63
|
+
} catch (e) {
|
|
64
|
+
throw friendly({ message: `The search engine did not answer ${method} ${path}: ${hide(world, e.message).split('\n')[0]}`, hint: 'Check that it is up at the launch URL.' });
|
|
65
|
+
}
|
|
66
|
+
const text = await res.text();
|
|
67
|
+
let parsed = null;
|
|
68
|
+
try { parsed = JSON.parse(text); } catch (e) { parsed = text; }
|
|
69
|
+
world.meiliAnswer = { status: res.status(), body: parsed, method, path };
|
|
70
|
+
return world.meiliAnswer;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Wait for a task (by its uid) to end; fails with the task's error when it failed. */
|
|
74
|
+
async function waitTask(world, enqueued, what) {
|
|
75
|
+
const uid = enqueued && enqueued.body && enqueued.body.taskUid;
|
|
76
|
+
if (uid === undefined) {
|
|
77
|
+
throw failure(world.page, `The search engine did not take ${what}: it answered ${enqueued.status} ${hide(world, JSON.stringify(enqueued.body)).slice(0, 200)}.`, {
|
|
78
|
+
hint: 'A write to the search engine answers 202 with a taskUid; check the request and the key.',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const end = Date.now() + WAIT.task;
|
|
82
|
+
while (Date.now() < end) {
|
|
83
|
+
const t = await api(world, 'GET', `/tasks/${uid}`);
|
|
84
|
+
const status = t.body && t.body.status;
|
|
85
|
+
if (status === 'succeeded') return t.body;
|
|
86
|
+
if (status === 'failed' || status === 'canceled') {
|
|
87
|
+
throw failure(world.page, `The search engine's task for ${what} ${status}: ${(t.body.error && t.body.error.message) || 'no reason given'}.`, {
|
|
88
|
+
hint: 'The task\'s error names what to change: a document\'s primary key, an attribute\'s name.',
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
await world.page.waitForTimeout(200);
|
|
92
|
+
}
|
|
93
|
+
throw failure(world.page, `The search engine's task for ${what} did not end within ${WAIT.task / 1000} s.`, { hint: 'A big batch takes longer: check the task in GET /tasks.' });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The value of a key a scenario made, by its name. */
|
|
97
|
+
function keyValue(world, name) {
|
|
98
|
+
const k = (world.meiliKeys || {})[name];
|
|
99
|
+
if (!k) {
|
|
100
|
+
throw friendly({ message: `The scenario has made no key "${name}".`, hint: `Make it first: Given the search engine has a key "${name}" that may only "search" on "books"` });
|
|
101
|
+
}
|
|
102
|
+
return k.key;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Split "a, b, c" into a list. */
|
|
106
|
+
function list(text) {
|
|
107
|
+
return String(text || '').split(',').map((s) => s.trim()).filter(Boolean);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Take the errors a step allows out of webship-js's list of the browser's errors, from a point (the list's length when
|
|
112
|
+
* the moment began) to now: only those matching the patterns, and only in that window. Counts them for the report.
|
|
113
|
+
*/
|
|
114
|
+
function allowErrors(world, since, patterns, moment) {
|
|
115
|
+
const errs = world._jsErrors || [];
|
|
116
|
+
const kept = errs.slice(0, since);
|
|
117
|
+
let allowed = 0;
|
|
118
|
+
for (const e of errs.slice(since)) {
|
|
119
|
+
const text = `${e.message}${e.url ? ` @ ${e.url}` : ''}`;
|
|
120
|
+
if (patterns.some((re) => re.test(e.message) || re.test(text))) allowed += 1;
|
|
121
|
+
else kept.push(e);
|
|
122
|
+
}
|
|
123
|
+
if (world._jsErrors) world._jsErrors.splice(0, world._jsErrors.length, ...kept);
|
|
124
|
+
if (allowed) {
|
|
125
|
+
world.meiliAllowed = world.meiliAllowed || [];
|
|
126
|
+
world.meiliAllowed.push(`${moment}: ${allowed}`);
|
|
127
|
+
}
|
|
128
|
+
return allowed;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// The requests the page sends (for the check that it sends none outside the site), and what the scenario makes.
|
|
132
|
+
Before(function () {
|
|
133
|
+
this.meiliKeys = {};
|
|
134
|
+
this.meiliIndexes = new Set();
|
|
135
|
+
this.meiliRequests = [];
|
|
136
|
+
this.meiliAllowed = [];
|
|
137
|
+
const page = this.page;
|
|
138
|
+
if (page) page.on('request', (r) => { this.meiliRequests.push(r.url()); });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// What a scenario made, deleted: its keys and its indexes. The browser errors its dashboard steps allowed are counted
|
|
142
|
+
// in the report.
|
|
143
|
+
After(async function () {
|
|
144
|
+
if (this.meiliAllowed && this.meiliAllowed.length && typeof this.attach === 'function') {
|
|
145
|
+
await this.attach(`Browser errors the dashboard steps allowed, at their moments: ${this.meiliAllowed.join('; ')}`, 'text/plain');
|
|
146
|
+
}
|
|
147
|
+
if (!this.page || !settings(this).masterKey) return;
|
|
148
|
+
for (const k of Object.values(this.meiliKeys || {})) {
|
|
149
|
+
await api(this, 'DELETE', `/keys/${k.uid}`).catch(() => {});
|
|
150
|
+
}
|
|
151
|
+
for (const uid of this.meiliIndexes || []) {
|
|
152
|
+
const there = await api(this, 'GET', `/indexes/${encodeURIComponent(uid)}`).catch(() => null);
|
|
153
|
+
if (!there || there.status !== 200) continue;
|
|
154
|
+
const t = await api(this, 'DELETE', `/indexes/${encodeURIComponent(uid)}`).catch(() => null);
|
|
155
|
+
if (t && t.status === 202) await waitTask(this, t, `deleting the index ${uid}`).catch(() => {});
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
module.exports = { WAIT, settings, masterKey, base, hide, api, waitTask, keyValue, list, allowErrors, failure, friendly };
|