agentpath 0.0.0-stage → 0.1.0-alpha.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.
Files changed (127) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +101 -2
  3. package/THIRD_PARTY_NOTICES +12 -0
  4. package/apps/demo-site/dist/site.js +92 -0
  5. package/apps/studio/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
  6. package/apps/studio/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
  7. package/apps/studio/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
  8. package/apps/studio/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
  9. package/apps/studio/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
  10. package/apps/studio/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
  11. package/apps/studio/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
  12. package/apps/studio/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
  13. package/apps/studio/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
  14. package/apps/studio/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
  15. package/apps/studio/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
  16. package/apps/studio/dist/assets/index-DH4wJPHm.js +67 -0
  17. package/apps/studio/dist/assets/index-DrnNh2ip.css +1 -0
  18. package/apps/studio/dist/index.html +14 -0
  19. package/audit.md +173 -0
  20. package/autonomous-mode.md +104 -0
  21. package/ci.md +118 -0
  22. package/coding-agents.md +143 -0
  23. package/concepts.md +145 -0
  24. package/contracts.md +356 -0
  25. package/examples/tasks/ambiguous.json +98 -0
  26. package/examples/tasks/assertion-failure.json +98 -0
  27. package/examples/tasks/broken.json +98 -0
  28. package/examples/tasks/fixed.json +98 -0
  29. package/examples/tasks/recovery.json +19 -0
  30. package/images/compare-recovery-desktop.jpg +0 -0
  31. package/images/compare-variants-desktop.jpg +0 -0
  32. package/images/overview-desktop.jpg +0 -0
  33. package/images/run-autonomous-passed-desktop.jpg +0 -0
  34. package/images/run-failed-desktop.jpg +0 -0
  35. package/images/run-failed-mobile.jpg +0 -0
  36. package/images/run-missing-artifact-desktop.jpg +0 -0
  37. package/images/task-editor-laptop.jpg +0 -0
  38. package/installation.md +102 -0
  39. package/ledger.md +62 -0
  40. package/licenses/-fontsource-variable-geist-mono.txt +93 -0
  41. package/licenses/-fontsource-variable-geist.txt +93 -0
  42. package/licenses/lucide-react.txt +43 -0
  43. package/licenses/react-dom.txt +21 -0
  44. package/licenses/react.txt +21 -0
  45. package/licenses/scheduler.txt +21 -0
  46. package/licenses/tailwindcss.txt +21 -0
  47. package/licenses/zod.txt +21 -0
  48. package/package.json +165 -4
  49. package/packages/cli/bin/agentpath.js +39 -0
  50. package/packages/cli/dist/audit-rerun.js +384 -0
  51. package/packages/cli/dist/audit.js +387 -0
  52. package/packages/cli/dist/change-summaries.js +87 -0
  53. package/packages/cli/dist/ci-report.js +88 -0
  54. package/packages/cli/dist/demo.js +88 -0
  55. package/packages/cli/dist/doctor.js +265 -0
  56. package/packages/cli/dist/explain.js +89 -0
  57. package/packages/cli/dist/git.js +7 -0
  58. package/packages/cli/dist/inspect.js +118 -0
  59. package/packages/cli/dist/ledger.js +509 -0
  60. package/packages/cli/dist/local.js +102 -0
  61. package/packages/cli/dist/main.js +708 -0
  62. package/packages/cli/dist/mcp.js +231 -0
  63. package/packages/cli/dist/paths.js +6 -0
  64. package/packages/cli/dist/playwright-fixture.js +185 -0
  65. package/packages/cli/dist/playwright-reporter.js +49 -0
  66. package/packages/cli/dist/tasks.js +37 -0
  67. package/packages/cli/dist/test-source.js +283 -0
  68. package/packages/cli/dist/watch.js +96 -0
  69. package/packages/contracts/dist/agent-settings.js +18 -0
  70. package/packages/contracts/dist/agent.js +175 -0
  71. package/packages/contracts/dist/audit.js +115 -0
  72. package/packages/contracts/dist/guidance.js +339 -0
  73. package/packages/contracts/dist/index.js +14 -0
  74. package/packages/contracts/dist/inspect.js +63 -0
  75. package/packages/contracts/dist/ledger.js +80 -0
  76. package/packages/contracts/dist/local.js +171 -0
  77. package/packages/contracts/dist/outcomes.js +94 -0
  78. package/packages/contracts/dist/platform.js +105 -0
  79. package/packages/contracts/dist/playwright-live.js +76 -0
  80. package/packages/contracts/dist/policy.js +93 -0
  81. package/packages/contracts/dist/run.js +178 -0
  82. package/packages/contracts/dist/task-issues.js +39 -0
  83. package/packages/contracts/dist/task.js +185 -0
  84. package/packages/contracts/dist/watch.js +57 -0
  85. package/packages/core/dist/action-feedback.js +103 -0
  86. package/packages/core/dist/actions.js +160 -0
  87. package/packages/core/dist/agent-loop.js +122 -0
  88. package/packages/core/dist/artifacts.js +156 -0
  89. package/packages/core/dist/assertions.js +120 -0
  90. package/packages/core/dist/autonomous.js +1 -0
  91. package/packages/core/dist/browser.js +196 -0
  92. package/packages/core/dist/compare.js +141 -0
  93. package/packages/core/dist/control-state.js +62 -0
  94. package/packages/core/dist/diagnostics.js +134 -0
  95. package/packages/core/dist/evidence-diff.js +93 -0
  96. package/packages/core/dist/evidence.js +203 -0
  97. package/packages/core/dist/index.js +22 -0
  98. package/packages/core/dist/inspect.js +257 -0
  99. package/packages/core/dist/integrity.js +176 -0
  100. package/packages/core/dist/liveness.js +114 -0
  101. package/packages/core/dist/local-files.js +61 -0
  102. package/packages/core/dist/locators.js +214 -0
  103. package/packages/core/dist/model-provider.js +88 -0
  104. package/packages/core/dist/observation.js +257 -0
  105. package/packages/core/dist/playwright-live.js +519 -0
  106. package/packages/core/dist/playwright-results.js +543 -0
  107. package/packages/core/dist/policy.js +182 -0
  108. package/packages/core/dist/privacy.js +93 -0
  109. package/packages/core/dist/recovery.js +54 -0
  110. package/packages/core/dist/retention.js +204 -0
  111. package/packages/core/dist/run-index.js +177 -0
  112. package/packages/core/dist/run-plan.js +28 -0
  113. package/packages/core/dist/run-repository.js +518 -0
  114. package/packages/core/dist/runner.js +913 -0
  115. package/packages/core/dist/storage.js +63 -0
  116. package/packages/core/dist/task-fingerprint.js +36 -0
  117. package/packages/core/dist/test-change-rerun.js +84 -0
  118. package/packages/core/dist/test-changes.js +896 -0
  119. package/packages/core/dist/timing.js +41 -0
  120. package/packages/core/dist/watch.js +165 -0
  121. package/packages/server/dist/index.js +265 -0
  122. package/packages/server/dist/task-store.js +103 -0
  123. package/quickstart.md +85 -0
  124. package/security.md +136 -0
  125. package/try-on-your-repo.md +54 -0
  126. package/understanding-results.md +159 -0
  127. package/writing-tasks.md +196 -0
