kryptheon 0.1.47 → 0.1.48

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.
@@ -313,6 +313,72 @@ function applyBaseline(file, key, current, renamed) {
313
313
 
314
314
  // ---------------------------------------------------------------------------
315
315
 
316
+ /**
317
+ * A save that never left the browser: the last click was on a button that
318
+ * saves (Save, Add, Create...) inside a form or dialog, no write - POST, PUT,
319
+ * PATCH, DELETE - was sent after it, and the form is still open with a field
320
+ * the browser would not take, or one left empty. Returns { step, field,
321
+ * state: 'invalid' | 'empty', message } or null. Found on blind round 17: a
322
+ * first replay that stopped this way was blamed on "a step that cannot
323
+ * repeat" - the field was the reason, and it was never named.
324
+ *
325
+ * `clicks` and `invalids` carry the browser's own time; `writes` the time
326
+ * each write was seen here, which is never earlier than it was sent - so a
327
+ * write is never missed, only ever counted when in doubt.
328
+ */
329
+ function stuckForm(input) {
330
+ const clicks = (input && input.clicks) || [];
331
+ const last = clicks[clicks.length - 1];
332
+ if (!last || !last.inForm || !replay.CREATES.test(String(last.name || '').trim()) || replay.SIGNUP.test(last.name)) return null;
333
+ if (((input && input.writes) || []).some((at) => at >= last.at)) return null;
334
+ const step = oneLine(last.name).slice(0, 60);
335
+ const stopped = ((input && input.invalids) || []).filter((e) => e.at >= last.at && e.field);
336
+ if (stopped.length) return { step: step, field: stopped[0].field, state: 'invalid', message: stopped[0].message || '' };
337
+ const open = (input && input.open) || [];
338
+ // A field the browser would not take, then a required one left empty; an
339
+ // optional empty field only inside an open dialog, where the save was.
340
+ const pick = open.find((f) => f.invalid && !f.empty) || open.find((f) => f.empty && (f.required || f.invalid)) ||
341
+ open.find((f) => f.empty && f.inDialog);
342
+ if (!pick || !pick.field) return null;
343
+ return { step: step, field: pick.field, state: pick.empty ? 'empty' : 'invalid', message: pick.empty ? '' : pick.message || '' };
344
+ }
345
+
346
+ // Runs inside the page: the fields of an open form or dialog that the
347
+ // browser would not take, or that are empty. Null when none is open.
348
+ function openFormFields() {
349
+ const visible = (el) => {
350
+ const r = el.getBoundingClientRect();
351
+ const st = window.getComputedStyle(el);
352
+ return (r.width > 0 || r.height > 0) && st.visibility !== 'hidden' && st.display !== 'none';
353
+ };
354
+ const nameOf = (el) => {
355
+ let label = (el.labels && el.labels[0] && (el.labels[0].innerText || el.labels[0].textContent)) || '';
356
+ if (!label) label = el.getAttribute('aria-label') || el.getAttribute('placeholder') || el.getAttribute('name') || el.id || '';
357
+ return label.replace(/\s+/g, ' ').replace(/\s*\*\s*$/, '').trim().slice(0, 60);
358
+ };
359
+ // An open dialog first: the save was in it. Only with none open, the forms.
360
+ const dialogs = Array.from(document.querySelectorAll('dialog[open], [role="dialog"], [role="alertdialog"], [aria-modal="true"]')).filter(visible);
361
+ const holders = dialogs.length ? dialogs : Array.from(document.querySelectorAll('form')).filter(visible);
362
+ if (!holders.length) return null;
363
+ const out = [];
364
+ const seen = new Set();
365
+ for (const holder of holders) {
366
+ for (const el of holder.querySelectorAll('input, textarea, select')) {
367
+ if (seen.has(el)) continue;
368
+ seen.add(el);
369
+ const type = (el.getAttribute('type') || '').toLowerCase();
370
+ if (['hidden', 'submit', 'button', 'reset', 'image', 'checkbox', 'radio', 'file'].includes(type)) continue;
371
+ if (el.disabled || el.readOnly || !visible(el)) continue;
372
+ const invalid = typeof el.checkValidity === 'function' && !el.checkValidity();
373
+ const empty = String(el.value == null ? '' : el.value).trim() === '';
374
+ if (!invalid && !empty) continue;
375
+ out.push({ field: nameOf(el), invalid: invalid, empty: empty, message: invalid ? String(el.validationMessage || '').slice(0, 160) : '',
376
+ required: el.required === true || el.getAttribute('aria-required') === 'true', inDialog: dialogs.length > 0 });
377
+ }
378
+ }
379
+ return out;
380
+ }
381
+
316
382
  const test = base.test.extend({
317
383
  page: async ({ page }, use, testInfo) => {
318
384
  const consoleErrors = [];
@@ -332,6 +398,48 @@ const test = base.test.extend({
332
398
  }
333
399
  });
334
400
 
401
+ // What a save that never left the browser needs to be said: the clicks,
402
+ // the writes, and the fields the browser stopped (stuckForm). Read only
403
+ // when the test fails; never a failure of its own.
404
+ const clicks = [];
405
+ const writeTimes = [];
406
+ const invalids = [];
407
+ page.on('request', (req) => {
408
+ if (/^(POST|PUT|PATCH|DELETE)$/.test(req.method())) writeTimes.push(Date.now());
409
+ });
410
+ try {
411
+ await page.exposeBinding('__kxSeen', (source, e) => {
412
+ if (!e || typeof e !== 'object') return;
413
+ if (e.type === 'click') clicks.push({ name: String(e.name || ''), inForm: Boolean(e.inForm), at: Number(e.at) || 0 });
414
+ else if (e.type === 'invalid') invalids.push({ field: String(e.field || ''), message: String(e.message || ''), at: Number(e.at) || 0 });
415
+ });
416
+ await page.addInitScript(() => {
417
+ const say = (e) => {
418
+ try {
419
+ window.__kxSeen(e);
420
+ } catch (err) {
421
+ /* a note, never a failure */
422
+ }
423
+ };
424
+ document.addEventListener('click', (e) => {
425
+ const el = e.target && e.target.closest ? e.target.closest('button, [role="button"], input[type="submit"], input[type="button"], a') : null;
426
+ if (!el) return;
427
+ const name = el.getAttribute('aria-label') || (el.tagName === 'INPUT' ? el.value : el.innerText || el.textContent) || '';
428
+ say({ type: 'click', name: name.replace(/\s+/g, ' ').trim().slice(0, 80), at: Date.now(),
429
+ inForm: Boolean(el.closest('form, dialog, [role="dialog"], [role="alertdialog"], [aria-modal="true"]')) });
430
+ }, true);
431
+ document.addEventListener('invalid', (e) => {
432
+ const el = e.target;
433
+ if (!el || typeof el.getAttribute !== 'function') return;
434
+ const label = (el.labels && el.labels[0] && el.labels[0].textContent) || el.getAttribute('aria-label') ||
435
+ el.getAttribute('placeholder') || el.getAttribute('name') || el.id || '';
436
+ say({ type: 'invalid', field: label.replace(/\s+/g, ' ').replace(/\s*\*\s*$/, '').trim().slice(0, 60), message: String(el.validationMessage || '').slice(0, 160), at: Date.now() });
437
+ }, true);
438
+ });
439
+ } catch (e) {
440
+ /* without it, a failure is said as before */
441
+ }
442
+
335
443
  const key = baselineKey(testInfo.file, testInfo.title);
336
444
 
337
445
  // The steps this recording actually performs. Comments, blank lines and
@@ -366,6 +474,13 @@ const test = base.test.extend({
366
474
  }
367
475
  const previous = readBaselines(BASELINE_FILE)[key];
368
476
  const fresh = newObservations({ consoleErrors, failedRequests }, previous);
477
+ let open = null;
478
+ try {
479
+ open = clicks.length ? await page.evaluate(openFormFields) : null;
480
+ } catch (e) {
481
+ open = null; // page may already be closed
482
+ }
483
+ const stuck = stuckForm({ clicks: clicks, writes: writeTimes, invalids: invalids, open: open });
369
484
  try {
370
485
  await testInfo.attach('kryptheon-observations', {
371
486
  body: JSON.stringify({
@@ -375,6 +490,7 @@ const test = base.test.extend({
375
490
  pageShowed: signature.describeSignature(captured),
376
491
  failedRequests: fresh.failedRequests.slice(0, MAX_ITEMS),
377
492
  consoleErrors: fresh.consoleErrors.slice(0, MAX_ITEMS),
493
+ stuckForm: stuck,
378
494
  }),
379
495
  contentType: 'application/json',
380
496
  });
@@ -477,5 +593,6 @@ module.exports = {
477
593
  upgradeBaseline: upgradeBaseline,
478
594
  stampRecording: stampRecording,
479
595
  rememberSteps: rememberSteps,
596
+ stuckForm: stuckForm,
480
597
  BASELINE_FILE: BASELINE_FILE,
481
598
  };
package/kryptheon-heal.js CHANGED
@@ -1,215 +1,254 @@
1
- // A button that was renamed has not broken the flow.
2
- //
3
- // Found on a real Lovable app: "Reserve a table" became "Reserve here", the
4
- // replay stopped at the first step that clicked it, and the report said the
5
- // flow was broken. It was not - the same button was in the same place, and
6
- // everything after it (the form, the email check, the date check, the
7
- // confirmation) was never looked at. One rename hid the whole flow, and the
8
- // person had to go and prove that themselves.
9
- //
10
- // So when a step cannot find its element by the recorded name, this asks what
11
- // changed on the page since the flow last passed. The rule is deliberately
12
- // narrow: exactly one name of that kind is gone, it is the one the step
13
- // wanted, and exactly one new name of the same role has appeared in its
14
- // place. Then the step uses the new one and the report says so. Anything
15
- // looser - two new buttons, the old name still somewhere, a page that has
16
- // not finished drawing - is not a rename this can prove, and the step fails
17
- // exactly as it did before, with the same message.
18
- //
19
- // What the page looked like before comes from two places. Every step that
20
- // finds its element by name notes the names of its role on the page at that
21
- // moment, and the baseline keeps them from the last passing run. A project
22
- // recorded before this existed has none of those yet, and falls back to the
23
- // buttons, links and fields the baseline saw on the flow's last page - which
24
- // only proves a rename when the step's page and the last page are the same.
25
- //
26
- // Only page.getByRole with a plain string name is watched: that is what
27
- // codegen writes for nearly every click and fill. Assertions are never
28
- // redirected. A check someone added on purpose says what they expect to see,
29
- // and quietly checking something else instead would make it a lie.
30
-
31
- // How long a step waits for its element by the recorded name before asking
32
- // whether it was renamed. A step whose element is merely slow is not
33
- // affected: when the page does not prove a rename, the step carries on
34
- // waiting exactly as it always did.
35
- const PROBE_MS = 5000;
36
-
37
- const ACTIONS = [
38
- 'click', 'dblclick', 'tap', 'hover', 'focus',
39
- 'fill', 'clear', 'press', 'pressSequentially', 'type',
40
- 'check', 'uncheck', 'setChecked', 'selectOption', 'setInputFiles',
41
- ];
42
-
43
- // The same groups the baseline signature uses: its "actions" are buttons and
44
- // links, its "fields" are form controls.
45
- const ACTION_ROLES = new Set(['button', 'link']);
46
- const FIELD_ROLES = new Set(['textbox', 'searchbox', 'combobox', 'listbox', 'spinbutton', 'checkbox', 'radio', 'slider', 'switch']);
47
-
48
- function norm(text) {
49
- return String(text == null ? '' : text).replace(/\s+/g, ' ').trim().toLowerCase();
50
- }
51
-
52
- /**
53
- * Names on the page by role, read from Playwright's accessibility snapshot -
54
- * the same names getByRole matches against, so nothing is guessed.
55
- *
56
- * - button "Reserve here"
57
- * - link "Story":
58
- * - textbox "Email" [disabled]
59
- */
60
- function namesByRole(snapshot) {
61
- const out = new Map();
62
- for (const line of String(snapshot || '').split('\n')) {
63
- const m = line.match(/^\s*- ([a-z]+) "((?:[^"\\]|\\.)*)"/);
64
- if (!m) continue;
65
- let name;
66
- try {
67
- name = JSON.parse('"' + m[2] + '"');
68
- } catch (e) {
69
- continue;
70
- }
71
- if (!name) continue;
72
- if (!out.has(m[1])) out.set(m[1], []);
73
- out.get(m[1]).push(name);
74
- }
75
- return out;
76
- }
77
-
78
- /**
79
- * The whole decision, with nothing but names in and out.
80
- *
81
- * `wanted` is the name the step was recorded with, `before` and `now` the
82
- * names of that kind on the page then and now, and `candidates` the names
83
- * now on the page with the step's own role. Returns the new name, or null
84
- * when the page does not prove a rename.
85
- */
86
- function decideRename(wanted, before, now, candidates) {
87
- const target = norm(wanted);
88
- if (!target || !Array.isArray(before) || !before.length) return null;
89
- const nowSet = new Set((now || []).map(norm));
90
- const beforeSet = new Set(before.map(norm));
91
-
92
- const gone = [...beforeSet].filter((n) => !nowSet.has(n));
93
- const added = [...nowSet].filter((n) => !beforeSet.has(n));
94
- if (gone.length !== 1 || added.length !== 1) return null;
95
- // getByRole matches a name that contains the recorded one, so the name
96
- // that went is the step's own only if it contains what was recorded.
97
- if (!gone[0].includes(target)) return null;
98
-
99
- // Of the step's own role. A button renamed while the recorded link went
100
- // missing is two changes, not one rename.
101
- return (candidates || []).find((c) => norm(c) === added[0]) || null;
102
- }
103
-
104
- /**
105
- * Watches one test's page. `memory` is what the last passing run noted per
106
- * step, `fallback` the last page's signature; either may be missing.
107
- */
108
- function watch(page, options) {
109
- const opts = options || {};
110
- const memory = opts.memory && typeof opts.memory === 'object' ? opts.memory : null;
111
- const fallback = opts.fallback && typeof opts.fallback === 'object' ? opts.fallback : null;
112
- const probeMs = opts.probeMs || PROBE_MS;
113
-
114
- const learned = {};
115
- const renamed = [];
116
- let counter = 0;
117
-
118
- const byRole = page.getByRole.bind(page);
119
-
120
- async function readRoles() {
121
- try {
122
- return namesByRole(await page.locator('body').ariaSnapshot({ timeout: 3000 }));
123
- } catch (e) {
124
- return null; // a page that cannot be read proves nothing
125
- }
126
- }
127
-
128
- async function findRename(key, role, name) {
129
- const roles = await readRoles();
130
- if (!roles) return null;
131
- const ofRole = roles.get(role) || [];
132
- if (memory && Array.isArray(memory[key])) {
133
- return decideRename(name, memory[key], ofRole, ofRole);
134
- }
135
- if (!fallback) return null;
136
- const kind = ACTION_ROLES.has(role) ? 'actions' : FIELD_ROLES.has(role) ? 'fields' : null;
137
- if (!kind || !Array.isArray(fallback[kind])) return null;
138
- const group = kind === 'actions' ? ACTION_ROLES : FIELD_ROLES;
139
- const now = [];
140
- for (const [r, names] of roles) if (group.has(r)) now.push(...names);
141
- return decideRename(name, fallback[kind], now, ofRole);
142
- }
143
-
144
- async function act(locator, real, method, args, role, name) {
145
- const key = counter++ + ' ' + role + ' ' + name;
146
- const there = await locator
147
- .first()
148
- .waitFor({ state: 'attached', timeout: probeMs })
149
- .then(() => true, () => false);
150
- if (there) {
151
- const roles = await readRoles();
152
- if (roles) learned[key] = roles.get(role) || [];
153
- return real.apply(locator, args);
154
- }
155
-
156
- const newName = await findRename(key, role, name);
157
- if (newName) {
158
- const replacement = byRole(role, { name: newName, exact: true });
159
- const count = await replacement.count().catch(() => 0);
160
- if (count === 1) {
161
- renamed.push({ role: role, was: name, now: newName });
162
- return replacement[method].apply(replacement, args);
163
- }
164
- }
165
- // Not a rename this can prove: the step fails the way it always did.
166
- return real.apply(locator, args);
167
- }
168
-
169
- page.getByRole = function (role, roleOptions) {
170
- const locator = byRole(role, roleOptions);
171
- const name = roleOptions && typeof roleOptions.name === 'string' ? roleOptions.name : null;
172
- if (!name) return locator;
173
- for (const method of ACTIONS) {
174
- const real = locator[method];
175
- if (typeof real !== 'function') continue;
176
- locator[method] = function () {
177
- return act(locator, real, method, Array.prototype.slice.call(arguments), role, name);
178
- };
179
- }
180
- return locator;
181
- };
182
-
183
- return {
184
- learned: learned,
185
- renamed: renamed,
186
- };
187
- }
188
-
189
- /**
190
- * What to keep for next time: the steps this run found by their recorded
191
- * name, over whatever was kept before. A step that had to follow a rename
192
- * keeps its old note - that note is what lets the next run prove the same
193
- * rename again.
194
- */
195
- function mergeMemory(previous, learned) {
196
- const out = {};
197
- if (previous && typeof previous === 'object') Object.assign(out, previous);
198
- if (learned && typeof learned === 'object') Object.assign(out, learned);
199
- return out;
200
- }
201
-
202
- /** The report line, shared by the terminal and the history. */
203
- function describeRename(entry) {
204
- const what = entry.role === 'link' ? 'link' : entry.role === 'button' ? 'button' : entry.role;
205
- return 'The ' + what + ' "' + entry.was + '" is now called "' + entry.now + '" - I used "' + entry.now + '" and carried on.';
206
- }
207
-
208
- module.exports = {
209
- PROBE_MS: PROBE_MS,
210
- namesByRole: namesByRole,
211
- decideRename: decideRename,
212
- watch: watch,
213
- mergeMemory: mergeMemory,
214
- describeRename: describeRename,
215
- };
1
+ // A button that was renamed has not broken the flow.
2
+ //
3
+ // Found on a real Lovable app: "Reserve a table" became "Reserve here", the
4
+ // replay stopped at the first step that clicked it, and the report said the
5
+ // flow was broken. It was not - the same button was in the same place, and
6
+ // everything after it (the form, the email check, the date check, the
7
+ // confirmation) was never looked at. One rename hid the whole flow, and the
8
+ // person had to go and prove that themselves.
9
+ //
10
+ // So when a step cannot find its element by the recorded name, this asks what
11
+ // changed on the page since the flow last passed. The rule is deliberately
12
+ // narrow: exactly one name of that kind is gone, it is the one the step
13
+ // wanted, and exactly one new name of the same role has appeared in its
14
+ // place. Then the step uses the new one and the report says so. Anything
15
+ // looser - two new buttons, the old name still somewhere, a page that has
16
+ // not finished drawing - is not a rename this can prove, and the step fails
17
+ // exactly as it did before, with the same message.
18
+ //
19
+ // What the page looked like before comes from two places. Every step that
20
+ // finds its element by name notes the names of its role on the page at that
21
+ // moment, and the baseline keeps them from the last passing run. A project
22
+ // recorded before this existed has none of those yet, and falls back to the
23
+ // buttons, links and fields the baseline saw on the flow's last page - which
24
+ // only proves a rename when the step's page and the last page are the same.
25
+ //
26
+ // Only page.getByRole with a plain string name is watched: that is what
27
+ // codegen writes for nearly every click and fill. Assertions are never
28
+ // redirected. A check someone added on purpose says what they expect to see,
29
+ // and quietly checking something else instead would make it a lie.
30
+
31
+ // How long a step waits for its element by the recorded name before asking
32
+ // whether it was renamed. A step whose element is merely slow is not
33
+ // affected: when the page does not prove a rename, the step carries on
34
+ // waiting exactly as it always did.
35
+ const PROBE_MS = 5000;
36
+
37
+ const ACTIONS = [
38
+ 'click', 'dblclick', 'tap', 'hover', 'focus',
39
+ 'fill', 'clear', 'press', 'pressSequentially', 'type',
40
+ 'check', 'uncheck', 'setChecked', 'selectOption', 'setInputFiles',
41
+ ];
42
+
43
+ // The same groups the baseline signature uses: its "actions" are buttons and
44
+ // links, its "fields" are form controls.
45
+ const ACTION_ROLES = new Set(['button', 'link']);
46
+ const FIELD_ROLES = new Set(['textbox', 'searchbox', 'combobox', 'listbox', 'spinbutton', 'checkbox', 'radio', 'slider', 'switch']);
47
+
48
+ function norm(text) {
49
+ return String(text == null ? '' : text).replace(/\s+/g, ' ').trim().toLowerCase();
50
+ }
51
+
52
+ /**
53
+ * Names on the page by role, read from Playwright's accessibility snapshot -
54
+ * the same names getByRole matches against, so nothing is guessed.
55
+ *
56
+ * - button "Reserve here"
57
+ * - link "Story":
58
+ * - textbox "Email" [disabled]
59
+ */
60
+ function namesByRole(snapshot) {
61
+ const out = new Map();
62
+ for (const line of String(snapshot || '').split('\n')) {
63
+ const m = line.match(/^\s*- ([a-z]+) "((?:[^"\\]|\\.)*)"/);
64
+ if (!m) continue;
65
+ let name;
66
+ try {
67
+ name = JSON.parse('"' + m[2] + '"');
68
+ } catch (e) {
69
+ continue;
70
+ }
71
+ if (!name) continue;
72
+ if (!out.has(m[1])) out.set(m[1], []);
73
+ out.get(m[1]).push(name);
74
+ }
75
+ return out;
76
+ }
77
+
78
+ /**
79
+ * The whole decision, with nothing but names in and out.
80
+ *
81
+ * `wanted` is the name the step was recorded with, `before` and `now` the
82
+ * names of that kind on the page then and now, and `candidates` the names
83
+ * now on the page with the step's own role. Returns the new name, or null
84
+ * when the page does not prove a rename.
85
+ */
86
+ function decideRename(wanted, before, now, candidates) {
87
+ const target = norm(wanted);
88
+ if (!target || !Array.isArray(before) || !before.length) return null;
89
+ const nowSet = new Set((now || []).map(norm));
90
+ const beforeSet = new Set(before.map(norm));
91
+
92
+ const gone = [...beforeSet].filter((n) => !nowSet.has(n));
93
+ const added = [...nowSet].filter((n) => !beforeSet.has(n));
94
+ if (gone.length !== 1 || added.length !== 1) return null;
95
+ // getByRole matches a name that contains the recorded one, so the name
96
+ // that went is the step's own only if it contains what was recorded.
97
+ if (!gone[0].includes(target)) return null;
98
+
99
+ // Of the step's own role. A button renamed while the recorded link went
100
+ // missing is two changes, not one rename.
101
+ return (candidates || []).find((c) => norm(c) === added[0]) || null;
102
+ }
103
+
104
+ // How much of what was around a step must still be there for its page to be
105
+ // the same page. A rename is one name changing on a page that is otherwise
106
+ // the same; a step that finds itself on another page altogether has not had
107
+ // its field renamed. Found on blind round 17 (Lumen): a click that did not
108
+ // land left the flow on a study's own page, and the search box of the studies
109
+ // list was "renamed" to that page's only textbox, "Add a note to this study".
110
+ const SAME_PAGE = 0.5;
111
+ const PAGE_NAMES = 80;
112
+ // Kept next to a step's own note, under its key with this added.
113
+ const PAGE_KEY = ' @page';
114
+
115
+ /** Everything named on the page but the step's own role: what places it. */
116
+ function pageNames(roles, role) {
117
+ const out = [];
118
+ for (const [r, names] of roles) if (r !== role) out.push(...names);
119
+ return out.slice(0, PAGE_NAMES);
120
+ }
121
+
122
+ /**
123
+ * Whether the page now is the page a step was on before: at least half of
124
+ * what was named around it is still there. Nothing remembered (a note from
125
+ * before this existed) proves nothing either way, and is not held against it.
126
+ */
127
+ function samePage(before, now) {
128
+ if (!Array.isArray(before) || !before.length) return true;
129
+ const here = new Set((now || []).map(norm));
130
+ const kept = before.filter((name) => here.has(norm(name))).length;
131
+ return kept / before.length >= SAME_PAGE;
132
+ }
133
+
134
+ /**
135
+ * Watches one test's page. `memory` is what the last passing run noted per
136
+ * step, `fallback` the last page's signature; either may be missing.
137
+ */
138
+ function watch(page, options) {
139
+ const opts = options || {};
140
+ const memory = opts.memory && typeof opts.memory === 'object' ? opts.memory : null;
141
+ const fallback = opts.fallback && typeof opts.fallback === 'object' ? opts.fallback : null;
142
+ const probeMs = opts.probeMs || PROBE_MS;
143
+
144
+ const learned = {};
145
+ const renamed = [];
146
+ let counter = 0;
147
+
148
+ const byRole = page.getByRole.bind(page);
149
+
150
+ async function readRoles() {
151
+ try {
152
+ return namesByRole(await page.locator('body').ariaSnapshot({ timeout: 3000 }));
153
+ } catch (e) {
154
+ return null; // a page that cannot be read proves nothing
155
+ }
156
+ }
157
+
158
+ async function findRename(key, role, name) {
159
+ const roles = await readRoles();
160
+ if (!roles) return null;
161
+ const ofRole = roles.get(role) || [];
162
+ if (memory && Array.isArray(memory[key])) {
163
+ if (!samePage(memory[key + PAGE_KEY], pageNames(roles, role))) return null;
164
+ return decideRename(name, memory[key], ofRole, ofRole);
165
+ }
166
+ if (!fallback) return null;
167
+ const kind = ACTION_ROLES.has(role) ? 'actions' : FIELD_ROLES.has(role) ? 'fields' : null;
168
+ if (!kind || !Array.isArray(fallback[kind])) return null;
169
+ // The last page's headings, the only part of it that is not the names
170
+ // being compared: another page is not a rename.
171
+ if (!samePage(fallback.headings, roles.get('heading') || [])) return null;
172
+ const group = kind === 'actions' ? ACTION_ROLES : FIELD_ROLES;
173
+ const now = [];
174
+ for (const [r, names] of roles) if (group.has(r)) now.push(...names);
175
+ return decideRename(name, fallback[kind], now, ofRole);
176
+ }
177
+
178
+ async function act(locator, real, method, args, role, name) {
179
+ const key = counter++ + ' ' + role + ' ' + name;
180
+ const there = await locator
181
+ .first()
182
+ .waitFor({ state: 'attached', timeout: probeMs })
183
+ .then(() => true, () => false);
184
+ if (there) {
185
+ const roles = await readRoles();
186
+ if (roles) {
187
+ learned[key] = roles.get(role) || [];
188
+ learned[key + PAGE_KEY] = pageNames(roles, role);
189
+ }
190
+ return real.apply(locator, args);
191
+ }
192
+
193
+ const newName = await findRename(key, role, name);
194
+ if (newName) {
195
+ const replacement = byRole(role, { name: newName, exact: true });
196
+ const count = await replacement.count().catch(() => 0);
197
+ if (count === 1) {
198
+ renamed.push({ role: role, was: name, now: newName });
199
+ return replacement[method].apply(replacement, args);
200
+ }
201
+ }
202
+ // Not a rename this can prove: the step fails the way it always did.
203
+ return real.apply(locator, args);
204
+ }
205
+
206
+ page.getByRole = function (role, roleOptions) {
207
+ const locator = byRole(role, roleOptions);
208
+ const name = roleOptions && typeof roleOptions.name === 'string' ? roleOptions.name : null;
209
+ if (!name) return locator;
210
+ for (const method of ACTIONS) {
211
+ const real = locator[method];
212
+ if (typeof real !== 'function') continue;
213
+ locator[method] = function () {
214
+ return act(locator, real, method, Array.prototype.slice.call(arguments), role, name);
215
+ };
216
+ }
217
+ return locator;
218
+ };
219
+
220
+ return {
221
+ learned: learned,
222
+ renamed: renamed,
223
+ };
224
+ }
225
+
226
+ /**
227
+ * What to keep for next time: the steps this run found by their recorded
228
+ * name, over whatever was kept before. A step that had to follow a rename
229
+ * keeps its old note - that note is what lets the next run prove the same
230
+ * rename again.
231
+ */
232
+ function mergeMemory(previous, learned) {
233
+ const out = {};
234
+ if (previous && typeof previous === 'object') Object.assign(out, previous);
235
+ if (learned && typeof learned === 'object') Object.assign(out, learned);
236
+ return out;
237
+ }
238
+
239
+ /** The report line, shared by the terminal and the history. */
240
+ function describeRename(entry) {
241
+ const what = entry.role === 'link' ? 'link' : entry.role === 'button' ? 'button' : entry.role;
242
+ return 'The ' + what + ' "' + entry.was + '" is now called "' + entry.now + '" - I used "' + entry.now + '" and carried on.';
243
+ }
244
+
245
+ module.exports = {
246
+ PROBE_MS: PROBE_MS,
247
+ namesByRole: namesByRole,
248
+ decideRename: decideRename,
249
+ samePage: samePage,
250
+ pageNames: pageNames,
251
+ watch: watch,
252
+ mergeMemory: mergeMemory,
253
+ describeRename: describeRename,
254
+ };