@rexezuge/tooling 1.2.0 → 1.4.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.
Files changed (153) hide show
  1. package/README.md +66 -10
  2. package/dist/eslint-boundaries.d.ts +31 -7
  3. package/dist/eslint-boundaries.d.ts.map +1 -1
  4. package/dist/eslint-boundaries.js +1 -138
  5. package/dist/eslint-boundaries.js.map +7 -1
  6. package/dist/eslint-rules.js +1 -273
  7. package/dist/eslint-rules.js.map +7 -1
  8. package/dist/eslint.d.ts +12 -7
  9. package/dist/eslint.d.ts.map +1 -1
  10. package/dist/eslint.js +1 -189
  11. package/dist/eslint.js.map +7 -1
  12. package/dist/functions/pages-proxy.d.ts.map +1 -1
  13. package/dist/functions/pages-proxy.js +1 -131
  14. package/dist/functions/pages-proxy.js.map +7 -1
  15. package/dist/index.d.ts +20 -14
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +1 -28
  18. package/dist/index.js.map +7 -1
  19. package/dist/scripts/backup/d1-target.d.ts +15 -5
  20. package/dist/scripts/backup/d1-target.d.ts.map +1 -1
  21. package/dist/scripts/backup/d1-target.js +1 -31
  22. package/dist/scripts/backup/d1-target.js.map +7 -1
  23. package/dist/scripts/backup/destination-config.d.ts +24 -8
  24. package/dist/scripts/backup/destination-config.d.ts.map +1 -1
  25. package/dist/scripts/backup/destination-config.js +1 -57
  26. package/dist/scripts/backup/destination-config.js.map +7 -1
  27. package/dist/scripts/backup/encrypt-backup.js +2 -97
  28. package/dist/scripts/backup/encrypt-backup.js.map +7 -1
  29. package/dist/scripts/backup/naming.d.ts +3 -1
  30. package/dist/scripts/backup/naming.d.ts.map +1 -1
  31. package/dist/scripts/backup/naming.js +1 -58
  32. package/dist/scripts/backup/naming.js.map +7 -1
  33. package/dist/scripts/check-god-files.d.ts +40 -14
  34. package/dist/scripts/check-god-files.d.ts.map +1 -1
  35. package/dist/scripts/check-god-files.js +2 -271
  36. package/dist/scripts/check-god-files.js.map +7 -1
  37. package/dist/scripts/ensure-spa-shell-stub.js +1 -29
  38. package/dist/scripts/ensure-spa-shell-stub.js.map +7 -1
  39. package/dist/scripts/init-secrets.d.ts +0 -16
  40. package/dist/scripts/init-secrets.d.ts.map +1 -1
  41. package/dist/scripts/init-secrets.js +2 -239
  42. package/dist/scripts/init-secrets.js.map +7 -1
  43. package/dist/scripts/lib/cli-args.d.ts.map +1 -1
  44. package/dist/scripts/lib/cli-args.js +1 -116
  45. package/dist/scripts/lib/cli-args.js.map +7 -1
  46. package/dist/scripts/lib/github-actions.js +3 -38
  47. package/dist/scripts/lib/github-actions.js.map +7 -1
  48. package/dist/scripts/lib/wrangler-table.d.ts.map +1 -1
  49. package/dist/scripts/lib/wrangler-table.js +2 -99
  50. package/dist/scripts/lib/wrangler-table.js.map +7 -1
  51. package/dist/scripts/migrations-lock.js +1 -44
  52. package/dist/scripts/migrations-lock.js.map +7 -1
  53. package/dist/scripts/prepare-wrangler-config.js +1 -49
  54. package/dist/scripts/prepare-wrangler-config.js.map +7 -1
  55. package/dist/scripts/spa-shell.d.ts +12 -4
  56. package/dist/scripts/spa-shell.d.ts.map +1 -1
  57. package/dist/scripts/spa-shell.js +3 -160
  58. package/dist/scripts/spa-shell.js.map +7 -1
  59. package/dist/scripts/validate-locales.d.ts +13 -5
  60. package/dist/scripts/validate-locales.d.ts.map +1 -1
  61. package/dist/scripts/validate-locales.js +4 -350
  62. package/dist/scripts/validate-locales.js.map +7 -1
  63. package/dist/scripts/verify-migrations.d.ts +27 -9
  64. package/dist/scripts/verify-migrations.d.ts.map +1 -1
  65. package/dist/scripts/verify-migrations.js +2 -312
  66. package/dist/scripts/verify-migrations.js.map +7 -1
  67. package/dist/scripts/verify-spa-shell.js +1 -52
  68. package/dist/scripts/verify-spa-shell.js.map +7 -1
  69. package/dist/scripts/wrangler-config/cli.js +3 -51
  70. package/dist/scripts/wrangler-config/cli.js.map +7 -1
  71. package/dist/scripts/wrangler-config/patches.d.ts +10 -4
  72. package/dist/scripts/wrangler-config/patches.d.ts.map +1 -1
  73. package/dist/scripts/wrangler-config/patches.js +4 -141
  74. package/dist/scripts/wrangler-config/patches.js.map +7 -1
  75. package/dist/scripts/wrangler-config/resources.d.ts +19 -7
  76. package/dist/scripts/wrangler-config/resources.d.ts.map +1 -1
  77. package/dist/scripts/wrangler-config/resources.js +1 -290
  78. package/dist/scripts/wrangler-config/resources.js.map +7 -1
  79. package/dist/scripts/wrangler-config/types.d.ts +24 -8
  80. package/dist/scripts/wrangler-config/types.d.ts.map +1 -1
  81. package/dist/scripts/wrangler-config/types.js +1 -49
  82. package/dist/scripts/wrangler-config/types.js.map +7 -1
  83. package/dist/test/integration-migrations.d.ts +29 -12
  84. package/dist/test/integration-migrations.d.ts.map +1 -1
  85. package/dist/test/integration-migrations.js +2 -171
  86. package/dist/test/integration-migrations.js.map +7 -1
  87. package/dist/test/mocks/cloudflare-workers.js +1 -90
  88. package/dist/test/mocks/cloudflare-workers.js.map +7 -1
  89. package/dist/vite.d.ts +5 -3
  90. package/dist/vite.d.ts.map +1 -1
  91. package/dist/vite.js +3 -125
  92. package/dist/vite.js.map +7 -1
  93. package/dist/vitest-aliases.d.ts +90 -0
  94. package/dist/vitest-aliases.d.ts.map +1 -0
  95. package/dist/vitest-aliases.js +1 -0
  96. package/dist/vitest-aliases.js.map +7 -0
  97. package/dist/vitest-integration.d.ts +96 -0
  98. package/dist/vitest-integration.d.ts.map +1 -0
  99. package/dist/vitest-integration.js +1 -0
  100. package/dist/vitest-integration.js.map +7 -0
  101. package/dist/vitest-repo.d.ts +154 -0
  102. package/dist/vitest-repo.d.ts.map +1 -0
  103. package/dist/vitest-repo.js +1 -0
  104. package/dist/vitest-repo.js.map +7 -0
  105. package/dist/vitest-web.d.ts +76 -19
  106. package/dist/vitest-web.d.ts.map +1 -1
  107. package/dist/vitest-web.js +1 -72
  108. package/dist/vitest-web.js.map +7 -1
  109. package/dist/vitest.d.ts +70 -19
  110. package/dist/vitest.d.ts.map +1 -1
  111. package/dist/vitest.js +1 -128
  112. package/dist/vitest.js.map +7 -1
  113. package/package.json +9 -3
  114. package/src/eslint-boundaries.ts +112 -11
  115. package/src/eslint.test.ts +40 -9
  116. package/src/eslint.ts +14 -9
  117. package/src/functions/pages-proxy.test.ts +3 -3
  118. package/src/functions/pages-proxy.ts +4 -2
  119. package/src/index.ts +23 -14
  120. package/src/scripts/backup/backup-rules.test.ts +4 -4
  121. package/src/scripts/backup/d1-target.ts +16 -6
  122. package/src/scripts/backup/destination-config.ts +27 -9
  123. package/src/scripts/backup/encrypt-backup.ts +12 -10
  124. package/src/scripts/backup/naming.ts +3 -1
  125. package/src/scripts/check-god-files.test.ts +19 -19
  126. package/src/scripts/check-god-files.ts +62 -33
  127. package/src/scripts/ensure-spa-shell-stub.ts +1 -1
  128. package/src/scripts/init-secrets.ts +20 -10
  129. package/src/scripts/lib/cli-args.ts +6 -3
  130. package/src/scripts/lib/wrangler-table.ts +5 -3
  131. package/src/scripts/migrations-lock.ts +2 -2
  132. package/src/scripts/prepare-wrangler-config.ts +3 -3
  133. package/src/scripts/spa-shell.test.ts +18 -18
  134. package/src/scripts/spa-shell.ts +26 -14
  135. package/src/scripts/validate-locales.test.ts +18 -18
  136. package/src/scripts/validate-locales.ts +43 -25
  137. package/src/scripts/verify-migrations.test.ts +8 -8
  138. package/src/scripts/verify-migrations.ts +47 -24
  139. package/src/scripts/verify-spa-shell.ts +1 -1
  140. package/src/scripts/wrangler-config/patches.ts +13 -5
  141. package/src/scripts/wrangler-config/resources.ts +34 -18
  142. package/src/scripts/wrangler-config/types.ts +24 -8
  143. package/src/test/integration-migrations.test.ts +7 -7
  144. package/src/test/integration-migrations.ts +33 -14
  145. package/src/vite.test.ts +25 -14
  146. package/src/vite.ts +5 -5
  147. package/src/vitest-aliases.test.ts +53 -0
  148. package/src/vitest-aliases.ts +125 -0
  149. package/src/vitest-factories.test.ts +127 -0
  150. package/src/vitest-integration.ts +118 -0
  151. package/src/vitest-repo.ts +269 -0
  152. package/src/vitest-web.ts +110 -28
  153. package/src/vitest.ts +96 -47
