@jeffawe/understudy 0.2.2 → 0.3.0

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.
@@ -13,7 +13,7 @@
13
13
  * in a string of JavaScript.
14
14
  */
15
15
  /** Actions the IR can express. Mirrors the `steps.action` CHECK constraint. */
16
- export type RecordedAction = 'goto' | 'click' | 'fill' | 'press' | 'select' | 'check' | 'uncheck' | 'upload' | 'wait_url' | 'wait_text' | 'dispatch_click' | 'scroll_container' | 'snapshot' | 'assert';
16
+ export type RecordedAction = 'goto' | 'click' | 'fill' | 'press' | 'select' | 'check' | 'uncheck' | 'upload' | 'wait_url' | 'wait_text' | 'dispatch_click' | 'scroll_container' | 'snapshot' | 'assert' | 'human' | 'wait_response';
17
17
  /**
18
18
  * How we came to believe an event's role and name — the honesty field.
19
19
  *
@@ -183,3 +183,51 @@ export declare function redactValue(fieldHint: string, value: string, inputType?
183
183
  value?: string;
184
184
  valueRef?: string;
185
185
  };
186
+ /**
187
+ * The value references a recording really needs filled in — the login values
188
+ * a run asks for before it opens a browser.
189
+ *
190
+ * Only steps that take a value count. Script import used to store
191
+ * `click({ force: true })` as `valueRef: '{ force: true }'`, and every caller
192
+ * then demanded a login value by that name, stopping the run before it began.
193
+ */
194
+ export declare function valueRefsOf(events: ReadonlyArray<Pick<RawEvent, 'action' | 'valueRef'>>): string[];
195
+ /**
196
+ * Should this step skip Playwright's actionability checks — `click({ force: true })`?
197
+ *
198
+ * Needed for controls the app hides behind a styled overlay, like custom
199
+ * checkboxes under their label. Kept as `hints.force`; flows saved before that
200
+ * carry it as the stringified options in `valueRef`, and are read the same way
201
+ * so they need no re-saving.
202
+ */
203
+ export declare function forced(event: Pick<RawEvent, 'action' | 'valueRef' | 'hints'>): boolean;
204
+ /**
205
+ * A response to wait for: `POST /api/visits` answered 201, and its JSON says
206
+ * `commission.amount` is above 0.
207
+ */
208
+ export interface ResponseCheck {
209
+ /** Address: a substring, or a glob (`**\/api/visits`). */
210
+ url: string;
211
+ method?: string;
212
+ status?: number;
213
+ /** A dot path into the JSON body, e.g. "commission.amount" or "items.0.id". */
214
+ field?: string;
215
+ /** The field equals this. */
216
+ equals?: string | number | boolean;
217
+ /** The field is a number above this. */
218
+ above?: number;
219
+ }
220
+ /** Read a response check from a step's value + hints, or from a hints object of its own. */
221
+ export declare function responseCheckOf(url: unknown, hints: Record<string, unknown> | undefined): ResponseCheck | undefined;
222
+ /** `POST /api/visits → 201, commission.amount > 0` */
223
+ export declare function describeResponse(c: ResponseCheck): string;
224
+ /** How a human step shows it's done, if it says. */
225
+ export interface HumanUntil {
226
+ /** The address contains this (or matches it as a glob, as in wait_url). */
227
+ url?: string;
228
+ /** This text appears on the page. */
229
+ text?: string;
230
+ /** The server answers like this — for a step done somewhere the page can't see. */
231
+ response?: ResponseCheck;
232
+ }
233
+ export declare const humanUntil: (event: Pick<RawEvent, "hints">) => HumanUntil | undefined;
@@ -98,3 +98,63 @@ export function redactValue(fieldHint, value, inputType, secretSignal = fieldHin
98
98
  }
99
99
  return { value };
100
100
  }
