@broberg/lens-daemon 0.1.52 → 0.1.53

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.52",
3
+ "version": "0.1.53",
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",
@@ -0,0 +1,34 @@
1
+ // F010.11 — a one-time code field split into one box per character (Microsoft's
2
+ // «Enter your code»). Shared by fill-from-owner and fill-from-mail: the code is
3
+ // typed key by key from the first box and the page's own auto-advance moves focus.
4
+ import type { Locator } from 'playwright';
5
+
6
+ export async function isOneCharBox(loc: Locator): Promise<boolean> {
7
+ return loc.evaluate((el) => (el as HTMLInputElement).maxLength === 1).catch(() => false);
8
+ }
9
+
10
+ /** The joined value of the one-character boxes around `el`. */
11
+ function joinedBoxes(el: Element): string {
12
+ let group: Element | null = el.parentElement;
13
+ 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)
17
+ .join('');
18
+ }
19
+
20
+ /** Type `value` across the boxes and prove the joined boxes hold it. `failure`
21
+ * starts the error message; the value itself is never in it. */
22
+ export async function typeIntoBoxes(loc: Locator, value: string, failure: string, timeout: number): Promise<void> {
23
+ await loc.focus({ timeout });
24
+ await loc.pressSequentially(value, { timeout });
25
+ let got: string;
26
+ try {
27
+ got = await loc.evaluate(joinedBoxes);
28
+ } catch (err) {
29
+ // The last digit often submits the form; the page leaving is the code landing.
30
+ if (/Execution context was destroyed|Cannot find context|has been closed|detached/i.test(err instanceof Error ? err.message : String(err))) return;
31
+ throw err;
32
+ }
33
+ if (got !== value) throw new Error(`${failure} across the boxes, found ${got.length} (value withheld)`);
34
+ }
package/src/mail-fill.ts CHANGED
@@ -10,6 +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
14
 
14
15
  export interface MailFrom {
15
16
  to: string;
@@ -112,6 +113,10 @@ export async function fillFromMail(
112
113
  ): Promise<void> {
113
114
  const { code, id } = await waitForMailCode(mf, since, source);
114
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);
118
+ return;
119
+ }
115
120
  try {
116
121
  await fillAndVerify(loc, code, timeout);
117
122
  } catch {
package/src/owner-fill.ts CHANGED
@@ -13,6 +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
17
 
17
18
  export type OwnerKind = 'code' | 'password' | 'text';
18
19
 
@@ -89,9 +90,8 @@ export async function fillFromOwner(loc: Locator, of: OwnerFrom, fillTimeout: nu
89
90
  if (!res.answer) throw new Error('fill from owner: ARS answered with an empty value');
90
91
  // F010.11 — a code field split into one box per character (Microsoft's «Enter your
91
92
  // code»): type it key by key from the first box and let the page advance.
92
- const oneCharBox = await loc.evaluate((el) => (el as HTMLInputElement).maxLength === 1).catch(() => false);
93
- if (oneCharBox && res.answer.length > 1) {
94
- await typeIntoBoxes(loc, res.answer, kind, fillTimeout);
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);
95
95
  return;
96
96
  }
97
97
  try {
@@ -101,28 +101,3 @@ export async function fillFromOwner(loc: Locator, of: OwnerFrom, fillTimeout: nu
101
101
  }
102
102
  }
103
103
 
104
- /** F010.11 — the joined value of the one-character boxes around `el`. */
105
- function joinedBoxes(el: Element): string {
106
- let group: Element | null = el.parentElement;
107
- while (group && group.querySelectorAll('input').length < 2 && group.parentElement) group = group.parentElement;
108
- return [...(group?.querySelectorAll('input') ?? [])]
109
- .filter((i) => (i as HTMLInputElement).maxLength === 1)
110
- .map((i) => (i as HTMLInputElement).value)
111
- .join('');
112
- }
113
-
114
- async function typeIntoBoxes(loc: Locator, answer: string, kind: string, timeout: number): Promise<void> {
115
- await loc.focus({ timeout });
116
- await loc.pressSequentially(answer, { timeout });
117
- let got: string;
118
- try {
119
- got = await loc.evaluate(joinedBoxes);
120
- } catch (err) {
121
- // The last digit often submits the form; the page leaving is the code landing.
122
- if (/Execution context was destroyed|Cannot find context|has been closed|detached/i.test(err instanceof Error ? err.message : String(err))) return;
123
- throw err;
124
- }
125
- if (got !== answer) {
126
- throw new Error(`fill from owner did not land: expected a ${answer.length}-character ${kind} across the boxes, found ${got.length} (value withheld)`);
127
- }
128
- }