kryptheon 0.1.43 → 0.1.46
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 +40 -0
- package/bin/kryptheon.js +3436 -3242
- package/kryptheon-code.js +328 -6
- package/kryptheon-dashboard-page.js +101 -10
- package/kryptheon-dashboard.html +7 -1
- package/kryptheon-dashboard.js +1121 -1009
- package/kryptheon-edge.js +777 -0
- package/kryptheon-look.js +1 -1
- package/kryptheon-probe-fixture.js +267 -0
- package/kryptheon-probe-reporter.js +29 -0
- package/kryptheon-probe.config.js +37 -0
- package/kryptheon-probe.js +274 -0
- package/kryptheon-replay.js +0 -0
- package/package.json +10 -4
package/kryptheon-look.js
CHANGED
|
@@ -44,7 +44,7 @@ function look(root, runners, options) {
|
|
|
44
44
|
const wanted = (diff.first ? ['frontend', 'regression', 'data'] : diff.checks).slice();
|
|
45
45
|
for (const id of opts.force || []) if (!wanted.includes(id)) wanted.push(id);
|
|
46
46
|
const plan = wanted.map((id) => {
|
|
47
|
-
if (!available.has(id)) return { id: id, state: '
|
|
47
|
+
if (!available.has(id)) return { id: id, state: 'coming soon' };
|
|
48
48
|
if (!enabled[id]) return { id: id, state: 'switched off in the dashboard' };
|
|
49
49
|
if (id === 'data') return { id: id, state: 'needs your database - run npx kryptheon-night' };
|
|
50
50
|
if (id === 'regression' && !before.recordings) return { id: id, state: 'no recordings yet - npx kryptheon record <url>' };
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// The fixture the probe copies run under - never the recordings themselves.
|
|
2
|
+
//
|
|
3
|
+
// A probe is a copy of a recording, changed on purpose: one field given an
|
|
4
|
+
// unusual value (kryptheon-edge.js), or one step clicked twice. What matters
|
|
5
|
+
// in such a run is not whether it passed - a form that refuses a bad value is
|
|
6
|
+
// doing its job - but what the app did: did the page throw, did the server
|
|
7
|
+
// answer 5xx, what did the app write, and did a tag typed into a field come
|
|
8
|
+
// back as real markup. This writes all of that to one JSON file per run.
|
|
9
|
+
//
|
|
10
|
+
// It never touches the baselines or the history: a probe is not a check of
|
|
11
|
+
// the recorded flow, and must never change what "the last time it worked"
|
|
12
|
+
// means for one.
|
|
13
|
+
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const base = require('@playwright/test');
|
|
17
|
+
|
|
18
|
+
const OUT = process.env.KRYPTHEON_PROBE_OUT || '';
|
|
19
|
+
const GAP_MS = Number(process.env.KRYPTHEON_PROBE_GAP_MS || 1000);
|
|
20
|
+
// After a "too many requests", the app is asked to wait this long before the
|
|
21
|
+
// next run, so one 429 does not turn every run after it into another.
|
|
22
|
+
const BACKOFF_MS = Number(process.env.KRYPTHEON_PROBE_BACKOFF_MS || 10000);
|
|
23
|
+
const MAX_BODY = 20000;
|
|
24
|
+
const WRITE = /^(POST|PUT|PATCH|DELETE)$/;
|
|
25
|
+
// Values under these names are never kept, whatever request they were in.
|
|
26
|
+
const SECRET_KEY = /(pass(word|code)?|secret|token|otp|card|cvv|cvc|\bpin\b|api[_-]?key|authorization)/i;
|
|
27
|
+
const MARKER = require('./kryptheon-probe.js').MARKER;
|
|
28
|
+
|
|
29
|
+
function sleep(ms) {
|
|
30
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Every value the recording reads from .env. They are what a sign-in sends,
|
|
34
|
+
// and they must not reach a results file in any form.
|
|
35
|
+
function secretValues() {
|
|
36
|
+
return String(process.env.KRYPTHEON_PROBE_SECRETS || '')
|
|
37
|
+
.split(',')
|
|
38
|
+
.map((name) => name.trim())
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.map((name) => process.env[name])
|
|
41
|
+
.filter((v) => typeof v === 'string' && v.length >= 3);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function hideValues(text, secrets) {
|
|
45
|
+
let s = String(text == null ? '' : text);
|
|
46
|
+
for (const v of secrets) s = s.split(v).join('[hidden]');
|
|
47
|
+
// encodeURIComponent: a form post carries the password encoded.
|
|
48
|
+
for (const v of secrets) s = s.split(encodeURIComponent(v)).join('[hidden]');
|
|
49
|
+
return s;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function hideKeys(value) {
|
|
53
|
+
if (Array.isArray(value)) return value.map(hideKeys);
|
|
54
|
+
if (value && typeof value === 'object') {
|
|
55
|
+
const out = {};
|
|
56
|
+
for (const k of Object.keys(value)) out[k] = SECRET_KEY.test(k) ? '[hidden]' : hideKeys(value[k]);
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A request body as it may be kept: secrets taken out, by value and by name.
|
|
64
|
+
* JSON and form posts are kept as data, so a later run can be compared field
|
|
65
|
+
* by field; anything else only as text.
|
|
66
|
+
*/
|
|
67
|
+
function keptBody(raw, contentType, secrets) {
|
|
68
|
+
if (raw == null || raw === '') return { text: '' };
|
|
69
|
+
const text = String(raw);
|
|
70
|
+
if (/json/i.test(contentType || '') || /^\s*[[{]/.test(text)) {
|
|
71
|
+
try {
|
|
72
|
+
const data = hideKeys(JSON.parse(text));
|
|
73
|
+
return { data: JSON.parse(hideValues(JSON.stringify(data), secrets)) };
|
|
74
|
+
} catch (err) {
|
|
75
|
+
/* not JSON after all */
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (/x-www-form-urlencoded/i.test(contentType || '')) {
|
|
79
|
+
const data = {};
|
|
80
|
+
for (const [k, v] of new URLSearchParams(text)) data[k] = SECRET_KEY.test(k) ? '[hidden]' : v;
|
|
81
|
+
return { data: JSON.parse(hideValues(JSON.stringify(data), secrets)) };
|
|
82
|
+
}
|
|
83
|
+
return { text: hideValues(text, secrets).slice(0, MAX_BODY) };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function where(url) {
|
|
87
|
+
try {
|
|
88
|
+
const u = new URL(url);
|
|
89
|
+
return { host: u.host, origin: u.origin, path: u.pathname };
|
|
90
|
+
} catch (err) {
|
|
91
|
+
return { host: '', origin: '', path: String(url || '').split('?')[0] };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function firstLine(text) {
|
|
96
|
+
return String(text || '').replace(/\x1b\[[0-9;]*[A-Za-z]/g, '').split(/\r?\n/).map((l) => l.trim()).filter(Boolean)[0] || '';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Every element the app made out of a probe tag. An attribute or a text node
|
|
100
|
+
// holding the same characters is not one: only markup the browser parsed is.
|
|
101
|
+
async function findMarkers(page) {
|
|
102
|
+
try {
|
|
103
|
+
return await page.evaluate((attr) => Array.prototype.slice.call(document.querySelectorAll('[' + attr + ']')).slice(0, 10).map((el) => ({
|
|
104
|
+
id: el.getAttribute(attr),
|
|
105
|
+
tag: el.tagName.toLowerCase(),
|
|
106
|
+
html: el.outerHTML.slice(0, 200),
|
|
107
|
+
path: location.pathname,
|
|
108
|
+
})), MARKER);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const test = base.test.extend({
|
|
115
|
+
page: async ({ page }, use, testInfo) => {
|
|
116
|
+
const secrets = secretValues();
|
|
117
|
+
const events = [];
|
|
118
|
+
const fields = {};
|
|
119
|
+
let seq = 0;
|
|
120
|
+
const add = (e) => events.push(Object.assign({ seq: ++seq }, e));
|
|
121
|
+
|
|
122
|
+
// A gap before every run, and a longer one after the app said "too many
|
|
123
|
+
// requests": the app is being asked many times in a row, and a rate limit
|
|
124
|
+
// is the app protecting itself, not a problem in it.
|
|
125
|
+
let backoff = false;
|
|
126
|
+
try {
|
|
127
|
+
backoff = Boolean(OUT) && fs.existsSync(path.join(OUT, 'rate-limited'));
|
|
128
|
+
} catch (err) {
|
|
129
|
+
backoff = false;
|
|
130
|
+
}
|
|
131
|
+
await sleep(backoff ? BACKOFF_MS : GAP_MS);
|
|
132
|
+
|
|
133
|
+
page.on('pageerror', (err) => add({ kind: 'error', text: hideValues(firstLine((err && err.message) || String(err)), secrets).slice(0, 300) }));
|
|
134
|
+
page.on('response', (res) => {
|
|
135
|
+
const req = res.request();
|
|
136
|
+
const method = req.method();
|
|
137
|
+
const status = res.status();
|
|
138
|
+
const at = where(res.url());
|
|
139
|
+
if (status === 429) {
|
|
140
|
+
add({ kind: 'limited', method: method, host: at.host, path: at.path, status: status });
|
|
141
|
+
try {
|
|
142
|
+
if (OUT) fs.writeFileSync(path.join(OUT, 'rate-limited'), '1');
|
|
143
|
+
} catch (err) {
|
|
144
|
+
/* a note, never a failure */
|
|
145
|
+
}
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (status >= 500) add({ kind: 'server', method: method, host: at.host, path: at.path, status: status });
|
|
149
|
+
if (WRITE.test(method)) {
|
|
150
|
+
let raw = null;
|
|
151
|
+
try {
|
|
152
|
+
raw = req.postData();
|
|
153
|
+
} catch (err) {
|
|
154
|
+
raw = null;
|
|
155
|
+
}
|
|
156
|
+
const type = (req.headers() || {})['content-type'] || '';
|
|
157
|
+
const kept = keptBody(raw, type, secrets);
|
|
158
|
+
// A write that carried a secret is the sign-in, not data the app
|
|
159
|
+
// keeps - it is never listed as test data left behind.
|
|
160
|
+
const signIn = JSON.stringify(kept).indexOf('[hidden]') !== -1;
|
|
161
|
+
add(Object.assign({ kind: 'write', method: method, host: at.host, origin: at.origin, path: at.path, status: status, signIn: signIn }, kept));
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Called from inside a probe copy, around the steps that matter.
|
|
166
|
+
page.__kxMark = async (name) => {
|
|
167
|
+
// Let what the last step started arrive before it is counted.
|
|
168
|
+
try {
|
|
169
|
+
await page.waitForLoadState('networkidle', { timeout: 3000 });
|
|
170
|
+
} catch (err) {
|
|
171
|
+
/* a page that never goes quiet is still marked */
|
|
172
|
+
}
|
|
173
|
+
add({ kind: 'mark', name: String(name) });
|
|
174
|
+
};
|
|
175
|
+
// What kind of field a recorded fill typed into, read from the page
|
|
176
|
+
// itself: whether it is required, a number, a date, how long it may be.
|
|
177
|
+
page.__kxField = async (line, locator) => {
|
|
178
|
+
try {
|
|
179
|
+
fields[line] = await locator.first().evaluate((el) => {
|
|
180
|
+
const label = (el.labels && el.labels[0] && el.labels[0].textContent) || '';
|
|
181
|
+
return {
|
|
182
|
+
tag: el.tagName.toLowerCase(),
|
|
183
|
+
type: (el.getAttribute('type') || '').toLowerCase(),
|
|
184
|
+
required: el.required === true || el.getAttribute('aria-required') === 'true' || /\*\s*$/.test(label.trim()),
|
|
185
|
+
min: el.getAttribute('min'),
|
|
186
|
+
maxLength: el.maxLength > 0 ? el.maxLength : null,
|
|
187
|
+
pattern: el.getAttribute('pattern'),
|
|
188
|
+
label: label.replace(/\s+/g, ' ').trim().slice(0, 80),
|
|
189
|
+
placeholder: (el.getAttribute('placeholder') || '').slice(0, 80),
|
|
190
|
+
name: (el.getAttribute('name') || el.id || '').slice(0, 80),
|
|
191
|
+
};
|
|
192
|
+
}, null, { timeout: 5000 });
|
|
193
|
+
} catch (err) {
|
|
194
|
+
fields[line] = null; // not knowing is never read as "not required"
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// Writes still on their way. A recording often ends right after the step
|
|
199
|
+
// that saves, and the answer to that save is the proof - so the run waits
|
|
200
|
+
// for it before it looks at anything or opens the page again, which would
|
|
201
|
+
// cut the answer off.
|
|
202
|
+
const inFlight = new Set();
|
|
203
|
+
page.on('request', (req) => {
|
|
204
|
+
if (WRITE.test(req.method())) inFlight.add(req);
|
|
205
|
+
});
|
|
206
|
+
const settled = (req) => inFlight.delete(req);
|
|
207
|
+
page.on('requestfinished', settled);
|
|
208
|
+
page.on('requestfailed', settled);
|
|
209
|
+
|
|
210
|
+
const started = Date.now();
|
|
211
|
+
await use(page);
|
|
212
|
+
|
|
213
|
+
for (let waited = 0; inFlight.size && waited < 10000; waited += 100) await sleep(100);
|
|
214
|
+
try {
|
|
215
|
+
await page.waitForLoadState('networkidle', { timeout: 3000 });
|
|
216
|
+
} catch (err) {
|
|
217
|
+
/* a page that never goes quiet is still read */
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Markup made from a probe tag: on the page the flow ended on, and on the
|
|
221
|
+
// same address opened again - which is what proves it was saved and is
|
|
222
|
+
// shown to whoever opens it next. A GET, never a reload: reloading a page
|
|
223
|
+
// that came from a form post would send the form again.
|
|
224
|
+
const markers = { live: await findMarkers(page), reopened: [] };
|
|
225
|
+
try {
|
|
226
|
+
const url = page.url();
|
|
227
|
+
if (/^https?:/.test(url)) {
|
|
228
|
+
await page.goto(url, { waitUntil: 'load', timeout: 15000 });
|
|
229
|
+
try {
|
|
230
|
+
await page.waitForLoadState('networkidle', { timeout: 5000 });
|
|
231
|
+
} catch (err) {
|
|
232
|
+
/* the markers are read either way */
|
|
233
|
+
}
|
|
234
|
+
markers.reopened = await findMarkers(page);
|
|
235
|
+
}
|
|
236
|
+
} catch (err) {
|
|
237
|
+
/* the page could not be opened again; what was live still counts */
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const error = testInfo.error || (testInfo.errors && testInfo.errors[0]) || null;
|
|
241
|
+
const file = path.basename(testInfo.file);
|
|
242
|
+
let failedLine = null;
|
|
243
|
+
if (error && error.stack) {
|
|
244
|
+
const m = new RegExp(file.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ':(\\d+)').exec(error.stack);
|
|
245
|
+
if (m) failedLine = Number(m[1]);
|
|
246
|
+
}
|
|
247
|
+
const result = {
|
|
248
|
+
id: file.replace(/\.kxprobe\.js$/, ''),
|
|
249
|
+
status: testInfo.status,
|
|
250
|
+
error: error ? hideValues(firstLine(error.message), secrets).slice(0, 300) : null,
|
|
251
|
+
failedLine: failedLine,
|
|
252
|
+
events: events,
|
|
253
|
+
fields: fields,
|
|
254
|
+
markers: markers,
|
|
255
|
+
ms: Date.now() - started,
|
|
256
|
+
};
|
|
257
|
+
if (OUT) {
|
|
258
|
+
try {
|
|
259
|
+
fs.writeFileSync(path.join(OUT, result.id + '.json'), JSON.stringify(result), 'utf8');
|
|
260
|
+
} catch (err) {
|
|
261
|
+
/* the run is then reported as not finished, never as clean */
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
module.exports = { test: test, expect: base.expect, keptBody: keptBody, hideValues: hideValues, MARKER: MARKER };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Says one line per probe run as it ends, in a form kryptheon-probe.js reads
|
|
2
|
+
// back and turns into words: "KXPROBE <done> <total> <id> <status>". Nothing
|
|
3
|
+
// else - a probe that fails is usually the app refusing a bad value, and a
|
|
4
|
+
// failure report for it would say the opposite of what happened.
|
|
5
|
+
|
|
6
|
+
class ProbeReporter {
|
|
7
|
+
onBegin(config, suite) {
|
|
8
|
+
this.total = suite.allTests().length;
|
|
9
|
+
this.done = 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
onTestEnd(test, result) {
|
|
13
|
+
this.done += 1;
|
|
14
|
+
const id = String(test.location.file).replace(/^.*[\\/]/, '').replace(/\.kxprobe\.js$/, '');
|
|
15
|
+
console.log('KXPROBE ' + this.done + ' ' + this.total + ' ' + id + ' ' + result.status);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
onError(error) {
|
|
19
|
+
// The run itself could not start - a broken copy, a missing browser.
|
|
20
|
+
const text = String((error && error.message) || error).split(/\r?\n/)[0];
|
|
21
|
+
console.log('KXPROBE-ERROR ' + text.slice(0, 300));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
printsToStdio() {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = ProbeReporter;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The Playwright config for probe copies (kryptheon-probe.js). Kept apart from
|
|
3
|
+
// playwright.config.js on purpose: probes live in the store, not in tests/;
|
|
4
|
+
// they report through their own reporter; and their screenshots and traces
|
|
5
|
+
// go to a folder of their own - Playwright empties its output folder on every
|
|
6
|
+
// run, and the last check's evidence must not be wiped by a probe.
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const { defineConfig } = require('@playwright/test');
|
|
9
|
+
|
|
10
|
+
const PACKAGE_DIR = __dirname;
|
|
11
|
+
const USER_DIR = process.cwd();
|
|
12
|
+
const PROBE_DIR = process.env.KRYPTHEON_PROBE_DIR || path.join(USER_DIR, 'tests');
|
|
13
|
+
|
|
14
|
+
// The same .env reading as playwright.config.js, for the same reasons.
|
|
15
|
+
const secrets = require(path.join(PACKAGE_DIR, 'kryptheon-secrets.js'));
|
|
16
|
+
const values = secrets.readEnvFile(path.join(USER_DIR, '.env'));
|
|
17
|
+
for (const name of Object.keys(values)) {
|
|
18
|
+
if (!secrets.isSet(process.env[name])) process.env[name] = values[name];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = defineConfig({
|
|
22
|
+
testDir: PROBE_DIR,
|
|
23
|
+
testMatch: ['**/*.kxprobe.js'],
|
|
24
|
+
outputDir: path.join(PROBE_DIR, 'output'),
|
|
25
|
+
fullyParallel: false,
|
|
26
|
+
workers: 1,
|
|
27
|
+
retries: 0,
|
|
28
|
+
// Room for the gap before each run and for opening the page again after it.
|
|
29
|
+
timeout: 60000,
|
|
30
|
+
reporter: [[path.join(PACKAGE_DIR, 'kryptheon-probe-reporter.js')]],
|
|
31
|
+
use: {
|
|
32
|
+
headless: true,
|
|
33
|
+
channel: 'chromium',
|
|
34
|
+
screenshot: 'off',
|
|
35
|
+
trace: 'off',
|
|
36
|
+
},
|
|
37
|
+
});
|
|
@@ -0,0 +1,274 @@
|
|
|
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) {
|
|
84
|
+
const live = liveLooking(addresses);
|
|
85
|
+
const lines = [
|
|
86
|
+
what,
|
|
87
|
+
'',
|
|
88
|
+
'It runs against: ' + (addresses.length ? addresses.join(', ') : 'the addresses in your recordings'),
|
|
89
|
+
'It ADDS TEST DATA to your app, and that data STAYS there - Kryptheon does not delete it.',
|
|
90
|
+
'Most of it contains the word "' + TEST_WORD + '", so you can find it. Empty, negative and date values cannot carry it;',
|
|
91
|
+
'every write that was accepted is listed when it ends.',
|
|
92
|
+
'It never creates an account, and uses only the sign-in you recorded.',
|
|
93
|
+
];
|
|
94
|
+
if (live.length) {
|
|
95
|
+
lines.push('');
|
|
96
|
+
lines.push('WARNING: ' + live.join(', ') + (live.length === 1 ? ' is' : ' are') + ' not this computer. If that is your live app,');
|
|
97
|
+
lines.push('real people will see this test data. Run it only against your own test copy.');
|
|
98
|
+
}
|
|
99
|
+
return lines;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A recording turned into a probe: the same steps, run under the probe
|
|
104
|
+
* fixture. `edits` changes single lines - by original line number - and
|
|
105
|
+
* `before` puts lines in front of one. Line numbers are always the
|
|
106
|
+
* recording's own, so a finding points at the line the person can open.
|
|
107
|
+
*/
|
|
108
|
+
function probeSource(source, options) {
|
|
109
|
+
const opts = options || {};
|
|
110
|
+
const fixture = JSON.stringify(FIXTURE.split(path.sep).join('/'));
|
|
111
|
+
const lines = String(source || '').split('\n');
|
|
112
|
+
const out = [];
|
|
113
|
+
lines.forEach((line, i) => {
|
|
114
|
+
const n = i + 1;
|
|
115
|
+
let text = line.replace(/(\bfrom\s*|\brequire\(\s*)(['"])((?:[^'"]*[\\/])?kryptheon-fixture(?:\.js)?|kryptheon)\2/, (all, lead) => lead + fixture);
|
|
116
|
+
if (opts.before && opts.before[n]) for (const extra of [].concat(opts.before[n])) out.push(extra);
|
|
117
|
+
if (opts.edits && Object.prototype.hasOwnProperty.call(opts.edits, n)) text = opts.edits[n];
|
|
118
|
+
out.push(text);
|
|
119
|
+
if (opts.after && opts.after[n]) for (const extra of [].concat(opts.after[n])) out.push(extra);
|
|
120
|
+
});
|
|
121
|
+
return out.join('\n');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** A short id that is safe as a file name and unique within one run. */
|
|
125
|
+
function probeId(index, label) {
|
|
126
|
+
return String(index).padStart(3, '0') + '-' + String(label || 'probe').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 40);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The folder probes for this project are written to and run from. */
|
|
130
|
+
function probeDirFor(root, env) {
|
|
131
|
+
return path.join(store.pathsFor(root, env).dir, 'probes');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function removeDir(dir) {
|
|
135
|
+
try {
|
|
136
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
137
|
+
} catch (err) {
|
|
138
|
+
/* left for the next run to clear */
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function findPlaywrightCli() {
|
|
143
|
+
try {
|
|
144
|
+
return require.resolve('@playwright/test/cli', { paths: [PACKAGE_DIR] });
|
|
145
|
+
} catch (err) {
|
|
146
|
+
/* fall through */
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
return path.join(path.dirname(require.resolve('@playwright/test/package.json', { paths: [PACKAGE_DIR] })), 'cli.js');
|
|
150
|
+
} catch (err) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// A stopped run takes its browsers with it. On Windows killing a process
|
|
156
|
+
// leaves its children running, so the whole tree is ended.
|
|
157
|
+
function killTree(child) {
|
|
158
|
+
if (!child || child.exitCode !== null) return;
|
|
159
|
+
if (process.platform === 'win32') {
|
|
160
|
+
spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' });
|
|
161
|
+
} else {
|
|
162
|
+
try {
|
|
163
|
+
child.kill('SIGTERM');
|
|
164
|
+
} catch (err) {
|
|
165
|
+
/* already gone */
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Writes these probes and runs them, one at a time. Returns a handle:
|
|
172
|
+
* handle.done -> Promise<{ code, stopped, results: { id: result } }>
|
|
173
|
+
* handle.stop() -> ends the run now; what finished is kept
|
|
174
|
+
* `say` gets a line per finished run ("3 of 12: ..."), and `labels` turns an
|
|
175
|
+
* id into the words said for it.
|
|
176
|
+
*/
|
|
177
|
+
function runProbes(root, probes, options) {
|
|
178
|
+
const opts = options || {};
|
|
179
|
+
const say = typeof opts.say === 'function' ? opts.say : () => {};
|
|
180
|
+
const hide = typeof opts.hide === 'function' ? opts.hide : (s) => s;
|
|
181
|
+
const runDir = path.join(probeDirFor(root, opts.env), crypto.randomBytes(4).toString('hex'));
|
|
182
|
+
const outDir = path.join(runDir, 'results');
|
|
183
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
184
|
+
for (const p of probes) fs.writeFileSync(path.join(runDir, p.id + '.kxprobe.js'), p.source, 'utf8');
|
|
185
|
+
|
|
186
|
+
let child = null;
|
|
187
|
+
let stopped = false;
|
|
188
|
+
const cli = findPlaywrightCli();
|
|
189
|
+
const done = new Promise((resolve) => {
|
|
190
|
+
const finish = (code) => {
|
|
191
|
+
const results = {};
|
|
192
|
+
for (const p of probes) {
|
|
193
|
+
try {
|
|
194
|
+
results[p.id] = JSON.parse(fs.readFileSync(path.join(outDir, p.id + '.json'), 'utf8'));
|
|
195
|
+
} catch (err) {
|
|
196
|
+
/* not finished: said as not tested by whoever reads this */
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (!opts.keep) removeDir(runDir);
|
|
200
|
+
resolve({ code: code, stopped: stopped, results: results });
|
|
201
|
+
};
|
|
202
|
+
if (!cli) {
|
|
203
|
+
say('The testing engine is missing from this install. Reinstalling usually fixes it: npm install -D kryptheon');
|
|
204
|
+
return finish(null);
|
|
205
|
+
}
|
|
206
|
+
if (stopped || !probes.length) return finish(null);
|
|
207
|
+
const env = Object.assign({}, process.env, opts.childEnv || {}, {
|
|
208
|
+
KRYPTHEON_PROBE_DIR: runDir,
|
|
209
|
+
KRYPTHEON_PROBE_OUT: outDir,
|
|
210
|
+
KRYPTHEON_PROBE_SECRETS: (opts.secretNames || []).join(','),
|
|
211
|
+
});
|
|
212
|
+
if (opts.gapMs != null) env.KRYPTHEON_PROBE_GAP_MS = String(opts.gapMs);
|
|
213
|
+
child = spawn(process.execPath, [cli, 'test', '--config', CONFIG], {
|
|
214
|
+
cwd: root, env: env, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true,
|
|
215
|
+
});
|
|
216
|
+
const pending = { out: '', err: '' };
|
|
217
|
+
const feed = (which) => (chunk) => {
|
|
218
|
+
pending[which] += chunk.toString();
|
|
219
|
+
const parts = pending[which].split(/\r?\n/);
|
|
220
|
+
pending[which] = parts.pop();
|
|
221
|
+
for (const line of parts) heard(line);
|
|
222
|
+
};
|
|
223
|
+
const heard = (line) => {
|
|
224
|
+
const m = /^KXPROBE (\d+) (\d+) (\S+) (\S+)/.exec(line);
|
|
225
|
+
if (m) {
|
|
226
|
+
const label = (opts.labels && opts.labels[m[3]]) || m[3];
|
|
227
|
+
say(' ' + m[1] + ' of ' + m[2] + ': ' + label + (m[4] === 'interrupted' ? ' - stopped' : ''));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const e = /^KXPROBE-ERROR (.*)$/.exec(line);
|
|
231
|
+
if (e) say(' The run could not start: ' + hide(e[1]));
|
|
232
|
+
// Anything else Playwright prints is its own business - stack traces
|
|
233
|
+
// and the like - and is not passed on.
|
|
234
|
+
};
|
|
235
|
+
child.stdout.on('data', feed('out'));
|
|
236
|
+
child.stderr.on('data', feed('err'));
|
|
237
|
+
child.on('error', (err) => {
|
|
238
|
+
say('Could not start the browser run: ' + err.message);
|
|
239
|
+
finish(null);
|
|
240
|
+
});
|
|
241
|
+
child.on('close', (code) => {
|
|
242
|
+
if (pending.out) heard(pending.out);
|
|
243
|
+
finish(code);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
return {
|
|
247
|
+
done: done,
|
|
248
|
+
stop() {
|
|
249
|
+
stopped = true;
|
|
250
|
+
killTree(child);
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** The request a write went to, said the way a person reads it. */
|
|
256
|
+
function writeWords(w) {
|
|
257
|
+
return w.method + ' ' + (w.host ? w.host : '') + w.path + ' -> ' + w.status;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
module.exports = {
|
|
261
|
+
MAX_RUNS: MAX_RUNS,
|
|
262
|
+
TEST_WORD: TEST_WORD,
|
|
263
|
+
MARKER: MARKER,
|
|
264
|
+
FIXTURE: FIXTURE,
|
|
265
|
+
isLocalAddress: isLocalAddress,
|
|
266
|
+
addressesOf: addressesOf,
|
|
267
|
+
liveLooking: liveLooking,
|
|
268
|
+
consentLines: consentLines,
|
|
269
|
+
probeSource: probeSource,
|
|
270
|
+
probeId: probeId,
|
|
271
|
+
probeDirFor: probeDirFor,
|
|
272
|
+
runProbes: runProbes,
|
|
273
|
+
writeWords: writeWords,
|
|
274
|
+
};
|
package/kryptheon-replay.js
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kryptheon",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.46",
|
|
4
4
|
"description": "Record what you do in your app and get plain-language reports when it breaks.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -44,7 +44,12 @@
|
|
|
44
44
|
"kryptheon-archivo.woff2",
|
|
45
45
|
"kryptheon-changes.js",
|
|
46
46
|
"kryptheon-look.js",
|
|
47
|
-
"kryptheon-autostart.js"
|
|
47
|
+
"kryptheon-autostart.js",
|
|
48
|
+
"kryptheon-edge.js",
|
|
49
|
+
"kryptheon-probe.js",
|
|
50
|
+
"kryptheon-probe-fixture.js",
|
|
51
|
+
"kryptheon-probe-reporter.js",
|
|
52
|
+
"kryptheon-probe.config.js"
|
|
48
53
|
],
|
|
49
54
|
"engines": {
|
|
50
55
|
"node": ">=20.6.0"
|
|
@@ -53,7 +58,7 @@
|
|
|
53
58
|
"@playwright/test": "^1.62.1"
|
|
54
59
|
},
|
|
55
60
|
"scripts": {
|
|
56
|
-
"check": "node kryptheon-store.check.js && node kryptheon-code.check.js && node kryptheon-constdata.check.js && node kryptheon-listitems.check.js && node kryptheon-accept.check.js && node kryptheon-keys.check.js && node kryptheon-dashboard.check.js && node kryptheon-autostart.check.js && node kryptheon-changes.check.js && node kryptheon-look.check.js && node kryptheon-verify.check.js && node kryptheon-recheck.check.js && node kryptheon-reporter.check.js && node kryptheon-signature.check.js && node kryptheon-signature.stream.check.js && node kryptheon-signature.baseline.check.js && node kryptheon-signature.settle.check.js && node kryptheon-signature.number.check.js && node kryptheon-env.check.js && node kryptheon-watchdog.check.js && node kryptheon-phone.check.js && node kryptheon-homedash.check.js && node kryptheon-record.check.js && node kryptheon-selectors.check.js && node kryptheon-recordings.check.js && node kryptheon-testfiles.check.js && node kryptheon-cleanup.check.js && node kryptheon-orphan.check.js && node kryptheon-project.check.js && node kryptheon-setup.check.js && node kryptheon-browser.check.js && node kryptheon-heal.check.js && node kryptheon-load.check.js && node kryptheon-package.check.js",
|
|
61
|
+
"check": "node kryptheon-store.check.js && node kryptheon-code.check.js && node kryptheon-constdata.check.js && node kryptheon-listitems.check.js && node kryptheon-accept.check.js && node kryptheon-keys.check.js && node kryptheon-dashboard.check.js && node kryptheon-edge.check.js && node kryptheon-autostart.check.js && node kryptheon-changes.check.js && node kryptheon-look.check.js && node kryptheon-verify.check.js && node kryptheon-recheck.check.js && node kryptheon-reporter.check.js && node kryptheon-signature.check.js && node kryptheon-signature.stream.check.js && node kryptheon-signature.baseline.check.js && node kryptheon-signature.settle.check.js && node kryptheon-signature.number.check.js && node kryptheon-env.check.js && node kryptheon-watchdog.check.js && node kryptheon-phone.check.js && node kryptheon-homedash.check.js && node kryptheon-record.check.js && node kryptheon-selectors.check.js && node kryptheon-recordings.check.js && node kryptheon-testfiles.check.js && node kryptheon-cleanup.check.js && node kryptheon-orphan.check.js && node kryptheon-project.check.js && node kryptheon-setup.check.js && node kryptheon-browser.check.js && node kryptheon-heal.check.js && node kryptheon-load.check.js && node kryptheon-package.check.js",
|
|
57
62
|
"check:store": "node kryptheon-store.check.js",
|
|
58
63
|
"check:code": "node kryptheon-code.check.js",
|
|
59
64
|
"check:constdata": "node kryptheon-constdata.check.js",
|
|
@@ -84,7 +89,8 @@
|
|
|
84
89
|
"check:package": "node kryptheon-package.check.js",
|
|
85
90
|
"prepublishOnly": "node kryptheon-package.check.js",
|
|
86
91
|
"check:testfiles": "node kryptheon-testfiles.check.js",
|
|
87
|
-
"check:listitems": "node kryptheon-listitems.check.js"
|
|
92
|
+
"check:listitems": "node kryptheon-listitems.check.js",
|
|
93
|
+
"check:edge": "node kryptheon-edge.check.js"
|
|
88
94
|
},
|
|
89
95
|
"keywords": [
|
|
90
96
|
"playwright",
|