@rexezuge/tooling 1.1.0 → 1.3.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 (89) hide show
  1. package/README.md +66 -10
  2. package/dist/eslint-boundaries.d.ts +19 -3
  3. package/dist/eslint-boundaries.d.ts.map +1 -1
  4. package/dist/eslint-boundaries.js +89 -3
  5. package/dist/eslint-boundaries.js.map +1 -1
  6. package/dist/eslint.d.ts +12 -7
  7. package/dist/eslint.d.ts.map +1 -1
  8. package/dist/eslint.js +13 -8
  9. package/dist/eslint.js.map +1 -1
  10. package/dist/index.d.ts +20 -14
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +10 -7
  13. package/dist/index.js.map +1 -1
  14. package/dist/scripts/backup/d1-target.js +1 -1
  15. package/dist/scripts/backup/d1-target.js.map +1 -1
  16. package/dist/scripts/backup/encrypt-backup.js +2 -2
  17. package/dist/scripts/backup/encrypt-backup.js.map +1 -1
  18. package/dist/scripts/check-god-files.js +2 -2
  19. package/dist/scripts/check-god-files.js.map +1 -1
  20. package/dist/scripts/ensure-spa-shell-stub.js +1 -1
  21. package/dist/scripts/ensure-spa-shell-stub.js.map +1 -1
  22. package/dist/scripts/init-secrets.d.ts +1 -1
  23. package/dist/scripts/init-secrets.d.ts.map +1 -1
  24. package/dist/scripts/init-secrets.js +3 -3
  25. package/dist/scripts/init-secrets.js.map +1 -1
  26. package/dist/scripts/migrations-lock.js +1 -1
  27. package/dist/scripts/migrations-lock.js.map +1 -1
  28. package/dist/scripts/prepare-wrangler-config.js +3 -3
  29. package/dist/scripts/prepare-wrangler-config.js.map +1 -1
  30. package/dist/scripts/verify-spa-shell.js +1 -1
  31. package/dist/scripts/verify-spa-shell.js.map +1 -1
  32. package/dist/scripts/wrangler-config/patches.d.ts +1 -1
  33. package/dist/scripts/wrangler-config/patches.d.ts.map +1 -1
  34. package/dist/scripts/wrangler-config/patches.js +1 -1
  35. package/dist/scripts/wrangler-config/patches.js.map +1 -1
  36. package/dist/scripts/wrangler-config/resources.d.ts +1 -1
  37. package/dist/scripts/wrangler-config/resources.d.ts.map +1 -1
  38. package/dist/scripts/wrangler-config/resources.js +4 -4
  39. package/dist/scripts/wrangler-config/resources.js.map +1 -1
  40. package/dist/vitest-aliases.d.ts +90 -0
  41. package/dist/vitest-aliases.d.ts.map +1 -0
  42. package/dist/vitest-aliases.js +81 -0
  43. package/dist/vitest-aliases.js.map +1 -0
  44. package/dist/vitest-integration.d.ts +96 -0
  45. package/dist/vitest-integration.d.ts.map +1 -0
  46. package/dist/vitest-integration.js +64 -0
  47. package/dist/vitest-integration.js.map +1 -0
  48. package/dist/vitest-repo.d.ts +154 -0
  49. package/dist/vitest-repo.d.ts.map +1 -0
  50. package/dist/vitest-repo.js +146 -0
  51. package/dist/vitest-repo.js.map +1 -0
  52. package/dist/vitest-web.d.ts +76 -19
  53. package/dist/vitest-web.d.ts.map +1 -1
  54. package/dist/vitest-web.js +62 -25
  55. package/dist/vitest-web.js.map +1 -1
  56. package/dist/vitest.d.ts +70 -19
  57. package/dist/vitest.d.ts.map +1 -1
  58. package/dist/vitest.js +42 -37
  59. package/dist/vitest.js.map +1 -1
  60. package/package.json +8 -2
  61. package/src/eslint-boundaries.ts +99 -6
  62. package/src/eslint.test.ts +22 -1
  63. package/src/eslint.ts +14 -9
  64. package/src/functions/pages-proxy.test.ts +2 -2
  65. package/src/index.ts +23 -14
  66. package/src/scripts/backup/backup-rules.test.ts +3 -3
  67. package/src/scripts/backup/d1-target.ts +1 -1
  68. package/src/scripts/backup/encrypt-backup.ts +2 -2
  69. package/src/scripts/check-god-files.test.ts +1 -1
  70. package/src/scripts/check-god-files.ts +2 -2
  71. package/src/scripts/ensure-spa-shell-stub.ts +1 -1
  72. package/src/scripts/init-secrets.ts +3 -3
  73. package/src/scripts/migrations-lock.ts +1 -1
  74. package/src/scripts/prepare-wrangler-config.ts +3 -3
  75. package/src/scripts/spa-shell.test.ts +1 -1
  76. package/src/scripts/validate-locales.test.ts +1 -1
  77. package/src/scripts/verify-migrations.test.ts +1 -1
  78. package/src/scripts/verify-spa-shell.ts +1 -1
  79. package/src/scripts/wrangler-config/patches.ts +1 -1
  80. package/src/scripts/wrangler-config/resources.ts +4 -4
  81. package/src/test/integration-migrations.test.ts +1 -1
  82. package/src/vite.test.ts +1 -1
  83. package/src/vitest-aliases.test.ts +53 -0
  84. package/src/vitest-aliases.ts +127 -0
  85. package/src/vitest-factories.test.ts +127 -0
  86. package/src/vitest-integration.ts +118 -0
  87. package/src/vitest-repo.ts +269 -0
  88. package/src/vitest-web.ts +110 -28
  89. package/src/vitest.ts +96 -47