@@ -0,0 +1,269 @@
1
+ /**
2
+ * The single-file `projects` composition: backend (`node`) plus SPA (DOM).
3
+ *
4
+ * Provenance: AWS-AccessBridge (split by `.ts`/`.tsx` extension),
5
+ * Mail-Meow (split by `test/web/**` directory, with per-project `setupFiles`
6
+ * and the React plugin on the web project), CalDAV-Bridge (split by directory
7
+ * with `happy-dom`). The two-file repos (Durable-DAV-Router, Mail-Otter) keep
8
+ * `defineUnitVitestConfig()` + `defineWebVitestConfig()`; this factory is the
9
+ * same two halves as `projects` entries in one file, sharing one root coverage
10
+ * gate.
11
+ *
12
+ * Canonical decisions (all three sources agree):
13
+ *
14
+ * 1. **Coverage lives at the root**, not per project: one floor for the whole
15
+ * unit suite. (Durable-DAV-Router's warning about a shared floor stands for
16
+ * the two-file split; within one file the three sources all gate once.)
17
+ * 2. **Each project repeats the alias table.** Vitest projects do not inherit
18
+ * the root `resolve` (Mail-Meow), so a suite resolving `@scope/*`
19
+ * differently from its sibling fails like a product bug. Both entries carry
20
+ * the full table — backend with `@/` → API, web with `@/` + `~/` → SPA.
21
+ * 3. **The web project keeps `pool: 'threads'`** and its DOM from
22
+ * `defineWebVitestConfig()`; the backend project keeps `environment:
23
+ * 'node'` and `server.deps.inline` from `defineUnitVitestConfig()`.
24
+ */
25
+
26
+ import { defineConfig, type Plugin } from 'vitest/config';
27
+ import { fileURLToPath } from 'node:url';
28
+ import { DEFAULT_CLOUDFLARE_MOCKS, DEFAULT_WORKSPACE_PACKAGES, KIT_NO_EXTERNAL, workspaceAliases } from './vitest-aliases.ts';
29
+ import type { VitestAlias } from './vitest-aliases.ts';
30
+ import type { CoverageThresholds } from './vitest.ts';
31
+
32
+ /**
33
+ * The backend half of the `projects` pair.
34
+ */
35
+ export interface RepoBackendOptions {
36
+ /**
37
+ * Which suites to collect. Defaults to the whole unit suite minus SPA and
38
+ * integration shapes.
39
+ */
40
+ readonly include?: readonly string[];
41
+ /**
42
+ * Suite paths to skip, replacing the default.
43
+ */
44
+ readonly exclude?: readonly string[];
45
+ /**
46
+ * Setup files run before each backend test file.
47
+ */
48
+ readonly setupFiles?: readonly string[];
49
+ /**
50
+ * `node_modules` specifiers to inline through Vite. Defaults to `['dofs']`.
51
+ */
52
+ readonly serverDepsInline?: readonly string[];
53
+ /**
54
+ * Extra resolver entries for the backend project only.
55
+ */
56
+ readonly aliases?: readonly VitestAlias[];
57
+ }
58
+
59
+ /**
60
+ * The SPA half of the `projects` pair.
61
+ */
62
+ export interface RepoWebOptions {
63
+ /**
64
+ * The DOM implementation. `jsdom` is the family default; `happy-dom` is
65
+ * CalDAV-Bridge's.
66
+ */
67
+ readonly environment?: 'jsdom' | 'happy-dom';
68
+ /**
69
+ * Which suites to collect. Defaults to every SPA shape the family uses.
70
+ */
71
+ readonly include?: readonly string[];
72
+ /**
73
+ * Suite paths to skip, replacing the default.
74
+ */
75
+ readonly exclude?: readonly string[];
76
+ /**
77
+ * Setup files run before each SPA test file.
78
+ */
79
+ readonly setupFiles?: readonly string[];
80
+ /**
81
+ * Vite plugins the SPA needs (`react()` for JSX).
82
+ */
83
+ readonly plugins?: readonly Plugin[];
84
+ /**
85
+ * Extra resolver entries for the web project only.
86
+ */
87
+ readonly aliases?: readonly VitestAlias[];
88
+ }
89
+
90
+ /**
91
+ * What `defineRepoVitestConfig` accepts.
92
+ */
93
+ export interface RepoVitestConfigOptions {
94
+ /**
95
+ * The consuming repo's npm scope, e.g. `'@myapp'`.
96
+ */
97
+ readonly scope: string;
98
+ /**
99
+ * Workspace package directory names. Defaults to the family superset.
100
+ */
101
+ readonly packages?: readonly string[];
102
+ /**
103
+ * Which `cloudflare:*` mocks to alias. Defaults to workers/sockets/workflows.
104
+ */
105
+ readonly mocks?: readonly string[];
106
+ /**
107
+ * The repo root, as a file URL.
108
+ */
109
+ readonly root?: string;
110
+ /**
111
+ * Extra resolver entries applied to *both* projects (e.g. the `hono`
112
+ * dedupe). Per-project extras go on `backend.aliases` / `web.aliases`.
113
+ */
114
+ readonly aliases?: readonly VitestAlias[];
115
+ /**
116
+ * Resolver `noExternal` patterns, merged after {@link KIT_NO_EXTERNAL}.
117
+ */
118
+ readonly noExternal?: readonly (string | RegExp)[];
119
+ /**
120
+ * The backend half. Omitted fields take the `defineUnitVitestConfig`
121
+ * defaults.
122
+ */
123
+ readonly backend?: RepoBackendOptions;
124
+ /**
125
+ * The SPA half. Omitted fields take the `defineWebVitestConfig` defaults.
126
+ */
127
+ readonly web?: RepoWebOptions;
128
+ /**
129
+ * Coverage `include` globs. Defaults to the worker, the SPA and every
130
+ * workspace package's `src`.
131
+ */
132
+ readonly coverageInclude?: readonly string[];
133
+ /**
134
+ * Coverage `exclude` globs, merged with the canonical set.
135
+ */
136
+ readonly coverageExclude?: readonly string[];
137
+ /**
138
+ * Coverage floors. Omitted by default; a repo that wants them sets them.
139
+ */
140
+ readonly thresholds?: CoverageThresholds;
141
+ /**
142
+ * Where the coverage report is written.
143
+ */
144
+ readonly reportsDirectory?: string;
145
+ }
146
+
147
+ /**
148
+ * Backend suites excluded from the backend project: the SPA shapes live in the
149
+ * web project, and integration is its own config.
150
+ */
151
+ const BACKEND_EXCLUDE: readonly string[] = [
152
+ '**/node_modules/**',
153
+ '**/dist/**',
154
+ 'test/integration/**',
155
+ 'test/web/**',
156
+ 'test/**/web-*.test.{ts,tsx}',
157
+ 'apps/web/**/*.test.{ts,tsx}',
158
+ ];
159
+
160
+ /**
161
+ * SPA suite shapes collected by the web project.
162
+ */
163
+ const WEB_INCLUDE: readonly string[] = ['test/web/**/*.test.{ts,tsx}', 'test/**/web-*.test.{ts,tsx}', 'apps/web/**/*.test.{ts,tsx}'];
164
+
165
+ /**
166
+ * Coverage `exclude`: the union of the unit and web halves' canonical sets.
167
+ */
168
+ const CANONICAL_COVERAGE_EXCLUDE: readonly string[] = [
169
+ 'scripts/**',
170
+ '**/*.test.ts',
171
+ '**/*.test.tsx',
172
+ '**/*.d.ts',
173
+ '**/index.ts',
174
+ '**/types.d.ts',
175
+ '**/types.ts',
176
+ '**/model/**',
177
+ '**/generated/**',
178
+ '**/D1Types.ts',
179
+ '**/ServiceEnv.ts',
180
+ '**/env.d.ts',
181
+ ];
182
+
183
+ /**
184
+ * Build the single-file `projects` config.
185
+ *
186
+ * ```ts
187
+ * // vitest.config.mts
188
+ * import { defineRepoVitestConfig } from '@rexezuge/tooling/vitest-repo';
189
+ * export default defineRepoVitestConfig({
190
+ * scope: '@myapp',
191
+ * web: { environment: 'happy-dom' },
192
+ * thresholds: { statements: 87, branches: 77, functions: 88, lines: 90 },
193
+ * });
194
+ * ```
195
+ */
196
+ export function defineRepoVitestConfig(options: RepoVitestConfigOptions): ReturnType<typeof defineConfig> {
197
+ const root = options.root ?? new URL('.', import.meta.url).href;
198
+ const packages = options.packages ?? DEFAULT_WORKSPACE_PACKAGES;
199
+ const mocks = options.mocks ?? DEFAULT_CLOUDFLARE_MOCKS;
200
+ const noExternal = [...KIT_NO_EXTERNAL, ...(options.noExternal ?? [])];
201
+ const backend = options.backend ?? {};
202
+ const web = options.web ?? {};
203
+
204
+ const backendAliases = [
205
+ ...workspaceAliases({ scope: options.scope, root, packages, mocks }),
206
+ ...(options.aliases ?? []),
207
+ ...(backend.aliases ?? []),
208
+ ];
209
+ // `@/` means the SPA in the web project (Mail-Otter's SPA source imports
210
+ // through it); the workspace table's `@/` → API entry is dropped and the
211
+ // SPA's own prepended. `~/` → SPA either way.
212
+ const webSrc = fileURLToPath(new URL('apps/web/src', root));
213
+ const webAliases: VitestAlias[] = [
214
+ { find: /^@\//, replacement: `${webSrc}/` },
215
+ { find: /^~\//, replacement: `${webSrc}/` },
216
+ // Drop the workspace table's `@/`/`~/` (RegExp) entries — the SPA's own
217
+ // prepended above win — and keep the `@scope/*` + `cloudflare:*` strings.
218
+ ...workspaceAliases({ scope: options.scope, root, packages, mocks }).filter((alias) => typeof alias.find === 'string'),
219
+ ...(options.aliases ?? []),
220
+ ...(web.aliases ?? []),
221
+ ];
222
+
223
+ return defineConfig({
224
+ test: {
225
+ projects: [
226
+ {
227
+ resolve: { alias: backendAliases, noExternal },
228
+ test: {
229
+ name: 'backend',
230
+ globals: true,
231
+ environment: 'node',
232
+ include: [...(backend.include ?? ['test/**/*.test.{ts,tsx}'])],
233
+ exclude: [...(backend.exclude ?? BACKEND_EXCLUDE)],
234
+ ...(backend.setupFiles !== undefined && { setupFiles: [...backend.setupFiles] }),
235
+ server: { deps: { inline: [...(backend.serverDepsInline ?? ['dofs'])] } },
236
+ },
237
+ },
238
+ {
239
+ ...(web.plugins !== undefined && { plugins: [...web.plugins] }),
240
+ resolve: { alias: webAliases, noExternal },
241
+ test: {
242
+ name: 'web',
243
+ globals: true,
244
+ environment: web.environment ?? 'jsdom',
245
+ include: [...(web.include ?? WEB_INCLUDE)],
246
+ exclude: [...(web.exclude ?? ['**/node_modules/**', '**/dist/**', 'test/integration/**'])],
247
+ pool: 'threads',
248
+ ...(web.setupFiles !== undefined && { setupFiles: [...web.setupFiles] }),
249
+ },
250
+ },
251
+ ],
252
+ coverage: {
253
+ provider: 'v8',
254
+ reporter: ['text', 'lcov', 'html'],
255
+ reportsDirectory: options.reportsDirectory ?? './coverage',
256
+ include: [
257
+ ...(options.coverageInclude ?? [
258
+ 'apps/api/src/**/*.ts',
259
+ 'apps/background/src/**/*.ts',
260
+ 'apps/web/src/**/*.{ts,tsx}',
261
+ 'packages/**/src/**/*.ts',
262
+ ]),
263
+ ],
264
+ exclude: [...CANONICAL_COVERAGE_EXCLUDE, ...(options.coverageExclude ?? [])],
265
+ ...(options.thresholds !== undefined && { thresholds: { ...options.thresholds } }),
266
+ },
267
+ },
268
+ });
269
+ }
package/src/vitest-web.ts CHANGED
@@ -1,39 +1,68 @@
1
1
  /**
2
2
  * The SPA's test and coverage gate.
3
3
  *
4
- * Provenance: converged from Durable-DAV-Router's and Mail-Otter's
5
- * `vitest.web.config.mts`, which are the two repos that gave the frontend its own
6
- * suite. Durable-DAV-Router's is the canonical shape — it is the one that records
7
- * *why* the split exists; Mail-Otter's contributes the React plugin and the
8
- * `apps/web/**` include.
4
+ * Provenance: converged from all nine repos' SPA suites — Durable-DAV-Router's
5
+ * and Mail-Otter's `vitest.web.config.mts` (the two repos that gave the frontend
6
+ * its own file), Mail-Meow's and CalDAV-Bridge's `projects` entries (the two
7
+ * that keep backend and SPA in one file), and AWS-AccessBridge's
8
+ * extension-split `dom` project. Durable-DAV-Router's file is the canonical
9
+ * shape — it is the one that records *why* the split exists; Mail-Otter's
10
+ * contributes the React plugin and the `apps/web/**` include; CalDAV-Bridge
11
+ * contributes `happy-dom` and the `test/web/**` directory convention;
12
+ * Mail-Meow contributes `setupFiles` and the note that projects do not inherit
13
+ * the root `resolve`.
9
14
  *
10
15
  * Canonical decisions:
11
16
  *
12
- * 1. **It is a separate config, not a `projects` entry in one.** With a single
13
- * config the SPA suites ran under `node` and the worker suites under `jsdom`
14
- * in the same pass, so a test could pass in one environment and fail in the
15
- * other with neither run noticing.
17
+ * 1. **A DOM for every file here**, rather than a `@vitest-environment` pragma
18
+ * per file: these are the tests that need a DOM, and the pragma is one more
19
+ * place to forget. `jsdom` is the family default (seven repos);
20
+ * `happy-dom` is CalDAV-Bridge's, and is one option away.
16
21
  * 2. **`pool: 'threads'`, bounded, rather than one forked worker per file.** Each
17
22
  * jsdom document is a whole browser environment; a fork per file exhausts
18
23
  * memory on a constrained machine, and when it does the run dies partway with
19
24
  * a bare "Worker exited unexpectedly" and no test reports a failure — which
20
25
  * reads as a flaky suite rather than as an out-of-memory kill.
21
- * 3. **jsdom is set here for every file**, rather than a `@vitest-environment`
22
- * pragma per file: these are the tests that need a DOM, and the pragma is one
23
- * more place to forget.
24
- * 4. **`include` covers both shapes the repos use** — the root-suite `web-*`
25
- * naming convention and suites bundled under `apps/web` — and the same globs
26
- * are what `defineUnitVitestConfig()` excludes.
26
+ * 3. **`include` covers every shape the family uses** — the `test/web/**`
27
+ * directory convention, the root-suite `web-*` naming convention, and suites
28
+ * bundled under `apps/web` — and the same globs are what
29
+ * `defineUnitVitestConfig()` excludes, so a file is collected by exactly one
30
+ * config.
31
+ * 4. **Projects do not inherit the root `resolve`** (Mail-Meow): a
32
+ * single-file `projects` composition must repeat the alias table per
33
+ * project. `defineRepoVitestConfig()` does that; two-file repos get it from
34
+ * these factories directly.
35
+ * 5. **`@/` means the SPA here, not the API worker.** The unit config maps
36
+ * `@/` to `apps/api/src`; this config maps both `@/` (Mail-Otter's SPA
37
+ * source) and `~/` (Mail-Meow's and CalDAV-Bridge's SPA tests) to
38
+ * `apps/web/src`. Workspace `@scope/*` imports still resolve to the
39
+ * packages, so web tests reach shared logic the same way backend tests do.
27
40
  */
28
41
 
29
42
  import { defineConfig, type Plugin } from 'vitest/config';
30
43
  import { fileURLToPath } from 'node:url';
31
- import type { CoverageThresholds, VitestAlias } from './vitest';
44
+ import { DEFAULT_CLOUDFLARE_MOCKS, DEFAULT_WORKSPACE_PACKAGES, KIT_NO_EXTERNAL, workspaceAliases } from './vitest-aliases.ts';
45
+ import type { VitestAlias } from './vitest-aliases.ts';
46
+ import type { CoverageThresholds } from './vitest.ts';
32
47
 
33
48
  /**
34
49
  * What `defineWebVitestConfig` accepts.
35
50
  */
36
51
  export interface WebVitestConfigOptions {
52
+ /**
53
+ * The consuming repo's npm scope, e.g. `'@myapp'`. When supplied, `@scope/*`
54
+ * workspace aliases are generated so web tests reach shared logic; when
55
+ * omitted, only the `cloudflare:*`, `@/` and `~/` aliases apply.
56
+ */
57
+ readonly scope?: string;
58
+ /**
59
+ * Workspace package directory names. Defaults to the family superset.
60
+ */
61
+ readonly packages?: readonly string[];
62
+ /**
63
+ * Which `cloudflare:*` mocks to alias. Defaults to workers/sockets/workflows.
64
+ */
65
+ readonly mocks?: readonly string[];
37
66
  /**
38
67
  * Vite plugins the SPA needs — `@vitejs/plugin-react` for a React app. Passed
39
68
  * by the consumer rather than imported here, because the kit must stay
@@ -41,7 +70,7 @@ export interface WebVitestConfigOptions {
41
70
  */
42
71
  readonly plugins?: readonly Plugin[];
43
72
  /**
44
- * Extra resolver entries.
73
+ * Extra resolver entries, applied after the canonical ones.
45
74
  */
46
75
  readonly aliases?: readonly VitestAlias[];
47
76
  /**
@@ -49,9 +78,38 @@ export interface WebVitestConfigOptions {
49
78
  */
50
79
  readonly root?: string;
51
80
  /**
52
- * Which suites to collect. Defaults to both shapes the source repos use.
81
+ * The DOM implementation. `jsdom` is the family default; `happy-dom` is
82
+ * CalDAV-Bridge's (lighter, and sufficient for its hook/service suites).
83
+ */
84
+ readonly environment?: 'jsdom' | 'happy-dom';
85
+ /**
86
+ * Which suites to collect. Defaults to every shape the family uses.
53
87
  */
54
88
  readonly include?: readonly string[];
89
+ /**
90
+ * Suite paths to skip, replacing the default.
91
+ */
92
+ readonly exclude?: readonly string[];
93
+ /**
94
+ * Setup files run before each test file.
95
+ */
96
+ readonly setupFiles?: readonly string[];
97
+ /**
98
+ * `node_modules` specifiers to inline through Vite rather than externalize.
99
+ */
100
+ readonly serverDepsInline?: readonly string[];
101
+ /**
102
+ * Resolver `noExternal` patterns, merged after {@link KIT_NO_EXTERNAL}.
103
+ */
104
+ readonly noExternal?: readonly (string | RegExp)[];
105
+ /**
106
+ * Coverage `include` globs. Defaults to the SPA source.
107
+ */
108
+ readonly coverageInclude?: readonly string[];
109
+ /**
110
+ * Coverage `exclude` globs, merged with the canonical set.
111
+ */
112
+ readonly coverageExclude?: readonly string[];
55
113
  /**
56
114
  * Coverage floors. Omitted by default; see `defineUnitVitestConfig`.
57
115
  */
@@ -64,10 +122,16 @@ export interface WebVitestConfigOptions {
64
122
  }
65
123
 
66
124
  /**
67
- * The default suite globs: the root-suite `web-*` convention and the
68
- * bundled-under-`apps/web` convention, so either layout works unchanged.
125
+ * The default suite globs: every shape the family uses — the `test/web/**`
126
+ * directory convention, the root-suite `web-*` convention, and the
127
+ * bundled-under-`apps/web` convention — so any layout works unchanged.
128
+ */
129
+ const DEFAULT_INCLUDE: readonly string[] = ['test/web/**/*.test.{ts,tsx}', 'test/**/web-*.test.{ts,tsx}', 'apps/web/**/*.test.{ts,tsx}'];
130
+
131
+ /**
132
+ * Coverage `exclude` globs for the SPA half.
69
133
  */
70
- const DEFAULT_INCLUDE: readonly string[] = ['test/**/web-*.test.{ts,tsx}', 'apps/web/**/*.test.{ts,tsx}'];
134
+ const CANONICAL_COVERAGE_EXCLUDE: readonly string[] = ['**/*.test.{ts,tsx}', '**/*.d.ts', '**/types.ts', '**/generated/**'];
71
135
 
72
136
  /**
73
137
  * Build the SPA's test config.
@@ -76,32 +140,50 @@ const DEFAULT_INCLUDE: readonly string[] = ['test/**/web-*.test.{ts,tsx}', 'apps
76
140
  * // vitest.web.config.mts
77
141
  * import react from '@vitejs/plugin-react';
78
142
  * import { defineWebVitestConfig } from '@rexezuge/tooling/vitest-web';
79
- * export default defineWebVitestConfig({ plugins: [react()] });
143
+ * export default defineWebVitestConfig({ scope: '@myapp', plugins: [react()] });
80
144
  * ```
81
145
  */
82
146
  export function defineWebVitestConfig(options: WebVitestConfigOptions = {}): ReturnType<typeof defineConfig> {
83
147
  const root = options.root ?? new URL('.', import.meta.url).href;
84
148
  const webSrc = fileURLToPath(new URL('apps/web/src', root));
85
- const aliases: VitestAlias[] = [{ find: /^@\//, replacement: `${webSrc}/` }, ...(options.aliases ?? [])];
149
+ // The workspace table maps `@/` to the API worker; here `@/` means the SPA,
150
+ // so drop its `@/`/`~/` entries and prepend the SPA's own. String finds
151
+ // (`@scope/*`, `cloudflare:*`) survive; a placeholder scope contributes none.
152
+ const scopePart = workspaceAliases({
153
+ scope: options.scope ?? '@unused',
154
+ root,
155
+ packages: options.packages ?? DEFAULT_WORKSPACE_PACKAGES,
156
+ mocks: options.mocks ?? DEFAULT_CLOUDFLARE_MOCKS,
157
+ }).filter((alias) => typeof alias.find === 'string' && !alias.find.startsWith('@unused'));
158
+ const aliases: VitestAlias[] = [
159
+ { find: /^@\//, replacement: `${webSrc}/` },
160
+ { find: /^~\//, replacement: `${webSrc}/` },
161
+ ...scopePart.filter((alias) => typeof alias.find === 'string' && (alias.find.startsWith('cloudflare:') || options.scope !== undefined)),
162
+ ...(options.aliases ?? []),
163
+ ];
86
164
 
87
165
  return defineConfig({
88
166
  ...(options.plugins !== undefined && { plugins: [...options.plugins] }),
89
- resolve: { alias: aliases },
167
+ resolve: { alias: aliases, noExternal: [...KIT_NO_EXTERNAL, ...(options.noExternal ?? [])] },
90
168
  test: {
91
169
  globals: true,
92
- environment: 'jsdom',
170
+ environment: options.environment ?? 'jsdom',
93
171
  include: [...(options.include ?? DEFAULT_INCLUDE)],
94
172
  // Threads, bounded, rather than one forked worker per file — see the
95
173
  // header. A custom `exclude` replaces Vitest's defaults, so `node_modules`
96
174
  // must be re-listed.
97
175
  pool: 'threads',
98
- exclude: ['**/node_modules/**', '**/dist/**', 'test/integration/**'],
176
+ exclude: [...(options.exclude ?? ['**/node_modules/**', '**/dist/**', 'test/integration/**'])],
177
+ ...(options.setupFiles !== undefined && { setupFiles: [...options.setupFiles] }),
178
+ ...(options.serverDepsInline !== undefined && {
179
+ server: { deps: { inline: [...options.serverDepsInline] } },
180
+ }),
99
181
  coverage: {
100
182
  provider: 'v8',
101
183
  reporter: ['text', 'lcov'],
102
184
  reportsDirectory: options.reportsDirectory ?? './coverage-spa',
103
- include: ['apps/web/src/**/*.{ts,tsx}'],
104
- exclude: ['**/*.test.{ts,tsx}', '**/*.d.ts', '**/types.ts', '**/generated/**'],
185
+ include: [...(options.coverageInclude ?? ['apps/web/src/**/*.{ts,tsx}'])],
186
+ exclude: [...CANONICAL_COVERAGE_EXCLUDE, ...(options.coverageExclude ?? [])],
105
187
  ...(options.thresholds !== undefined && { thresholds: { ...options.thresholds } }),
106
188
  },
107
189
  },