@panomc/sdk 1.0.0-dev.9 → 2.0.0-dev.62

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 (163) hide show
  1. package/api/routes.core.json +3702 -0
  2. package/bin/api/__tests__/api-compat.test.mjs +97 -0
  3. package/bin/api/__tests__/check-paths.test.mjs +113 -0
  4. package/bin/api/__tests__/cli.test.mjs +44 -0
  5. package/bin/api/__tests__/extract-routes.test.mjs +132 -0
  6. package/bin/api/__tests__/fixtures/core-new/Pano/api/paths.old-new.json +82 -0
  7. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
  8. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
  9. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
  10. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
  11. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
  12. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
  13. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
  14. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
  15. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
  16. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
  17. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
  18. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
  19. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
  20. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
  21. package/bin/api/__tests__/fixtures/plugin-new/api/paths.old-new.json +66 -0
  22. package/bin/api/__tests__/fixtures/plugin-new/build.gradle.kts +42 -0
  23. package/bin/api/__tests__/fixtures/plugin-new/gradle.properties +4 -0
  24. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
  25. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
  26. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
  27. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
  28. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
  29. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
  30. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
  31. package/bin/api/__tests__/fixtures/plugin-new/src/panel/Things.js +4 -0
  32. package/bin/api/__tests__/fixtures/plugin-new/src/theme/Logo.svelte +9 -0
  33. package/bin/api/__tests__/fixtures/plugin-new/src/theme/api.js +29 -0
  34. package/bin/api/__tests__/fixtures/plugin-old/build.gradle.kts +22 -0
  35. package/bin/api/__tests__/fixtures/plugin-old/gradle.properties +4 -0
  36. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
  37. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
  38. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
  39. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
  40. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
  41. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
  42. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
  43. package/bin/api/__tests__/fixtures/plugin-old/src/panel/Things.js +4 -0
  44. package/bin/api/__tests__/fixtures/plugin-old/src/theme/Logo.svelte +9 -0
  45. package/bin/api/__tests__/fixtures/plugin-old/src/theme/api.js +29 -0
  46. package/bin/api/__tests__/fixtures/refusals/gradle.properties +1 -0
  47. package/bin/api/__tests__/fixtures/refusals/src/main/kotlin/x/Bad.kt +39 -0
  48. package/bin/api/__tests__/fixtures/unresolved/src/main/kotlin/x/Orphan.kt +19 -0
  49. package/bin/api/__tests__/helpers.mjs +48 -0
  50. package/bin/api/__tests__/migrate-v1.test.mjs +377 -0
  51. package/bin/api/__tests__/openapi-snapshot.test.mjs +120 -0
  52. package/bin/api/api-compat.mjs +204 -0
  53. package/bin/api/check-paths.mjs +135 -0
  54. package/bin/api/cli.mjs +83 -0
  55. package/bin/api/extract-routes.mjs +129 -0
  56. package/bin/api/js-scan.mjs +337 -0
  57. package/bin/api/kotlin.mjs +366 -0
  58. package/bin/api/migrate-v1.mjs +330 -0
  59. package/bin/api/openapi-snapshot.mjs +112 -0
  60. package/bin/api/project.mjs +172 -0
  61. package/bin/api/rename-table.mjs +66 -0
  62. package/bin/api/util.mjs +194 -0
  63. package/bin/pano-api.js +5 -0
  64. package/build/plugin-api-rollup.js +46 -0
  65. package/core/css/pano-tokens.css +225 -0
  66. package/core/favicon/apple-touch-icon.png +0 -0
  67. package/core/favicon/favicon-96x96.png +0 -0
  68. package/core/favicon/favicon.ico +0 -0
  69. package/core/favicon/favicon.svg +1 -0
  70. package/core/favicon/site.webmanifest +21 -0
  71. package/core/favicon/web-app-manifest-192x192.png +0 -0
  72. package/core/favicon/web-app-manifest-512x512.png +0 -0
  73. package/core/favicon_old/android-icon-144x144.png +0 -0
  74. package/core/favicon_old/android-icon-192x192.png +0 -0
  75. package/core/favicon_old/android-icon-36x36.png +0 -0
  76. package/core/favicon_old/android-icon-48x48.png +0 -0
  77. package/core/favicon_old/android-icon-72x72.png +0 -0
  78. package/core/favicon_old/android-icon-96x96.png +0 -0
  79. package/core/favicon_old/apple-icon-114x114.png +0 -0
  80. package/core/favicon_old/apple-icon-120x120.png +0 -0
  81. package/core/favicon_old/apple-icon-144x144.png +0 -0
  82. package/core/favicon_old/apple-icon-152x152.png +0 -0
  83. package/core/favicon_old/apple-icon-180x180.png +0 -0
  84. package/core/favicon_old/apple-icon-57x57.png +0 -0
  85. package/core/favicon_old/apple-icon-60x60.png +0 -0
  86. package/core/favicon_old/apple-icon-72x72.png +0 -0
  87. package/core/favicon_old/apple-icon-76x76.png +0 -0
  88. package/core/favicon_old/apple-icon-precomposed.png +0 -0
  89. package/core/favicon_old/apple-icon.png +0 -0
  90. package/core/favicon_old/browserconfig.xml +11 -0
  91. package/core/favicon_old/favicon-16x16.png +0 -0
  92. package/core/favicon_old/favicon-32x32.png +0 -0
  93. package/core/favicon_old/favicon-96x96.png +0 -0
  94. package/core/favicon_old/favicon.ico +0 -0
  95. package/core/favicon_old/manifest.json +41 -0
  96. package/core/favicon_old/ms-icon-144x144.png +0 -0
  97. package/core/favicon_old/ms-icon-150x150.png +0 -0
  98. package/core/favicon_old/ms-icon-310x310.png +0 -0
  99. package/core/favicon_old/ms-icon-70x70.png +0 -0
  100. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.eot +0 -0
  101. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.svg +483 -0
  102. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.ttf +0 -0
  103. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff +0 -0
  104. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff2 +0 -0
  105. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.eot +0 -0
  106. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.svg +486 -0
  107. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.ttf +0 -0
  108. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff +0 -0
  109. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff2 +0 -0
  110. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.eot +0 -0
  111. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.svg +484 -0
  112. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.ttf +0 -0
  113. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff +0 -0
  114. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff2 +0 -0
  115. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.eot +0 -0
  116. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.svg +486 -0
  117. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.ttf +0 -0
  118. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff +0 -0
  119. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff2 +0 -0
  120. package/core/js/ControllerRegistry.js +421 -0
  121. package/core/js/NotificationManager.js +62 -0
  122. package/core/js/PluginAPI.js +39 -0
  123. package/core/js/PluginManager.js +851 -0
  124. package/core/js/RouteMatcher.js +142 -0
  125. package/core/js/__tests__/ControllerRegistry.test.js +378 -0
  126. package/core/js/__tests__/NotificationManager.test.js +57 -0
  127. package/core/js/__tests__/PluginAPI.test.js +25 -0
  128. package/core/js/__tests__/RouteMatcher.test.js +33 -0
  129. package/core/js/__tests__/accessClient.test.js +582 -0
  130. package/core/js/__tests__/api.util.v1.test.js +336 -0
  131. package/core/js/__tests__/devBounce.test.js +28 -0
  132. package/core/js/__tests__/html.util.test.js +18 -0
  133. package/core/js/__tests__/pluginFolder.util.test.js +106 -0
  134. package/core/js/__tests__/pluginPackage.test.js +134 -0
  135. package/core/js/__tests__/pluginUiZip.test.js +47 -0
  136. package/core/js/__tests__/sdkLevel.test.js +34 -0
  137. package/core/js/api.util.js +433 -0
  138. package/core/js/html.util.js +26 -0
  139. package/core/js/pluginFolder.util.js +95 -0
  140. package/core/js/tooltip.util.js +59 -0
  141. package/core/js/variables.js +95 -0
  142. package/core/js/version.util.js +100 -0
  143. package/core/scss/_pano-tokens.scss +51 -0
  144. package/core/scss/custom-bootstrap.scss +196 -0
  145. package/core/scss/fonts.scss +85 -0
  146. package/core/scss/main.scss +389 -0
  147. package/core/scss/themes.scss +120 -0
  148. package/package.json +63 -11
  149. package/src/__tests__/controllers.test.js +220 -0
  150. package/src/__tests__/views.spike.test.js +472 -0
  151. package/src/components/{index.js → panel.js} +9 -7
  152. package/src/components/theme.js +18 -0
  153. package/src/controllers.js +127 -0
  154. package/src/index.js +1 -1
  155. package/src/runtime-specifiers.js +88 -0
  156. package/src/svelte.js +3 -2
  157. package/src/types.js +5 -3
  158. package/src/utils/api.js +42 -1
  159. package/src/utils/auth.js +8 -0
  160. package/src/utils/component.js +4 -2
  161. package/src/utils/route.js +16 -0
  162. package/src/utils/text.js +8 -0
  163. package/src/views.js +29 -0
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Simple route matcher that supports literals, dynamic segments [param],
3
+ * catch-all segments [...param], and basic regex patterns.
4
+ */
5
+ function safeDecode(s) {
6
+ try {
7
+ return decodeURIComponent(s);
8
+ } catch {
9
+ return s;
10
+ }
11
+ }
12
+
13
+ export class RouteMatcher {
14
+ /**
15
+ * Matches a path against a pattern.
16
+ * @param {string} pattern - The route pattern (e.g., '/announcements/[id]')
17
+ * @param {string} path - The actual path (e.g., '/announcements/123')
18
+ * @returns {object|null} - Params object if matched, null otherwise
19
+ */
20
+ static match(pattern, path) {
21
+ // Normalize path: remove query params and trailing slash
22
+ const [pathOnly] = path.split('?');
23
+ const normalizedPath = pathOnly === '/' ? '/' : pathOnly.replace(/\/$/, '');
24
+ const normalizedPattern = pattern === '/' ? '/' : pattern.replace(/\/$/, '');
25
+
26
+ // 1. Literal match
27
+ if (normalizedPattern === normalizedPath) {
28
+ return {};
29
+ }
30
+
31
+ // 2. Regex match
32
+ // Regex must be explicitly opted in with a `re:` prefix — we must NOT infer "this is a
33
+ // regex" from a leading/trailing slash, since a normal plugin path with a trailing slash
34
+ // (e.g. `/news/`) would otherwise be reinterpreted as an UNANCHORED regex and silently
35
+ // hijack unrelated routes. The body is anchored as `^(?:...)$` so it can't partial-match.
36
+ if (pattern.startsWith('re:')) {
37
+ try {
38
+ const regex = new RegExp(`^(?:${pattern.slice(3)})$`);
39
+ const match = normalizedPath.match(regex);
40
+ if (match) {
41
+ return match.groups || {};
42
+ }
43
+ } catch (e) {
44
+ console.error('Invalid regex pattern:', pattern, e);
45
+ }
46
+ }
47
+
48
+ // 3. Segment-based match (SvelteKit style)
49
+ const patternSegments = normalizedPattern.split('/').filter(Boolean);
50
+ const pathSegments = normalizedPath.split('/').filter(Boolean);
51
+
52
+ const params = {};
53
+
54
+ // Check for catch-all [...param]
55
+ const catchAllIndex = patternSegments.findIndex((s) => s.startsWith('[...') && s.endsWith(']'));
56
+
57
+ if (catchAllIndex !== -1) {
58
+ // SvelteKit only allows a rest param as the FINAL segment. A catch-all that isn't last
59
+ // would greedily absorb every trailing segment and ignore the pattern's suffix, so a
60
+ // pattern like `/[...rest]/edit` would wrongly match `/a/b/c`. Reject it outright.
61
+ if (catchAllIndex !== patternSegments.length - 1) return null;
62
+
63
+ // Logic for catch-all
64
+ if (pathSegments.length < catchAllIndex) return null;
65
+
66
+ for (let i = 0; i < catchAllIndex; i++) {
67
+ if (!this.segmentsMatch(patternSegments[i], pathSegments[i], params)) {
68
+ return null;
69
+ }
70
+ }
71
+
72
+ const paramName = patternSegments[catchAllIndex].slice(4, -1);
73
+ params[paramName] = pathSegments.slice(catchAllIndex).map(safeDecode).join('/');
74
+ return params;
75
+ }
76
+
77
+ // Normal segment match
78
+ if (patternSegments.length !== pathSegments.length) {
79
+ return null;
80
+ }
81
+
82
+ for (let i = 0; i < patternSegments.length; i++) {
83
+ if (!this.segmentsMatch(patternSegments[i], pathSegments[i], params)) {
84
+ return null;
85
+ }
86
+ }
87
+
88
+ return params;
89
+ }
90
+
91
+ static segmentsMatch(patternSegment, pathSegment, params) {
92
+ if (patternSegment.startsWith('[') && patternSegment.endsWith(']')) {
93
+ const paramName = patternSegment.slice(1, -1);
94
+ params[paramName] = safeDecode(pathSegment);
95
+ return true;
96
+ }
97
+ if (patternSegment.startsWith(':')) {
98
+ const paramName = patternSegment.slice(1);
99
+ params[paramName] = safeDecode(pathSegment);
100
+ return true;
101
+ }
102
+ return patternSegment === pathSegment;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Canonical form of a route key/path used for the exact-match fast path: query stripped and a
108
+ * single trailing slash removed (but `/` preserved). Used at BOTH registration and lookup so
109
+ * `/foo` and `/foo/` collapse to one entry and the exact-match map lookup always hits instead
110
+ * of falling through to the slower (and buggier) regex/segment branch.
111
+ * @param {string} path
112
+ * @returns {string}
113
+ */
114
+ export function canonicalizeRouteKey(path) {
115
+ const [pathOnly] = String(path).split('?');
116
+ return pathOnly === '/' ? '/' : pathOnly.replace(/\/$/, '');
117
+ }
118
+
119
+ /**
120
+ * Finds a registered page matching the given path.
121
+ * @param {Object} registeredPages - Map of patterns to page objects
122
+ * @param {string} path - The path to match
123
+ * @returns {Object|null} - The matching page object with params, or null
124
+ */
125
+ export function findMatch(registeredPages, path) {
126
+ // First, try exact match for performance
127
+ const normalizedPath = canonicalizeRouteKey(path);
128
+
129
+ if (registeredPages[normalizedPath]) {
130
+ return { ...registeredPages[normalizedPath], params: {} };
131
+ }
132
+
133
+ // Then, try more complex matches
134
+ for (const pattern in registeredPages) {
135
+ const params = RouteMatcher.match(pattern, path);
136
+ if (params) {
137
+ return { ...registeredPages[pattern], params };
138
+ }
139
+ }
140
+
141
+ return null;
142
+ }
@@ -0,0 +1,378 @@
1
+ import { afterEach, beforeEach, describe, expect, spyOn, test } from 'bun:test';
2
+ import * as registry from '../ControllerRegistry.js';
3
+
4
+ const KEY = '__PANO_CONTROLLERS__';
5
+
6
+ /** hand-built definition: { name, version, scope, eager, create() } */
7
+ function makeDef({ name = 'cart', version = 1, scope, eager, load } = {}) {
8
+ const def = {
9
+ name,
10
+ version,
11
+ scope,
12
+ eager,
13
+ load,
14
+ created: [],
15
+ destroyed: 0,
16
+ create(host, params, initial, ctx) {
17
+ let state = { count: 0, ...(initial ?? {}) };
18
+ const subs = new Set();
19
+ const controller = {
20
+ name,
21
+ version,
22
+ get: () => state,
23
+ subscribe(run) {
24
+ subs.add(run);
25
+ run(state);
26
+ return () => subs.delete(run);
27
+ },
28
+ actions: {
29
+ add() {
30
+ state = { ...state, count: state.count + 1 };
31
+ subs.forEach((fn) => fn(state));
32
+ },
33
+ },
34
+ destroy() {
35
+ def.destroyed += 1;
36
+ },
37
+ host,
38
+ params,
39
+ ctx,
40
+ };
41
+ def.created.push(controller);
42
+ return controller;
43
+ },
44
+ };
45
+ return def;
46
+ }
47
+
48
+ const makeHost = (tag = 'h') => ({ tag, browser: true, request: async () => ({}) });
49
+
50
+ function enterBrowser() {
51
+ globalThis.window = {};
52
+ globalThis.document = {};
53
+ }
54
+ function leaveBrowser() {
55
+ delete globalThis.window;
56
+ delete globalThis.document;
57
+ }
58
+
59
+ let warn;
60
+ beforeEach(() => {
61
+ delete globalThis[KEY];
62
+ warn = spyOn(console, 'warn').mockImplementation(() => {});
63
+ });
64
+ afterEach(() => {
65
+ leaveBrowser();
66
+ warn.mockRestore();
67
+ delete globalThis[KEY];
68
+ });
69
+
70
+ describe('state on globalThis', () => {
71
+ test('lives on __PANO_CONTROLLERS__ and is shared by name', () => {
72
+ registry.register('pano-plugin-market', 'market', [makeDef()]);
73
+ expect(globalThis[KEY]).toBeDefined();
74
+ expect(registry.has('market/cart')).toBe(true);
75
+ expect(globalThis[KEY].defs.has('market/cart')).toBe(true);
76
+ });
77
+ });
78
+
79
+ describe('register / has / list', () => {
80
+ test('array and object definitions, has with version, list shape', () => {
81
+ registry.register('pano-plugin-market', 'market', { cart: makeDef({ name: 'cart', version: 2 }) });
82
+ registry.register('pano-plugin-market', 'market', [makeDef({ name: 'format', scope: 'instance' })]);
83
+ expect(registry.has('market/cart')).toBe(true);
84
+ expect(registry.has('market/cart', 2)).toBe(true);
85
+ expect(registry.has('market/cart', 1)).toBe(false);
86
+ expect(registry.has('market/nope')).toBe(false);
87
+ expect(registry.list()).toEqual([
88
+ { name: 'market/cart', version: 2, pluginId: 'pano-plugin-market', scope: 'app' },
89
+ { name: 'market/format', version: 1, pluginId: 'pano-plugin-market', scope: 'instance' },
90
+ ]);
91
+ expect(registry.pluginIdOf('market')).toBe('pano-plugin-market');
92
+ });
93
+
94
+ test('registering a name again replaces it', () => {
95
+ registry.setHostFactory(() => makeHost());
96
+ registry.register('p', 'market', [makeDef({ version: 1 })]);
97
+ registry.register('p', 'market', [makeDef({ version: 2 })]);
98
+ expect(registry.list()).toHaveLength(1);
99
+ expect(registry.has('market/cart', 2)).toBe(true);
100
+ });
101
+
102
+ test('a definition without create() is skipped, not thrown', () => {
103
+ expect(() => registry.register('p', 'market', [{ name: 'broken', version: 1 }])).not.toThrow();
104
+ expect(registry.has('market/broken')).toBe(false);
105
+ });
106
+ });
107
+
108
+ describe('use: unknown name and version rule', () => {
109
+ beforeEach(() => registry.setHostFactory(() => makeHost()));
110
+
111
+ test('unknown name returns null, warns once, never throws', () => {
112
+ expect(registry.use('market/cart')).toBeNull();
113
+ expect(registry.use('market/cart')).toBeNull();
114
+ expect(warn).toHaveBeenCalledTimes(1);
115
+ expect(warn.mock.calls[0][0]).toContain("controller 'market/cart' is not registered");
116
+ });
117
+
118
+ test('no version wanted returns the registered one', () => {
119
+ registry.register('p', 'market', [makeDef({ version: 3 })]);
120
+ expect(registry.use('market/cart')).not.toBeNull();
121
+ });
122
+
123
+ test('explicit version matches or mismatches', () => {
124
+ registry.register('p', 'market', [makeDef({ version: 2 })]);
125
+ expect(registry.use('market/cart', { version: 2 })).not.toBeNull();
126
+ expect(registry.use('market/cart', { version: 1 })).toBeNull();
127
+ });
128
+
129
+ test('theme pin is used when no explicit version', () => {
130
+ registry.register('p', 'market', [makeDef({ version: 2 })]);
131
+ registry.setThemePins({ 'market/cart': 1 });
132
+ expect(registry.use('market/cart')).toBeNull();
133
+ registry.setThemePins({ 'market/cart': 2 });
134
+ expect(registry.use('market/cart')).not.toBeNull();
135
+ });
136
+
137
+ test('explicit version beats the theme pin', () => {
138
+ registry.register('p', 'market', [makeDef({ version: 2 })]);
139
+ registry.setThemePins({ 'market/cart': 1 });
140
+ expect(registry.use('market/cart', { version: 2 })).not.toBeNull();
141
+ });
142
+
143
+ test('mismatch reports to onMismatch with wanted and available, and warns', () => {
144
+ registry.register('p', 'market', [makeDef({ version: 2 })]);
145
+ registry.setThemePins({ 'market/cart': 1 });
146
+ const seen = [];
147
+ const off = registry.onMismatch((m) => seen.push(m));
148
+ registry.use('market/cart');
149
+ expect(seen).toEqual([{ kind: 'controller', name: 'market/cart', wanted: 1, available: 2 }]);
150
+ expect(warn.mock.calls.at(-1)[0]).toContain("'market/cart' is at version 2, this theme was written for 1");
151
+ off();
152
+ registry.use('market/cart');
153
+ expect(seen).toHaveLength(1);
154
+ });
155
+
156
+ test('a throwing onMismatch listener does not break use()', () => {
157
+ const err = spyOn(console, 'error').mockImplementation(() => {});
158
+ registry.register('p', 'market', [makeDef({ version: 2 })]);
159
+ registry.onMismatch(() => {
160
+ throw new Error('boom');
161
+ });
162
+ expect(registry.use('market/cart', { version: 1 })).toBeNull();
163
+ err.mockRestore();
164
+ });
165
+
166
+ test('load honours the same version rule', async () => {
167
+ registry.register('p', 'market', [makeDef({ version: 2, load: async () => ({ ok: 1 }) })]);
168
+ expect(await registry.load('market/cart', { version: 1 })).toBeNull();
169
+ expect(await registry.load('market/nope')).toBeNull();
170
+ });
171
+ });
172
+
173
+ describe('browser: scopes and eager', () => {
174
+ beforeEach(() => {
175
+ enterBrowser();
176
+ registry.setHostFactory(() => makeHost('browser'));
177
+ });
178
+
179
+ test('app scope is cached per name', () => {
180
+ const d = makeDef();
181
+ registry.register('p', 'market', [d]);
182
+ const a = registry.use('market/cart');
183
+ const b = registry.use('market/cart');
184
+ expect(a).toBe(b);
185
+ expect(d.created).toHaveLength(1);
186
+ });
187
+
188
+ test('instance scope is new per call', () => {
189
+ const d = makeDef({ scope: 'instance' });
190
+ registry.register('p', 'market', [d]);
191
+ expect(registry.use('market/cart')).not.toBe(registry.use('market/cart'));
192
+ expect(d.created).toHaveLength(2);
193
+ });
194
+
195
+ test('params and initial reach create()', () => {
196
+ const d = makeDef({ scope: 'instance' });
197
+ registry.register('p', 'market', [d]);
198
+ const c = registry.use('market/cart', { params: { id: 7 }, initial: { count: 5 } });
199
+ expect(c.params).toEqual({ id: 7 });
200
+ expect(c.get().count).toBe(5);
201
+ });
202
+
203
+ test('eager controller is created inside register() and destroyed on reset()', () => {
204
+ const d = makeDef({ eager: true });
205
+ registry.register('p', 'market', [d]);
206
+ expect(d.created).toHaveLength(1);
207
+ expect(registry.use('market/cart')).toBe(d.created[0]);
208
+ registry.reset();
209
+ expect(d.destroyed).toBe(1);
210
+ expect(registry.has('market/cart')).toBe(false);
211
+ expect(registry.list()).toEqual([]);
212
+ });
213
+
214
+ test('eager waits for the host factory, then starts', () => {
215
+ registry.setHostFactory(null);
216
+ const d = makeDef({ eager: true });
217
+ registry.register('p', 'market', [d]);
218
+ expect(d.created).toHaveLength(0);
219
+ registry.setHostFactory(() => makeHost());
220
+ expect(d.created).toHaveLength(1);
221
+ });
222
+
223
+ test('re-registering destroys the cached controller of that name', () => {
224
+ const first = makeDef();
225
+ registry.register('p', 'market', [first]);
226
+ registry.use('market/cart');
227
+ registry.register('p', 'market', [makeDef()]);
228
+ expect(first.destroyed).toBe(1);
229
+ });
230
+
231
+ test('reset clears the cache so the next register builds a fresh controller', () => {
232
+ const d = makeDef();
233
+ registry.register('p', 'market', [d]);
234
+ const a = registry.use('market/cart');
235
+ registry.reset();
236
+ const d2 = makeDef();
237
+ registry.register('p', 'market', [d2]);
238
+ expect(registry.use('market/cart')).not.toBe(a);
239
+ });
240
+
241
+ test('reset keeps host factory and pins (the engine sets them once)', () => {
242
+ registry.setThemePins({ 'market/cart': 1 });
243
+ registry.reset();
244
+ registry.register('p', 'market', [makeDef({ version: 2 })]);
245
+ expect(registry.use('market/cart')).toBeNull();
246
+ });
247
+
248
+ test('the browser host is built once', () => {
249
+ let calls = 0;
250
+ registry.setHostFactory(() => {
251
+ calls += 1;
252
+ return makeHost();
253
+ });
254
+ registry.register('p', 'market', [makeDef({ scope: 'instance' })]);
255
+ registry.use('market/cart');
256
+ registry.use('market/cart');
257
+ expect(calls).toBe(1);
258
+ });
259
+
260
+ test('ctx.use reaches a sibling of the same plugin on the same host', () => {
261
+ const format = makeDef({ name: 'format' });
262
+ const cart = makeDef({ name: 'cart', scope: 'instance' });
263
+ registry.register('p', 'market', [format, cart]);
264
+ const c = registry.use('market/cart');
265
+ const sib = c.ctx.use('format');
266
+ expect(sib).toBe(registry.use('market/format'));
267
+ expect(c.ctx.use('missing')).toBeNull();
268
+ });
269
+ });
270
+
271
+ describe('server: no cache, host per call from event', () => {
272
+ test('nothing is cached and eager does not start', () => {
273
+ const hosts = [];
274
+ registry.setHostFactory((event) => {
275
+ const h = makeHost(event?.id);
276
+ hosts.push(h);
277
+ return h;
278
+ });
279
+ const d = makeDef({ eager: true });
280
+ registry.register('p', 'market', [d]);
281
+ expect(d.created).toHaveLength(0);
282
+
283
+ const a = registry.use('market/cart', { event: { id: 'r1' } });
284
+ const b = registry.use('market/cart', { event: { id: 'r2' } });
285
+ expect(a).not.toBe(b);
286
+ expect(a.host.tag).toBe('r1');
287
+ expect(b.host.tag).toBe('r2');
288
+ expect(hosts).toHaveLength(2);
289
+ expect(globalThis[KEY].cache.size).toBe(0);
290
+ });
291
+
292
+ test('load gets the host of its own event', async () => {
293
+ registry.setHostFactory((event) => makeHost(event?.id));
294
+ const d = makeDef({ load: async ({ host, params }) => ({ tag: host.tag, params }) });
295
+ registry.register('p', 'market', [d]);
296
+ expect(await registry.load('market/cart', { event: { id: 'r9' }, params: { a: 1 } })).toEqual({
297
+ tag: 'r9',
298
+ params: { a: 1 },
299
+ });
300
+ });
301
+
302
+ test('load without a loader resolves {}', async () => {
303
+ registry.setHostFactory(() => makeHost());
304
+ registry.register('p', 'market', [makeDef()]);
305
+ expect(await registry.load('market/cart', { event: {} })).toEqual({});
306
+ });
307
+
308
+ test('use before a host factory returns null', () => {
309
+ registry.register('p', 'market', [makeDef()]);
310
+ expect(registry.use('market/cart')).toBeNull();
311
+ });
312
+ });
313
+
314
+ describe('sample mode', () => {
315
+ test('use() returns fixed-state sample controllers and leaves the app cache alone', () => {
316
+ enterBrowser();
317
+ registry.setHostFactory(() => makeHost());
318
+ const d = makeDef();
319
+ registry.register('p', 'market', [d]);
320
+
321
+ const real = registry.use('market/cart');
322
+ real.actions.add();
323
+ expect(real.get().count).toBe(1);
324
+ const cacheSize = globalThis[KEY].cache.size;
325
+ const createdBefore = d.created.length;
326
+
327
+ registry.setSamples({ 'market/cart': { count: 9 } });
328
+ const sample = registry.use('market/cart');
329
+ expect(sample).not.toBe(real);
330
+ expect(sample.get().count).toBe(9);
331
+ sample.actions.add('x', 1);
332
+ expect(sample.calls).toEqual([{ name: 'add', args: ['x', 1] }]);
333
+ expect(sample.get().count).toBe(9);
334
+ expect(globalThis[KEY].cache.size).toBe(cacheSize);
335
+
336
+ registry.setSamples(null);
337
+ expect(registry.use('market/cart')).toBe(real);
338
+ expect(real.get().count).toBe(1);
339
+ // the throwaway controller used to read the initial state was destroyed, the real one was not
340
+ expect(d.created.length).toBe(createdBefore + 1);
341
+ expect(d.destroyed).toBe(1);
342
+ });
343
+
344
+ test('a name the samples do not mention still gets a sample controller', () => {
345
+ registry.setHostFactory(() => makeHost());
346
+ registry.register('p', 'market', [makeDef()]);
347
+ registry.setSamples({});
348
+ expect(registry.use('market/cart').get().count).toBe(0);
349
+ });
350
+
351
+ test('createSample on the definition wins when present', () => {
352
+ registry.setHostFactory(() => makeHost());
353
+ const d = makeDef();
354
+ d.createSample = (patch) => ({ custom: true, patch });
355
+ registry.register('p', 'market', [d]);
356
+ registry.setSamples({ 'market/cart': { count: 4 } });
357
+ expect(registry.use('market/cart')).toEqual({ custom: true, patch: { count: 4 } });
358
+ });
359
+ });
360
+
361
+ describe('the exposed object', () => {
362
+ test('controllers has the whole surface of doc 02 section 2', () => {
363
+ for (const k of [
364
+ 'register',
365
+ 'reset',
366
+ 'use',
367
+ 'load',
368
+ 'has',
369
+ 'list',
370
+ 'setHostFactory',
371
+ 'setThemePins',
372
+ 'setSamples',
373
+ 'onMismatch',
374
+ ]) {
375
+ expect(typeof registry.controllers[k]).toBe('function');
376
+ }
377
+ });
378
+ });
@@ -0,0 +1,57 @@
1
+ import { beforeEach, describe, expect, test } from 'bun:test';
2
+ import {
3
+ addListener,
4
+ addPluginListener,
5
+ notificationTextKey,
6
+ onNotificationClick,
7
+ resetPluginListeners,
8
+ } from '../NotificationManager.js';
9
+
10
+ beforeEach(() => resetPluginListeners());
11
+
12
+ describe('NotificationManager (TC-6)', () => {
13
+ test('text key with and without pluginId', () => {
14
+ expect(notificationTextKey({ type: 'X' })).toBe('notifications.X');
15
+ expect(notificationTextKey({ type: 'X', pluginId: 'market' })).toBe(
16
+ 'plugins.market.notifications.X',
17
+ );
18
+ });
19
+ test('plugin listener called, no navigation', () => {
20
+ const calls = [];
21
+ const nav = [];
22
+ addPluginListener('A', (n) => calls.push(n.type));
23
+ onNotificationClick({ type: 'A', details: { href: '/x' } }, (p) => nav.push(p));
24
+ expect(calls).toEqual(['A']);
25
+ expect(nav).toEqual([]);
26
+ });
27
+ test('core listener runs before plugin listener', () => {
28
+ const order = [];
29
+ addListener('B', () => order.push('core'));
30
+ addPluginListener('B', () => order.push('plugin'));
31
+ onNotificationClick({ type: 'B' });
32
+ expect(order).toEqual(['core', 'plugin']);
33
+ });
34
+ test('reset clears plugin listeners', () => {
35
+ const calls = [];
36
+ const nav = [];
37
+ addPluginListener('C', () => calls.push(1));
38
+ resetPluginListeners();
39
+ onNotificationClick({ type: 'C', details: { href: '/y' } }, (p) => nav.push(p));
40
+ expect(calls).toEqual([]);
41
+ expect(nav).toEqual(['/y']);
42
+ });
43
+ test('href navigation only for safe local paths', () => {
44
+ const nav = [];
45
+ const go = (p) => nav.push(p);
46
+ for (const href of ['//evil.com', '/a\\b', '/a\nb', 'https://x.com', 'x', '', undefined, 5]) {
47
+ onNotificationClick({ type: 'D', details: { href } }, go);
48
+ }
49
+ onNotificationClick({ type: 'D' }, go);
50
+ expect(nav).toEqual([]);
51
+ onNotificationClick({ type: 'D', details: { href: '/orders/1?a=b' } }, go);
52
+ expect(nav).toEqual(['/orders/1?a=b']);
53
+ });
54
+ test('no navigate callback does not throw', () => {
55
+ expect(() => onNotificationClick({ type: 'E', details: { href: '/a' } })).not.toThrow();
56
+ });
57
+ });
@@ -0,0 +1,25 @@
1
+ import { describe, expect, mock, test } from 'bun:test';
2
+
3
+ mock.module('$app/paths', () => ({ base: '' }));
4
+ mock.module('../PluginManager.js', () => ({ registeredPages: {} }));
5
+
6
+ const { createFeatureSet } = await import('../PluginAPI.js');
7
+
8
+ describe('createFeatureSet (TC-7)', () => {
9
+ test('has', () => {
10
+ const f = createFeatureSet(['b', 'a']);
11
+ expect(f.has('a')).toBe(true);
12
+ expect(f.has('z')).toBe(false);
13
+ });
14
+ test('sorted list, deduplicated', () => {
15
+ expect(createFeatureSet(['c', 'a', 'b', 'a']).list()).toEqual(['a', 'b', 'c']);
16
+ });
17
+ test('frozen', () => {
18
+ const f = createFeatureSet(['a']);
19
+ expect(Object.isFrozen(f)).toBe(true);
20
+ expect(() => {
21
+ 'use strict';
22
+ f.has = () => true;
23
+ }).toThrow();
24
+ });
25
+ });
@@ -0,0 +1,33 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { RouteMatcher, findMatch } from '../RouteMatcher.js';
3
+
4
+ describe('RouteMatcher decoding (TC-4)', () => {
5
+ test('decodes a [slug] param', () => {
6
+ expect(RouteMatcher.match('/p/[slug]', '/p/caf%C3%A9')).toEqual({ slug: 'café' });
7
+ });
8
+ test('decodes a :param', () => {
9
+ expect(RouteMatcher.match('/p/:slug', '/p/caf%C3%A9')).toEqual({ slug: 'café' });
10
+ });
11
+ test('malformed escape kept raw', () => {
12
+ expect(RouteMatcher.match('/p/[slug]', '/p/%E0%A4%A')).toEqual({ slug: '%E0%A4%A' });
13
+ });
14
+ test('%2F stays inside one segment', () => {
15
+ expect(RouteMatcher.match('/p/[slug]', '/p/a%2Fb')).toEqual({ slug: 'a/b' });
16
+ });
17
+ test('catch-all decodes each segment', () => {
18
+ expect(RouteMatcher.match('/f/[...rest]', '/f/a%20b/caf%C3%A9')).toEqual({ rest: 'a b/café' });
19
+ });
20
+ test('literal segments are not decoded', () => {
21
+ expect(RouteMatcher.match('/caf%C3%A9/[id]', '/café/1')).toBeNull();
22
+ expect(RouteMatcher.match('/caf%C3%A9/[id]', '/caf%C3%A9/1')).toEqual({ id: '1' });
23
+ });
24
+ test('re: groups are not decoded', () => {
25
+ expect(RouteMatcher.match('re:/p/(?<x>.+)', '/p/a%20b')).toEqual({ x: 'a%20b' });
26
+ });
27
+ test('literal route wins over a dynamic one registered first', () => {
28
+ const pages = { '/p/[slug]': { id: 'dyn' }, '/p/new': { id: 'lit' } };
29
+ expect(findMatch(pages, '/p/new').id).toBe('lit');
30
+ expect(findMatch(pages, '/p/new/').id).toBe('lit');
31
+ expect(findMatch(pages, '/p/other')).toEqual({ id: 'dyn', params: { slug: 'other' } });
32
+ });
33
+ });