kryptheon 0.1.41 → 0.1.42

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/bin/kryptheon.js CHANGED
@@ -19,6 +19,7 @@ const secrets = require(require('path').join(__dirname, '..', 'kryptheon-secrets
19
19
  const replay = require('../kryptheon-replay.js');
20
20
  const selectors = require('../kryptheon-selectors.js');
21
21
  const recordings = require('../kryptheon-recordings.js');
22
+ const testfiles = require('../kryptheon-testfiles.js');
22
23
  const cleanup = require('../kryptheon-cleanup.js');
23
24
  const baselines = require('../kryptheon-baselines.js');
24
25
  const project = require('../kryptheon-project.js');
@@ -184,21 +185,11 @@ function ensureBrowser() {
184
185
  return true;
185
186
  }
186
187
 
188
+ // Only Kryptheon's recordings: the project's own unit tests in tests/ are not
189
+ // ours to run, count or call "not a real recording" (kryptheon-testfiles.js).
187
190
  function listSpecFiles() {
188
191
  if (!fs.existsSync(TESTS_DIR)) return [];
189
- let entries = [];
190
- try {
191
- entries = fs.readdirSync(TESTS_DIR, { recursive: true });
192
- } catch (err) {
193
- try {
194
- entries = fs.readdirSync(TESTS_DIR);
195
- } catch (err2) {
196
- return [];
197
- }
198
- }
199
- return entries
200
- .map(String)
201
- .filter((name) => /\.(spec|test)\.(c|m)?[jt]sx?$/.test(name));
192
+ return testfiles.recordingNames(TESTS_DIR);
202
193
  }
203
194
 
204
195
  // A plain GET on Node's own http/https, deliberately NOT fetch. fetch keeps
@@ -405,7 +396,7 @@ function timestampName() {
405
396
  'recorded-' +
406
397
  d.getFullYear() + pad(d.getMonth() + 1) + pad(d.getDate()) +
407
398
  '-' + pad(d.getHours()) + pad(d.getMinutes()) + pad(d.getSeconds()) +
408
- '.spec.js'
399
+ testfiles.recordingExtension(USER_DIR)
409
400
  );
410
401
  }
411
402
 
@@ -550,11 +541,12 @@ async function nameRecording(relativeFile) {
550
541
  const safeName = name.replace(/'/g, "\\'");
551
542
  const updated = src.replace(/\btest\(\s*(['"])test\1/, "test('" + safeName + "'");
552
543
 
553
- let target = path.join('tests', slugify(name) + '.spec.js');
544
+ const ext = testfiles.recordingExtension(USER_DIR);
545
+ let target = path.join('tests', slugify(name) + ext);
554
546
  let attempt = 1;
555
547
  while (fs.existsSync(path.join(USER_DIR, target)) && path.join(USER_DIR, target) !== full) {
556
548
  attempt += 1;
557
- target = path.join('tests', slugify(name) + '-' + attempt + '.spec.js');
549
+ target = path.join('tests', slugify(name) + '-' + attempt + ext);
558
550
  }
559
551
 
560
552
  try {
@@ -1837,6 +1829,21 @@ function check(options) {
1837
1829
  }
1838
1830
  }
1839
1831
 
1832
+ // A recording named *.spec.js in a project whose own test runner collects
1833
+ // *.spec.js too: that runner tries to run it and fails. Said, with the name
1834
+ // that only Kryptheon runs - the file is the person's, so not renamed here.
1835
+ const runner = testfiles.unitRunnerOf(USER_DIR);
1836
+ const clashing = runner ? triage.runnable.filter((name) => !/\.kryptheon\.js$/.test(name)) : [];
1837
+ if (clashing.length) {
1838
+ console.log('');
1839
+ console.log(' Your own tests (' + runner + ') also pick up ' + recordings.plural(clashing.length, 'recording') + ' here, and fail on ' +
1840
+ (clashing.length === 1 ? 'it' : 'them') + '.');
1841
+ console.log(' Rename ' + (clashing.length === 1 ? 'it' : 'each') + ' to end in .kryptheon.js - only Kryptheon runs those:');
1842
+ for (const name of clashing) {
1843
+ console.log(' ' + path.join('tests', name) + ' -> ' + path.join('tests', testfiles.bareName(name.replace(/\.(spec|test)\.[cm]?[jt]sx?$/, '')) + '.kryptheon.js'));
1844
+ }
1845
+ }
1846
+
1840
1847
  for (const name of triage.notRecordings) {
1841
1848
  console.log('');
1842
1849
  console.log(' Skipped ' + path.join('tests', name));
@@ -2425,6 +2432,23 @@ const NIGHT_WORDS = {
2425
2432
 
2426
2433
  const NIGHT_SAYS = { 0: 'nothing got through', 1: 'something got through - see Findings', 2: 'it could not check' };
2427
2434
 
2435
+ /**
2436
+ * The one line the dashboard shows for a database check. Exit 0 is "nothing
2437
+ * got through" only when everything was tested: found on blind round 13, the
2438
+ * page said "Result: nothing got through" while kryptheon-night itself said 7
2439
+ * tables could not be acted on at all. What it printed says how many.
2440
+ */
2441
+ function scanSummary(code, printed) {
2442
+ const said = NIGHT_SAYS[code] || 'it ended with exit ' + code;
2443
+ if (code === 2) return said;
2444
+ const text = String(printed || '').replace(/\s+/g, ' ');
2445
+ const m = /(\d+) (tables? (?:was|were) NOT checked|parts? of your app (?:was|were) not tested)/.exec(text);
2446
+ if (!m) return said;
2447
+ const n = Number(m[1]);
2448
+ return said + ' - but ' + n + (/^table/.test(m[2]) ? (n === 1 ? ' table was' : ' tables were') : (n === 1 ? ' part was' : ' parts were')) +
2449
+ ' not tested, so that is not a green light. See what it printed.';
2450
+ }
2451
+
2428
2452
  function runFromDashboard(id, opened, say) {
2429
2453
  if (id === 'frontend') {
2430
2454
  say('Reading the code in ' + USER_DIR + ' ...');
@@ -2486,9 +2510,11 @@ function recordFromDashboard(address, say, options) {
2486
2510
 
2487
2511
  /** One database action, through kryptheon-night, with the string in its environment only. */
2488
2512
  function databaseFromDashboard(action, connection, say) {
2489
- return nightChild(NIGHT_WORDS[action], childEnv({ KN_DATABASE_URL: connection }), say).then(({ code }) => {
2513
+ const heard = [];
2514
+ const listen = (line) => { heard.push(String(line)); say(line); };
2515
+ return nightChild(NIGHT_WORDS[action], childEnv({ KN_DATABASE_URL: connection }), listen).then(({ code }) => {
2490
2516
  if (code === null || code === undefined) return { ok: false, why: 'kryptheon-night could not be started' };
2491
- if (action === 'scan') return { ok: code !== 2, exit: code, summary: NIGHT_SAYS[code] || 'it ended with exit ' + code };
2517
+ if (action === 'scan') return { ok: code !== 2, exit: code, summary: scanSummary(code, heard.join('\n')) };
2492
2518
  return code === 2 && action !== 'night'
2493
2519
  ? { ok: false, exit: code, why: 'it could not do that - the lines above say why' }
2494
2520
  : code === 2 ? { ok: false, exit: code, why: 'nothing to read back - the lines above say why' }
@@ -180,7 +180,8 @@ function flowPrompt(t, lastPass) {
180
180
 
181
181
  function countRecordings(root) {
182
182
  try {
183
- return fs.readdirSync(path.join(root, 'tests')).filter((n) => /\.(spec|test)\.[cm]?[jt]s$/.test(n)).length;
183
+ // Recordings only - not the project's own unit tests in tests/.
184
+ return require('./kryptheon-testfiles.js').recordingNames(path.join(root, 'tests')).length;
184
185
  } catch (err) {
185
186
  return 0;
186
187
  }
@@ -667,7 +668,30 @@ function appAddress(value) {
667
668
  const text = value.trim();
668
669
  if (!text || text.length > 2000 || /[\s\x00-\x1f]/.test(text) || text.startsWith('-')) return null;
669
670
  if (/^[a-z][a-z0-9+.-]*:\/\//i.test(text)) return /^https?:\/\/[^/]/i.test(text) ? text : null;
670
- return /^[a-z0-9.-]+(:\d{1,5})?(\/\S*)?$/i.test(text) ? text : null;
671
+ // A path never starts with "//": "http//localhost:3000" is a mistyped
672
+ // scheme, not a host called "http".
673
+ return /^[a-z0-9.-]+(:\d{1,5})?(\/(?!\/)\S*)?$/i.test(text) ? text : null;
674
+ }
675
+
676
+ /**
677
+ * What was meant, when an address was typed with its start mistyped:
678
+ * "ttp://127.0.0.1:4180" (blind round 13, the first letter lost), "htp://",
679
+ * "http//", "http:/", "htps://". Null when it does not look like that.
680
+ */
681
+ function meantAddress(value) {
682
+ const text = String(value || '').trim();
683
+ const m = /^(h?t{1,3}p{1,2}(s?)|htps|hhttps?)(:\/{0,3}|\/{1,3}|:)(?!\/)(\S+)$/i.exec(text);
684
+ if (!m) return null;
685
+ const secure = /s$/i.test(m[1]);
686
+ const fixed = (secure ? 'https://' : 'http://') + m[4];
687
+ return fixed !== text && appAddress(fixed) ? fixed : null;
688
+ }
689
+
690
+ function notAnAddress(value, example) {
691
+ const meant = meantAddress(value);
692
+ return meant
693
+ ? 'that is not a web address - did you mean ' + meant + '?'
694
+ : 'that is not a web address - it should look like ' + example;
671
695
  }
672
696
 
673
697
  /** The connection string as given, or null when there is none or it is absurd. */
@@ -873,7 +897,7 @@ function createServer(root, options) {
873
897
  }
874
898
  if (url.pathname === '/api/record' && typeof opts.record === 'function') {
875
899
  const address = appAddress(input.url);
876
- if (!address) return json(res, 400, { ok: false, why: 'that is not a web address - it should look like http://localhost:3000' });
900
+ if (!address) return json(res, 400, { ok: false, why: notAnAddress(input.url, 'http://localhost:3000') });
877
901
  const phone = input.phone === true;
878
902
  return begin('record', 'Recording ' + address + (phone ? ' at phone size' : ''),
879
903
  'npx kryptheon record ' + address + (phone ? ' --phone' : ''), (say) => opts.record(address, say, { phone: phone }));
@@ -913,7 +937,7 @@ function createServer(root, options) {
913
937
  // outside check start from it. Empty clears it.
914
938
  const raw = typeof input.address === 'string' ? input.address.trim() : '';
915
939
  const address = raw ? appAddress(raw) : '';
916
- if (address === null) return json(res, 400, { ok: false, why: 'that is not a web address - it should look like https://my-app.lovable.app' });
940
+ if (address === null) return json(res, 400, { ok: false, why: notAnAddress(raw, 'https://my-app.lovable.app') });
917
941
  const full = address && !/^https?:\/\//i.test(address) ? 'https://' + address : address;
918
942
  return saveAppAddress(store.pathsFor(root, env), full)
919
943
  ? json(res, 200, { ok: true, address: full })
@@ -969,6 +993,7 @@ module.exports = {
969
993
  buildState: buildState,
970
994
  flowPrompt: flowPrompt,
971
995
  appAddress: appAddress,
996
+ meantAddress: meantAddress,
972
997
  secretsOf: secretsOf,
973
998
  hideSecrets: hideSecrets,
974
999
  hideSecretsIn: hideSecretsIn,
@@ -113,7 +113,7 @@ function chooseFromList(names, answer) {
113
113
  const lower = said.toLowerCase();
114
114
  const matches = list.filter((name) => {
115
115
  const n = String(name).toLowerCase();
116
- return n === lower || n === lower + '.spec.js' || n.replace(/\.spec\.js$/, '') === lower;
116
+ return n === lower || n === lower + '.spec.js' || n.replace(/\.(spec|kryptheon)\.js$/, '') === lower;
117
117
  });
118
118
  if (matches.length === 1) return { action: 'remove', names: matches };
119
119
  if (matches.length > 1) {
@@ -0,0 +1,98 @@
1
+ // Which files in tests/ are Kryptheon's recordings, and what a new one is
2
+ // called.
3
+ //
4
+ // Found in blind round 13 (a TypeScript service with vitest): its own unit
5
+ // test, tests/incidents.test.ts, was taken for a recording - check said it
6
+ // "only opens a page and stops", and the dashboard showed "Record your main
7
+ // flows: Done, 1 recording held" before anything was recorded. Then the
8
+ // first real recording, saved as tests/<name>.spec.js, was picked up by the
9
+ // project's own `npm test` (vitest), which failed on it: "Playwright Test did
10
+ // not expect test() to be called here". A tool that breaks the app's own
11
+ // tests is one nobody keeps.
12
+ //
13
+ // So: a recording is a file that uses Playwright or Kryptheon's fixture -
14
+ // anything else in tests/ belongs to the project and is left alone. And in a
15
+ // project whose unit-test runner collects *.spec.js / *.test.js, a new
16
+ // recording is saved as <name>.kryptheon.js, which only Kryptheon runs.
17
+
18
+ const fs = require('fs');
19
+ const path = require('path');
20
+
21
+ const SPEC_NAME = /\.(spec|test)\.[cm]?[jt]sx?$/;
22
+ const OWN_NAME = /\.kryptheon\.js$/;
23
+ // What a recording imports: Playwright directly (older or hand-made ones,
24
+ // repaired by check) or the fixture - by package name, or by any path that
25
+ // ends in kryptheon-fixture(.js), relative or absolute.
26
+ const USES_PLAYWRIGHT = /(['"`])(?:@playwright\/test|[^'"`\r\n]*kryptheon-fixture(?:\.js)?)\1/;
27
+
28
+ /** Whether this file in tests/ is one of Kryptheon's recordings. */
29
+ function isRecordingFile(name, source) {
30
+ if (!SPEC_NAME.test(name) && !OWN_NAME.test(name)) return false;
31
+ return USES_PLAYWRIGHT.test(String(source || ''));
32
+ }
33
+
34
+ /** The recordings in a tests folder, as paths relative to it. */
35
+ function recordingNames(testsDir) {
36
+ let entries = [];
37
+ try {
38
+ entries = fs.readdirSync(testsDir, { recursive: true });
39
+ } catch (err) {
40
+ try {
41
+ entries = fs.readdirSync(testsDir);
42
+ } catch (err2) {
43
+ return [];
44
+ }
45
+ }
46
+ return entries.map(String).filter((name) => {
47
+ if (!SPEC_NAME.test(name) && !OWN_NAME.test(name)) return false;
48
+ try {
49
+ return isRecordingFile(name, fs.readFileSync(path.join(testsDir, name), 'utf8'));
50
+ } catch (err) {
51
+ return false;
52
+ }
53
+ });
54
+ }
55
+
56
+ /**
57
+ * The unit-test runner this project uses that would also collect
58
+ * tests/*.spec.js or tests/*.test.js: 'vitest', 'jest', 'mocha' - or null.
59
+ */
60
+ function unitRunnerOf(projectDir) {
61
+ let pkg = {};
62
+ try {
63
+ pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), 'utf8'));
64
+ } catch (err) {
65
+ pkg = {};
66
+ }
67
+ const deps = Object.assign({}, pkg.dependencies, pkg.devDependencies);
68
+ const script = String((pkg.scripts && pkg.scripts.test) || '');
69
+ let files = [];
70
+ try {
71
+ files = fs.readdirSync(projectDir);
72
+ } catch (err) {
73
+ files = [];
74
+ }
75
+ const has = (re) => files.some((f) => re.test(f));
76
+ if (deps.vitest || /\bvitest\b/.test(script) || has(/^vitest\.config\./)) return 'vitest';
77
+ if (deps.jest || pkg.jest || /\bjest\b/.test(script) || has(/^jest\.config\./)) return 'jest';
78
+ if (deps.mocha || /\bmocha\b/.test(script) || has(/^\.mocharc/)) return 'mocha';
79
+ return null;
80
+ }
81
+
82
+ /** What a new recording's file name ends in, in this project. */
83
+ function recordingExtension(projectDir) {
84
+ return unitRunnerOf(projectDir) ? '.kryptheon.js' : '.spec.js';
85
+ }
86
+
87
+ /** A recording's name without its ending: "login" for login.spec.js or login.kryptheon.js. */
88
+ function bareName(name) {
89
+ return String(name).replace(OWN_NAME, '').replace(/\.spec\.js$/, '');
90
+ }
91
+
92
+ module.exports = {
93
+ isRecordingFile: isRecordingFile,
94
+ recordingNames: recordingNames,
95
+ unitRunnerOf: unitRunnerOf,
96
+ recordingExtension: recordingExtension,
97
+ bareName: bareName,
98
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.41",
3
+ "version": "0.1.42",
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",
@@ -28,6 +28,7 @@
28
28
  "kryptheon-heal.js",
29
29
  "kryptheon-selectors.js",
30
30
  "kryptheon-recordings.js",
31
+ "kryptheon-testfiles.js",
31
32
  "kryptheon-cleanup.js",
32
33
  "kryptheon-baselines.js",
33
34
  "kryptheon-project.js",
@@ -52,7 +53,7 @@
52
53
  "@playwright/test": "^1.62.1"
53
54
  },
54
55
  "scripts": {
55
- "check": "node kryptheon-store.check.js && node kryptheon-code.check.js && node kryptheon-constdata.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-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",
56
+ "check": "node kryptheon-store.check.js && node kryptheon-code.check.js && node kryptheon-constdata.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",
56
57
  "check:store": "node kryptheon-store.check.js",
57
58
  "check:code": "node kryptheon-code.check.js",
58
59
  "check:constdata": "node kryptheon-constdata.check.js",
@@ -81,7 +82,8 @@
81
82
  "check:heal": "node kryptheon-heal.check.js",
82
83
  "check:load": "node kryptheon-load.check.js",
83
84
  "check:package": "node kryptheon-package.check.js",
84
- "prepublishOnly": "node kryptheon-package.check.js"
85
+ "prepublishOnly": "node kryptheon-package.check.js",
86
+ "check:testfiles": "node kryptheon-testfiles.check.js"
85
87
  },
86
88
  "keywords": [
87
89
  "playwright",
@@ -41,6 +41,9 @@ loadEnv(path.join(USER_DIR, '.env'));
41
41
  module.exports = defineConfig({
42
42
  // The tests belong to the user, so they are found in their folder.
43
43
  testDir: path.join(USER_DIR, 'tests'),
44
+ // Playwright's own pattern, plus <name>.kryptheon.js: the name a recording
45
+ // gets in a project whose unit-test runner would also collect *.spec.js.
46
+ testMatch: ['**/*.@(spec|test).?(c|m)[jt]s?(x)', '**/*.kryptheon.js'],
44
47
  // Screenshots and other artefacts default to sitting next to the config,
45
48
  // which once installed means inside node_modules. They belong to the user's
46
49
  // project, so they go to its store - outside the project, where a commit of