@webmcp-agents/testing 0.0.0-stage → 0.5.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/dist/vitest.js ADDED
@@ -0,0 +1,164 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { join } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ // src/vitest.ts
6
+ /**
7
+ * `@webmcp-agents/testing/vitest`: the two Vitest projects a WebMCP app needs, so a config is two lines.
8
+ *
9
+ * export default defineConfig({ test: { projects: [webAgentsProject("unit"), webAgentsProject("browser")] } });
10
+ *
11
+ * Node only (it runs in your Vitest config). It imports nothing of the kit by relative path, so the repo's own
12
+ * configs can load it from source (`../testing/src/vitest.ts`).
13
+ */
14
+ const SPECIFIERS = {
15
+ "@webmcp-agents/testing/setup": "setup",
16
+ "@webmcp-agents/testing/matchers": "matchers"
17
+ };
18
+ /**
19
+ * The kit's own setup files by absolute path, next to this module: `./setup.js` in the package's `dist`, `./setup.ts`
20
+ * in its source. Vite then loads them like any test file (the app's resolve conditions apply).
21
+ */
22
+ const ownFile = (specifier) => {
23
+ const name = SPECIFIERS[specifier];
24
+ if (!name) return specifier;
25
+ const ext = import.meta.url.endsWith(".ts") ? ".ts" : ".js";
26
+ return fileURLToPath(new URL(`./${name}${ext}`, import.meta.url));
27
+ };
28
+ const require = createRequire(import.meta.url);
29
+ /** `playwright()` from `@vitest/browser-playwright`, or a sentence naming the package. */
30
+ function playwrightProvider() {
31
+ let module;
32
+ try {
33
+ module = require("@vitest/browser-playwright");
34
+ } catch (err) {
35
+ throw new Error("webAgentsProject(\"browser\") needs @vitest/browser-playwright, which is not installed: add it to your devDependencies (pnpm add -D @vitest/browser-playwright).", { cause: err });
36
+ }
37
+ return module.playwright();
38
+ }
39
+ /** Resolvable from the project being tested. */
40
+ const installed = (specifier) => {
41
+ try {
42
+ createRequire(join(process.cwd(), "vitest.config.js")).resolve(specifier);
43
+ return true;
44
+ } catch {
45
+ return false;
46
+ }
47
+ };
48
+ /**
49
+ * A dependency the project being tested lists itself. Asked of its package.json, not of the resolver: under Vitest a
50
+ * package the project does not have can still resolve (through the kit), and Vite then fails to pre-bundle it by name.
51
+ */
52
+ const declared = (name) => {
53
+ try {
54
+ const pkg = JSON.parse(readFileSync(join(process.cwd(), "package.json"), "utf8"));
55
+ return [
56
+ "dependencies",
57
+ "devDependencies",
58
+ "peerDependencies",
59
+ "optionalDependencies"
60
+ ].some((field) => pkg[field]?.[name] !== void 0);
61
+ } catch {
62
+ return false;
63
+ }
64
+ };
65
+ /** Resolvable from the kit itself (its own dependencies and peers). */
66
+ const kitHas = (specifier) => {
67
+ try {
68
+ require.resolve(specifier);
69
+ return true;
70
+ } catch {
71
+ return false;
72
+ }
73
+ };
74
+ const REACT = [
75
+ "react",
76
+ "react/jsx-runtime",
77
+ "react/jsx-dev-runtime",
78
+ "react-dom",
79
+ "react-dom/client"
80
+ ];
81
+ /**
82
+ * Pre-bundled up front: a dependency discovered mid-run reloads Vite and the test with it (on a cold cache, so in CI
83
+ * first). React (one copy), and `ai/test`, which `mockChatServer` loads on its first request.
84
+ *
85
+ * The kit's own files name `react-dom` and `ai/test` in `import()`s, which Vite resolves as soon as it serves the
86
+ * file, used or not. A project without React of its own (a no-framework page) still reaches the kit's copy that way:
87
+ * it is then listed through the kit (`"@webmcp-agents/testing > react-dom"`), the form Vite takes for a dependency of
88
+ * a package it does not pre-bundle.
89
+ */
90
+ function knownDeps() {
91
+ const viaKit = installed("@webmcp-agents/testing/package.json") ? (dep) => `@webmcp-agents/testing > ${dep}` : void 0;
92
+ const list = (deps, probe) => {
93
+ if (declared(probe)) return deps;
94
+ return viaKit && kitHas(probe) ? deps.map(viaKit) : [];
95
+ };
96
+ return [...list(REACT, "react"), ...list(["ai/test"], "ai")];
97
+ }
98
+ /**
99
+ * One Vitest project with the kit's defaults. `extends: true`, so the root config's plugins, `resolve` and `define`
100
+ * apply to it too.
101
+ *
102
+ * - `"unit"`: Node; `**\/*.test.{ts,tsx}` minus browser tests; setup `@webmcp-agents/testing/matchers`.
103
+ * - `"browser"`: Chromium through Playwright, headless, viewport 414 × 800; `**\/*.browser.test.{ts,tsx}`; setup
104
+ * `@webmcp-agents/testing/setup` (polyfill, reduced motion, deterministic ids, cleanup and leak checks); React
105
+ * pre-bundled once.
106
+ *
107
+ * @example
108
+ * export default defineConfig({ test: { projects: [webAgentsProject("unit"), webAgentsProject("browser")] } });
109
+ * @example
110
+ * // a page without WebMCP: only the matchers
111
+ * webAgentsProject("browser", { name: "no-webmcp", include: ["src/no-webmcp.browser.test.ts"], setupFiles: ["@webmcp-agents/testing/matchers"] })
112
+ */
113
+ function webAgentsProject(kind, options = {}) {
114
+ if (kind !== "unit" && kind !== "browser") throw new TypeError(`webAgentsProject takes "unit" or "browser", got ${JSON.stringify(kind)}.`);
115
+ const { name = kind, define, optimizeDeps, test: extra = {} } = options;
116
+ if (kind === "unit") return {
117
+ extends: true,
118
+ ...define ? { define } : {},
119
+ test: {
120
+ name,
121
+ environment: "node",
122
+ include: options.include ?? ["**/*.test.{ts,tsx}"],
123
+ exclude: [
124
+ "**/node_modules/**",
125
+ "**/dist/**",
126
+ "**/*.browser.test.*",
127
+ ...options.exclude ?? []
128
+ ],
129
+ setupFiles: (options.setupFiles ?? ["@webmcp-agents/testing/matchers"]).map(ownFile),
130
+ ...extra
131
+ }
132
+ };
133
+ return {
134
+ extends: true,
135
+ ...define ? { define } : {},
136
+ optimizeDeps: {
137
+ include: [...knownDeps(), ...optimizeDeps?.include ?? []],
138
+ exclude: ["@webmcp-agents/testing"]
139
+ },
140
+ test: {
141
+ name,
142
+ include: options.include ?? ["**/*.browser.test.{ts,tsx}"],
143
+ exclude: [
144
+ "**/node_modules/**",
145
+ "**/dist/**",
146
+ ...options.exclude ?? []
147
+ ],
148
+ setupFiles: (options.setupFiles ?? ["@webmcp-agents/testing/setup"]).map(ownFile),
149
+ provide: { webAgentsMotion: options.motion ?? "reduced" },
150
+ browser: {
151
+ enabled: true,
152
+ headless: true,
153
+ provider: playwrightProvider(),
154
+ instances: [{ browser: "chromium" }],
155
+ viewport: options.viewport ?? {
156
+ width: 414,
157
+ height: 800
158
+ }
159
+ },
160
+ ...extra
161
+ }
162
+ };
163
+ }
164
+ export { webAgentsProject };
@@ -0,0 +1,29 @@
1
+ // src/helpers/wait.ts
2
+ const tick = () => typeof requestAnimationFrame === "function" ? Promise.race([new Promise((r) => requestAnimationFrame(r)), new Promise((r) => setTimeout(r, 50))]) : new Promise((r) => setTimeout(r, 16));
3
+ /**
4
+ * Resolve once `check` returns true (it may be async), polling every frame (every 16 ms in Node). Resolves to the ms
5
+ * it took. Rejects after `timeout` with `Timed out after <timeout> ms waiting for <label>`. The second argument is the
6
+ * timeout (ms), the label, or both as options.
7
+ *
8
+ * @example
9
+ * await until(() => cursorVisible(), 2000, "the cursor");
10
+ * await until(async () => (await toolNames()).includes("add_to_cart"), { label: "add_to_cart to register" });
11
+ * await until(() => !!document.modelContext, "the polyfill");
12
+ */
13
+ async function until(check, timeoutOrOptions = {}, label) {
14
+ const options = typeof timeoutOrOptions === "number" ? {
15
+ timeout: timeoutOrOptions,
16
+ label
17
+ } : typeof timeoutOrOptions === "string" ? { label: timeoutOrOptions } : timeoutOrOptions;
18
+ const { timeout = 3e3 } = options;
19
+ const what = options.label ?? label ?? "condition";
20
+ const started = performance.now();
21
+ while (!await check()) {
22
+ if (performance.now() - started > timeout) throw new Error(`Timed out after ${timeout} ms waiting for ${what}`);
23
+ await tick();
24
+ }
25
+ return performance.now() - started;
26
+ }
27
+ /** `setTimeout` as a promise. Prefer {@link until} when there is something to wait for. */
28
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
29
+ export { until as n, sleep as t };
package/package.json CHANGED
@@ -1,6 +1,136 @@
1
1
  {
2
2
  "name": "@webmcp-agents/testing",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.5.0",
4
+ "type": "module",
5
+ "description": "Test WebMCP tools, confirmations, agents, the chat endpoint and privacy with Vitest: fixtures, worded matchers and model-free mocks.",
6
+ "license": "MIT",
7
+ "sideEffects": [
8
+ "./src/index.ts",
9
+ "./src/matchers.ts",
10
+ "./src/setup.ts",
11
+ "./dist/index.js",
12
+ "./dist/matchers.js",
13
+ "./dist/matchers-*.js",
14
+ "./dist/setup.js"
15
+ ],
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "exports": {
21
+ ".": {
22
+ "@webmcp-agents/source": "./src/index.ts",
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.js"
25
+ },
26
+ "./matchers": {
27
+ "@webmcp-agents/source": "./src/matchers.ts",
28
+ "types": "./dist/matchers.d.ts",
29
+ "import": "./dist/matchers.js"
30
+ },
31
+ "./setup": {
32
+ "@webmcp-agents/source": "./src/setup.ts",
33
+ "types": "./dist/setup.d.ts",
34
+ "import": "./dist/setup.js"
35
+ },
36
+ "./vitest": {
37
+ "@webmcp-agents/source": "./src/vitest.ts",
38
+ "types": "./dist/vitest.d.ts",
39
+ "import": "./dist/vitest.js"
40
+ },
41
+ "./chat": {
42
+ "@webmcp-agents/source": "./src/chat/index.ts",
43
+ "types": "./dist/chat.d.ts",
44
+ "import": "./dist/chat.js"
45
+ },
46
+ "./server": {
47
+ "@webmcp-agents/source": "./src/server/index.ts",
48
+ "types": "./dist/server.d.ts",
49
+ "import": "./dist/server.js"
50
+ },
51
+ "./telemetry": {
52
+ "@webmcp-agents/source": "./src/telemetry/index.ts",
53
+ "types": "./dist/telemetry.d.ts",
54
+ "import": "./dist/telemetry.js"
55
+ },
56
+ "./playwright": {
57
+ "@webmcp-agents/source": "./src/playwright/index.ts",
58
+ "types": "./dist/playwright.d.ts",
59
+ "import": "./dist/playwright.js"
60
+ },
61
+ "./package.json": "./package.json"
62
+ },
63
+ "peerDependencies": {
64
+ "@playwright/test": ">=1.50",
65
+ "@vitest/browser-playwright": ">=4",
66
+ "ai": ">=7",
67
+ "react": ">=18",
68
+ "react-dom": ">=18",
69
+ "vitest": ">=4",
70
+ "vitest-browser-react": ">=1",
71
+ "@webmcp-agents/chat": "^0.5.0",
72
+ "@webmcp-agents/telemetry": "^0.5.0"
73
+ },
74
+ "peerDependenciesMeta": {
75
+ "@playwright/test": {
76
+ "optional": true
77
+ },
78
+ "@vitest/browser-playwright": {
79
+ "optional": true
80
+ },
81
+ "@webmcp-agents/chat": {
82
+ "optional": true
83
+ },
84
+ "@webmcp-agents/telemetry": {
85
+ "optional": true
86
+ },
87
+ "ai": {
88
+ "optional": true
89
+ },
90
+ "react": {
91
+ "optional": true
92
+ },
93
+ "react-dom": {
94
+ "optional": true
95
+ },
96
+ "vitest-browser-react": {
97
+ "optional": true
98
+ }
99
+ },
100
+ "dependencies": {
101
+ "@webmcp-agents/core": "^0.5.0"
102
+ },
103
+ "devDependencies": {
104
+ "@ai-sdk/provider": "^4.0.36",
105
+ "@playwright/test": "^1.65.0",
106
+ "@types/react": "^19.3.0",
107
+ "@types/react-dom": "^19.3.0",
108
+ "@vitejs/plugin-react": "^6.2.1",
109
+ "@vitest/browser-playwright": "^5.1.0",
110
+ "ai": "^7.0.163",
111
+ "react": "^19.3.1",
112
+ "react-dom": "^19.3.1",
113
+ "tsdown": "^0.23.7",
114
+ "vite": "^8.3.6",
115
+ "vitest": "^5.1.0",
116
+ "zod": "^4.6.7",
117
+ "@webmcp-agents/chat": "^0.5.0",
118
+ "@webmcp-agents/telemetry": "^0.5.0"
119
+ },
120
+ "repository": {
121
+ "type": "git",
122
+ "url": "git+https://github.com/webmcp-agents/webmcp-agents.git",
123
+ "directory": "packages/testing"
124
+ },
125
+ "homepage": "https://webmcp-agents.dev",
126
+ "publishConfig": {
127
+ "access": "public"
128
+ },
129
+ "scripts": {
130
+ "build": "tsdown",
131
+ "dev": "tsdown --watch",
132
+ "typecheck": "tsc -p tsconfig.json",
133
+ "test": "vitest run",
134
+ "check-exports": "publint && attw --pack . --profile esm-only"
135
+ }
6
136
  }