@owlmeans/test-ui 0.1.18-rc.9 → 0.1.18

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.
Files changed (69) hide show
  1. package/README.md +10 -7
  2. package/agent-meta/manifest.json +3 -3
  3. package/agent-meta/skills/testing-ui/SKILL.md +209 -23
  4. package/build/browser/types.d.ts +22 -0
  5. package/build/browser/types.d.ts.map +1 -0
  6. package/build/browser/types.js +2 -0
  7. package/build/browser/types.js.map +1 -0
  8. package/build/browser.d.ts +8 -16
  9. package/build/browser.d.ts.map +1 -1
  10. package/build/browser.js +38 -41
  11. package/build/browser.js.map +1 -1
  12. package/build/consent/types.d.ts +36 -0
  13. package/build/consent/types.d.ts.map +1 -0
  14. package/build/consent/types.js +2 -0
  15. package/build/consent/types.js.map +1 -0
  16. package/build/consent.d.ts +4 -0
  17. package/build/consent.d.ts.map +1 -0
  18. package/build/consent.js +41 -0
  19. package/build/consent.js.map +1 -0
  20. package/build/consts.local.d.ts +9 -0
  21. package/build/consts.local.d.ts.map +1 -0
  22. package/build/consts.local.js +9 -0
  23. package/build/consts.local.js.map +1 -0
  24. package/build/index.d.ts +8 -0
  25. package/build/index.d.ts.map +1 -1
  26. package/build/index.js +3 -0
  27. package/build/index.js.map +1 -1
  28. package/build/marketing-consent.d.ts +5 -0
  29. package/build/marketing-consent.d.ts.map +1 -0
  30. package/build/marketing-consent.js +4 -0
  31. package/build/marketing-consent.js.map +1 -0
  32. package/build/mount.d.ts +2 -27
  33. package/build/mount.d.ts.map +1 -1
  34. package/build/mount.js +11 -4
  35. package/build/mount.js.map +1 -1
  36. package/build/page/types.d.ts +141 -0
  37. package/build/page/types.d.ts.map +1 -0
  38. package/build/page/types.js +2 -0
  39. package/build/page/types.js.map +1 -0
  40. package/build/page.d.ts +4 -0
  41. package/build/page.d.ts.map +1 -0
  42. package/build/page.js +168 -0
  43. package/build/page.js.map +1 -0
  44. package/build/supervisor/types.d.ts +56 -0
  45. package/build/supervisor/types.d.ts.map +1 -0
  46. package/build/supervisor/types.js +2 -0
  47. package/build/supervisor/types.js.map +1 -0
  48. package/build/supervisor.d.ts +9 -86
  49. package/build/supervisor.d.ts.map +1 -1
  50. package/build/supervisor.js +65 -123
  51. package/build/supervisor.js.map +1 -1
  52. package/build/types.d.ts +46 -0
  53. package/build/types.d.ts.map +1 -0
  54. package/build/types.js +2 -0
  55. package/build/types.js.map +1 -0
  56. package/package.json +11 -11
  57. package/src/browser/types.ts +22 -0
  58. package/src/browser.ts +43 -40
  59. package/src/consent/types.ts +38 -0
  60. package/src/consent.ts +48 -0
  61. package/src/consts.local.ts +12 -0
  62. package/src/index.ts +8 -0
  63. package/src/marketing-consent.ts +8 -0
  64. package/src/mount.ts +12 -32
  65. package/src/page/types.ts +144 -0
  66. package/src/page.ts +194 -0
  67. package/src/supervisor/types.ts +55 -0
  68. package/src/supervisor.ts +83 -187
  69. package/src/types.ts +47 -0
package/src/supervisor.ts CHANGED
@@ -1,204 +1,100 @@
1
- import { mkdir } from 'node:fs/promises'
2
- import { join, resolve } from 'node:path'
3
1
  import type { Page } from 'playwright'
4
- import type { Auth, AuthCredentials, AuthRole } from '@owlmeans/auth'
5
- import {
6
- AuthroizationType, AuthRole as Role, AuthenticationType, buildSupervisorPayload
7
- } from '@owlmeans/auth'
2
+ import { type Auth, type AuthCredentials, AuthroizationType, AuthRole as Role, AuthenticationType, buildSupervisorPayload } from '@owlmeans/auth'
8
3
  import { makeKeyPairModel } from '@owlmeans/basic-keys'
