kryptheon 0.1.48 → 0.1.50
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/README.md +435 -333
- package/bin/kryptheon.js +231 -16
- package/kryptheon-code.js +158 -2
- package/kryptheon-dashboard-page.js +78 -6
- package/kryptheon-dashboard.html +9 -0
- package/kryptheon-dashboard.js +241 -13
- package/kryptheon-edge.js +83 -14
- package/kryptheon-heal.js +57 -0
- package/kryptheon-payments-run.js +501 -0
- package/kryptheon-payments.js +626 -0
- package/kryptheon-probe-fixture.js +175 -8
- package/kryptheon-probe.js +368 -317
- package/kryptheon-reliability.js +430 -40
- package/kryptheon-replay.js +0 -0
- package/kryptheon-secrets.js +14 -3
- package/package.json +12 -5
package/kryptheon-probe.js
CHANGED
|
@@ -1,317 +1,368 @@
|
|
|
1
|
-
// Runs changed copies of the person's recordings - probes - and reads back
|
|
2
|
-
// what each one saw. Shared by the input check (kryptheon-edge.js) and the
|
|
3
|
-
// reliability check.
|
|
4
|
-
//
|
|
5
|
-
// Three promises this file keeps:
|
|
6
|
-
//
|
|
7
|
-
// - The recordings in tests/ are never changed. Copies are written into the
|
|
8
|
-
// project's store, run from there under kryptheon-probe-fixture.js, and
|
|
9
|
-
// removed when the run ends - so a probe can never change a baseline, the
|
|
10
|
-
// history, or the file the person recorded.
|
|
11
|
-
// - Nothing here starts on its own. A probe adds data to the app it runs
|
|
12
|
-
// against, so the caller asks first (consentLines), and an address that is
|
|
13
|
-
// not this computer needs the person themselves to say yes.
|
|
14
|
-
// - What is not tested is said as not tested: a run that was stopped, rate
|
|
15
|
-
// limited, or never finished is never counted as clean.
|
|
16
|
-
|
|
17
|
-
const fs = require('fs');
|
|
18
|
-
const path = require('path');
|
|
19
|
-
const crypto = require('crypto');
|
|
20
|
-
const { spawn, spawnSync } = require('child_process');
|
|
21
|
-
const store = require('./kryptheon-store.js');
|
|
22
|
-
|
|
23
|
-
const PACKAGE_DIR = __dirname;
|
|
24
|
-
const FIXTURE = path.join(PACKAGE_DIR, 'kryptheon-probe-fixture.js');
|
|
25
|
-
const CONFIG = path.join(PACKAGE_DIR, 'kryptheon-probe.config.js');
|
|
26
|
-
// The most runs one press of the button may make: each one adds data to the
|
|
27
|
-
// app, and each one takes a browser's worth of time.
|
|
28
|
-
const MAX_RUNS = 40;
|
|
29
|
-
// The word every value Kryptheon types carries where a value can carry one,
|
|
30
|
-
// so the person can find what was left behind.
|
|
31
|
-
const TEST_WORD = 'kryptheon-test';
|
|
32
|
-
// The attribute on the tag the input check types. Only an element the
|
|
33
|
-
// browser parsed carries it - text that merely shows the characters does not.
|
|
34
|
-
const MARKER = 'data-kryptheon-probe';
|
|
35
|
-
|
|
36
|
-
/** This computer, or a name that only ever means a development copy. */
|
|
37
|
-
function isLocalAddress(url) {
|
|
38
|
-
let host;
|
|
39
|
-
try {
|
|
40
|
-
host = new URL(url).hostname.toLowerCase();
|
|
41
|
-
} catch (err) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
return (
|
|
45
|
-
host === 'localhost' ||
|
|
46
|
-
host === '127.0.0.1' ||
|
|
47
|
-
host === '0.0.0.0' ||
|
|
48
|
-
host === '::1' ||
|
|
49
|
-
host === '[::1]' ||
|
|
50
|
-
host.endsWith('.local') ||
|
|
51
|
-
host.endsWith('.localhost') ||
|
|
52
|
-
host.endsWith('.test')
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** Every address a recording opens, in order, each once. */
|
|
57
|
-
function addressesOf(source) {
|
|
58
|
-
const out = [];
|
|
59
|
-
const re = /\bpage\s*\.\s*goto\s*\(\s*(['"`])([^'"`]+)\1/g;
|
|
60
|
-
let m;
|
|
61
|
-
while ((m = re.exec(String(source || '')))) {
|
|
62
|
-
let origin = m[2];
|
|
63
|
-
try {
|
|
64
|
-
origin = new URL(m[2]).origin;
|
|
65
|
-
} catch (err) {
|
|
66
|
-
/* kept as written */
|
|
67
|
-
}
|
|
68
|
-
if (!out.includes(origin)) out.push(origin);
|
|
69
|
-
}
|
|
70
|
-
return out;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** Of these addresses, the ones that are not this computer. */
|
|
74
|
-
function liveLooking(addresses) {
|
|
75
|
-
return (addresses || []).filter((a) => !isLocalAddress(a));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* What is said before a probe run, in the terminal and on the page alike:
|
|
80
|
-
* what it does, where, that it leaves data behind, and - for an address that
|
|
81
|
-
* is not this computer - that real people may see it.
|
|
82
|
-
*/
|
|
83
|
-
function consentLines(what, addresses, options) {
|
|
84
|
-
const live = liveLooking(addresses);
|
|
85
|
-
// The input check types its own values, which can carry a marker; the
|
|
86
|
-
// reliability check replays what was recorded, which cannot.
|
|
87
|
-
const marked = !(options && options.marker === false);
|
|
88
|
-
const lines = [
|
|
89
|
-
what,
|
|
90
|
-
'',
|
|
91
|
-
'It runs against: ' + (addresses.length ? addresses.join(', ') : 'the addresses in your recordings'),
|
|
92
|
-
'It ADDS TEST DATA to your app, and that data STAYS there - Kryptheon does not delete it.',
|
|
93
|
-
].concat(marked ? [
|
|
94
|
-
'Most of it contains the word "' + TEST_WORD + '", so you can find it. Empty, negative and date values cannot carry it;',
|
|
95
|
-
'every write that was accepted is listed when it ends.',
|
|
96
|
-
] : [
|
|
97
|
-
'The data is what your recorded flows save, saved again each time they are replayed (and twice on a double-click);',
|
|
98
|
-
'every write that was accepted is listed when it ends.',
|
|
99
|
-
], [
|
|
100
|
-
'It never creates an account, and uses only the sign-in you recorded.',
|
|
101
|
-
]);
|
|
102
|
-
if (live.length) {
|
|
103
|
-
lines.push('');
|
|
104
|
-
lines.push('WARNING: ' + live.join(', ') + (live.length === 1 ? ' is' : ' are') + ' not this computer. If that is your live app,');
|
|
105
|
-
lines.push('real people will see this test data. Run it only against your own test copy.');
|
|
106
|
-
}
|
|
107
|
-
return lines;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* A recording turned into a probe: the same steps, run under the probe
|
|
112
|
-
* fixture. `edits` changes single lines - by original line number - and
|
|
113
|
-
* `before` puts lines in front of one. Line numbers are always the
|
|
114
|
-
* recording's own, so a finding points at the line the person can open.
|
|
115
|
-
*/
|
|
116
|
-
function probeSource(source, options) {
|
|
117
|
-
const opts = options || {};
|
|
118
|
-
const fixture = JSON.stringify(FIXTURE.split(path.sep).join('/'));
|
|
119
|
-
const lines = String(source || '').split('\n');
|
|
120
|
-
const out = [];
|
|
121
|
-
lines.forEach((line, i) => {
|
|
122
|
-
const n = i + 1;
|
|
123
|
-
let text = line.replace(/(\bfrom\s*|\brequire\(\s*)(['"])((?:[^'"]*[\\/])?kryptheon-fixture(?:\.js)?|kryptheon)\2/, (all, lead) => lead + fixture);
|
|
124
|
-
if (opts.before && opts.before[n]) for (const extra of [].concat(opts.before[n])) out.push(extra);
|
|
125
|
-
if (opts.edits && Object.prototype.hasOwnProperty.call(opts.edits, n)) text = opts.edits[n];
|
|
126
|
-
out.push(text);
|
|
127
|
-
if (opts.after && opts.after[n]) for (const extra of [].concat(opts.after[n])) out.push(extra);
|
|
128
|
-
});
|
|
129
|
-
return out.join('\n');
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** A short id that is safe as a file name and unique within one run. */
|
|
133
|
-
function probeId(index, label) {
|
|
134
|
-
return String(index).padStart(3, '0') + '-' + String(label || 'probe').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 40);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** The folder probes for this project are written to and run from. */
|
|
138
|
-
function probeDirFor(root, env) {
|
|
139
|
-
return path.join(store.pathsFor(root, env).dir, 'probes');
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function removeDir(dir) {
|
|
143
|
-
try {
|
|
144
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
145
|
-
} catch (err) {
|
|
146
|
-
/* left for the next run to clear */
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function findPlaywrightCli() {
|
|
151
|
-
try {
|
|
152
|
-
return require.resolve('@playwright/test/cli', { paths: [PACKAGE_DIR] });
|
|
153
|
-
} catch (err) {
|
|
154
|
-
/* fall through */
|
|
155
|
-
}
|
|
156
|
-
try {
|
|
157
|
-
return path.join(path.dirname(require.resolve('@playwright/test/package.json', { paths: [PACKAGE_DIR] })), 'cli.js');
|
|
158
|
-
} catch (err) {
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// A stopped run takes its browsers with it. On Windows killing a process
|
|
164
|
-
// leaves its children running, so the whole tree is ended.
|
|
165
|
-
function killTree(child) {
|
|
166
|
-
if (!child || child.exitCode !== null) return;
|
|
167
|
-
if (process.platform === 'win32') {
|
|
168
|
-
spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' });
|
|
169
|
-
} else {
|
|
170
|
-
try {
|
|
171
|
-
child.kill('SIGTERM');
|
|
172
|
-
} catch (err) {
|
|
173
|
-
/* already gone */
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Writes these probes and runs them, one at a time. Returns a handle:
|
|
180
|
-
* handle.done -> Promise<{ code, stopped, results: { id: result } }>
|
|
181
|
-
* handle.stop() -> ends the run now; what finished is kept
|
|
182
|
-
* `say` gets a line per finished run ("3 of 12: ..."), and `labels` turns an
|
|
183
|
-
* id into the words said for it.
|
|
184
|
-
*/
|
|
185
|
-
function runProbes(root, probes, options) {
|
|
186
|
-
const opts = options || {};
|
|
187
|
-
const say = typeof opts.say === 'function' ? opts.say : () => {};
|
|
188
|
-
const hide = typeof opts.hide === 'function' ? opts.hide : (s) => s;
|
|
189
|
-
const runDir = path.join(probeDirFor(root, opts.env), crypto.randomBytes(4).toString('hex'));
|
|
190
|
-
const outDir = path.join(runDir, 'results');
|
|
191
|
-
fs.mkdirSync(outDir, { recursive: true });
|
|
192
|
-
for (const p of probes) fs.writeFileSync(path.join(runDir, p.id + '.kxprobe.js'), p.source, 'utf8');
|
|
193
|
-
|
|
194
|
-
let child = null;
|
|
195
|
-
let stopped = false;
|
|
196
|
-
const cli = findPlaywrightCli();
|
|
197
|
-
const done = new Promise((resolve) => {
|
|
198
|
-
const finish = (code) => {
|
|
199
|
-
const results = {};
|
|
200
|
-
for (const p of probes) {
|
|
201
|
-
try {
|
|
202
|
-
results[p.id] = JSON.parse(fs.readFileSync(path.join(outDir, p.id + '.json'), 'utf8'));
|
|
203
|
-
} catch (err) {
|
|
204
|
-
/* not finished: said as not tested by whoever reads this */
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (!opts.keep) removeDir(runDir);
|
|
208
|
-
resolve({ code: code, stopped: stopped, results: results });
|
|
209
|
-
};
|
|
210
|
-
if (!cli) {
|
|
211
|
-
say('The testing engine is missing from this install. Reinstalling usually fixes it: npm install -D kryptheon');
|
|
212
|
-
return finish(null);
|
|
213
|
-
}
|
|
214
|
-
if (stopped || !probes.length) return finish(null);
|
|
215
|
-
const env = Object.assign({}, process.env, opts.childEnv || {}, {
|
|
216
|
-
KRYPTHEON_PROBE_DIR: runDir,
|
|
217
|
-
KRYPTHEON_PROBE_OUT: outDir,
|
|
218
|
-
KRYPTHEON_PROBE_SECRETS: (opts.secretNames || []).join(','),
|
|
219
|
-
});
|
|
220
|
-
if (opts.gapMs != null) env.KRYPTHEON_PROBE_GAP_MS = String(opts.gapMs);
|
|
221
|
-
child = spawn(process.execPath, [cli, 'test', '--config', CONFIG], {
|
|
222
|
-
cwd: root, env: env, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true,
|
|
223
|
-
});
|
|
224
|
-
const pending = { out: '', err: '' };
|
|
225
|
-
const feed = (which) => (chunk) => {
|
|
226
|
-
pending[which] += chunk.toString();
|
|
227
|
-
const parts = pending[which].split(/\r?\n/);
|
|
228
|
-
pending[which] = parts.pop();
|
|
229
|
-
for (const line of parts) heard(line);
|
|
230
|
-
};
|
|
231
|
-
const heard = (line) => {
|
|
232
|
-
const m = /^KXPROBE (\d+) (\d+) (\S+) (\S+)/.exec(line);
|
|
233
|
-
if (m) {
|
|
234
|
-
const label = (opts.labels && opts.labels[m[3]]) || m[3];
|
|
235
|
-
say(' ' + m[1] + ' of ' + m[2] + ': ' + label + (m[4] === 'interrupted' ? ' - stopped' : ''));
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
const e = /^KXPROBE-ERROR (.*)$/.exec(line);
|
|
239
|
-
if (e) say(' The run could not start: ' + hide(e[1]));
|
|
240
|
-
// Anything else Playwright prints is its own business - stack traces
|
|
241
|
-
// and the like - and is not passed on.
|
|
242
|
-
};
|
|
243
|
-
child.stdout.on('data', feed('out'));
|
|
244
|
-
child.stderr.on('data', feed('err'));
|
|
245
|
-
child.on('error', (err) => {
|
|
246
|
-
say('Could not start the browser run: ' + err.message);
|
|
247
|
-
finish(null);
|
|
248
|
-
});
|
|
249
|
-
child.on('close', (code) => {
|
|
250
|
-
if (pending.out) heard(pending.out);
|
|
251
|
-
finish(code);
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
return {
|
|
255
|
-
done: done,
|
|
256
|
-
stop() {
|
|
257
|
-
stopped = true;
|
|
258
|
-
killTree(child);
|
|
259
|
-
},
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// Each probe check keeps its last press in the store under this name.
|
|
264
|
-
const REPORT_FILES = { edge: 'edge-last.json', reliability: 'reliability-last.json' };
|
|
265
|
-
|
|
266
|
-
function reportFile(root, check, env) {
|
|
267
|
-
return path.join(store.pathsFor(root, env).dir, REPORT_FILES[check]);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
function loadReport(root, check, env) {
|
|
271
|
-
try {
|
|
272
|
-
return JSON.parse(fs.readFileSync(reportFile(root, check, env), 'utf8'));
|
|
273
|
-
} catch (err) {
|
|
274
|
-
return null;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Every address a press would reach: the pages the recordings open, and the
|
|
280
|
-
* places any earlier plain replay - of either check - saw the app save to.
|
|
281
|
-
* This is what the person is asked about, so a database somewhere else is
|
|
282
|
-
* named before it is written to.
|
|
283
|
-
*/
|
|
284
|
-
function knownAddresses(root, sources, env) {
|
|
285
|
-
const out = [];
|
|
286
|
-
for (const source of sources) for (const a of addressesOf(source)) if (!out.includes(a)) out.push(a);
|
|
287
|
-
for (const check of Object.keys(REPORT_FILES)) {
|
|
288
|
-
const last = loadReport(root, check, env);
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
1
|
+
// Runs changed copies of the person's recordings - probes - and reads back
|
|
2
|
+
// what each one saw. Shared by the input check (kryptheon-edge.js) and the
|
|
3
|
+
// reliability check.
|
|
4
|
+
//
|
|
5
|
+
// Three promises this file keeps:
|
|
6
|
+
//
|
|
7
|
+
// - The recordings in tests/ are never changed. Copies are written into the
|
|
8
|
+
// project's store, run from there under kryptheon-probe-fixture.js, and
|
|
9
|
+
// removed when the run ends - so a probe can never change a baseline, the
|
|
10
|
+
// history, or the file the person recorded.
|
|
11
|
+
// - Nothing here starts on its own. A probe adds data to the app it runs
|
|
12
|
+
// against, so the caller asks first (consentLines), and an address that is
|
|
13
|
+
// not this computer needs the person themselves to say yes.
|
|
14
|
+
// - What is not tested is said as not tested: a run that was stopped, rate
|
|
15
|
+
// limited, or never finished is never counted as clean.
|
|
16
|
+
|
|
17
|
+
const fs = require('fs');
|
|
18
|
+
const path = require('path');
|
|
19
|
+
const crypto = require('crypto');
|
|
20
|
+
const { spawn, spawnSync } = require('child_process');
|
|
21
|
+
const store = require('./kryptheon-store.js');
|
|
22
|
+
|
|
23
|
+
const PACKAGE_DIR = __dirname;
|
|
24
|
+
const FIXTURE = path.join(PACKAGE_DIR, 'kryptheon-probe-fixture.js');
|
|
25
|
+
const CONFIG = path.join(PACKAGE_DIR, 'kryptheon-probe.config.js');
|
|
26
|
+
// The most runs one press of the button may make: each one adds data to the
|
|
27
|
+
// app, and each one takes a browser's worth of time.
|
|
28
|
+
const MAX_RUNS = 40;
|
|
29
|
+
// The word every value Kryptheon types carries where a value can carry one,
|
|
30
|
+
// so the person can find what was left behind.
|
|
31
|
+
const TEST_WORD = 'kryptheon-test';
|
|
32
|
+
// The attribute on the tag the input check types. Only an element the
|
|
33
|
+
// browser parsed carries it - text that merely shows the characters does not.
|
|
34
|
+
const MARKER = 'data-kryptheon-probe';
|
|
35
|
+
|
|
36
|
+
/** This computer, or a name that only ever means a development copy. */
|
|
37
|
+
function isLocalAddress(url) {
|
|
38
|
+
let host;
|
|
39
|
+
try {
|
|
40
|
+
host = new URL(url).hostname.toLowerCase();
|
|
41
|
+
} catch (err) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return (
|
|
45
|
+
host === 'localhost' ||
|
|
46
|
+
host === '127.0.0.1' ||
|
|
47
|
+
host === '0.0.0.0' ||
|
|
48
|
+
host === '::1' ||
|
|
49
|
+
host === '[::1]' ||
|
|
50
|
+
host.endsWith('.local') ||
|
|
51
|
+
host.endsWith('.localhost') ||
|
|
52
|
+
host.endsWith('.test')
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Every address a recording opens, in order, each once. */
|
|
57
|
+
function addressesOf(source) {
|
|
58
|
+
const out = [];
|
|
59
|
+
const re = /\bpage\s*\.\s*goto\s*\(\s*(['"`])([^'"`]+)\1/g;
|
|
60
|
+
let m;
|
|
61
|
+
while ((m = re.exec(String(source || '')))) {
|
|
62
|
+
let origin = m[2];
|
|
63
|
+
try {
|
|
64
|
+
origin = new URL(m[2]).origin;
|
|
65
|
+
} catch (err) {
|
|
66
|
+
/* kept as written */
|
|
67
|
+
}
|
|
68
|
+
if (!out.includes(origin)) out.push(origin);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Of these addresses, the ones that are not this computer. */
|
|
74
|
+
function liveLooking(addresses) {
|
|
75
|
+
return (addresses || []).filter((a) => !isLocalAddress(a));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What is said before a probe run, in the terminal and on the page alike:
|
|
80
|
+
* what it does, where, that it leaves data behind, and - for an address that
|
|
81
|
+
* is not this computer - that real people may see it.
|
|
82
|
+
*/
|
|
83
|
+
function consentLines(what, addresses, options) {
|
|
84
|
+
const live = liveLooking(addresses);
|
|
85
|
+
// The input check types its own values, which can carry a marker; the
|
|
86
|
+
// reliability check replays what was recorded, which cannot.
|
|
87
|
+
const marked = !(options && options.marker === false);
|
|
88
|
+
const lines = [
|
|
89
|
+
what,
|
|
90
|
+
'',
|
|
91
|
+
'It runs against: ' + (addresses.length ? addresses.join(', ') : 'the addresses in your recordings'),
|
|
92
|
+
'It ADDS TEST DATA to your app, and that data STAYS there - Kryptheon does not delete it.',
|
|
93
|
+
].concat(marked ? [
|
|
94
|
+
'Most of it contains the word "' + TEST_WORD + '", so you can find it. Empty, negative and date values cannot carry it;',
|
|
95
|
+
'every write that was accepted is listed when it ends.',
|
|
96
|
+
] : [
|
|
97
|
+
'The data is what your recorded flows save, saved again each time they are replayed (and twice on a double-click);',
|
|
98
|
+
'every write that was accepted is listed when it ends.',
|
|
99
|
+
], [
|
|
100
|
+
'It never creates an account, and uses only the sign-in you recorded.',
|
|
101
|
+
]);
|
|
102
|
+
if (live.length) {
|
|
103
|
+
lines.push('');
|
|
104
|
+
lines.push('WARNING: ' + live.join(', ') + (live.length === 1 ? ' is' : ' are') + ' not this computer. If that is your live app,');
|
|
105
|
+
lines.push('real people will see this test data. Run it only against your own test copy.');
|
|
106
|
+
}
|
|
107
|
+
return lines;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A recording turned into a probe: the same steps, run under the probe
|
|
112
|
+
* fixture. `edits` changes single lines - by original line number - and
|
|
113
|
+
* `before` puts lines in front of one. Line numbers are always the
|
|
114
|
+
* recording's own, so a finding points at the line the person can open.
|
|
115
|
+
*/
|
|
116
|
+
function probeSource(source, options) {
|
|
117
|
+
const opts = options || {};
|
|
118
|
+
const fixture = JSON.stringify(FIXTURE.split(path.sep).join('/'));
|
|
119
|
+
const lines = String(source || '').split('\n');
|
|
120
|
+
const out = [];
|
|
121
|
+
lines.forEach((line, i) => {
|
|
122
|
+
const n = i + 1;
|
|
123
|
+
let text = line.replace(/(\bfrom\s*|\brequire\(\s*)(['"])((?:[^'"]*[\\/])?kryptheon-fixture(?:\.js)?|kryptheon)\2/, (all, lead) => lead + fixture);
|
|
124
|
+
if (opts.before && opts.before[n]) for (const extra of [].concat(opts.before[n])) out.push(extra);
|
|
125
|
+
if (opts.edits && Object.prototype.hasOwnProperty.call(opts.edits, n)) text = opts.edits[n];
|
|
126
|
+
out.push(text);
|
|
127
|
+
if (opts.after && opts.after[n]) for (const extra of [].concat(opts.after[n])) out.push(extra);
|
|
128
|
+
});
|
|
129
|
+
return out.join('\n');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A short id that is safe as a file name and unique within one run. */
|
|
133
|
+
function probeId(index, label) {
|
|
134
|
+
return String(index).padStart(3, '0') + '-' + String(label || 'probe').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 40);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The folder probes for this project are written to and run from. */
|
|
138
|
+
function probeDirFor(root, env) {
|
|
139
|
+
return path.join(store.pathsFor(root, env).dir, 'probes');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function removeDir(dir) {
|
|
143
|
+
try {
|
|
144
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
145
|
+
} catch (err) {
|
|
146
|
+
/* left for the next run to clear */
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function findPlaywrightCli() {
|
|
151
|
+
try {
|
|
152
|
+
return require.resolve('@playwright/test/cli', { paths: [PACKAGE_DIR] });
|
|
153
|
+
} catch (err) {
|
|
154
|
+
/* fall through */
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
return path.join(path.dirname(require.resolve('@playwright/test/package.json', { paths: [PACKAGE_DIR] })), 'cli.js');
|
|
158
|
+
} catch (err) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// A stopped run takes its browsers with it. On Windows killing a process
|
|
164
|
+
// leaves its children running, so the whole tree is ended.
|
|
165
|
+
function killTree(child) {
|
|
166
|
+
if (!child || child.exitCode !== null) return;
|
|
167
|
+
if (process.platform === 'win32') {
|
|
168
|
+
spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' });
|
|
169
|
+
} else {
|
|
170
|
+
try {
|
|
171
|
+
child.kill('SIGTERM');
|
|
172
|
+
} catch (err) {
|
|
173
|
+
/* already gone */
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Writes these probes and runs them, one at a time. Returns a handle:
|
|
180
|
+
* handle.done -> Promise<{ code, stopped, results: { id: result } }>
|
|
181
|
+
* handle.stop() -> ends the run now; what finished is kept
|
|
182
|
+
* `say` gets a line per finished run ("3 of 12: ..."), and `labels` turns an
|
|
183
|
+
* id into the words said for it.
|
|
184
|
+
*/
|
|
185
|
+
function runProbes(root, probes, options) {
|
|
186
|
+
const opts = options || {};
|
|
187
|
+
const say = typeof opts.say === 'function' ? opts.say : () => {};
|
|
188
|
+
const hide = typeof opts.hide === 'function' ? opts.hide : (s) => s;
|
|
189
|
+
const runDir = path.join(probeDirFor(root, opts.env), crypto.randomBytes(4).toString('hex'));
|
|
190
|
+
const outDir = path.join(runDir, 'results');
|
|
191
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
192
|
+
for (const p of probes) fs.writeFileSync(path.join(runDir, p.id + '.kxprobe.js'), p.source, 'utf8');
|
|
193
|
+
|
|
194
|
+
let child = null;
|
|
195
|
+
let stopped = false;
|
|
196
|
+
const cli = findPlaywrightCli();
|
|
197
|
+
const done = new Promise((resolve) => {
|
|
198
|
+
const finish = (code) => {
|
|
199
|
+
const results = {};
|
|
200
|
+
for (const p of probes) {
|
|
201
|
+
try {
|
|
202
|
+
results[p.id] = JSON.parse(fs.readFileSync(path.join(outDir, p.id + '.json'), 'utf8'));
|
|
203
|
+
} catch (err) {
|
|
204
|
+
/* not finished: said as not tested by whoever reads this */
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (!opts.keep) removeDir(runDir);
|
|
208
|
+
resolve({ code: code, stopped: stopped, results: results });
|
|
209
|
+
};
|
|
210
|
+
if (!cli) {
|
|
211
|
+
say('The testing engine is missing from this install. Reinstalling usually fixes it: npm install -D kryptheon');
|
|
212
|
+
return finish(null);
|
|
213
|
+
}
|
|
214
|
+
if (stopped || !probes.length) return finish(null);
|
|
215
|
+
const env = Object.assign({}, process.env, opts.childEnv || {}, {
|
|
216
|
+
KRYPTHEON_PROBE_DIR: runDir,
|
|
217
|
+
KRYPTHEON_PROBE_OUT: outDir,
|
|
218
|
+
KRYPTHEON_PROBE_SECRETS: (opts.secretNames || []).join(','),
|
|
219
|
+
});
|
|
220
|
+
if (opts.gapMs != null) env.KRYPTHEON_PROBE_GAP_MS = String(opts.gapMs);
|
|
221
|
+
child = spawn(process.execPath, [cli, 'test', '--config', CONFIG], {
|
|
222
|
+
cwd: root, env: env, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true,
|
|
223
|
+
});
|
|
224
|
+
const pending = { out: '', err: '' };
|
|
225
|
+
const feed = (which) => (chunk) => {
|
|
226
|
+
pending[which] += chunk.toString();
|
|
227
|
+
const parts = pending[which].split(/\r?\n/);
|
|
228
|
+
pending[which] = parts.pop();
|
|
229
|
+
for (const line of parts) heard(line);
|
|
230
|
+
};
|
|
231
|
+
const heard = (line) => {
|
|
232
|
+
const m = /^KXPROBE (\d+) (\d+) (\S+) (\S+)/.exec(line);
|
|
233
|
+
if (m) {
|
|
234
|
+
const label = (opts.labels && opts.labels[m[3]]) || m[3];
|
|
235
|
+
say(' ' + m[1] + ' of ' + m[2] + ': ' + label + (m[4] === 'interrupted' ? ' - stopped' : ''));
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const e = /^KXPROBE-ERROR (.*)$/.exec(line);
|
|
239
|
+
if (e) say(' The run could not start: ' + hide(e[1]));
|
|
240
|
+
// Anything else Playwright prints is its own business - stack traces
|
|
241
|
+
// and the like - and is not passed on.
|
|
242
|
+
};
|
|
243
|
+
child.stdout.on('data', feed('out'));
|
|
244
|
+
child.stderr.on('data', feed('err'));
|
|
245
|
+
child.on('error', (err) => {
|
|
246
|
+
say('Could not start the browser run: ' + err.message);
|
|
247
|
+
finish(null);
|
|
248
|
+
});
|
|
249
|
+
child.on('close', (code) => {
|
|
250
|
+
if (pending.out) heard(pending.out);
|
|
251
|
+
finish(code);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
return {
|
|
255
|
+
done: done,
|
|
256
|
+
stop() {
|
|
257
|
+
stopped = true;
|
|
258
|
+
killTree(child);
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Each probe check keeps its last press in the store under this name.
|
|
264
|
+
const REPORT_FILES = { edge: 'edge-last.json', reliability: 'reliability-last.json' };
|
|
265
|
+
|
|
266
|
+
function reportFile(root, check, env) {
|
|
267
|
+
return path.join(store.pathsFor(root, env).dir, REPORT_FILES[check]);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function loadReport(root, check, env) {
|
|
271
|
+
try {
|
|
272
|
+
return JSON.parse(fs.readFileSync(reportFile(root, check, env), 'utf8'));
|
|
273
|
+
} catch (err) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Every address a press would reach: the pages the recordings open, and the
|
|
280
|
+
* places any earlier plain replay - of either check - saw the app save to.
|
|
281
|
+
* This is what the person is asked about, so a database somewhere else is
|
|
282
|
+
* named before it is written to.
|
|
283
|
+
*/
|
|
284
|
+
function knownAddresses(root, sources, env) {
|
|
285
|
+
const out = [];
|
|
286
|
+
for (const source of sources) for (const a of addressesOf(source)) if (!out.includes(a)) out.push(a);
|
|
287
|
+
for (const check of Object.keys(REPORT_FILES)) {
|
|
288
|
+
const last = loadReport(root, check, env);
|
|
289
|
+
// Only hosts the page itself saved to. A report from before that rule
|
|
290
|
+
// also holds the hosts an embedded checkout called on its own (round 18:
|
|
291
|
+
// Razorpay's, Stripe's, Sentry's), which would keep asking for a false
|
|
292
|
+
// yes; it is not read, and the next plain replay - which still holds the
|
|
293
|
+
// unusual values back from any remote host not said yes to - learns the
|
|
294
|
+
// real ones again.
|
|
295
|
+
if (!last || last.writeHostsFrom !== 'page+vendor') continue;
|
|
296
|
+
for (const h of last.writeHosts || []) if (!out.includes(h)) out.push(h);
|
|
297
|
+
}
|
|
298
|
+
return out;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** The request a write went to, said the way a person reads it. */
|
|
302
|
+
function writeWords(w) {
|
|
303
|
+
return w.method + ' ' + (w.host ? w.host : '') + w.path + ' -> ' + w.status + (w.to ? ' (to ' + w.to + ')' : '');
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// A redirect to one of these is the app turning the write away: to sign in,
|
|
307
|
+
// or back with an error in the address.
|
|
308
|
+
const SIGN_IN_PAGE = /^\/?(.*\/)?(log-?in|sign-?in|auth)(\/|$|\?|#)/i;
|
|
309
|
+
const ERROR_IN_ADDRESS = /[?&#](error|errors|err|invalid|fail|failed|failure)(=|&|$)/i;
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Whether the app took a write. A 2xx, or - for a form that posts and then
|
|
313
|
+
* redirects (Post, Redirect, Get) - a redirect that is not to the sign-in
|
|
314
|
+
* page and carries no error in its address. A redirect with no address seen
|
|
315
|
+
* is not counted either way: not taken. Found on blind round 19: on an app of
|
|
316
|
+
* plain HTML forms every save answered 302, and each was read as refused.
|
|
317
|
+
*/
|
|
318
|
+
function accepted(w) {
|
|
319
|
+
if (!w) return false;
|
|
320
|
+
if (w.status >= 200 && w.status < 300) return true;
|
|
321
|
+
if (!(w.status >= 300 && w.status < 400) || w.status === 304 || !w.to) return false;
|
|
322
|
+
return !SIGN_IN_PAGE.test(w.to) && !ERROR_IN_ADDRESS.test(w.to);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* A redirect straight back to the page the form was on - whatever message it
|
|
327
|
+
* carries in its address: a save that shows the page again, or a form sent
|
|
328
|
+
* back to be filled in - the answer alone does not say which. Found on blind
|
|
329
|
+
* round 21: /courses/new?notice=Seat limit must be... was not seen as the
|
|
330
|
+
* form's own page, and a dozen refusals were said as saves.
|
|
331
|
+
*/
|
|
332
|
+
function backToForm(w) {
|
|
333
|
+
const page = (u) => String(u || '').split(/[?#]/)[0];
|
|
334
|
+
return Boolean(w && w.to && w.from && w.status >= 300 && w.status < 400 && page(w.to) === page(w.from));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The address a redirect sent the browser to, with its numbers taken out:
|
|
339
|
+
* two answers that went to the same place with the same message are the same
|
|
340
|
+
* answer; /team?notice=Invitation sent and /team?notice=Enter a valid email
|
|
341
|
+
* are not.
|
|
342
|
+
*/
|
|
343
|
+
function answerShape(w) {
|
|
344
|
+
return String((w && w.to) || '').replace(/\d+/g, '#');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
module.exports = {
|
|
348
|
+
MAX_RUNS: MAX_RUNS,
|
|
349
|
+
TEST_WORD: TEST_WORD,
|
|
350
|
+
MARKER: MARKER,
|
|
351
|
+
FIXTURE: FIXTURE,
|
|
352
|
+
isLocalAddress: isLocalAddress,
|
|
353
|
+
addressesOf: addressesOf,
|
|
354
|
+
liveLooking: liveLooking,
|
|
355
|
+
consentLines: consentLines,
|
|
356
|
+
probeSource: probeSource,
|
|
357
|
+
probeId: probeId,
|
|
358
|
+
probeDirFor: probeDirFor,
|
|
359
|
+
runProbes: runProbes,
|
|
360
|
+
writeWords: writeWords,
|
|
361
|
+
accepted: accepted,
|
|
362
|
+
backToForm: backToForm,
|
|
363
|
+
answerShape: answerShape,
|
|
364
|
+
REPORT_FILES: REPORT_FILES,
|
|
365
|
+
reportFile: reportFile,
|
|
366
|
+
loadReport: loadReport,
|
|
367
|
+
knownAddresses: knownAddresses,
|
|
368
|
+
};
|