slicejs-web-framework 4.0.2 → 4.1.1

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 (73) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +97 -97
  3. package/Slice/Components/Structural/ContextManager/ContextManager.js +422 -422
  4. package/Slice/Components/Structural/ContextManager/ContextManagerDebugger.js +470 -470
  5. package/Slice/Components/Structural/Controller/Controller.js +1406 -1406
  6. package/Slice/Components/Structural/Controller/allowedValuesValidation.js +52 -52
  7. package/Slice/Components/Structural/Debugger/Debugger.html +72 -72
  8. package/Slice/Components/Structural/Debugger/Debugger.js +1505 -1505
  9. package/Slice/Components/Structural/EventManager/EventManager.js +651 -651
  10. package/Slice/Components/Structural/EventManager/EventManagerDebugger.js +985 -985
  11. package/Slice/Components/Structural/LeakInspector/LeakInspector.js +296 -296
  12. package/Slice/Components/Structural/Logger/Log.js +10 -10
  13. package/Slice/Components/Structural/Logger/LogViewer/LogViewer.js +662 -662
  14. package/Slice/Components/Structural/Logger/Logger.js +225 -225
  15. package/Slice/Components/Structural/Router/Router.js +820 -810
  16. package/Slice/Components/Structural/Router/applyRouteMetadata.js +39 -0
  17. package/Slice/Components/Structural/StylesManager/StylesManager.js +82 -82
  18. package/Slice/Components/Structural/StylesManager/ThemeManager/ThemeManager.js +175 -175
  19. package/Slice/Slice.js +711 -710
  20. package/api/framework/devDepsOptimizer.js +335 -335
  21. package/api/framework/express.js +3 -3
  22. package/api/framework/precompressedStatic.js +102 -93
  23. package/api/framework/publicEnvResolver.js +123 -123
  24. package/api/framework/securityMiddleware.js +197 -197
  25. package/api/framework/seo.js +387 -0
  26. package/api/framework/server.js +777 -410
  27. package/api/index.js +14 -4
  28. package/package.json +6 -2
  29. package/sliceConfig.schema.json +122 -122
  30. package/src/App/index.html +69 -69
  31. package/src/App/index.js +20 -20
  32. package/src/App/style.css +11 -11
  33. package/src/Components/AppComponents/AboutSection/AboutSection.css +9 -9
  34. package/src/Components/AppComponents/AboutSection/AboutSection.html +8 -8
  35. package/src/Components/AppComponents/AboutSection/AboutSection.js +12 -12
  36. package/src/Components/AppComponents/AppShell/AppShell.css +10 -10
  37. package/src/Components/AppComponents/AppShell/AppShell.html +4 -4
  38. package/src/Components/AppComponents/AppShell/AppShell.js +36 -36
  39. package/src/Components/AppComponents/HomeSection/HomeSection.css +20 -20
  40. package/src/Components/AppComponents/HomeSection/HomeSection.html +10 -10
  41. package/src/Components/AppComponents/HomeSection/HomeSection.js +19 -19
  42. package/src/Components/components.js +8 -8
  43. package/src/public/Styles/sliceStyles.css +34 -34
  44. package/src/public/Themes/Dark.css +42 -42
  45. package/src/public/Themes/Light.css +31 -31
  46. package/src/public/Themes/Slice.css +47 -47
  47. package/src/public/manifest.json +15 -15
  48. package/src/public/robots.txt +4 -4
  49. package/src/public/sitemap.xml +8 -8
  50. package/src/routes.js +9 -9
  51. package/src/sliceConfig.json +72 -72
  52. package/types/index.d.ts +219 -219
  53. package/.opencode/opencode.json +0 -15
  54. package/Slice/tests/build-bundled-component-without-category.test.js +0 -103
  55. package/Slice/tests/build-js-only-visual-components.test.js +0 -144
  56. package/Slice/tests/build-singleton.test.js +0 -244
  57. package/Slice/tests/bundle-v2-runtime-contract.test.js +0 -747
  58. package/Slice/tests/context-patch-use.test.js +0 -95
  59. package/Slice/tests/destroy-cascade.test.js +0 -215
  60. package/Slice/tests/event-emitters.test.js +0 -79
  61. package/Slice/tests/event-registry.test.js +0 -141
  62. package/Slice/tests/fixtures/real-runtime-loader.mjs +0 -21
  63. package/Slice/tests/leak-orphans.test.js +0 -131
  64. package/Slice/tests/props-allowed-values-validation.test.js +0 -119
  65. package/Slice/tests/public-env-runtime-accessors.test.js +0 -44
  66. package/Slice/tests/public-env-typed-accessors.test.js +0 -66
  67. package/Slice/tests/router-loading-finally.test.js +0 -68
  68. package/api/tests/dev-deps-optimizer.test.js +0 -137
  69. package/api/tests/dev-server-external.test.js +0 -56
  70. package/api/tests/precompressed-static.test.js +0 -119
  71. package/api/tests/public-env-resolver.test.js +0 -193
  72. package/tests/router-build-failure.test.js +0 -125
  73. package/tests/router-emit-ordering.test.js +0 -63