package/LICENSE.md ADDED
@@ -0,0 +1,105 @@
1
+ # Functional Source License, Version 1.1, ALv2 Future License
2
+
3
+ ## Abbreviation
4
+
5
+ FSL-1.1-ALv2
6
+
7
+ ## Notice
8
+
9
+ Copyright 2026 Dhruv Joshi
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the Apache License, Version 2.0 that is effective on the second anniversary of
91
+ the date we make the Software available. On or after that date, you may use the
92
+ Software under the Apache License, Version 2.0, in which case the following
93
+ will apply:
94
+
95
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
96
+ this file except in compliance with the License.
97
+
98
+ You may obtain a copy of the License at
99
+
100
+ http://www.apache.org/licenses/LICENSE-2.0
101
+
102
+ Unless required by applicable law or agreed to in writing, software distributed
103
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
+ specific language governing permissions and limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,102 @@
1
- # Temporary Holding Version
1
+ # AgentPath: check AI-made changes to Playwright tests
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ AgentPath checks the changes coding agents make to Playwright tests before they merge. `agentpath audit --base origin/main e2e` lists skips, removed assertions and steps, and changed locators and expected values in a pull request; with `--rerun` it runs the old tests on the new build and says which changes the build needed and whether the evidence points to the test or the site. `agentpath ledger` lists the tests a repository has turned off, and `agentpath explain test-results` explains Playwright failures from the files Playwright already writes. These read git and Playwright's output and never edit a test: start with [Try it on your own repository](try-on-your-repo.md) and the [CI recipe](ci.md).
4
+
5
+ AgentPath also runs semantic browser tasks in Chromium and records the actions, independent assertions, and available evidence. Studio opens those recordings and compares runs locally. Deterministic runs require Node and Chromium. Autonomous runs additionally require a configured model endpoint and remain experimental.
6
+
7
+ ## Install 0.1.0-alpha.2 for private evaluation
8
+
9
+ Use Node 22.13 or later on the 22.x line, or Node 24 and later. From a writable directory, use the actual tarball path supplied to you.
10
+
11
+ ```sh
12
+ npm install --global --ignore-scripts ./agentpath-0.1.0-alpha.2.tgz
13
+ agentpath --version
14
+ agentpath --help
15
+ agentpath browser install
16
+ agentpath doctor
17
+ agentpath demo
18
+ ```
19
+
20
+ For a project-local install, run `npm install --ignore-scripts ./agentpath-0.1.0-alpha.2.tgz` in a project with a `package.json`. Use `./node_modules/.bin/agentpath` on macOS or Linux and `.\node_modules\.bin\agentpath.cmd` in Windows PowerShell. A global install requires write access to the npm prefix. Use a local install or a user-owned prefix if needed. Uninstall with `npm uninstall --global agentpath` or `npm uninstall agentpath` respectively.
21
+
22
+ This prerelease is supplied as a tarball. Install only the tarball supplied by your operator. The tarball includes the compiled CLI, local API, Studio and synthetic examples. The only external install prerequisites are Node, npm, access to npm runtime dependencies, and Chromium host libraries. The browser command uses the installed Playwright CLI to download the matching build. Ordinary npm installation and doctor do not download the browser. On Linux, ask your administrator to provision any missing system libraries and ensure the OS permits the Chromium sandbox. Playwright documents the matching `install-deps chromium` command for supported distributions. [Playwright browser setup](https://playwright.dev/docs/browsers#install-system-dependencies)
23
+
24
+ ## Offline guides
25
+
26
+ For a project-local install, open `node_modules/agentpath/README.md` in a Markdown viewer. For a global install, run `npm root --global` and open `agentpath/README.md` under that directory. These guides and their figures ship with the package:
27
+
28
+ - [Try it on your own repository](try-on-your-repo.md): `audit` and `ledger` in a few minutes
29
+ - [Auditing test changes](audit.md) and [the skip ledger](ledger.md)
30
+ - [CI recipe](ci.md)
31
+ - [Coding agents](coding-agents.md): `explain`, the Playwright reporter and fixture, and the MCP server
32
+ - [Quickstart](quickstart.md)
33
+ - [Core concepts](concepts.md)
34
+ - [Understanding results](understanding-results.md)
35
+ - [Writing tasks](writing-tasks.md)
36
+ - [Optional autonomous mode](autonomous-mode.md)
37
+ - [Security and privacy limitations](security.md)
38
+
39
+ ## Follow the demo
40
+
41
+ Open the Studio URL printed by `agentpath demo`. The command writes four real recordings into a new temporary workspace while Studio starts. The Overview's **Guided demo** then links each one:
42
+
43
+ 1. **See a verified pass.** Open the passing cart and read its independent assertions.
44
+ 2. **Find the broken control.** Open the missing-label failure and choose **Jump to failed step**. Step 4 shows the quantity select, its screenshot and its accessibility snapshot.
45
+ 3. **Compare the failure with the pass.** The comparison opens with both runs selected. The cart definitions differ, so it compares them as scenario variants and reports **Observed improvement**.
46
+ 4. **Verify a recovery.** The exact same workshop task, run before and after the synthetic service is repaired, reports **Verified task recovery**, with the first divergence at assertion 1.
47
+
48
+ The [quickstart](quickstart.md) explains each step with screenshots. The terminal also prints direct links to the same runs and comparisons. The site stays on the repaired workshop for later runs.
49
+
50
+ Press Ctrl+C to stop both loopback servers. The workspace path is printed at startup. Copy out any evidence you want to keep before the system cleans its temporary directory. Reopen the recordings later with `agentpath studio --workspace "<demo workspace>"`. The task URLs point to the original demo server, so start a new demo to get another working set.
51
+
52
+ Use `--port 0 --site-port 0` to choose ephemeral ports explicitly. When the default ports are busy, the demo chooses free ones and writes the actual URLs into the task files.
53
+
54
+ ## Use your own workspace
55
+
56
+ ```sh
57
+ agentpath doctor --json
58
+ agentpath init
59
+ agentpath studio --workspace .
60
+ ```
61
+
62
+ In a source checkout `agentpath` is not on your PATH: use `pnpm agentpath <command>` for every command on this page.
63
+
64
+ `init` writes an editable example without overwriting an existing file. The starter points to the synthetic site on port 4173. To run it unchanged, start `agentpath demo --site-port 4173` and use a second terminal for the example below. For a different site, edit the base URL, exact allowed hosts, steps and independent assertions.
65
+
66
+ ```sh
67
+ agentpath run agentpath.task.json --json
68
+ agentpath runs list
69
+ agentpath report <run-id>
70
+ agentpath compare <baseline-id> <candidate-id>
71
+ ```
72
+
73
+ `run` prints each run's ID and, for a failure, the `agentpath report` command that shows it again. `report` and `compare` print a readable summary; add `--json` for the full record. Studio and CLI share a workspace when launched from the same directory. Use `--runs-dir "<demo workspace>/.agentpath/runs"` with run, report and compare to operate on the demo recordings. The demo tasks are under `<demo workspace>/.agentpath/tasks/`. `run` exits 1 for a failed verdict and still saves the evidence. Use `compare --variants` for compatible variants and default comparison for identical task definitions. `agentpath runs reindex` rebuilds the derived SQLite index when needed. The saved run directories are authoritative. Older metadata and missing artifacts may limit a comparison.
74
+
75
+ `doctor` writes a temporary storage probe, launches sandboxed Chromium against a synthetic loopback page, and checks local Studio health. Pass `--workspace <path>` to check a particular workspace. Use `--port 0` if your Studio is already open. JSON output uses `schemaVersion: 1` with `version`, `ready`, and `checks`, where each check has `id`, `status` (PASS, WARNING or FAIL), `message`, and `resolution`. Exit 2 signals a required check failed. An unavailable optional model service produces a warning.
76
+
77
+ CLI exit codes are the same for every command: 0 when a run passed or a command succeeded, 1 when a task failed, a gate failed or integrity found a mismatch, 2 for a usage or runtime error, and 130 or 143 when SIGINT or SIGTERM cancelled the command. `audit` also exits 3 when its gate is not decided because something was not read ([audit](audit.md#what-fails-the-gate)).
78
+
79
+ ## Existing Playwright tests
80
+
81
+ AgentPath can explain failures from a Playwright suite you already have, without task files and without changing a test:
82
+
83
+ ```sh
84
+ npx playwright test --trace retain-on-failure
85
+ agentpath explain test-results
86
+ ```
87
+
88
+ To see the same explanation in every run's own output, install the tarball in the project (`npm install --save-dev --ignore-scripts ./agentpath-0.1.0-alpha.2.tgz`) and add the reporter to `playwright.config`:
89
+
90
+ ```js
91
+ reporter: [["list"], ["agentpath/playwright-reporter"]],
92
+ ```
93
+
94
+ For each failed `getByRole` with a string name or `getByLabel` locator, it says whether the page Playwright recorded points to the test or the site, and names any other locator form it does not support. It reads only the files the run wrote: it changes no verdict and applies no suggestion.
95
+
96
+ ## Optional models and privacy
97
+
98
+ In the Studio task editor, choose **Autonomous model execution** and configure an installed Ollama or OpenAI-compatible model, its endpoint, and the task's independent assertions. Numeric loopback endpoints are allowed by default. External model use requires the exact normalized base URL in `AGENTPATH_MODEL_ENDPOINT`. Put credentials only in `AGENTPATH_MODEL_API_KEY` for that explicitly approved endpoint. Doctor checks the default local Ollama model list and an explicitly configured OpenAI-compatible model list without running inference. Task goals and bounded browser observations go to the configured provider when you run an autonomous task. See [Autonomous mode](autonomous-mode.md).
99
+
100
+ Run only against authorized development and test sites. Studio and the demo bind to IPv4 loopback. Use a trusted local account and workspace. Browser destination checks and the per-run proxy are guardrails. Use network-level isolation where a hard egress boundary is required. Inspect screenshots, accessibility snapshots, task files, and reports before sharing. Opt-in native browser traces can contain unredacted page data and credentials. AgentPath sends no onboarding telemetry.
101
+
102
+ For source development, use the private repository's pinned pnpm version and run `pnpm install --frozen-lockfile`, `pnpm install:chromium` and `pnpm build`. Then run `pnpm agentpath demo`. Only the source path needs pnpm or a build. In a source checkout, use `pnpm agentpath <command>` for any CLI command. `pnpm demo` starts only the fixture site, and `pnpm dev` builds and starts standalone Studio.
@@ -0,0 +1,12 @@
1
+ Components incorporated into the compiled Studio assets
2
+
3
+ react@19.3.1 (MIT), licenses/react.txt
4
+ react-dom@19.3.1 (MIT), licenses/react-dom.txt
5
+ lucide-react@1.74.0 (ISC), licenses/lucide-react.txt
6
+ tailwindcss@4.3.5 (MIT), licenses/tailwindcss.txt
7
+ @fontsource-variable/geist@5.3.0 (OFL-1.1), licenses/-fontsource-variable-geist.txt
8
+ @fontsource-variable/geist-mono@5.3.0 (OFL-1.1), licenses/-fontsource-variable-geist-mono.txt
9
+ scheduler@0.28.0 (MIT), licenses/scheduler.txt
10
+ zod@4.6.7 (MIT), licenses/zod.txt
11
+
12
+ Node runtime dependencies are installed by npm and include their own terms and notices.
@@ -0,0 +1,92 @@
1
+ import { createServer } from "node:http";
2
+ const product = "Trail Mug";
3
+ function layout(title, content) {
4
+ return `<!doctype html>
5
+ <html lang="en">
6
+ <head>
7
+ <meta charset="utf-8">
8
+ <meta name="viewport" content="width=device-width, initial-scale=1">
9
+ <title>${title} | AgentPath Demo Shop</title>
10
+ <link rel="stylesheet" href="/shop.css">
11
+ </head>
12
+ <body>
13
+ <header><strong>AgentPath Demo Shop</strong></header>
14
+ <main><h1>${title}</h1>${content}</main>
15
+ </body>
16
+ </html>`;
17
+ }
18
+ function listing(variant) {
19
+ return layout("Products", `<ul><li><a href="/${variant}/products/trail-mug">${product}</a> <span>$16.00</span></li></ul>`);
20
+ }
21
+ function detail(variant) {
22
+ const label = variant === "broken" ? '<span>Quantity</span>' : '<label for="quantity">Quantity</label>';
23
+ const extraButton = variant === "ambiguous" ? '<button type="submit">Add to cart</button>' : "";
24
+ return layout(product, `
25
+ <p>Insulated stainless steel mug for the trail. <strong>$16.00</strong></p>
26
+ <form aria-label="Purchase Trail Mug">
27
+ <div>${label} <select id="quantity" name="quantity">
28
+ <option value="1">1</option>
29
+ <option value="2">2</option>
30
+ <option value="3">3</option>
31
+ </select></div>
32
+ <button type="submit">Add to cart</button>
33
+ ${extraButton}
34
+ </form>
35
+ <section aria-label="Cart" aria-live="polite"><h2>Cart</h2><p>Cart is empty.</p></section>
36
+ <script src="/cart.js" defer></script>`);
37
+ }
38
+ const cart = `
39
+ const form = document.querySelector("form");
40
+ const message = document.querySelector("section p");
41
+ let total = 0;
42
+ form.addEventListener("submit", (event) => {
43
+ event.preventDefault();
44
+ total += Number(new FormData(form).get("quantity"));
45
+ message.textContent = "Trail Mug × " + total;
46
+ });`;
47
+ const css = `
48
+ :root { font: 1rem/1.6 system-ui, sans-serif; color: #19352e; background: #f4f6f2; }
49
+ body { max-width: 50rem; margin: 0 auto; padding: 2rem 1.25rem; }
50
+ header { border-bottom: 1px solid #b8c8bd; padding-bottom: 1rem; }
51
+ main { margin-top: 2rem; }
52
+ form, section { background: white; padding: 1rem 1.25rem; margin-top: 1.25rem; border: 1px solid #b8c8bd; border-radius: .5rem; }
53
+ button, select { font: inherit; padding: .25rem .5rem; }
54
+ button { display: inline-block; margin-top: .75rem; }
55
+ a { color: #2f5e4d; } a:hover { text-decoration-thickness: .15em; }
56
+ h2 { margin: 0; }
57
+ `;
58
+ export function createDemoServer(options = {}) {
59
+ return createServer((request, response) => {
60
+ const path = (request.url ?? "/").split("?")[0];
61
+ response.setHeader("cache-control", "no-store");
62
+ if (path === "/cart.js" || path === "/shop.css") {
63
+ response.setHeader("content-type", path === "/cart.js" ? "text/javascript; charset=utf-8" : "text/css; charset=utf-8");
64
+ response.end(path === "/cart.js" ? cart : css);
65
+ return;
66
+ }
67
+ response.setHeader("content-type", "text/html; charset=utf-8");
68
+ const match = /^\/(fixed|broken|ambiguous)(\/products\/trail-mug)?\/?$/.exec(path ?? "");
69
+ if (path === "/recovery") {
70
+ const message = options.recoveryReady?.() ? "Reserved successfully." : "Reservation is unavailable.";
71
+ response.end(layout("Reserve a workshop seat", `
72
+ <p>One seat is available in the browser testing workshop.</p>
73
+ <button id="reserve" type="button">Reserve seat</button>
74
+ <section role="status" aria-label="Reservation"><p>No reservation yet.</p></section>
75
+ <script>
76
+ document.getElementById("reserve").onclick = () => {
77
+ document.querySelector("section p").textContent = ${JSON.stringify(message)};
78
+ };
79
+ </script>`));
80
+ }
81
+ else if (path === "/") {
82
+ response.end(listing("fixed"));
83
+ }
84
+ else if (match?.[1]) {
85
+ response.end(match[2] ? detail(match[1]) : listing(match[1]));
86
+ }
87
+ else {
88
+ response.statusCode = 404;
89
+ response.end(layout("Page not found", "<p>Return to the <a href='/'>shop</a>.</p>"));
90
+ }
91
+ });
92
+ }