@broberg/lens-daemon 0.1.77 → 0.1.78

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@broberg/lens-daemon",
3
- "version": "0.1.77",
3
+ "version": "0.1.78",
4
4
  "description": "The Lens module of the cardmem daemon (flows, critic, coverage, capture sessions, the /lens/* routes and the cardmem-lens MCP) as a package. The daemon mounts createLensRouter(deps) + createLensMcpRouter(deps). Bun runtime.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/code-boxes.ts CHANGED
@@ -1,19 +1,45 @@
1
1
  // F010.11 — a one-time code field split into one box per character (Microsoft's
2
2
  // «Enter your code»). Shared by fill-from-owner and fill-from-mail: the code is
3
3
  // typed key by key from the first box and the page's own auto-advance moves focus.
4
+ // F009.143 — also the variant whose FIRST box takes the whole code (maxlength =
5
+ // code length, autocomplete=one-time-code) and spreads it (vn-leker's login), and
6
+ // a flow anchored on the GROUP's testid rather than on one of its inputs.
4
7
  import type { Locator } from 'playwright';
5
8
 
9
+ /** The input a code is typed into: the anchor itself, or — when the anchor is a
10
+ * wrapper such as the boxes' group — the first visible input inside it. */
11
+ export async function codeInput(loc: Locator): Promise<Locator> {
12
+ const tag = await loc.evaluate((el) => el.tagName).catch(() => '');
13
+ if (tag === 'INPUT' || tag === 'TEXTAREA') return loc;
14
+ const inner = loc.locator('input:not([type=hidden])');
15
+ return (await inner.count()) > 0 ? inner.first() : loc;
16
+ }
17
+
18
+ /** True when `el` is one box of a split code field: either a one-character box,
19
+ * or a wider box whose siblings in its group are all one-character boxes. */
6
20
  export async function isOneCharBox(loc: Locator): Promise<boolean> {
7
- return loc.evaluate((el) => (el as HTMLInputElement).maxLength === 1).catch(() => false);
21
+ return loc
22
+ .evaluate((el) => {
23
+ const input = el as HTMLInputElement;
24
+ if (input.maxLength === 1) return true;
25
+ let group: Element | null = el.parentElement;
26
+ while (group && group.querySelectorAll('input').length < 2 && group.parentElement) group = group.parentElement;
27
+ const others = [...(group?.querySelectorAll('input') ?? [])].filter((i) => i !== el) as HTMLInputElement[];
28
+ return others.length > 0 && others.every((i) => i.maxLength === 1);
29
+ })
30
+ .catch(() => false);
8
31
  }
9
32
 
10
- /** The joined value of the one-character boxes around `el`. */
33
+ /** The joined value of the code boxes around `el`. */
11
34
  function joinedBoxes(el: Element): string {
12
35
  let group: Element | null = el.parentElement;
13
36
  while (group && group.querySelectorAll('input').length < 2 && group.parentElement) group = group.parentElement;
14
- return [...(group?.querySelectorAll('input') ?? [])]
15
- .filter((i) => (i as HTMLInputElement).maxLength === 1)
16
- .map((i) => (i as HTMLInputElement).value)
37
+ const inputs = [...(group?.querySelectorAll('input') ?? [])] as HTMLInputElement[];
38
+ // A box wider than one character belongs to the code only when it is the one
39
+ // we typed into (F009.143); any other wide input in the group is not a box.
40
+ return inputs
41
+ .filter((i) => i.maxLength === 1 || i === el)
42
+ .map((i) => i.value)
17
43
  .join('');
18
44
  }
19
45
 