101
+ /**
102
+ * Actions that never take a value: their first argument in Playwright is an
103
+ * options object (`click({ force: true })`), not something typed or chosen.
104
+ */
105
+ const VALUELESS_ACTIONS = new Set(['click', 'dispatch_click', 'check', 'uncheck']);
106
+ /**
107
+ * The value references a recording really needs filled in — the login values
108
+ * a run asks for before it opens a browser.
109
+ *
110
+ * Only steps that take a value count. Script import used to store
111
+ * `click({ force: true })` as `valueRef: '{ force: true }'`, and every caller
112
+ * then demanded a login value by that name, stopping the run before it began.
113
+ */
114
+ export function valueRefsOf(events) {
115
+ const refs = events.filter((e) => e.valueRef && !VALUELESS_ACTIONS.has(e.action)).map((e) => e.valueRef);
116
+ return [...new Set(refs)];
117
+ }
118
+ /**
119
+ * Should this step skip Playwright's actionability checks — `click({ force: true })`?
120
+ *
121
+ * Needed for controls the app hides behind a styled overlay, like custom
122
+ * checkboxes under their label. Kept as `hints.force`; flows saved before that
123
+ * carry it as the stringified options in `valueRef`, and are read the same way
124
+ * so they need no re-saving.
125
+ */
126
+ export function forced(event) {
127
+ if (event.hints?.force === true)
128
+ return true;
129
+ return VALUELESS_ACTIONS.has(event.action) && /\bforce\s*:\s*true\b/.test(event.valueRef ?? '');
130
+ }
131
+ /** Read a response check from a step's value + hints, or from a hints object of its own. */
132
+ export function responseCheckOf(url, hints) {
133
+ if (typeof url !== 'string' || !url)
134
+ return undefined;
135
+ const h = hints ?? {};
136
+ const eq = h.equals;
137
+ return {
138
+ url,
139
+ ...(typeof h.method === 'string' ? { method: h.method.toUpperCase() } : {}),
140
+ ...(typeof h.status === 'number' ? { status: h.status } : {}),
141
+ ...(typeof h.field === 'string' ? { field: h.field } : {}),
142
+ ...(typeof eq === 'string' || typeof eq === 'number' || typeof eq === 'boolean' ? { equals: eq } : {}),
143
+ ...(typeof h.above === 'number' ? { above: h.above } : {}),
144
+ };
145
+ }
146
+ /** `POST /api/visits → 201, commission.amount > 0` */
147
+ export function describeResponse(c) {
148
+ const field = c.field ? `, ${c.field}${c.equals !== undefined ? ` = ${JSON.stringify(c.equals)}` : c.above !== undefined ? ` > ${c.above}` : ' present'}` : '';
149
+ return `${c.method ? `${c.method} ` : ''}${c.url}${c.status !== undefined ? ` → ${c.status}` : ''}${field}`;
150
+ }
151
+ export const humanUntil = (event) => {
152
+ const url = typeof event.hints?.untilUrl === 'string' ? event.hints.untilUrl : undefined;
153
+ const text = typeof event.hints?.untilText === 'string' ? event.hints.untilText : undefined;
154
+ // `untilResponse: '/api/visits'`, or `{ url, method, status, field, equals | above }`.
155
+ const raw = event.hints?.untilResponse;
156
+ const response = typeof raw === 'string'
157
+ ? responseCheckOf(raw, undefined)
158
+ : raw && typeof raw === 'object' ? responseCheckOf(raw.url, raw) : undefined;
159
+ return url || text || response ? { ...(url ? { url } : {}), ...(text ? { text } : {}), ...(response ? { response } : {}) } : undefined;
160
+ };
@@ -23,7 +23,7 @@
23
23
  */
24
24
  import { type BrowserLease } from './browser.js';
25
25
  import { type VisualCheck } from './visual.js';
26
- import type { RawEvent, RawRecording } from './recording.js';
26
+ import { type HumanUntil, type RawEvent, type RawRecording } from './recording.js';
27
27
  import type { StorageState } from './auth-cache.js';
28
28
  import type { PendingDecision } from './types.js';
