@ultimat3/testing 31.19.0 → 31.21.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/README.md CHANGED
@@ -89,6 +89,7 @@ unitTest('the clock moves only when the test moves it', ({ clock }) => {
89
89
  | `island-states-resolve.ts` | a name a reader typed → the manifest it meant; a manifest → the island file it claims |
90
90
  | `framework-fixtures.ts` | registers both sets; the app registers only what it owns |
91
91
  | `registry-leak-guard.ts` | fails the run naming the FILE that left a process-global registry dirty, and restores the ones that can be restored at the same boundary |
92
+ | `rerun-declarations.ts` | a test file loaded again in the same process (`bun test --rerun-each`) has what its module BODY declared — `entity()`, `job()` — forgotten first, so it declares afresh instead of `X_ENTITY_DUPLICATE` / `X_JOB_DUPLICATE`. What a test or hook registers is the file's own to undo |
92
93
  | `registry-snapshot.ts` | `captureProcessRegistries()` / `restoreProcessRegistries()` — the locale config, the catalogs, the permission set, the role map, the tasks and the declared measurement actor, put back as a file inherited them. A module-scope declaration evaluates once per process (`bun test` without `--isolate`, `As of 2026-08`), so a neighbour's `clearPermissions()` is otherwise permanent |
93
94
  | `registry-isolation.ts` | `isolateEntityRegistry()` — an empty entity registry, and the process's back after. Its own entry point (`@ultimat3/testing/registry-isolation`), never the barrel: it value-imports `@ultimat3/entity`, and the barrel is what a tier-0 test imports for `expect` |
94
95
  | `quiet-logs.ts` | a green run prints the reporter and nothing else: the framework's log lines go to a sink that drops them. `LOG_LEVEL=info bun test <file>` shows them — naming `LOG_LEVEL` is the one escape hatch, and a level above `info` filters what the TERMINAL shows, never what the logger emits (below). A test asserting on log output hands `structuredLogger({ level, writer })` its own writer, or installs its own with `installLogSink` and calls the release it returns |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/testing",
3
- "version": "31.19.0",
3
+ "version": "31.21.0",
4
4
  "description": "Test harness: cloned template DBs per worker, frozen clock, sealed network, 6 test types",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -47,26 +47,26 @@
47
47
  "test": "bun test"
48
48
  },
49
49
  "dependencies": {
50
- "@ultimat3/action": "31.19.0",
51
- "@ultimat3/ai": "31.19.0",
52
- "@ultimat3/auth": "31.19.0",
53
- "@ultimat3/cache": "31.19.0",
54
- "@ultimat3/core": "31.19.0",
55
- "@ultimat3/db": "31.19.0",
56
- "@ultimat3/entity": "31.19.0",
57
- "@ultimat3/http": "31.19.0",
58
- "@ultimat3/i18n": "31.19.0",
59
- "@ultimat3/jobs": "31.19.0",
60
- "@ultimat3/mail": "31.19.0",
61
- "@ultimat3/notify": "31.19.0",
62
- "@ultimat3/policy": "31.19.0",
63
- "@ultimat3/pwa": "31.19.0",
64
- "@ultimat3/query": "31.19.0",
65
- "@ultimat3/realtime": "31.19.0",
66
- "@ultimat3/render": "31.19.0",
67
- "@ultimat3/schema": "31.19.0",
68
- "@ultimat3/scraping": "31.19.0",
69
- "@ultimat3/seo": "31.19.0",
70
- "@ultimat3/time": "31.19.0"
50
+ "@ultimat3/action": "31.21.0",
51
+ "@ultimat3/ai": "31.21.0",
52
+ "@ultimat3/auth": "31.21.0",
53
+ "@ultimat3/cache": "31.21.0",
54
+ "@ultimat3/core": "31.21.0",
55
+ "@ultimat3/db": "31.21.0",
56
+ "@ultimat3/entity": "31.21.0",
57
+ "@ultimat3/http": "31.21.0",
58
+ "@ultimat3/i18n": "31.21.0",
59
+ "@ultimat3/jobs": "31.21.0",
60
+ "@ultimat3/mail": "31.21.0",
61
+ "@ultimat3/notify": "31.21.0",
62
+ "@ultimat3/policy": "31.21.0",
63
+ "@ultimat3/pwa": "31.21.0",
64
+ "@ultimat3/query": "31.21.0",
65
+ "@ultimat3/realtime": "31.21.0",
66
+ "@ultimat3/render": "31.21.0",
67
+ "@ultimat3/schema": "31.21.0",
68
+ "@ultimat3/scraping": "31.21.0",
69
+ "@ultimat3/seo": "31.21.0",
70
+ "@ultimat3/time": "31.21.0"
71
71
  }
72
72
  }
@@ -16,7 +16,7 @@ let installed = false;
16
16
  * that never touched the package has nothing to reset, and loading it to reset would put its graph
17
17
  * into every `packages/core` test. Resolved once per process: the path does not move.
18
18
  */