@@ -21,10 +47,15 @@ function joinedBoxes(el: Element): string {
21
47
  * starts the error message; the value itself is never in it. */
22
48
  export async function typeIntoBoxes(loc: Locator, value: string, failure: string, timeout: number): Promise<void> {
23
49
  await loc.focus({ timeout });
50
+ // Hold the box itself, not the locator: when the last digit submits and the
51
+ // page navigates, a locator would wait for a box that no longer exists until
52
+ // it times out; a handle fails at once with «context destroyed».
53
+ const box = await loc.elementHandle({ timeout });
24
54
  await loc.pressSequentially(value, { timeout });
25
55
  let got: string;
26
56
  try {
27
- got = await loc.evaluate(joinedBoxes);
57
+ if (!box) throw new Error('detached');
58
+ got = await box.evaluate(joinedBoxes);
28
59
  } catch (err) {
29
60
  // The last digit often submits the form; the page leaving is the code landing.
30
61
  if (/Execution context was destroyed|Cannot find context|has been closed|detached/i.test(err instanceof Error ? err.message : String(err))) return;
package/src/mail-fill.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  // spent by then, and hiding it would mean not photographing the form at all.
11
11
  import type { Locator } from 'playwright';
12
12
  import { fillAndVerify } from './fill.ts';
13
- import { isOneCharBox, typeIntoBoxes } from './code-boxes.ts';
13
+ import { codeInput, isOneCharBox, typeIntoBoxes } from './code-boxes.ts';
14
14
 
15
15
  export interface MailFrom {
16
16
  to: string;
@@ -113,12 +113,13 @@ export async function fillFromMail(
113
113
  ): Promise<void> {
114
114
  const { code, id } = await waitForMailCode(mf, since, source);
115
115
  consumed.push({ to: mailboxOf(mf.to), id });
116
- if (code.length > 1 && (await isOneCharBox(loc))) {
117
- await typeIntoBoxes(loc, code, `fill from mail did not land: expected a ${code.length}-character code`, timeout);
116
+ const input = await codeInput(loc);
117
+ if (code.length > 1 && (await isOneCharBox(input))) {
118
+ await typeIntoBoxes(input, code, `fill from mail did not land: expected a ${code.length}-character code`, timeout);
118
119
  return;
119
120
  }
120
121
  try {
121
- await fillAndVerify(loc, code, timeout);
122
+ await fillAndVerify(input, code, timeout);
122
123
  } catch {
123
124
  throw new Error(`fill from mail did not land: expected a ${code.length}-character code in the field (value withheld)`);
124
125
  }
package/src/owner-fill.ts CHANGED
@@ -13,7 +13,7 @@ import { homedir } from 'node:os';
13
13
  import { join } from 'node:path';
14
14
  import type { Locator } from 'playwright';
15
15
  import { fillAndVerify } from './fill.ts';
16
- import { isOneCharBox, typeIntoBoxes } from './code-boxes.ts';
16
+ import { codeInput, isOneCharBox, typeIntoBoxes } from './code-boxes.ts';
17
17
 
18
18
  export type OwnerKind = 'code' | 'password' | 'text';
19
19
 
@@ -90,12 +90,13 @@ export async function fillFromOwner(loc: Locator, of: OwnerFrom, fillTimeout: nu
90
90
  if (!res.answer) throw new Error('fill from owner: ARS answered with an empty value');
91
91
  // F010.11 — a code field split into one box per character (Microsoft's «Enter your
92
92
  // code»): type it key by key from the first box and let the page advance.
93
- if (res.answer.length > 1 && (await isOneCharBox(loc))) {
94
- await typeIntoBoxes(loc, res.answer, `fill from owner did not land: expected a ${res.answer.length}-character ${kind}`, fillTimeout);
93
+ const input = await codeInput(loc);
94
+ if (res.answer.length > 1 && (await isOneCharBox(input))) {
95
+ await typeIntoBoxes(input, res.answer, `fill from owner did not land: expected a ${res.answer.length}-character ${kind}`, fillTimeout);
95
96
  return;
96
97
  }
97
98
  try {
98
- await fillAndVerify(loc, res.answer, fillTimeout);
99
+ await fillAndVerify(input, res.answer, fillTimeout);
99
100
  } catch {
100
101
  throw new Error(`fill from owner did not land: expected a ${res.answer.length}-character ${kind} in the field (value withheld)`);
101
102
  }