package/src/vitest.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * The unit-test Vitest config for the worker-and-packages half of the suite.
3
3
  *
4
- * Provenance: converged from the eight repos' `vitest.config.mts`. The coverage
4
+ * Provenance: converged from all nine repos' `vitest.config.mts`. The coverage
5
5
  * block is Durable-DAV-Router's (the most complete: v8 provider, explicit
6
6
  * include/exclude patterns, thresholds with the reasoning for why they sit just
7
- * under the measured value); the alias block is Durable-DAV's, which is the only
8
- * one that also aliases `cloudflare:workers` and `cloudflare:sockets` to the
9
- * Node mocks.
7
+ * under the measured value); the alias block is the union of Durable-DAV's
8
+ * `cloudflare:*` mocks, every repo's `@scope/* → src` workspace table, and the
9
+ * shared `@/` + `~/` prefixes — see {@link ./vitest-aliases.js}.
10
10
  *
11
11
  * Canonical decisions:
12
12
  *
@@ -17,29 +17,27 @@
17
17
  * invisible rather than counted as zero, which is the exact regression this
18
18
  * gate exists to catch.
19
19
  * 2. **The SPA is excluded here**, so this config and `defineWebVitestConfig()`
20
- * partition the suite rather than each running all of it. Every SPA suite is
21
- * named `web-*`. One number could not honestly describe both halves: the SPA's
22
- * presentational modules pulled a shared floor down 16 points, so a change
23
- * adding legitimate code in `apps/api` could fail the gate on the strength of
24
- * files two directories away.
20
+ * partition the suite rather than each running all of it. The default covers
21
+ * every suite shape the family uses (`test/web/**`, `test/**​/web-*`,
22
+ * `apps/web/**`); see `defineRepoVitestConfig()` for the single-file
23
+ * `projects` composition the AWS/Mail-Meow/CalDAV repos use.
25
24
  * 3. **A custom `exclude` re-lists `node_modules`** — `test/` is a workspace
26
25
  * project and therefore has its own, so without the re-list the glob reaches
27
26
  * into `test/node_modules` and tries to run other packages' suites.
28
27
  * 4. **Thresholds are a consumer argument**, not a kit default. A floor is a
29
28
  * per-repo ratchet measured against that repo's suite; the kit cannot know a
30
29
  * number, and guessing one is how floors get lowered.
30
+ * 5. **`@rexezuge/*` is never externalized** — see `KIT_NO_EXTERNAL`: the kit
31
+ * ships ESM `dist/` with bundler-style extensionless imports, which Node's
32
+ * resolver refuses, so the kit stays inside Vite's pipeline (the same
33
+ * resolver that bundles the worker).
31
34
  */
32
35
 
33
- import { defineConfig } from 'vitest/config';
34
- import { fileURLToPath } from 'node:url';
36
+ import { defineConfig, type Plugin } from 'vitest/config';
37
+ import { DEFAULT_CLOUDFLARE_MOCKS, DEFAULT_WORKSPACE_PACKAGES, KIT_NO_EXTERNAL, workspaceAliases } from './vitest-aliases.ts';
38
+ import type { VitestAlias } from './vitest-aliases.ts';
35
39
 
36
- /**
37
- * One `{ find, replacement }` pair for Vitest's resolver.
38
- */
39
- export interface VitestAlias {
40
- readonly find: string | RegExp;
41
- readonly replacement: string;
42
- }
40
+ export type { VitestAlias } from './vitest-aliases.ts';
43
41
 
