@9l/playwright 0.5.0 → 0.5.2

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/README.md CHANGED
@@ -10,7 +10,7 @@ completing does not verify that behavior.
10
10
  Published on [npm](https://www.npmjs.com/package/@9l/playwright):
11
11
 
12
12
  ```sh
13
- npm install --save-dev @9l/playwright@0.5.0 @playwright/test@1.64.0
13
+ npm install --save-dev @9l/playwright@0.5.2 @playwright/test@1.64.0
14
14
  npm exec playwright install chromium
15
15
  ```
16
16
 
package/dist/prove.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { BrowserContext, Request } from '@playwright/test';
2
2
  export declare const proveProtocol = "9l.prove/1";
3
- export declare const faultKinds: readonly ["abort", "http-500", "empty-json", "http-401", "http-403", "http-429", "malformed-json"];
3
+ export declare const faultKinds: readonly ["abort", "http-500", "empty-json", "http-401", "http-403", "http-429", "malformed-json", "redirect-cross-origin", "delay"];
4
4
  export type FaultKind = typeof faultKinds[number];
5
5
  export type Target = {
6
6
  method: string;
@@ -27,5 +27,9 @@ export declare function openProveChannel(env: NodeJS.ProcessEnv, testInfo: {
27
27
  testId: string;
28
28
  retry: number;
29
29
  }): ProveChannel | undefined;
30
+ export declare const redirectLocation = "http://redirect.invalid/";
31
+ export declare const delayHoldMs = 35000;
30
32
  export declare function replaceJSON(kind: 'empty-json' | 'malformed-json', text: string): string | undefined;
31
- export declare function instrument(context: BrowserContext, channel: ProveChannel): Promise<void>;
33
+ export declare function instrument(context: BrowserContext, channel: ProveChannel, options?: {
34
+ holdMs?: number;
35
+ }): Promise<void>;
package/dist/prove.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ProveChannel = exports.faultKinds = exports.proveProtocol = void 0;
3
+ exports.delayHoldMs = exports.redirectLocation = exports.ProveChannel = exports.faultKinds = exports.proveProtocol = void 0;
4
4
  exports.requestTarget = requestTarget;
5
5
  exports.matches = matches;
6
6
  exports.readFault = readFault;
@@ -18,7 +18,7 @@ const protocol_1 = require("./protocol");
18
18
  // enter the 9l.engine/1 stream, whose metadata excludes URLs by design.
19
19
  exports.proveProtocol = '9l.prove/1';
20
20
  // Must match prove.Kinds in internal/prove/prove.go.
21
- exports.faultKinds = ['abort', 'http-500', 'empty-json', 'http-401', 'http-403', 'http-429', 'malformed-json'];
21
+ exports.faultKinds = ['abort', 'http-500', 'empty-json', 'http-401', 'http-403', 'http-429', 'malformed-json', 'redirect-cross-origin', 'delay'];
22
22
  const maxRecords = 2000;
23
23
  const maxRecordBytes = 4096;
24
24
  const maxPathBytes = 2048;
@@ -123,12 +123,49 @@ function openProveChannel(env, testInfo) {
123
123
  channel.record({ type: 'capabilities', faults: [...exports.faultKinds] });
124
124
  return channel;
125
125
  }
126
+ // redirect-cross-origin sends the page to a fixed origin on a reserved,
127
+ // never-resolving name (RFC 2606), so following the redirect always fails
128
+ // and no application host is contacted; DNS may still be asked for the name.
129
+ exports.redirectLocation = 'http://redirect.invalid/';
130
+ // delay holds the response this long before letting it through: longer than
131
+ // Playwright's default 30s test timeout and 5s expect timeout, so a test that
132
+ // waits for the response fails unless its own timeouts are longer. The hold
133
+ // ends early when the context closes, so it never outlives the test.
134
+ exports.delayHoldMs = 35_000;
126
135
  const statusFaults = {
127
136
  'http-500': { status: 500 },
128
137
  'http-401': { status: 401 },
129
138
  'http-403': { status: 403 },
130
139
  'http-429': { status: 429, headers: { 'retry-after': '1' } },
140
+ 'redirect-cross-origin': { status: 302, headers: { location: exports.redirectLocation } },
131
141
  };
142
+ // Holds for one context. Each hold resolves after ms, or as soon as the
143
+ // context closes. One close listener serves every hold, so a page that polls
144
+ // the delayed request never piles up listeners. The timers never keep the
145
+ // worker process alive on their own.
146
+ function holds(context) {
147
+ const waiting = new Set();
148
+ let closed = false;
149
+ context.once('close', () => {
150
+ closed = true;
151
+ for (const release of [...waiting])
152
+ release();
153
+ });
154
+ return (ms) => new Promise(resolve => {
155
+ if (closed)
156
+ return resolve();
157
+ const release = () => { clearTimeout(timer); waiting.delete(release); resolve(); };
158
+ const timer = setTimeout(release, ms);
159
+ timer.unref?.();
160
+ waiting.add(release);
161
+ });
162
+ }
163
+ // The fault cannot replace a response the upstream never gave. The request is
164
+ // aborted rather than sent again: the upstream may already have acted on it.
165
+ function unreachable(route, channel, fault) {
166
+ channel.record({ type: 'not-applicable', fault: fault.id, reason: 'unreachable' });
167
+ return route.abort('failed').catch(() => { });
168
+ }
132
169
  // The replacement body for a JSON object or array, or undefined when the
133
170
  // response is neither. malformed-json keeps the first half of the trimmed
134
171
  // text: any proper prefix of an object or array leaves a bracket unclosed.
@@ -151,8 +188,9 @@ function isJSON(contentType) {
151
188
  const media = (contentType ?? '').split(';')[0].trim().toLowerCase();
152
189
  return media.endsWith('/json') || media.endsWith('+json');
153
190
  }
154
- async function instrument(context, channel) {
191
+ async function instrument(context, channel, options = {}) {
155
192
  const fault = channel.fault;
193
+ const holdMs = options.holdMs ?? exports.delayHoldMs;
156
194
  if (!fault) {
157
195
  // Observation is passive: listeners only, so the baseline run is unchanged.
158
196
  context.on('request', request => {
@@ -167,6 +205,7 @@ async function instrument(context, channel) {
167
205
  });
168
206
  return;
169
207
  }
208
+ const hold = fault.kind === 'delay' ? holds(context) : undefined;
170
209
  // Registered when the context is created, so routes the test adds later run
171
210
  // first; a request the test fulfills itself is never faulted.
172
211
  await context.route('**/*', async (route) => {
@@ -180,6 +219,24 @@ async function instrument(context, channel) {
180
219
  const status = statusFaults[fault.kind];
181
220
  if (status)
182
221
  return route.fulfill({ ...status, body: '' }).then(applied);
222
+ if (hold) {
223
+ // The request is dispatched first, so the application receives it as
224
+ // usual; only its response is held. The hold is the fault, so it is
225
+ // credited as soon as it starts, and the response follows once it ends
226
+ // unless the test is already over. A redirect is held and delivered as
227
+ // it came, not followed here, so the browser follows it itself and the
228
+ // page sees the same redirect as without the fault.
229
+ let response;
230
+ try {
231
+ response = await route.fetch({ maxRedirects: 0 });
232
+ }
233
+ catch {
234
+ return unreachable(route, channel, fault);
235
+ }
236
+ applied();
237
+ await hold(holdMs);
238
+ return route.fulfill({ response }).catch(() => { });
239
+ }
183
240
  const kind = fault.kind;
184
241
  // The JSON faults need the real response first; say why when it cannot
185
242
  // be replaced: the upstream was unreachable, or the body was not an
@@ -189,8 +246,7 @@ async function instrument(context, channel) {
189
246
  response = await route.fetch();
190
247
  }
191
248
  catch {
192
- channel.record({ type: 'not-applicable', fault: fault.id, reason: 'unreachable' });
193
- return route.continue().catch(() => { });
249
+ return unreachable(route, channel, fault);
194
250
  }
195
251
  const body = replaceJSON(kind, await response.text().catch(() => ''));
196
252
  if (body === undefined) {
package/dist/reporter.js CHANGED
@@ -3,6 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const node_crypto_1 = require("node:crypto");
4
4
  const node_fs_1 = require("node:fs");
5
5
  const protocol_1 = require("./protocol");
6
+ function insideAssertion(step) {
7
+ for (let ancestor = step.parent; ancestor; ancestor = ancestor.parent) {
8
+ if (ancestor.category === 'expect')
9
+ return true;
10
+ }
11
+ return false;
12
+ }
6
13
  // Worker logs, error messages, step titles, URLs and attachment bodies never enter
7
14
  // the protocol. IDs are hashes; Go persists only this bounded metadata stream.
8
15
  class EngineReporter {
@@ -50,6 +57,12 @@ class EngineReporter {
50
57
  this.emit('test_begin', { testId: this.testId(test), retry: result.retry });
51
58
  }
52
59
  onStepEnd(test, result, step) {
60
+ // expect.poll, toPass and custom matchers report the assertions they run,
61
+ // on every retry, as expect steps beneath their own, sometimes inside a
62
+ // test.step. A retrying assertion counts once, including the assertions
63
+ // inside it; its own step carries the final outcome.
64
+ if (step.category === 'expect' && insideAssertion(step))
65
+ return;
53
66
  const categories = { 'test.step': 'action', expect: 'assertion', 'pw:api': 'browser', fixture: 'fixture' };
54
67
  const operation = this.target && step.category === 'test.step' && step.location?.file === protocol_1.targetStepFile && step.title.startsWith('9l target ') ? step.title.slice('9l target '.length) : undefined;
55
68
  this.emit('step_end', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@9l/playwright",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Playwright fixtures and the versioned 9lives Go engine bridge",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/Quality-Max/9lives-runner#local-playwright-sdk",
@@ -24,7 +24,7 @@
24
24
  "9l": {
25
25
  "protocol": "9l.engine/1",
26
26
  "capabilities": ["steps", "artifact-metadata", "terminal-outcomes"],
27
- "faults": ["abort", "http-500", "empty-json", "http-401", "http-403", "http-429", "malformed-json"]
27
+ "faults": ["abort", "http-500", "empty-json", "http-401", "http-403", "http-429", "malformed-json", "redirect-cross-origin", "delay"]
28
28
  },
29
29
  "publishConfig": {"access": "public"}
30
30
  }