19
- function loaded(pkg: string, file: string): string | undefined {
19
+ export function loaded(pkg: string, file: string): string | undefined {
20
20
  const key = `${pkg}/${file}`;
21
21
  let path = paths.get(key);
22
22
  if (path === undefined) {
@@ -14,6 +14,7 @@ import {
14
14
  mergeSnapshots,
15
15
  restoreProcessRegistries,
16
16
  } from './registry-snapshot';
17
+ import { frameworkDeclarationRegistries, rerunDeclarations } from './rerun-declarations';
17
18
 
18
19
  /**
19
20
  * What is REPORTED, and why only these two. Both are BOOT installs — `declareTags` takes the
@@ -106,10 +107,22 @@ const repoRelative = (path: string): string => {
106
107
  */
107
108
  const BASELINE_HOOK = '__ultimateRegistryLeakBaseline';
108
109
 
109
- /** Appended, never prepended: an `import` is hoisted and would sample before the graph evaluates. */
110
+ /** Appended: the baseline is the file's whole module scope, its body's declarations included. */
110
111
  const SAMPLE_BASELINE = `\n;globalThis[${JSON.stringify(BASELINE_HOOK)}]?.();\n`;
111
112
 
112
- const hookHost = globalThis as typeof globalThis & { [BASELINE_HOOK]?: () => void };
113
+ /**
114
+ * PREPENDED: `import`s are hoisted, so this runs once the file's whole import graph has evaluated
115
+ * and before its body (measured on Bun 1.4.4) — the start of the window in which the BODY declares
116
+ * (`rerun-declarations.ts`). On line 1 with no newline, so every line number a stack trace or a
117
+ * coverage report names is the file's own.
118
+ */
119
+ const BODY_START_HOOK = '__ultimateBodyStart';
120
+ const SAMPLE_BODY_START = `;globalThis[${JSON.stringify(BODY_START_HOOK)}]?.();`;
121
+
122
+ const hookHost = globalThis as typeof globalThis & {
123
+ [BASELINE_HOOK]?: () => void;
124
+ [BODY_START_HOOK]?: () => void;
125
+ };
113
126
 
114
127
  let installed = false;
115
128
 
@@ -141,6 +154,7 @@ export function installRegistryLeakGuard(): void {
141
154
  // The locale config before any app code ran — preload time.
142
155
  const pristineLocales = JSON.stringify(captureProcessRegistries().locales);
143
156
  const inApp = isAppRoot();
157
+ const reruns = rerunDeclarations(frameworkDeclarationRegistries);
144
158
 
145
159
  const close = (): void => {
146
160
  if (current === undefined) return;
@@ -182,7 +196,9 @@ export function installRegistryLeakGuard(): void {
182
196
  // Called by the statement appended below, once per test file, from that file's own module scope:
183
197
  // everything the file's MODULE graph registered is its environment — importing an app module is
184
198
  // how an app declares its tags — and everything after this point is the file's own to undo.
199
+ hookHost[BODY_START_HOOK] = () => reruns.bodyStart();
185
200
  hookHost[BASELINE_HOOK] = () => {
201
+ reruns.bodyEnd();
186
202
  if (pending === undefined) return;
187
203
  accumulated = mergeSnapshots(accumulated, captureProcessRegistries());
188
204
  current = { file: pending, before: sampleRegistries(), snapshot: accumulated };
@@ -207,8 +223,9 @@ export function installRegistryLeakGuard(): void {
207
223
  runFileBoundary();
208
224
  close();
209
225
  pending = repoRelative(args.path);
226
+ reruns.loading(args.path);
210
227
  return {
211
- contents: `${await Bun.file(args.path).text()}${SAMPLE_BASELINE}`,
228
+ contents: `${SAMPLE_BODY_START}${await Bun.file(args.path).text()}${SAMPLE_BASELINE}`,
212
229
  loader: 'ts',
213
230
  };
214
231
  });
@@ -0,0 +1,127 @@
1
+ // One test file loaded AGAIN in the same process — `bun test --rerun-each`, CI's `flaky` job —
2
+ // re-evaluates its own module body and nothing it imports. What that BODY declared last run is
3
+ // forgotten as the file reloads, so `entity()` / `job()` at module scope declare afresh instead of
4
+ // throwing X_ENTITY_DUPLICATE / X_JOB_DUPLICATE. `registry-leak-guard.ts` owns WHEN.
5
+
6
+ import type { RegistryEntry } from '@ultimat3/entity';
7
+ import type { AnyJobHandle } from '@ultimat3/jobs';
8
+ import { registeredJobs, restoreJobs } from '@ultimat3/jobs';
9
+ import { loaded } from './per-test-reset';
10
+
11
+ /** A name-keyed process registry this module can read, prune and refill. */
12
+ export interface DeclarationRegistry {
13
+ readonly kind: string;
14
+ names(): readonly string[];
15
+ /** Removes `names`; answers what it removed, to hand back to `putBack`. */
16
+ forget(names: ReadonlySet<string>): readonly unknown[];
17
+ /** Re-registers each entry whose name is absent now; one the body declared again stays. */
18
+ putBack(entries: readonly unknown[]): void;
19
+ }
20
+
21
+ export interface RerunDeclarations {
22
+ /** The file at `path` is about to evaluate: forget what its body declared on its last run. */
23
+ loading(path: string): void;
24
+ /** Its imports have evaluated and its body is about to: the baseline its body adds to. */
25
+ bodyStart(): void;
26
+ /** Its body has evaluated: record what it declared; refill what this run did not redo. */
27
+ bodyEnd(): void;
28
+ }
29
+
30
+ /**
31
+ * Only the BODY's window — after its imports, before its first hook — and never what a test or
32
+ * hook registers: that is the file's own to undo, and a test's import inside a body would then be
33
+ * forgotten for a module that will not evaluate again. A name forgotten and not declared again
34
+ * (a top-level `await import()` that was a cache hit this time) is put back at `bodyEnd`.
35
+ */
36
+ export function rerunDeclarations(
37
+ registries: () => readonly DeclarationRegistry[],
38
+ ): RerunDeclarations {
39
+ const declaredBy = new Map<string, ReadonlyMap<string, ReadonlySet<string>>>();
40
+ let current: string | undefined;
41
+ let baseline = new Map<string, ReadonlySet<string>>();
42
+ let forgotten = new Map<string, readonly unknown[]>();
43
+ return {
44
+ loading(path) {
45
+ current = path;
46
+ forgotten = new Map();
47
+ const last = declaredBy.get(path);
48
+ if (last === undefined) return;
49
+ for (const registry of registries()) {
50
+ const names = last.get(registry.kind);
51
+ if (names !== undefined && names.size > 0) {
52
+ forgotten.set(registry.kind, registry.forget(names));
53
+ }
54
+ }
55
+ },
56
+ bodyStart() {
57
+ baseline = new Map(
58
+ registries().map((registry) => [registry.kind, new Set(registry.names())]),
59
+ );
60
+ },
61
+ bodyEnd() {
62
+ if (current === undefined) return;
63
+ const declared = new Map<string, ReadonlySet<string>>();
64
+ for (const registry of registries()) {
65
+ const before = baseline.get(registry.kind) ?? new Set<string>();
66
+ declared.set(registry.kind, new Set(registry.names().filter((name) => !before.has(name))));
67
+ const stash = forgotten.get(registry.kind);
68
+ if (stash !== undefined) registry.putBack(stash);
69
+ }
70
+ declaredBy.set(current, declared);
71
+ current = undefined;
72
+ forgotten = new Map();
73
+ },
74
+ };
75
+ }
76
+
77
+ /** Jobs: `@ultimat3/jobs` is already in every test's graph (`registry-snapshot.ts` imports it). */
78
+ const jobRegistry: DeclarationRegistry = {
79
+ kind: 'job',
80
+ names: () => registeredJobs().map((handle) => handle.name),
81
+ forget(names) {
82
+ const all = registeredJobs();
83
+ restoreJobs(all.filter((handle) => !names.has(handle.name)));
84
+ return all.filter((handle) => names.has(handle.name));
85
+ },
86
+ putBack(entries) {
87
+ const live = registeredJobs();
88
+ const present = new Set(live.map((handle) => handle.name));
89
+ const missing = (entries as readonly AnyJobHandle[]).filter((h) => !present.has(h.name));
90
+ if (missing.length > 0) restoreJobs([...live, ...missing]);
91
+ },
92
+ };
93
+
94
+ interface EntityRegistryModule {
95
+ readonly registeredEntities: () => readonly RegistryEntry[];
96
+ readonly registerEntity: (entry: RegistryEntry) => RegistryEntry;
97
+ readonly getEntity: (name: string) => RegistryEntry | undefined;
98
+ readonly clearRegistry: () => void;
99
+ }
100
+
101
+ /**
102
+ * Entities, only once something in this process loaded `@ultimat3/entity`: importing it here would
103
+ * put the entity registry into every `packages/core` test (`registry-isolation.ts`'s reason).
104
+ */
105
+ const entityRegistry = (module: EntityRegistryModule): DeclarationRegistry => ({
106
+ kind: 'entity',
107
+ names: () => module.registeredEntities().map((entry) => entry.name),
108
+ forget(names) {
109
+ const all = module.registeredEntities();
110
+ module.clearRegistry();
111
+ for (const entry of all) if (!names.has(entry.name)) module.registerEntity(entry);
112
+ return all.filter((entry) => names.has(entry.name));
113
+ },
114
+ putBack(entries) {
115
+ for (const entry of entries as readonly RegistryEntry[]) {
116
+ if (module.getEntity(entry.name) === undefined) module.registerEntity(entry);
117
+ }
118
+ },
119
+ });
120
+
121
+ /** The registries a framework test file declares into at module scope, as loaded right now. */
122
+ export function frameworkDeclarationRegistries(): readonly DeclarationRegistry[] {
123
+ const path = loaded('@ultimat3/entity', 'registry.ts');
124
+ if (path === undefined) return [jobRegistry];
125
+ // why: synchronous, as the body hooks are; the module is in the cache, so this evaluates nothing.
126
+ return [jobRegistry, entityRegistry(require(path) as EntityRegistryModule)];
127
+ }