29
29
  export interface StepOutcome {
@@ -66,6 +66,8 @@ export interface StepOutcome {
66
66
  attempts?: number;
67
67
  /** Set when a human took this step over rather than the executor running it. */
68
68
  viaHandoff?: boolean;
69
+ /** A `human` step: how it was finished — a person said so, or the page showed it. */
70
+ viaHuman?: 'answered' | 'page';
69
71
  /**
70
72
  * Set when role+name matched several elements and something more specific
71
73
  * had to disambiguate. Feeds `selectors.fragility` — a name that is not
@@ -85,9 +87,30 @@ export interface StepOutcome {
85
87
  roleHadNoMatch?: {
86
88
  fellBackTo: string;
87
89
  };
90
+ /** A failed assertion: what it actually found, e.g. the visible text near where the expected text should be. */
91
+ saw?: string;
92
+ /**
93
+ * The page this step ran on (its address just before acting). What a step's
94
+ * `url` should have been all along — a script read statically can only guess.
95
+ */
96
+ at?: string;
97
+ /**
98
+ * Found but deliberately NOT done: the step a `stopBefore` replay stopped at.
99
+ * Its control was there, visible and enabled; it was never clicked.
100
+ */
101
+ held?: true;
88
102
  }
103
+ /**
104
+ * The part of a page's text worth showing next to an expected string that
105
+ * wasn't there: a window around the longest run of the expected words that IS
106
+ * there ("Total Transactions 9" for an expected "Total Transactions 10"), or
107
+ * the start of the text if none is. Pure; exported for tests.
108
+ */
109
+ export declare function excerpt(actual: string, expected: string | undefined, width?: number): string;
89
110
  export interface CapturedSignal {
90
111
  kind: 'console' | 'pageerror' | 'requestfailed' | 'http';
112
+ /** console only: console.error or console.warn. Warnings are kept as evidence but aren't findings. */
113
+ level?: 'error' | 'warning';
91
114
  /** Step this happened during — the correlation that makes signals useful. */
92
115
  duringStep: number;
93
116
  text: string;
@@ -135,6 +158,18 @@ export interface ReplayResult {
135
158
  * elsewhere. Callers surface this alongside the verdict.
136
159
  */
137
160
  handoffs: HandoffRecord[];
161
+ /** Set when the replay stopped, as asked, before this step (see `stopBefore`). */
162
+ stoppedBefore?: number;
163
+ /** With `endChecks`: what the last page showed, as steps that would check it. */
164
+ endChecks?: EndCheck[];
165
+ }
166
+ /** A check of how the flow ended, offered for someone to approve — never added by itself. */
167
+ export interface EndCheck {
168
+ action: 'assert' | 'wait_text';
169
+ role?: string;
170
+ name?: string;
171
+ value?: string;
172
+ hints?: Record<string, unknown>;
138
173
  }
139
174
  export interface ReplayOptions {
140
175
  /**
@@ -217,6 +252,28 @@ export interface ReplayOptions {
217
252
  * which is right for a verification replay and wrong for a real test.
218
253
  */
219
254
  onDecision?: (decision: PendingDecision) => Promise<Record<string, unknown>>;
255
+ /**
256
+ * Stop before this step (by `seq`): find its control and check it is visible
257
+ * and enabled, then end the replay without doing it.
258
+ *
259
+ * This is how a flow that ends in a real purchase gets proven. Everything up
260
+ * to the commit is replayed for real; the commit itself is only looked at.
261
+ * A run that stops here is `ok` when every step before it passed and the
262
+ * control was ready — and says so in `stoppedBefore`.
263
+ */
264
+ stopBefore?: number;
265
+ /**
266
+ * Ask a person to do a `human` step, and wait for them.
267
+ *
268
+ * Aborted (`signal`) when the page shows the step is done before anyone
269
+ * answers — the run then carries on without them, and whoever asked should
270
+ * stop waiting. Omitted means a human step fails: nobody is there to ask.
271
+ */
272
+ onHuman?: (request: HumanRequest, signal: AbortSignal) => Promise<HumanAnswer>;
273
+ /** How long a human step waits, in ms. Default 15 minutes. */
274
+ humanTimeoutMs?: number;
275
+ /** After a full pass, read the last page into `endChecks` — what "ended right" could mean. */
276
+ endChecks?: boolean;
220
277
  /**
221
278
  * Turn visual checkpoints on. Opt-in because it only means something when
222
279
  * the reasoner can actually look at an image — Bedrock's adapter answers
@@ -245,6 +302,22 @@ export interface ReplayOptions {
245
302
  body: string;
246
303
  }>>;
247
304
  }
305
+ /** What a person is asked to do at a `human` step. */
306
+ export interface HumanRequest {
307
+ seq: number;
308
+ /** What to do, in words to read to them. */
309
+ instruction: string;
310
+ /** The page the run is on. */
311
+ url: string;
312
+ until?: HumanUntil;
313
+ }
314
+ /** done: they did it (the page is still checked, if the step says how). */
315
+ export type HumanAnswer = {
316
+ action: 'done';
317
+ } | {
318
+ action: 'abort';
319
+ reason?: string;
320
+ };
248
321
  /** What a decision may tell the executor to do. */
249
322
  export type DecisionAction = 'continue' | 'abort' | 'retry';
250
323
  /** Why the executor gave up on doing this itself. */
@@ -290,6 +363,13 @@ export type HandoffOutcome = {
290
363
  sig?: string;
291
364
  storageState?: StorageState;
292
365
  recordingHash?: string;
366
+ }
367
+ /**
368
+ * A person did the step in THIS run's own browser (it was headed), so there
369
+ * is nothing to adopt or reload: carry on from the page they left it on.
370
+ */
371
+ | {
372
+ action: 'continue';
293
373
  } | {
294
374
  action: 'skip';
295
375
  } | {