test: #3615771 Cross-origin Playwright tests of the embed snippet, and the embedding guide
Stacked on !33 (merged), !35 (merged), !36 (merged), !37 (merged), !39 (merged) and !40 (merged); review only the last commit(s) until those merge.
Closes #3615771 (closed).
What changes
Cross-origin Playwright suite in tests/playwright. The snippet runs on a second DDEV hostname against the real Drupal site, in Chromium, Firefox and WebKit. Only the LLM is scripted: the ai_answers_test provider answers "Test answer.", as in the module's own tests.
- 4 host pages:
plain(no CSP, a stylesheet that hides every form, input and button it reaches),strict(CSP withdefault-src 'none'and script, style, connect and img only from the Drupal origin),spa(mounts a fresh snippet on apushStateview, unmounts it on another),frame(the plain page in an iframe). - Each page has the snippet exactly as the placement form shows it: the setup renders the pages with
EmbedSnippet::build(). - Per page: shadow roots with and without
parseHTMLUnsafe, token fetched and sent, answer streamed, follow-up, feedback, 403 then new token and 1 retry, second 403 stands, 429 message with the question kept andRetry-Afterreadable by page code, secondv1.jsdoes nothing, disallowed origin gets nothing, no CSP violation, no uncaught error. Plus 1 SPA test: away and back withpushStateand the back button. - The 403 tests replace the token on the outgoing request. The 403 and its CORS headers are the server's.
Fixture module ai_answers_e2e (hidden, test only). E2eSite::setUp() creates the agent, 3 placements per browser (main, limited at 1 question an hour, denied) and writes the host pages. tearDown() removes them. Playwright's global setup and teardown run both through ddev exec drush, and uninstall only the modules the run enabled.
DDEV config to copy into a project, in tests/playwright/ddev: additional_hostnames: [ai-answers-embed-host] and an nginx_full server block that serves /tmp/ai-answers-e2e-host as static files.
Docs.
- New
docs/embedding.md: create a placement, allow the sites, copy the snippet, the CSP entries (script-src,style-src,connect-src, andimg-src/font-srcwhen needed), rate limits, security notes, troubleshooting (CORS, 403, 429, CSP, the 1 day asset cache). In the mkdocs nav, the docs index and the README. docs/placements.md: "The widget script" points at "Copy the snippet" instead of its own older snippet. The work in progress note is now a pointer to the guide.tests/playwright/README.md: setup and run.
CI
The Playwright suite does not run in drupal.org CI, and nothing is added to .gitlab-ci.yml. CI runs 1 site on 1 host, with no second origin. The same code is covered there by the Kernel tests (CORS, tokens, limits) and EmbedWidgetTest (FJS, same origin).
What CI does see: ESLint lints the Playwright files (CommonJS, with an import/no-unresolved exception for @playwright/test, which CI doesn't install), cspell, PHPCS and PHPStan check the test module. package.json is in tests/playwright, not the module root, so no CI job installs it.
Run locally, after the setup in tests/playwright/README.md:
cd tests/playwright
npm install
npm run install-browsers
npm testResults
Local, DDEV, Drupal 11.4, Playwright 1.65.1: 87 passed, 0 failed, in 3 consecutive runs.
| Page | Chromium | Firefox | WebKit |
|---|---|---|---|
| plain | 7/7 | 7/7 | 7/7 |
| strict | 7/7 | 7/7 | 7/7 |
| spa | 7/7 + 1 | 7/7 + 1 | 7/7 + 1 |
| frame | 7/7 | 7/7 | 7/7 |
All 3 browsers took the parseHTMLUnsafe path natively. The fallback was forced in its own test.
To check the suite can fail: with Access-Control-Expose-Headers removed from EmbedCorsMiddleware, the 4 Chromium 429 tests failed. It was reverted before the commit.
Also local: Unit 52, Kernel 181, Functional 11 passed. PHPStan, PHPCS (Drupal, DrupalPractice), cspell, ESLint clean on the new files.
Decisions to look at
- Static host pages served by nginx on a second hostname, not a Drupal route. The page under test has nothing of Drupal's.
- The strict CSP is a
<meta>tag, so the static server needs no per-file headers. A meta CSP covers every directive the widget needs. - Per-browser placements, so parallel browsers don't share rate limit buckets.
- The issue's extra FJS cases (follow-up, feedback, hostile stylesheet) are covered here, cross-origin, not in FJS.
EmbedWidgetTestalready covers the stubbed stream, references, 2 placements and 429 in CI. ai_answers_embed_teststill builds its own snippet. Moving it toEmbedSnippet::build()changesEmbedWidgetTest's selectors, and I can't run FJS locally. Can be a follow-up.
AI disclosure
Used AI to help with research, writing and code (writing the Playwright suite, fixture module and embedding guide, running the tests, drafting this description). The reasoning and positions are my own, and I reviewed everything before posting. (Drupal.org AI policy)