9
4
  import { createIdOfLength } from '@owlmeans/basic-ids'
10
5
  import { EnvelopeKind, makeEnvelopeModel } from '@owlmeans/basic-envelope'
11
6
  import { makeBearer } from '@owlmeans/test-auth'
12
-
13
- export interface PregenerateAuthTokenOptions {
14
- /** Target user id / email the token represents. */
15
- userId: string
16
- /**
17
- * Private key to sign the bearer with, e.g. `ed25519:<base64>` from
18
- * `.env.dev.secrets`. MUST be the key the target backend trusts as its own
19
- * signing key (it verifies bearers against `cfg.alias`/`cfg.service`).
20
- */
21
- pk: string
22
- scopes?: string[]
23
- role?: AuthRole
24
- entityId?: string
25
- profileId?: string
26
- /** `source` recorded in the Auth payload (usually the backend service alias). */
27
- source?: string
28
- }
29
-
30
- /**
31
- * Mint a ready-to-use `ED25519-BASIC-TOKEN ...` bearer offline, reusing
32
- * `@owlmeans/test-auth`'s `makeBearer`. This is the "set a token directly"
33
- * fast-path for e2e/integration tests: no UI, no live auth round-trip. The
34
- * backend accepts it because it is signed by the project's own trusted key.
35
- *
36
- * For a faithful end-to-end login (real supervisor plugin + registration), drive
37
- * {@link loginViaSupervisorForm} instead.
38
- */
39
- export const pregenerateAuthToken = async (opts: PregenerateAuthTokenOptions): Promise<string> => {
40
- const auth: Auth = {
41
- token: createIdOfLength(32),
42
- userId: opts.userId,
43
- profileId: opts.profileId ?? opts.userId,
44
- entitySlug: opts.entityId,
45
- scopes: opts.scopes ?? ['*'],
46
- role: opts.role ?? Role.User,
47
- type: AuthroizationType.Ed25519BasicToken,
48
- source: opts.source ?? '',
49
- isUser: true,
50
- createdAt: new Date()
7
+ import { makePageHelper } from './page.js'
8
+ import type { AcceptConsentOptions, SupervisorFormLoginOptions } from './page/types.js'
9
+ import type { PregenerateAuthTokenOptions, SupervisorApiAuthOptions, SupervisorAuthHelper } from './supervisor/types.js'
10
+
11
+ export const createSupervisorAuthHelper = (): SupervisorAuthHelper => {
12
+ const pregenerateAuthToken = async (opts: PregenerateAuthTokenOptions): Promise<string> => {
13
+ const auth: Auth = {
14
+ token: createIdOfLength(32),
15
+ userId: opts.userId,
16
+ profileId: opts.profileId ?? opts.userId,
17
+ entitySlug: opts.entityId,
18
+ scopes: opts.scopes ?? ['*'],
19
+ role: opts.role ?? Role.User,
20
+ type: AuthroizationType.Ed25519BasicToken,
21
+ source: opts.source ?? '',
22
+ isUser: true,
23
+ createdAt: new Date()
24
+ }
25
+
26
+ return makeBearer(auth, makeKeyPairModel(opts.pk))
51
27
  }
52
28
 
53
- return makeBearer(auth, makeKeyPairModel(opts.pk))
54
- }
55
-
56
- /**
57
- * Inject a pre-generated bearer into an app that uses the standard owlmeans
58
- * dispatcher: navigate to `/dispatcher?token=...` so the web app authenticates
59
- * and redirects home natively. Note: apps that override the DISPATCHER route
60
- * (e.g. to force an external IdP) should use {@link loginViaSupervisorForm}.
61
- */
62
- export const loginViaDispatcher = async (
63
- page: Page, baseUrl: string, token: string, opts?: { dispatcherPath?: string }
64
- ): Promise<void> => {
65
- const path = opts?.dispatcherPath ?? '/dispatcher'
66
- const url = new URL(path, baseUrl)
67
- url.searchParams.set('token', token)
68
- await page.goto(url.toString())
69
- // Wait until the dispatcher has navigated away (token consumed).
70
- await page.waitForURL(u => !u.pathname.startsWith(path), { timeout: 30_000 })
71
- }
72
-
73
- export interface SupervisorApiAuthOptions {
74
- /** Auth-manager API base URL (the backend serving /authentication/* and /authenticate). */
75
- apiBaseUrl: string
76
- /** Target user id / email to authenticate (and register on first use). */
77
- userId: string
78
- /** Supervisor private key, e.g. `ed25519:<base64>` from `.env.dev.secrets`. */
79
- pk: string
80
- /** Paths, overridable for non-default routing. */
81
- paths?: { init?: string, authenticate?: string, dispatch?: string }
82
- fetchImpl?: typeof fetch
83
- }
84
-
85
- const postJson = async (
86
- fetchImpl: typeof fetch, url: string, body: unknown
87
- ): Promise<any> => {
88
- const res = await fetchImpl(url, {
89
- method: 'POST',
90
- headers: { 'Content-Type': 'application/json' },
91
- body: JSON.stringify(body)
92
- })
93
- if (!res.ok) {
94
- throw new Error(`supervisor api ${url} -> ${res.status} ${await res.text().catch(() => '')}`)
29
+ const postJson = async (
30
+ fetchImpl: typeof fetch, url: string, body: unknown
31
+ ): Promise<any> => {
32
+ const res = await fetchImpl(url, {
33
+ method: 'POST',
34
+ headers: { 'Content-Type': 'application/json' },
35
+ body: JSON.stringify(body)
36
+ })
37
+ if (!res.ok) {
38
+ throw new Error(`supervisor api ${url} -> ${res.status} ${await res.text().catch(() => '')}`)
39
+ }
40
+ return res.json()
95
41
  }
96
- return res.json()
97
- }
98
42
 
99
- /**
100
- * Pregenerate a real bearer by driving the live PK-based supervisor flow over the
101
- * backend API (no browser): `/authentication/init` -> sign the challenge with the
102
- * supervisor key -> `/authentication/authenticate` -> `/authenticate` (dispatcher
103
- * exchange). This is the faithful "set a token directly via API" path
104
- * (registers the user on first use), independent of any front-end routing.
105
- *
106
- * Returns the final `ED25519-BASIC-TOKEN ...` bearer the backend accepts.
107
- */
108
- export const authenticateViaSupervisorApi = async (
109
- opts: SupervisorApiAuthOptions
110
- ): Promise<string> => {
111
- const fetchImpl = opts.fetchImpl ?? fetch
112
- const base = opts.apiBaseUrl.replace(/\/$/, '')
113
- const initPath = opts.paths?.init ?? '/authentication/init'
114
- const authPath = opts.paths?.authenticate ?? '/authentication/authenticate'
115
- const dispatchPath = opts.paths?.dispatch ?? '/authenticate'
116
-
117
- // 1. Ask the auth manager for a fresh, signed challenge envelope.
118
- const { challenge } = await postJson(fetchImpl, base + initPath, {
119
- type: AuthenticationType.Supervisor
120
- }) as { challenge: string }
121
-
122
- // 2. Sign the unwrapped challenge + userId + salt with the supervisor key.
123
- const challengeMsg = makeEnvelopeModel<string>(challenge, EnvelopeKind.Wrap).message()
124
- const salt = createIdOfLength(16)
125
- const signature = await makeKeyPairModel(opts.pk)
126
- .sign(buildSupervisorPayload(challengeMsg, opts.userId, salt))
127
-
128
- const credentials: AuthCredentials = {
129
- type: AuthenticationType.Supervisor,
130
- challenge,
131
- credential: JSON.stringify({ salt, signature }),
132
- userId: opts.userId,
133
- role: Role.User,
134
- scopes: ['*']
43
+ const authenticateViaSupervisorApi = async (
44
+ opts: SupervisorApiAuthOptions
45
+ ): Promise<string> => {
46
+ const fetchImpl = opts.fetchImpl ?? fetch
47
+ const base = opts.apiBaseUrl.replace(/\/$/, '')
48
+ const initPath = opts.paths?.init ?? '/authentication/init'
49
+ const authPath = opts.paths?.authenticate ?? '/authentication/authenticate'
50
+ const dispatchPath = opts.paths?.dispatch ?? '/authenticate'
51
+
52
+ // 1. Ask the auth manager for a fresh, signed challenge envelope.
53
+ const { challenge } = await postJson(fetchImpl, base + initPath, {
54
+ type: AuthenticationType.Supervisor
55
+ }) as { challenge: string }
56
+
57
+ // 2. Sign the unwrapped challenge + userId + salt with the supervisor key.
58
+ const challengeMsg = makeEnvelopeModel<string>(challenge, EnvelopeKind.Wrap).message()
59
+ const salt = createIdOfLength(16)
60
+ const signature = await makeKeyPairModel(opts.pk)
61
+ .sign(buildSupervisorPayload(challengeMsg, opts.userId, salt))
62
+
63
+ const credentials: AuthCredentials = {
64
+ type: AuthenticationType.Supervisor,
65
+ challenge,
66
+ credential: JSON.stringify({ salt, signature }),
67
+ userId: opts.userId,
68
+ role: Role.User,
69
+ scopes: ['*']
70
+ }
71
+
72
+ // 3. Exchange for the intermediate auth-manager token.
73
+ const intermediate = await postJson(fetchImpl, base + authPath, credentials) as { token: string }
74
+
75
+ // 4. Exchange the intermediate token for the final project bearer.
76
+ const final = await postJson(fetchImpl, base + dispatchPath, { token: intermediate.token }) as { token: string }
77
+
78
+ return final.token
135
79
  }
136
80
 
137
- // 3. Exchange for the intermediate auth-manager token.
138
- const intermediate = await postJson(fetchImpl, base + authPath, credentials) as { token: string }
139
-
140
- // 4. Exchange the intermediate token for the final project bearer.
141
- const final = await postJson(fetchImpl, base + dispatchPath, { token: intermediate.token }) as { token: string }
142
-
143
- return final.token
81
+ return { pregenerateAuthToken, authenticateViaSupervisorApi }
144
82
  }
145
83
 
146
- /**
147
- * Save a full-page screenshot to `<dir>/<name>.png`, creating `dir` if needed.
148
- * Returns the absolute file path. Use a host-backed folder (e.g. the project's
149
- * `tmp/tests/screenshots`) so the file is visible whether the test runs on the
150
- * host or inside a dev container.
151
- */
152
- export const saveScreenshot = async (page: Page, dir: string, name: string): Promise<string> => {
153
- const target = resolve(dir)
154
- await mkdir(target, { recursive: true })
155
- const file = join(target, `${name}.png`)
156
- await page.screenshot({ path: file, fullPage: true })
157
- return file
158
- }
84
+ export const supervisorAuthHelper = createSupervisorAuthHelper()
159
85
 
160
- export interface SupervisorFormLoginOptions {
161
- baseUrl: string
162
- /** Target user id / email. */
163
- userId: string
164
- /** Supervisor private key (e.g. `ed25519:<base64>` from `.env.dev.secrets`). */
165
- pk: string
166
- /** Login path; defaults to the standard supervisor route. */
167
- path?: string
168
- /** Predicate/URL fragment that signals a successful landing (default: leaving the login path). */
169
- expectPath?: string
170
- timeout?: number
171
- /** When set, capture the filled login form (`supervisor-login-form.png`) before submit. */
172
- screenshotDir?: string
173
- }
86
+ /** @deprecated compat:factory-refactor — use `supervisorAuthHelper.authenticateViaSupervisorApi(…)` */
87
+ export const authenticateViaSupervisorApi = async (opts: SupervisorApiAuthOptions): Promise<string> =>
88
+ await supervisorAuthHelper.authenticateViaSupervisorApi(opts)
174
89
 
175
- const DEFAULT_SUPERVISOR_PATH = '/authentication/login/pk-supervisor'
176
-
177
- /**
178
- * Drive the PK-based supervisor login FORM end-to-end (the faithful path that
179
- * exercises the real supervisor plugin + registration): navigate to the
180
- * supervisor login route, fill the user id + private key, submit, and wait for
181
- * the app to leave the login screen.
182
- */
183
- export const loginViaSupervisorForm = async (
184
- page: Page, opts: SupervisorFormLoginOptions
185
- ): Promise<void> => {
186
- const path = opts.path ?? DEFAULT_SUPERVISOR_PATH
187
- const timeout = opts.timeout ?? 60_000
188
- await page.goto(new URL(path, opts.baseUrl).toString())
189
-
190
- await page.getByTestId('supervisor-user-id').fill(opts.userId)
191
- await page.getByTestId('supervisor-pk').fill(opts.pk)
192
-
193
- if (opts.screenshotDir != null) {
194
- await saveScreenshot(page, opts.screenshotDir, 'supervisor-login-form')
195
- }
90
+ /** @deprecated compat:factory-refactor — use `makePageHelper(page).saveScreenshot(…)` */
91
+ export const saveScreenshot = async (page: Page, dir: string, name: string): Promise<string> =>
92
+ await makePageHelper(page).saveScreenshot(dir, name)
196
93
 
197
- await page.getByTestId('supervisor-submit').click()
94
+ /** @deprecated compat:factory-refactor — use `makePageHelper(page).acceptConsent(…)` */
95
+ export const acceptConsent = async (page: Page, opts?: AcceptConsentOptions): Promise<boolean> =>
96
+ await makePageHelper(page).acceptConsent(opts)
198
97
 
199
- if (opts.expectPath != null) {
200
- await page.waitForURL(url => url.toString().includes(opts.expectPath!), { timeout })
201
- } else {
202
- await page.waitForURL(url => !url.pathname.startsWith(path), { timeout })
203
- }
204
- }
98
+ /** @deprecated compat:factory-refactor — use `makePageHelper(page).loginViaSupervisorForm(…)` */
99
+ export const loginViaSupervisorForm = async (page: Page, opts: SupervisorFormLoginOptions): Promise<void> =>
100
+ await makePageHelper(page).loginViaSupervisorForm(opts)
package/src/types.ts ADDED
@@ -0,0 +1,47 @@
1
+ import type { Page } from 'playwright'
2
+ import type { ConsentGeoMock } from './consent/types.js'
3
+
4
+ export interface MountOptions {
5
+ /**
6
+ * URL the harness is reachable at. The consuming package is in charge
7
+ * of producing it — typically a Vite dev server pointed at
8
+ * `node_modules/@owlmeans/test-ui/harness/` plus a per-package
9
+ * `mount.tsx` that registers the components under test, OR an
10
+ * inlined `data:text/html,...` URL for the simplest smoke tests.
11
+ */
12
+ url: string
13
+ /**
14
+ * Component name registered in the harness. Appended as `?component=`.
15
+ * Omit when the harness mounts a single fixed root.
16
+ */
17
+ component?: string
18
+ /**
19
+ * JSON-serialisable props passed to the component. Encoded into the
20
+ * URL as `?props=<json>` for the harness's `mount.tsx` to read.
21
+ */
22
+ props?: Record<string, unknown>
23
+ /**
24
+ * What counts as "arrived". Defaults to `domcontentloaded`.
25
+ *
26
+ * NOT `load`, which is playwright's own default and is wrong for any page that is a real
27
+ * application: `load` waits for EVERY subresource, and an analytics beacon, a long-poll or a
28
+ * third-party pixel that never settles holds it open until the navigation times out — with the
29
+ * page fully rendered and working the whole time. The failure reads as "the site is down".
30
+ *
31
+ * A spec waits for the selector it actually needs; that is the assertion, not the load event.
32
+ */
33
+ waitUntil?: 'commit' | 'domcontentloaded' | 'load' | 'networkidle'
34
+ /** Navigation timeout in ms. */
35
+ timeout?: number
36
+ /**
37
+ * Answer the page's Cloudflare trace with this — installed BEFORE the first navigation, so the
38
+ * consent geo gate sees it on the very first load. See `consentGeoTestHelper.mockConsentGeo`.
39
+ */
40
+ consentGeo?: ConsentGeoMock
41
+ }
42
+
43
+ export interface Mounted {
44
+ page: Page
45
+ /** Closes the page's browser context — the shared browser stays alive. */
46
+ close: () => Promise<void>
47
+ }