@clipboard-health/playwright-toolkit 1.0.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.
- package/README.md +221 -0
- package/package.json +39 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +11 -0
- package/src/index.js.map +1 -0
- package/src/lib/adminAuthToken.d.ts +56 -0
- package/src/lib/adminAuthToken.js +369 -0
- package/src/lib/adminAuthToken.js.map +1 -0
- package/src/lib/cognitoDiagnostics.d.ts +41 -0
- package/src/lib/cognitoDiagnostics.js +331 -0
- package/src/lib/cognitoDiagnostics.js.map +1 -0
- package/src/lib/deployedAssets.d.ts +77 -0
- package/src/lib/deployedAssets.js +348 -0
- package/src/lib/deployedAssets.js.map +1 -0
- package/src/lib/mailpit.d.ts +86 -0
- package/src/lib/mailpit.js +252 -0
- package/src/lib/mailpit.js.map +1 -0
- package/src/lib/retry.d.ts +66 -0
- package/src/lib/retry.js +262 -0
- package/src/lib/retry.js.map +1 -0
- package/src/lib/setupRetry.d.ts +29 -0
- package/src/lib/setupRetry.js +55 -0
- package/src/lib/setupRetry.js.map +1 -0
- package/src/lib/traceparent.d.ts +31 -0
- package/src/lib/traceparent.js +66 -0
- package/src/lib/traceparent.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# `@clipboard-health/playwright-toolkit`
|
|
2
|
+
|
|
3
|
+
Shared anti-flake primitives for Clipboard Health Playwright suites.
|
|
4
|
+
|
|
5
|
+
The package owns retry policy, APM correlation, shared admin-token caching, deployed-asset checks, Mailpit polling, Cognito login diagnostics, and setup retry classification. Consuming repositories keep only configuration and domain-specific matching.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --save-dev @clipboard-health/playwright-toolkit
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`@playwright/test` is a peer dependency. The package supports Playwright 1.50 and newer.
|
|
14
|
+
|
|
15
|
+
## API map
|
|
16
|
+
|
|
17
|
+
| Local capability | Package replacement |
|
|
18
|
+
| --------------------------------------- | ---------------------------------------------------------------------------------------- |
|
|
19
|
+
| `retryWithBail` | `runWithRetry({ mode: { kind: "classified", ... } })` |
|
|
20
|
+
| `retryUntilPassOrTimeout` | `runWithRetry({ mode: { kind: "poll", ... } })` |
|
|
21
|
+
| Copy-pasted traceparent page fixture | `createTraceparentFixtures()` |
|
|
22
|
+
| Admin token promise cache and file lock | `generateAdminAuthToken()` or `getOrCreateAdminAuthToken()` |
|
|
23
|
+
| Deployed frontend/mobile asset loops | `verifyDeployedAssets()` and `waitForDeployedAssets()` |
|
|
24
|
+
| Mailpit search/fetch loops | `createMailpitClient()`, `fetchMagicLinkFromMailpit()`, `fetchEmailOtpCodeFromMailpit()` |
|
|
25
|
+
| Cognito OTP redirect debugging | `fillOtpAndWaitForCognitoRedirect()` |
|
|
26
|
+
| Setup HTTP and identity retry checks | `classifySetupRetry()` and `isRetryableHttpStatus()` |
|
|
27
|
+
|
|
28
|
+
## Retry contract
|
|
29
|
+
|
|
30
|
+
`runWithRetry` is the only retry abstraction in this package. It has two modes because retries and polling answer different questions.
|
|
31
|
+
|
|
32
|
+
### Classified retry
|
|
33
|
+
|
|
34
|
+
Use classified retry for an operation that should normally pass on the first attempt. The caller must provide `isTransient`. There is no default that retries arbitrary failures.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { runWithRetry } from "@clipboard-health/playwright-toolkit";
|
|
38
|
+
|
|
39
|
+
const result = await runWithRetry({
|
|
40
|
+
operationName: "create shift offer",
|
|
41
|
+
operation: async () => await createShiftOffer(),
|
|
42
|
+
mode: {
|
|
43
|
+
kind: "classified",
|
|
44
|
+
maxAttempts: 4,
|
|
45
|
+
delayMs: 2000,
|
|
46
|
+
isTransient: ({ error }) => isKnownCdcReadinessError(error),
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const shiftOffer = result.value;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
A legal classified retry satisfies the flaky-critic B1 contract:
|
|
54
|
+
|
|
55
|
+
- The operation is safe to repeat.
|
|
56
|
+
- `isTransient` positively identifies a known transient condition.
|
|
57
|
+
- Validation, permission, and other deterministic failures return `false`.
|
|
58
|
+
- The retry has a fixed attempt budget.
|
|
59
|
+
- Exhaustion throws `RetryError` with the attempt count, elapsed time, terminal reason, and last cause.
|
|
60
|
+
|
|
61
|
+
Do not use a broad status such as every `422` as the predicate. Match the specific readiness message or service condition that can resolve without changing the request.
|
|
62
|
+
|
|
63
|
+
### Poll until pass
|
|
64
|
+
|
|
65
|
+
Use poll mode for an idempotent readiness probe where failure means "not ready yet."
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
const result = await runWithRetry({
|
|
69
|
+
operationName: "wait for worker profile",
|
|
70
|
+
operation: async () => await assertWorkerProfileReady(),
|
|
71
|
+
mode: {
|
|
72
|
+
kind: "poll",
|
|
73
|
+
timeoutMs: 90_000,
|
|
74
|
+
intervalsMs: [1000, 2000, 3000, 5000],
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Set `mode.isTransient` when the probe can also throw deterministic failures. Returning `false` stops immediately with `reason: "non-transient"`.
|
|
80
|
+
|
|
81
|
+
Poll mode races each attempt against the remaining timeout budget and aborts the attempt's `signal` when the deadline expires. Pass that signal to network calls so timed-out work is cancelled:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
operation: async ({ signal }) =>
|
|
85
|
+
await fetch(readinessUrl, {
|
|
86
|
+
signal,
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Per-test traceparent fixture
|
|
91
|
+
|
|
92
|
+
Extend the repository's existing Playwright test object once:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { test as base } from "@playwright/test";
|
|
96
|
+
import {
|
|
97
|
+
createTraceparentFixtures,
|
|
98
|
+
type TraceparentFixtures,
|
|
99
|
+
} from "@clipboard-health/playwright-toolkit";
|
|
100
|
+
|
|
101
|
+
export const test = base.extend<TraceparentFixtures>(createTraceparentFixtures());
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The auto fixture creates one non-zero W3C `traceparent`, preserves project-level `extraHTTPHeaders`, installs the merged headers on the browser context before the test body runs, and adds a `traceparent` test annotation. Pass existing headers to `installTraceparentForTest` if a custom fixture installs the header manually.
|
|
105
|
+
|
|
106
|
+
## Admin tokens
|
|
107
|
+
|
|
108
|
+
`generateAdminAuthToken` runs `cbh auth gentoken user`, retries only approved transient CLI signatures, redacts the admin email from errors, and caches the bearer token behind an atomic filesystem lock.
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
const tokenEntry = await generateAdminAuthToken({
|
|
112
|
+
adminEmail: adminUser.email,
|
|
113
|
+
apiEnvironmentName: "staging",
|
|
114
|
+
clientName: "admin-app",
|
|
115
|
+
cacheDurationMs: 10 * 60 * 1000,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const adminAuthToken = tokenEntry.authToken;
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The cache key contains the environment and a SHA-256 email digest, not the email. The cache and lock files use mode `0600`. A process that acquires the lock re-reads the cache before generating, which prevents duplicate token mints across Playwright workers and shards.
|
|
122
|
+
|
|
123
|
+
Use `getOrCreateAdminAuthToken` when the repository needs a different token command:
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
const tokenEntry = await getOrCreateAdminAuthToken({
|
|
127
|
+
adminEmail,
|
|
128
|
+
apiEnvironmentName,
|
|
129
|
+
cacheDurationMs: 10 * 60 * 1000,
|
|
130
|
+
createToken: async () => await generateTokenWithRepositoryCli(),
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Deployed assets
|
|
135
|
+
|
|
136
|
+
Repository wrappers still discover assets and decide which files are runtime or fingerprinted. The package owns request concurrency, timeouts, cache busting, content-type checks, transient HTTP retries, attempt diagnostics, and stable-window polling.
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const report = await waitForDeployedAssets({
|
|
140
|
+
checks: localAssetManifest.map((asset) => ({
|
|
141
|
+
path: asset.path,
|
|
142
|
+
url: new URL(asset.path, deploymentBaseUrl).toString(),
|
|
143
|
+
method: asset.isFingerprintNamedJavaScript ? "GET" : "HEAD",
|
|
144
|
+
cacheMode: asset.isRuntimeAsset ? "cache-busted" : "normal",
|
|
145
|
+
expectedContentTypes: [asset.contentType],
|
|
146
|
+
})),
|
|
147
|
+
timeoutMs: 10 * 60 * 1000,
|
|
148
|
+
pollIntervalMs: 10_000,
|
|
149
|
+
stableWindowMs: 30_000,
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
HTTP `408`, `425`, `429`, and `5xx` responses are transient for asset delivery. Content-type mismatches and failed custom validators are deterministic unless the validator returns `isTransient: true`.
|
|
154
|
+
|
|
155
|
+
Use `validateResponse` for repository-specific checks such as `build-info.json` commit matching. The verifier does not consume a successful response body before calling the wrapper, so the callback can read it directly.
|
|
156
|
+
|
|
157
|
+
## Mailpit
|
|
158
|
+
|
|
159
|
+
Create a client from repository configuration, then use the typed pollers:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const mailpit = createMailpitClient({
|
|
163
|
+
password: process.env.MAILPIT_PASSWORD ?? "",
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const code = await fetchEmailOtpCodeFromMailpit({
|
|
167
|
+
client: mailpit,
|
|
168
|
+
email,
|
|
169
|
+
sentAfter: codeRequestedAt,
|
|
170
|
+
excludeCodes: [previousCode],
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
await page.getByLabel("Verification Code").fill(code.value);
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The pollers search newest-first, tolerate incomplete dates in search results, fetch at most three candidates per probe, and retry Mailpit network errors, `404`, `408`, `429`, and `5xx`. They return both the extracted value and source message ID.
|
|
177
|
+
|
|
178
|
+
## Cognito OTP and login diagnostics
|
|
179
|
+
|
|
180
|
+
`fillOtpAndWaitForCognitoRedirect` monitors `RespondToAuthChallenge` requests for `SMS_OTP` and `EMAIL_OTP` while it waits for the expected redirect.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
await fillOtpAndWaitForCognitoRedirect({
|
|
184
|
+
page,
|
|
185
|
+
testInfo,
|
|
186
|
+
otp,
|
|
187
|
+
expectedUrl: /\/dashboard/,
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
On failure, the error includes the sanitized Cognito request summary, response or request-failure detail, current URL, and visible page-text sample. When `testInfo` is provided, it attaches a redacted screenshot. `sanitizeCognitoDiagnosticText` and `isCognitoOtpChallengeRequest` are public for repository-specific login flows.
|
|
192
|
+
|
|
193
|
+
## Setup retry classification
|
|
194
|
+
|
|
195
|
+
`classifySetupRetry` separates identity collisions from transient infrastructure failures:
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
const classification = classifySetupRetry({
|
|
199
|
+
error,
|
|
200
|
+
isIdentityCollision: ({ error: candidate }) => isWorkerCreationPhoneCollision(candidate),
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
The result follows the flaky-critic C1 contract:
|
|
205
|
+
|
|
206
|
+
| Classification | Decision | Identity behavior |
|
|
207
|
+
| -------------------- | --------------------- | ------------------------------------------------------------- |
|
|
208
|
+
| `identity-collision` | `regenerate-identity` | Generate a fresh phone, email, or ID before the next attempt. |
|
|
209
|
+
| `transient` | `retry-same-identity` | Repeat the same request and identity. |
|
|
210
|
+
| `deterministic` | `do-not-retry` | Fail immediately. |
|
|
211
|
+
|
|
212
|
+
`isRetryableHttpStatus` returns `true` only for `408`, `429`, and `5xx`. A repository can add a narrow `isTransientError` predicate for a known non-HTTP transient signature.
|
|
213
|
+
|
|
214
|
+
## Migration checklist
|
|
215
|
+
|
|
216
|
+
1. Replace the local helper import with the package export.
|
|
217
|
+
2. Move repository-specific URLs, selectors, asset discovery, and error-message matching into a thin wrapper.
|
|
218
|
+
3. Keep retry classification narrow. Record why the operation is safe to repeat.
|
|
219
|
+
4. For identity collisions, generate the identity inside the retry operation so each collision attempt gets a fresh value.
|
|
220
|
+
5. Delete the local helper and port its boundary tests to the wrapper.
|
|
221
|
+
6. Run the consuming repository's Playwright unit tests and E2E setup checks.
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@clipboard-health/playwright-toolkit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Shared anti-flake primitives for Clipboard Health Playwright suites.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cognito",
|
|
7
|
+
"e2e",
|
|
8
|
+
"flaky-tests",
|
|
9
|
+
"mailpit",
|
|
10
|
+
"playwright",
|
|
11
|
+
"retry",
|
|
12
|
+
"testing",
|
|
13
|
+
"traceparent"
|
|
14
|
+
],
|
|
15
|
+
"bugs": "https://github.com/ClipboardHealth/core-utils/issues",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/ClipboardHealth/core-utils.git",
|
|
20
|
+
"directory": "packages/playwright-toolkit"
|
|
21
|
+
},
|
|
22
|
+
"type": "commonjs",
|
|
23
|
+
"main": "./src/index.js",
|
|
24
|
+
"typings": "./src/index.d.ts",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@clipboard-health/util-ts": "5.12.0",
|
|
30
|
+
"tslib": "2.8.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@playwright/test": "1.61.1"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@playwright/test": ">=1.50.0"
|
|
37
|
+
},
|
|
38
|
+
"types": "./src/index.d.ts"
|
|
39
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./lib/adminAuthToken"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./lib/cognitoDiagnostics"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./lib/deployedAssets"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./lib/mailpit"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./lib/retry"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./lib/setupRetry"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./lib/traceparent"), exports);
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/playwright-toolkit/src/index.ts"],"names":[],"mappings":";;;AAAA,+DAAqC;AACrC,mEAAyC;AACzC,+DAAqC;AACrC,wDAA8B;AAC9B,sDAA4B;AAC5B,2DAAiC;AACjC,4DAAkC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export interface AdminAuthTokenCacheEntry {
|
|
2
|
+
authToken: string;
|
|
3
|
+
expiresAtMs: number;
|
|
4
|
+
}
|
|
5
|
+
export interface GetOrCreateAdminAuthTokenParams {
|
|
6
|
+
adminEmail: string;
|
|
7
|
+
apiEnvironmentName: string;
|
|
8
|
+
cacheDurationMs: number;
|
|
9
|
+
createToken: () => Promise<string>;
|
|
10
|
+
cacheDirectory?: string | undefined;
|
|
11
|
+
lockStaleAfterMs?: number | undefined;
|
|
12
|
+
lockWaitTimeoutMs?: number | undefined;
|
|
13
|
+
lockRetryDelayMs?: number | undefined;
|
|
14
|
+
lockRetryJitterMs?: number | undefined;
|
|
15
|
+
nowImplementation?: (() => number) | undefined;
|
|
16
|
+
randomImplementation?: (() => number) | undefined;
|
|
17
|
+
sleepImplementation?: ((params: {
|
|
18
|
+
durationMs: number;
|
|
19
|
+
}) => Promise<void>) | undefined;
|
|
20
|
+
}
|
|
21
|
+
export interface AdminAuthTokenCommandResult {
|
|
22
|
+
stdout: string;
|
|
23
|
+
stderr: string;
|
|
24
|
+
}
|
|
25
|
+
export interface AdminAuthTokenCommandRunnerParams {
|
|
26
|
+
executable: string;
|
|
27
|
+
arguments: readonly string[];
|
|
28
|
+
timeoutMs: number;
|
|
29
|
+
}
|
|
30
|
+
export type AdminAuthTokenCommandRunner = (params: AdminAuthTokenCommandRunnerParams) => Promise<AdminAuthTokenCommandResult>;
|
|
31
|
+
export interface GenerateAdminAuthTokenParams extends Omit<GetOrCreateAdminAuthTokenParams, "createToken"> {
|
|
32
|
+
clientName?: string | undefined;
|
|
33
|
+
commandExecutable?: string | undefined;
|
|
34
|
+
commandRunner?: AdminAuthTokenCommandRunner | undefined;
|
|
35
|
+
commandTimeoutMs?: number | undefined;
|
|
36
|
+
generationMaxAttempts?: number | undefined;
|
|
37
|
+
generationRetryDelayMs?: number | undefined;
|
|
38
|
+
retryJitterMs?: number | undefined;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Returns a valid cached admin token or creates one while holding an atomic
|
|
42
|
+
* filesystem lock shared by Playwright workers, shards, and local processes.
|
|
43
|
+
*/
|
|
44
|
+
export declare function getOrCreateAdminAuthToken(params: GetOrCreateAdminAuthTokenParams): Promise<AdminAuthTokenCacheEntry>;
|
|
45
|
+
/**
|
|
46
|
+
* Generates an admin token with classified CLI retries, redacted errors, and
|
|
47
|
+
* the lock-serialized cross-process cache.
|
|
48
|
+
*/
|
|
49
|
+
export declare function generateAdminAuthToken(params: GenerateAdminAuthTokenParams): Promise<AdminAuthTokenCacheEntry>;
|
|
50
|
+
export declare function isRetryableAdminAuthTokenGenerationError(params: {
|
|
51
|
+
error: unknown;
|
|
52
|
+
}): boolean;
|
|
53
|
+
export declare function isAdminAuthTokenExpired(params: {
|
|
54
|
+
cacheEntry: Pick<AdminAuthTokenCacheEntry, "expiresAtMs">;
|
|
55
|
+
nowMs?: number;
|
|
56
|
+
}): boolean;
|