44
42
  /**
45
43
  * Coverage thresholds, in the four figures Vitest gates on.
@@ -58,14 +56,64 @@ export interface CoverageThresholds {
58
56
  */
59
57
  export interface UnitVitestConfigOptions {
60
58
  /**
61
- * Extra resolver entries, applied after the canonical ones.
59
+ * The consuming repo's npm scope, e.g. `'@myapp'`. When supplied, `@scope/*`
60
+ * workspace aliases are generated from `packages` (plus `background` under
61
+ * `apps/`); when omitted, only the `cloudflare:*`, `@/` and `~/` aliases
62
+ * apply and `aliases` carries the workspace table by hand.
63
+ */
64
+ readonly scope?: string;
65
+ /**
66
+ * Workspace package directory names. Defaults to the family superset; a repo
67
+ * without a domain package keeps the default harmlessly (its alias never
68
+ * matches).
69
+ */
70
+ readonly packages?: readonly string[];
71
+ /**
72
+ * Which `cloudflare:*` mocks to alias. Defaults to workers/sockets/workflows.
73
+ */
74
+ readonly mocks?: readonly string[];
75
+ /**
76
+ * Extra resolver entries, applied after the canonical ones (e.g. the `hono`
77
+ * dedupe two repos carry, or an `/index.ts` exact entry).
62
78
  */
63
79
  readonly aliases?: readonly VitestAlias[];
64
80
  /**
65
81
  * Where the repo root is, as a file URL. Defaults to the directory this
66
82
  * factory is *called* from, which is the repo root's `vitest.config.mts`.
83
+ * Pass `new URL('.', import.meta.url).href` explicitly from the caller —
84
+ * never rely on this module's own URL, which points into `node_modules`.
67
85
  */
68
86
  readonly root?: string;
87
+ /**
88
+ * Which suites to collect. Defaults to the whole unit suite.
89
+ */
90
+ readonly include?: readonly string[];
91
+ /**
92
+ * Suite paths to skip, replacing the default. Defaults re-list
93
+ * `node_modules`/`dist`/`integration` plus every SPA shape.
94
+ */
95
+ readonly exclude?: readonly string[];
96
+ /**
97
+ * `node_modules` specifiers to inline through Vite rather than externalize.
98
+ * Defaults to `['dofs']` (Durable-DAV: `dofs` imports `cloudflare:workers`,
99
+ * which Node rejects before `resolve.alias` is consulted).
100
+ */
101
+ readonly serverDepsInline?: readonly string[];
102
+ /**
103
+ * Resolver `noExternal` patterns. Defaults to keeping `@rexezuge/*` inside
104
+ * Vite's pipeline; see `KIT_NO_EXTERNAL`.
105
+ */
106
+ readonly noExternal?: readonly (string | RegExp)[];
107
+ /**
108
+ * Vite plugins for the suite (rare on the backend half; the SPA half passes
109
+ * `react()` here).
110
+ */
111
+ readonly plugins?: readonly Plugin[];
112
+ /**
113
+ * Setup files run before each test file (e.g. AWS's floci guard, Meow's
114
+ * backend/web setups).
115
+ */
116
+ readonly setupFiles?: readonly string[];
69
117
  /**
70
118
  * Coverage `include` globs. Defaults to the worker plus every workspace
71
119
  * package's `src`.
@@ -88,29 +136,13 @@ export interface UnitVitestConfigOptions {
88
136
  /**
89
137
  * The SPA's suites, excluded here so the two configs partition the suite.
90
138
  *
91
- * Every SPA suite is named `web-*`. `defineWebVitestConfig()` includes the same
92
- * glob, so a file is collected by exactly one config.
93
- */
94
- const SPA_FILES = 'test/**/web-*.test.{ts,tsx}';
95
-
96
- /**
97
- * The canonical alias set.
98
- *
99
- * Built relative to the consumer's repo root rather than the kit's directory,
100
- * so the factory describes *their* layout. A Vitest config is a standalone file
101
- * with no module graph to share through, and a suite resolving a workspace
102
- * import differently from its sibling fails in a way that looks like a product
103
- * bug — which is why these are stated once, here, rather than duplicated.
139
+ * Every shape the family uses: the `test/web/**` directory convention
140
+ * (CalDAV-Bridge, Mail-Meow), the root-suite `web-*` naming convention
141
+ * (Durable-DAV-Router), and suites bundled under `apps/web` (Mail-Otter).
142
+ * `defineWebVitestConfig()` includes the same globs, so a file is collected by
143
+ * exactly one config.
104
144
  */
105
- function canonicalAliases(root: string): VitestAlias[] {
106
- const at = (relative: string): string => fileURLToPath(new URL(relative, root));
107
- return [
108
- { find: 'cloudflare:workers', replacement: at('test/mocks/cloudflare-workers.ts') },
109
- { find: 'cloudflare:sockets', replacement: at('test/mocks/cloudflare-sockets.ts') },
110
- { find: /^@\//, replacement: `${at('apps/api/src')}/` },
111
- { find: /^~\//, replacement: `${at('apps/web/src')}/` },
112
- ];
113
- }
145
+ const SPA_FILES: readonly string[] = ['test/web/**', 'test/**/web-*.test.{ts,tsx}', 'apps/web/**/*.test.{ts,tsx}'];
114
146
 
115
147
  /**
116
148
  * Coverage `exclude` globs converged from the source repos.
@@ -124,6 +156,7 @@ function canonicalAliases(root: string): VitestAlias[] {
124
156
  const CANONICAL_COVERAGE_EXCLUDE: readonly string[] = [
125
157
  'scripts/**',
126
158
  '**/*.test.ts',
159
+ '**/*.test.tsx',
127
160
  '**/*.d.ts',
128
161
  '**/index.ts',
129
162
  '**/types.d.ts',
@@ -140,31 +173,47 @@ const CANONICAL_COVERAGE_EXCLUDE: readonly string[] = [
140
173
  * ```ts
141
174
  * // vitest.config.mts
142
175
  * import { defineUnitVitestConfig } from '@rexezuge/tooling/vitest';
143
- * export default defineUnitVitestConfig();
176
+ * export default defineUnitVitestConfig({
177
+ * scope: '@myapp',
178
+ * thresholds: { statements: 60, branches: 55, functions: 65, lines: 60 },
179
+ * });
144
180
  * ```
145
181
  */
146
182
  export function defineUnitVitestConfig(options: UnitVitestConfigOptions = {}): ReturnType<typeof defineConfig> {
147
183
  const root = options.root ?? new URL('.', import.meta.url).href;
148
184
  const coverageInclude = options.coverageInclude ?? ['apps/api/src/**/*.ts', 'apps/background/src/**/*.ts', 'packages/**/src/**/*.ts'];
149
185
  const coverageExclude = [...CANONICAL_COVERAGE_EXCLUDE, ...(options.coverageExclude ?? [])];
150
- const aliases = [...canonicalAliases(root), ...(options.aliases ?? [])];
186
+ const baseAliases = workspaceAliases({
187
+ scope: options.scope ?? '@unused',
188
+ root,
189
+ packages: options.scope === undefined ? [] : (options.packages ?? DEFAULT_WORKSPACE_PACKAGES),
190
+ mocks: options.mocks ?? DEFAULT_CLOUDFLARE_MOCKS,
191
+ });
192
+ // A placeholder scope contributes no `@unused/*` entries; the mocks plus
193
+ // `@/`/`~/` survive either way.
194
+ const aliases = [
195
+ ...baseAliases.filter((alias) => typeof alias.find !== 'string' || !alias.find.startsWith('@unused')),
196
+ ...(options.aliases ?? []),
197
+ ];
151
198
 
152
199
  return defineConfig({
153
- resolve: { alias: aliases },
200
+ ...(options.plugins !== undefined && { plugins: [...options.plugins] }),
201
+ resolve: { alias: aliases, noExternal: [...KIT_NO_EXTERNAL, ...(options.noExternal ?? [])] },
154
202
  test: {
155
203
  globals: true,
156
204
  environment: 'node',
157
- include: ['test/**/*.test.{ts,tsx}'],
205
+ include: [...(options.include ?? ['test/**/*.test.{ts,tsx}'])],
158
206
  // `test` is a workspace project and therefore has its own node_modules;
159
207
  // without the re-list, the glob reaches into it.
160
- exclude: ['**/node_modules/**', '**/dist/**', 'test/integration/**', SPA_FILES],
208
+ exclude: [...(options.exclude ?? ['**/node_modules/**', '**/dist/**', 'test/integration/**', ...SPA_FILES])],
209
+ ...(options.setupFiles !== undefined && { setupFiles: [...options.setupFiles] }),
161
210
  server: {
162
211
  deps: {
163
212
  // A `node_modules` dependency is externalized by default, so Node's
164
213
  // own ESM loader resolves a `cloudflare:workers` specifier and rejects
165
214
  // the scheme before `resolve.alias` is ever consulted. Inlining routes
166
215
  // it back through Vite, where the alias applies.
167
- inline: ['dofs'],
216
+ inline: [...(options.serverDepsInline ?? ['dofs'])],
168
217
  },
169
218
  },
170
219
  coverage: {