@@ -1,747 +0,0 @@
1
- import test from 'node:test';
2
- import assert from 'node:assert/strict';
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises';
4
- import { tmpdir } from 'node:os';
5
- import path from 'node:path';
6
- import { register } from 'node:module';
7
- import { pathToFileURL } from 'node:url';
8
-
9
- test('validateBundleModule rejects module missing Bundling V2 exports contract', async () => {
10
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
11
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
12
- await writeFile(
13
- loaderPath,
14
- `export async function resolve(specifier, context, nextResolve) {
15
- if (specifier === '/Components/components.js') {
16
- return {
17
- shortCircuit: true,
18
- url: 'data:text/javascript,export default {};',
19
- };
20
- }
21
- return nextResolve(specifier, context);
22
- }
23
- `,
24
- 'utf8'
25
- );
26
- register(pathToFileURL(loaderPath).href);
27
-
28
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
29
- const { default: Controller } = await import(controllerModuleUrl);
30
- const controller = new Controller();
31
- const originalWindow = globalThis.window;
32
-
33
- try {
34
- globalThis.window = {
35
- __slicePendingRegistrations: [],
36
- };
37
-
38
- assert.equal(
39
- typeof controller.validateBundleModule,
40
- 'function',
41
- 'Controller must expose validateBundleModule for Bundling V2 runtime contract checks'
42
- );
43
-
44
- if (typeof controller.validateBundleModule === 'function') {
45
- await assert.rejects(
46
- () => Promise.resolve(controller.validateBundleModule({ default: {} }, 'critical.js')),
47
- /missing bundling v2 exports contract/i
48
- );
49
- }
50
- } finally {
51
- globalThis.window = originalWindow;
52
- await rm(tempDir, { recursive: true, force: true });
53
- }
54
- });
55
-
56
- test('loadBundle marks requested bundle key and metadata bundleKey as loaded', async () => {
57
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
58
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
59
- await writeFile(
60
- loaderPath,
61
- `export async function resolve(specifier, context, nextResolve) {
62
- if (specifier === '/Components/components.js') {
63
- return {
64
- shortCircuit: true,
65
- url: 'data:text/javascript,export default {};',
66
- };
67
- }
68
- return nextResolve(specifier, context);
69
- }
70
- `,
71
- 'utf8'
72
- );
73
- register(pathToFileURL(loaderPath).href);
74
-
75
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
76
- const { default: Controller } = await import(controllerModuleUrl);
77
- const controller = new Controller();
78
- const originalSlice = globalThis.slice;
79
-
80
- controller.bundleConfig = {
81
- bundles: {
82
- routes: {
83
- dashboard: {
84
- file: 'dashboard.js',
85
- },
86
- },
87
- },
88
- };
89
-
90
- controller.importBundleOnce = async () => ({});
91
- controller.validateBundleModule = async () => ({
92
- metadata: {
93
- bundleKey: 'routes.dashboard.v2',
94
- type: 'route',
95
- },
96
- registerAll: async () => {},
97
- });
98
-
99
- try {
100
- globalThis.slice = {
101
- stylesManager: {},
102
- };
103
-
104
- await controller.loadBundle('dashboard');
105
-
106
- assert.equal(controller.loadedBundles.has('dashboard'), true);
107
- assert.equal(controller.loadedBundles.has('routes.dashboard.v2'), true);
108
- assert.equal(controller.criticalBundleLoaded, false);
109
- } finally {
110
- globalThis.slice = originalSlice;
111
- await rm(tempDir, { recursive: true, force: true });
112
- }
113
- });
114
-
115
- test('loadBundle resolves dependencies first and registers vendor-shared exports once', async () => {
116
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
117
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
118
- await writeFile(
119
- loaderPath,
120
- `export async function resolve(specifier, context, nextResolve) {
121
- if (specifier === '/Components/components.js') {
122
- return {
123
- shortCircuit: true,
124
- url: 'data:text/javascript,export default {};',
125
- };
126
- }
127
- return nextResolve(specifier, context);
128
- }
129
- `,
130
- 'utf8'
131
- );
132
- register(pathToFileURL(loaderPath).href);
133
-
134
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
135
- const { default: Controller } = await import(controllerModuleUrl);
136
- const controller = new Controller();
137
- const originalSlice = globalThis.slice;
138
- const originalWindow = globalThis.window;
139
-
140
- controller.bundleConfig = {
141
- bundles: {
142
- shared: {
143
- 'vendor-shared': {
144
- file: 'slice-bundle.vendor-shared.js',
145
- },
146
- },
147
- routes: {
148
- dashboard: {
149
- file: 'slice-bundle.dashboard.js',
150
- dependencies: ['vendor-shared'],
151
- },
152
- },
153
- },
154
- };
155
-
156
- const importOrder = [];
157
- const registerOrder = [];
158
- const importsByPath = {
159
- '/bundles/slice-bundle.vendor-shared.js': {
160
- SLICE_BUNDLE_META: {
161
- bundleKey: 'vendor-shared',
162
- type: 'shared',
163
- },
164
- SLICE_SHARED_DEPS: {
165
- 'vendors/dompurify': {
166
- sanitize: () => 'ok',
167
- },
168
- },
169
- registerAll: async () => {
170
- registerOrder.push('vendor-shared');
171
- },
172
- },
173
- '/bundles/slice-bundle.dashboard.js': {
174
- SLICE_BUNDLE_META: {
175
- bundleKey: 'dashboard',
176
- type: 'route',
177
- },
178
- registerAll: async () => {
179
- registerOrder.push('dashboard');
180
- },
181
- },
182
- };
183
-
184
- controller.importBundleOnce = async (bundlePath) => {
185
- importOrder.push(bundlePath);
186
- return importsByPath[bundlePath];
187
- };
188
-
189
- try {
190
- globalThis.slice = {
191
- stylesManager: {},
192
- };
193
- globalThis.window = {
194
- __SLICE_SHARED_DEPS__: {},
195
- };
196
-
197
- await controller.loadBundle('dashboard');
198
- await controller.loadBundle('dashboard');
199
-
200
- assert.deepEqual(importOrder, ['/bundles/slice-bundle.vendor-shared.js', '/bundles/slice-bundle.dashboard.js']);
201
- assert.deepEqual(registerOrder, ['vendor-shared', 'dashboard']);
202
- assert.equal(typeof globalThis.window.__SLICE_SHARED_DEPS__['vendors/dompurify']?.sanitize, 'function');
203
- } finally {
204
- globalThis.slice = originalSlice;
205
- globalThis.window = originalWindow;
206
- await rm(tempDir, { recursive: true, force: true });
207
- }
208
- });
209
-
210
- test('loadBundle resolves vendor-shared dependency from bundles.vendorShared config shape', async () => {
211
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
212
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
213
- await writeFile(
214
- loaderPath,
215
- `export async function resolve(specifier, context, nextResolve) {
216
- if (specifier === '/Components/components.js') {
217
- return {
218
- shortCircuit: true,
219
- url: 'data:text/javascript,export default {};',
220
- };
221
- }
222
- return nextResolve(specifier, context);
223
- }
224
- `,
225
- 'utf8'
226
- );
227
- register(pathToFileURL(loaderPath).href);
228
-
229
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
230
- const { default: Controller } = await import(controllerModuleUrl);
231
- const controller = new Controller();
232
- const originalSlice = globalThis.slice;
233
- const originalWindow = globalThis.window;
234
-
235
- controller.bundleConfig = {
236
- bundles: {
237
- vendorShared: {
238
- file: 'slice-bundle.vendor-shared.js',
239
- },
240
- routes: {
241
- dashboard: {
242
- file: 'slice-bundle.dashboard.js',
243
- dependencies: ['vendor-shared'],
244
- },
245
- },
246
- },
247
- };
248
-
249
- const importOrder = [];
250
- const registerOrder = [];
251
- const importsByPath = {
252
- '/bundles/slice-bundle.vendor-shared.js': {
253
- SLICE_BUNDLE_META: {
254
- bundleKey: 'vendor-shared',
255
- type: 'shared',
256
- },
257
- SLICE_SHARED_DEPS: {
258
- 'vendors/dompurify': {
259
- sanitize: () => 'ok',
260
- },
261
- },
262
- registerAll: async () => {
263
- registerOrder.push('vendor-shared');
264
- },
265
- },
266
- '/bundles/slice-bundle.dashboard.js': {
267
- SLICE_BUNDLE_META: {
268
- bundleKey: 'dashboard',
269
- type: 'route',
270
- },
271
- registerAll: async () => {
272
- registerOrder.push('dashboard');
273
- },
274
- },
275
- };
276
-
277
- controller.importBundleOnce = async (bundlePath) => {
278
- importOrder.push(bundlePath);
279
- return importsByPath[bundlePath];
280
- };
281
-
282
- try {
283
- globalThis.slice = {
284
- stylesManager: {},
285
- };
286
- globalThis.window = {
287
- __SLICE_SHARED_DEPS__: {},
288
- };
289
-
290
- await controller.loadBundle('dashboard');
291
-
292
- assert.deepEqual(importOrder, ['/bundles/slice-bundle.vendor-shared.js', '/bundles/slice-bundle.dashboard.js']);
293
- assert.deepEqual(registerOrder, ['vendor-shared', 'dashboard']);
294
- assert.equal(typeof globalThis.window.__SLICE_SHARED_DEPS__['vendors/dompurify']?.sanitize, 'function');
295
- } finally {
296
- globalThis.slice = originalSlice;
297
- globalThis.window = originalWindow;
298
- await rm(tempDir, { recursive: true, force: true });
299
- }
300
- });
301
-
302
- test('loadBundle registers vendor-shared dependencies from registerAll return when SLICE_SHARED_DEPS export is absent', async () => {
303
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
304
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
305
- await writeFile(
306
- loaderPath,
307
- `export async function resolve(specifier, context, nextResolve) {
308
- if (specifier === '/Components/components.js') {
309
- return {
310
- shortCircuit: true,
311
- url: 'data:text/javascript,export default {};',
312
- };
313
- }
314
- return nextResolve(specifier, context);
315
- }
316
- `,
317
- 'utf8'
318
- );
319
- register(pathToFileURL(loaderPath).href);
320
-
321
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
322
- const { default: Controller } = await import(controllerModuleUrl);
323
- const controller = new Controller();
324
- const originalSlice = globalThis.slice;
325
- const originalWindow = globalThis.window;
326
-
327
- controller.bundleConfig = {
328
- bundles: {
329
- vendorShared: {
330
- file: 'slice-bundle.vendor-shared.js',
331
- },
332
- routes: {
333
- dashboard: {
334
- file: 'slice-bundle.dashboard.js',
335
- dependencies: ['vendor-shared'],
336
- },
337
- },
338
- },
339
- };
340
-
341
- const importsByPath = {
342
- '/bundles/slice-bundle.vendor-shared.js': {
343
- SLICE_BUNDLE_META: {
344
- bundleKey: 'vendor-shared',
345
- type: 'shared',
346
- },
347
- registerAll: async () => ({
348
- 'vendors/dompurify': {
349
- sanitize: () => 'ok',
350
- },
351
- }),
352
- },
353
- '/bundles/slice-bundle.dashboard.js': {
354
- SLICE_BUNDLE_META: {
355
- bundleKey: 'dashboard',
356
- type: 'route',
357
- },
358
- registerAll: async () => {},
359
- },
360
- };
361
-
362
- controller.importBundleOnce = async (bundlePath) => importsByPath[bundlePath];
363
-
364
- try {
365
- globalThis.slice = {
366
- stylesManager: {},
367
- };
368
- globalThis.window = {
369
- __SLICE_SHARED_DEPS__: {},
370
- };
371
-
372
- await controller.loadBundle('dashboard');
373
-
374
- assert.equal(typeof globalThis.window.__SLICE_SHARED_DEPS__['vendors/dompurify']?.sanitize, 'function');
375
- } finally {
376
- globalThis.slice = originalSlice;
377
- globalThis.window = originalWindow;
378
- await rm(tempDir, { recursive: true, force: true });
379
- }
380
- });
381
-
382
- test('loadBundle appends bundle hash as query param when importing bundle files', async () => {
383
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
384
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
385
- await writeFile(
386
- loaderPath,
387
- `export async function resolve(specifier, context, nextResolve) {
388
- if (specifier === '/Components/components.js') {
389
- return {
390
- shortCircuit: true,
391
- url: 'data:text/javascript,export default {};',
392
- };
393
- }
394
- return nextResolve(specifier, context);
395
- }
396
- `,
397
- 'utf8'
398
- );
399
- register(pathToFileURL(loaderPath).href);
400
-
401
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
402
- const { default: Controller } = await import(controllerModuleUrl);
403
- const controller = new Controller();
404
- const originalSlice = globalThis.slice;
405
-
406
- controller.bundleConfig = {
407
- bundles: {
408
- routes: {
409
- dashboard: {
410
- file: 'slice-bundle.dashboard.js',
411
- hash: 'abc123hash',
412
- },
413
- },
414
- },
415
- };
416
-
417
- let importedPath = null;
418
- controller.importBundleOnce = async (bundlePath) => {
419
- importedPath = bundlePath;
420
- return {
421
- SLICE_BUNDLE_META: {
422
- bundleKey: 'dashboard',
423
- type: 'route',
424
- },
425
- registerAll: async () => {},
426
- };
427
- };
428
-
429
- try {
430
- globalThis.slice = {
431
- stylesManager: {},
432
- };
433
-
434
- await controller.loadBundle('dashboard');
435
- assert.equal(importedPath, '/bundles/slice-bundle.dashboard.js?v=abc123hash');
436
- } finally {
437
- globalThis.slice = originalSlice;
438
- await rm(tempDir, { recursive: true, force: true });
439
- }
440
- });
441
-
442
- test('loadBundle dedupes concurrent and repeated alias/case requests using canonical bundle key', async () => {
443
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
444
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
445
- await writeFile(
446
- loaderPath,
447
- `export async function resolve(specifier, context, nextResolve) {
448
- if (specifier === '/Components/components.js') {
449
- return {
450
- shortCircuit: true,
451
- url: 'data:text/javascript,export default {};',
452
- };
453
- }
454
- return nextResolve(specifier, context);
455
- }
456
- `,
457
- 'utf8'
458
- );
459
- register(pathToFileURL(loaderPath).href);
460
-
461
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
462
- const { default: Controller } = await import(controllerModuleUrl);
463
- const controller = new Controller();
464
- const originalSlice = globalThis.slice;
465
-
466
- controller.bundleConfig = {
467
- bundles: {
468
- routes: {
469
- dashboard: {
470
- file: 'slice-bundle.dashboard.js',
471
- },
472
- },
473
- },
474
- };
475
-
476
- let registerCallCount = 0;
477
- controller.importBundleOnce = async () => ({
478
- SLICE_BUNDLE_META: {
479
- bundleKey: 'routes.dashboard.v2',
480
- type: 'route',
481
- },
482
- registerAll: async () => {
483
- registerCallCount += 1;
484
- await new Promise((resolve) => setTimeout(resolve, 10));
485
- },
486
- });
487
-
488
- try {
489
- globalThis.slice = {
490
- stylesManager: {},
491
- };
492
-
493
- await Promise.all([
494
- controller.loadBundle('dashboard'),
495
- controller.loadBundle('DASHBOARD'),
496
- ]);
497
-
498
- await controller.loadBundle('Dashboard');
499
-
500
- assert.equal(registerCallCount, 1);
501
- assert.equal(controller.loadedBundles.has('dashboard'), true);
502
- assert.equal(controller.loadedBundles.has('routes.dashboard.v2'), true);
503
- assert.equal(controller.bundleLoadPromises.size, 0);
504
- } finally {
505
- globalThis.slice = originalSlice;
506
- await rm(tempDir, { recursive: true, force: true });
507
- }
508
- });
509
-
510
- test('registerVendorSharedDependencies only runs for vendor-shared canonical bundle or explicit metadata flag', async () => {
511
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
512
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
513
- await writeFile(
514
- loaderPath,
515
- `export async function resolve(specifier, context, nextResolve) {
516
- if (specifier === '/Components/components.js') {
517
- return {
518
- shortCircuit: true,
519
- url: 'data:text/javascript,export default {};',
520
- };
521
- }
522
- return nextResolve(specifier, context);
523
- }
524
- `,
525
- 'utf8'
526
- );
527
- register(pathToFileURL(loaderPath).href);
528
-
529
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
530
- const { default: Controller } = await import(controllerModuleUrl);
531
- const controller = new Controller();
532
- const originalWindow = globalThis.window;
533
-
534
- try {
535
- globalThis.window = {
536
- __SLICE_SHARED_DEPS__: {},
537
- };
538
-
539
- controller.registerVendorSharedDependencies(
540
- {
541
- SLICE_SHARED_DEPS: {
542
- shouldNotLoad: true,
543
- },
544
- },
545
- {
546
- type: 'shared',
547
- bundleKey: 'app-shared',
548
- },
549
- 'app-shared'
550
- );
551
-
552
- assert.equal(globalThis.window.__SLICE_SHARED_DEPS__.shouldNotLoad, undefined);
553
-
554
- controller.registerVendorSharedDependencies(
555
- {
556
- SLICE_SHARED_DEPS: {
557
- explicitFlagDependency: true,
558
- },
559
- },
560
- {
561
- type: 'shared',
562
- bundleKey: 'app-shared',
563
- registerVendorSharedDependencies: true,
564
- },
565
- 'app-shared'
566
- );
567
-
568
- assert.equal(globalThis.window.__SLICE_SHARED_DEPS__.explicitFlagDependency, true);
569
- } finally {
570
- globalThis.window = originalWindow;
571
- await rm(tempDir, { recursive: true, force: true });
572
- }
573
- });
574
-
575
- test('Slice init fails fast with contextual error on invalid Bundling V2 contract path', async () => {
576
- const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-init-loader-'));
577
- const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
578
- await writeFile(
579
- loaderPath,
580
- `export async function resolve(specifier, context, nextResolve) {
581
- if (specifier === '/Components/components.js') {
582
- return {
583
- shortCircuit: true,
584
- url: 'data:text/javascript,export default {};',
585
- };
586
- }
587
- return nextResolve(specifier, context);
588
- }
589
- `,
590
- 'utf8'
591
- );
592
- register(pathToFileURL(loaderPath).href);
593
-
594
- const originalWindow = globalThis.window;
595
- const originalDocument = globalThis.document;
596
- const originalFetch = globalThis.fetch;
597
- const originalAlert = globalThis.alert;
598
- const originalSliceDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'slice');
599
- const originalConsoleLog = console.log;
600
- const originalConsoleWarn = console.warn;
601
- const originalConsoleError = console.error;
602
- const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
603
- const { default: Controller } = await import(controllerModuleUrl);
604
- const originalLoadBundle = Controller.prototype.loadBundle;
605
-
606
- const loggedMessages = [];
607
-
608
- try {
609
- globalThis.window = {
610
- location: {
611
- pathname: '/dashboard',
612
- origin: 'http://localhost',
613
- },
614
- };
615
-
616
- Object.defineProperty(globalThis, 'slice', {
617
- configurable: true,
618
- get() {
619
- return globalThis.window?.slice;
620
- },
621
- set(value) {
622
- if (!globalThis.window) {
623
- globalThis.window = {};
624
- }
625
- globalThis.window.slice = value;
626
- },
627
- });
628
-
629
- Controller.prototype.loadBundle = async () => {
630
- throw new Error(
631
- 'Bundle "critical" missing Bundling V2 exports contract: requires SLICE_BUNDLE_META and registerAll'
632
- );
633
- };
634
-
635
- globalThis.document = {
636
- head: {
637
- appendChild() {},
638
- },
639
- body: {
640
- appendChild() {},
641
- },
642
- createElement() {
643
- return {
644
- appendChild() {},
645
- innerHTML: '',
646
- id: '',
647
- };
648
- },
649
- createTextNode() {
650
- return {};
651
- },
652
- addEventListener() {},
653
- };
654
-
655
- globalThis.alert = () => {};
656
- console.log = (...args) => {
657
- loggedMessages.push(args.map(String).join(' '));
658
- };
659
- console.warn = () => {};
660
- console.error = (...args) => {
661
- loggedMessages.push(args.map(String).join(' '));
662
- };
663
-
664
- globalThis.fetch = async (url) => {
665
- if (url === '/sliceConfig.json') {
666
- return {
667
- ok: true,
668
- json: async () => ({
669
- paths: {
670
- routesFile: '/routes.js',
671
- components: {},
672
- },
673
- themeManager: { enabled: false },
674
- stylesManager: { requestedStyles: [] },
675
- logger: { enabled: false },
676
- debugger: { enabled: false },
677
- loading: { enabled: false },
678
- events: { enabled: false },
679
- context: { enabled: false },
680
- }),
681
- };
682
- }
683
-
684
- if (url === '/slice-env.json') {
685
- return {
686
- ok: true,
687
- json: async () => ({ mode: 'production' }),
688
- };
689
- }
690
-
691
- if (url === '/bundles/bundle.config.json') {
692
- return {
693
- ok: true,
694
- json: async () => ({
695
- production: true,
696
- bundles: {
697
- critical: { file: 'critical.js' },
698
- routes: {},
699
- },
700
- routeBundles: {},
701
- }),
702
- };
703
- }
704
-
705
- throw new Error(`Unexpected fetch URL: ${url}`);
706
- };
707
-
708
- const sliceModuleUrl = new URL(`../Slice.js?fail-fast-test=${Date.now()}`, import.meta.url).href;
709
-
710
- // Failing fast means the failure propagates: a rejected import stops
711
- // consumers from running against a runtime that has no router and no
712
- // registered components. Previously init() logged and resolved anyway,
713
- // so the first symptom a developer saw was an unrelated TypeError
714
- // further downstream rather than this contextual error.
715
- await assert.rejects(
716
- () => import(sliceModuleUrl),
717
- /Bundling V2 initialization failed/,
718
- 'a failed init must reject the module import, not resolve a half-built runtime'
719
- );
720
-
721
- assert.ok(
722
- loggedMessages.some((message) => /Bundling V2 initialization failed/i.test(message)),
723
- 'init must log contextual error on Bundling V2 contract failure'
724
- );
725
-
726
- assert.equal(
727
- loggedMessages.some((message) => message.includes('Using individual component loading (no bundles found)')),
728
- false,
729
- 'init must not log fallback bundle message on Bundling V2 contract failure'
730
- );
731
- } finally {
732
- Controller.prototype.loadBundle = originalLoadBundle;
733
- globalThis.window = originalWindow;
734
- globalThis.document = originalDocument;
735
- globalThis.fetch = originalFetch;
736
- globalThis.alert = originalAlert;
737
- if (originalSliceDescriptor) {
738
- Object.defineProperty(globalThis, 'slice', originalSliceDescriptor);
739
- } else {
740
- delete globalThis.slice;
741
- }
742
- console.log = originalConsoleLog;
743
- console.warn = originalConsoleWarn;
744
- console.error = originalConsoleError;
745
- await rm(tempDir, { recursive: true, force: true });
746
- }
747
- });