@selfhelp/shared 1.2.5 → 1.3.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 (64) hide show
  1. package/CHANGELOG.md +341 -326
  2. package/LICENSE +381 -381
  3. package/README.md +57 -57
  4. package/dist/index.d.mts +25 -1
  5. package/dist/index.d.ts +25 -1
  6. package/dist/index.js +2 -0
  7. package/dist/index.mjs +2 -0
  8. package/package.json +102 -102
  9. package/src/api/__tests__/endpoints.test.ts +45 -0
  10. package/src/api/endpoints.ts +2 -0
  11. package/src/api/index.ts +5 -5
  12. package/src/assets/__tests__/resolveAssetUrl.test.ts +61 -61
  13. package/src/assets/index.ts +5 -5
  14. package/src/assets/resolveAssetUrl.ts +44 -44
  15. package/src/cms-classes/__tests__/classify.test.ts +63 -63
  16. package/src/cms-classes/allow-list.ts +158 -158
  17. package/src/cms-classes/classify.ts +90 -90
  18. package/src/cms-classes/index.ts +7 -7
  19. package/src/cms-classes/remap.ts +44 -44
  20. package/src/condition/__tests__/evaluate.test.ts +54 -54
  21. package/src/condition/evaluate.ts +121 -121
  22. package/src/condition/index.ts +5 -5
  23. package/src/index.ts +25 -25
  24. package/src/interpolation/__tests__/replaceCalcedValues.test.ts +41 -41
  25. package/src/interpolation/index.ts +5 -5
  26. package/src/interpolation/replaceCalcedValues.ts +27 -27
  27. package/src/plugin-sdk/define.ts +89 -89
  28. package/src/plugin-sdk/index.ts +103 -103
  29. package/src/plugin-sdk/lock.ts +124 -124
  30. package/src/plugin-sdk/manifest.ts +307 -307
  31. package/src/plugin-sdk/realtime.ts +277 -277
  32. package/src/plugin-sdk/registry.ts +105 -105
  33. package/src/plugin-sdk/runtime-shim.ts +140 -140
  34. package/src/plugin-sdk/types.ts +315 -315
  35. package/src/plugin-sdk/version.ts +209 -209
  36. package/src/registry/__tests__/registry.test.ts +135 -135
  37. package/src/registry/index.ts +94 -94
  38. package/src/registry/styles.registry.ts +295 -295
  39. package/src/testing/__tests__/testing-kit.test.ts +228 -228
  40. package/src/testing/index.ts +483 -483
  41. package/src/theme/index.ts +6 -6
  42. package/src/theme/tailwind.ts +73 -73
  43. package/src/theme/tokens.ts +177 -177
  44. package/src/types/api/auth.ts +22 -0
  45. package/src/types/api/envelope.ts +34 -34
  46. package/src/types/api/forms.ts +76 -76
  47. package/src/types/api/index.ts +9 -9
  48. package/src/types/api/language.ts +12 -12
  49. package/src/types/api/page.ts +9 -9
  50. package/src/types/index.ts +17 -17
  51. package/src/types/mantine/common.ts +142 -142
  52. package/src/types/pages.ts +107 -107
  53. package/src/types/styles/auth.ts +184 -184
  54. package/src/types/styles/base.ts +72 -72
  55. package/src/types/styles/composite.ts +131 -131
  56. package/src/types/styles/forms.ts +439 -439
  57. package/src/types/styles/interactive.ts +165 -165
  58. package/src/types/styles/layout.ts +184 -184
  59. package/src/types/styles/media.ts +68 -68
  60. package/src/types/styles/typography.ts +118 -118
  61. package/src/types/styles/unknown.ts +220 -220
  62. package/src/utils/__tests__/transformPageData.test.ts +69 -69
  63. package/src/utils/spacing.ts +64 -64
  64. package/src/utils/transformPageData.ts +99 -99
@@ -1,135 +1,135 @@
1
- /*
2
- * SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- * SPDX-License-Identifier: MPL-2.0
4
- */
5
-
6
- import { afterEach, describe, expect, it } from 'vitest';
7
- import {
8
- BASE_STYLE_REGISTRY,
9
- STYLE_REGISTRY,
10
- extendStyleRegistry,
11
- getMergedStyleRegistry,
12
- getPluginStyleRegistry,
13
- getRegistryEntry,
14
- getRegistryEntryAny,
15
- getStylePluginId,
16
- isKnownStyleName,
17
- isRegisteredStyleName,
18
- _resetPluginStyleRegistry,
19
- type IStyleRegistryEntry,
20
- } from '../index';
21
-
22
- /**
23
- * Contract coverage for the shared style registry — the runtime catalog the
24
- * frontend, mobile and plugin renderers all dispatch through. A renderer that
25
- * mis-resolves a style name (or crashes on an unknown one) breaks every page,
26
- * so these tests pin: the core catalog is present and well-shaped, lookups are
27
- * safe for unknown keys, and the plugin extension lifecycle (register → resolve
28
- * → collision-guard → dispose) behaves.
29
- */
30
-
31
- const ALLOWED_CATEGORIES = new Set<IStyleRegistryEntry['category']>([
32
- 'auth', 'layout', 'typography', 'media', 'interactive', 'forms', 'composite', 'plugin',
33
- ]);
34
-
35
- const PLUGIN_ID = 'qa-shp-registry-test';
36
- const pluginEntry: IStyleRegistryEntry = {
37
- description: 'QA plugin style for registry tests',
38
- category: 'plugin',
39
- frontendOnly: true,
40
- canHaveChildren: false,
41
- };
42
-
43
- describe('shared style registry', () => {
44
- // Plugin contributions live in module-level mutable state; reset after
45
- // every test so cases stay isolated and order-independent (Testing Rule 14/15).
46
- afterEach(() => {
47
- _resetPluginStyleRegistry();
48
- });
49
-
50
- describe('core catalog', () => {
51
- it('registers the expected representative core styles', () => {
52
- for (const name of ['login', 'container', 'text', 'button', 'form-record', 'accordion']) {
53
- expect(isKnownStyleName(name)).toBe(true);
54
- expect(getRegistryEntry(name)).toBeDefined();
55
- }
56
- });
57
-
58
- it('exposes STYLE_REGISTRY as the same closed core map', () => {
59
- expect(STYLE_REGISTRY).toBe(BASE_STYLE_REGISTRY);
60
- });
61
-
62
- it('keeps every core entry shape stable for renderer consumers', () => {
63
- for (const [name, entry] of Object.entries(BASE_STYLE_REGISTRY)) {
64
- expect(typeof entry.description, `${name}.description`).toBe('string');
65
- expect(entry.description.length, `${name}.description not empty`).toBeGreaterThan(0);
66
- expect(ALLOWED_CATEGORIES.has(entry.category), `${name}.category`).toBe(true);
67
- expect(entry.frontendOnly, `${name}.frontendOnly`).toBe(true);
68
- expect(typeof entry.canHaveChildren, `${name}.canHaveChildren`).toBe('boolean');
69
- }
70
- });
71
-
72
- it('reflects the documented canHaveChildren contract', () => {
73
- // Leaf style vs container style — renderers short-circuit children
74
- // traversal on this flag, so a flip is a real behaviour change.
75
- expect(getRegistryEntry('text')?.canHaveChildren).toBe(false);
76
- expect(getRegistryEntry('container')?.canHaveChildren).toBe(true);
77
- });
78
- });
79
-
80
- describe('lookups for unknown keys are safe', () => {
81
- const unknown = 'qa-not-a-real-style';
82
-
83
- it('returns undefined / false instead of throwing', () => {
84
- expect(getRegistryEntry(unknown)).toBeUndefined();
85
- expect(getRegistryEntryAny(unknown)).toBeUndefined();
86
- expect(isKnownStyleName(unknown)).toBe(false);
87
- expect(isRegisteredStyleName(unknown)).toBe(false);
88
- expect(getStylePluginId(unknown)).toBeUndefined();
89
- });
90
- });
91
-
92
- describe('plugin extension lifecycle', () => {
93
- it('registers, resolves and tags a plugin style without polluting the core map', () => {
94
- const dispose = extendStyleRegistry(
95
- { 'qa-survey': pluginEntry },
96
- { pluginId: PLUGIN_ID, pluginVersion: '1.0.0' },
97
- );
98
-
99
- // Resolvable via the merged/any lookups...
100
- expect(getRegistryEntryAny('qa-survey')).toMatchObject({ pluginId: PLUGIN_ID, category: 'plugin' });
101
- expect(isRegisteredStyleName('qa-survey')).toBe(true);
102
- expect(getStylePluginId('qa-survey')).toBe(PLUGIN_ID);
103
- expect(getPluginStyleRegistry()['qa-survey']).toBeDefined();
104
- expect(getMergedStyleRegistry()['qa-survey']).toBeDefined();
105
- // ...but it is NOT a core style.
106
- expect(isKnownStyleName('qa-survey')).toBe(false);
107
- expect(getRegistryEntry('qa-survey')).toBeUndefined();
108
-
109
- dispose();
110
- expect(isRegisteredStyleName('qa-survey')).toBe(false);
111
- expect(getPluginStyleRegistry()['qa-survey']).toBeUndefined();
112
- });
113
-
114
- it('rejects a plugin trying to shadow a core style', () => {
115
- expect(() => extendStyleRegistry(
116
- { container: pluginEntry },
117
- { pluginId: PLUGIN_ID },
118
- )).toThrow(/core style/i);
119
- });
120
-
121
- it('rejects a second plugin claiming an already-owned style', () => {
122
- extendStyleRegistry({ 'qa-shared-style': pluginEntry }, { pluginId: PLUGIN_ID });
123
- expect(() => extendStyleRegistry(
124
- { 'qa-shared-style': pluginEntry },
125
- { pluginId: 'qa-other-plugin' },
126
- )).toThrow(/already owned/i);
127
- });
128
-
129
- it('merged registry equals core when no plugins are registered', () => {
130
- expect(Object.keys(getMergedStyleRegistry()).sort())
131
- .toEqual(Object.keys(BASE_STYLE_REGISTRY).sort());
132
- expect(getPluginStyleRegistry()).toEqual({});
133
- });
134
- });
135
- });
1
+ /*
2
+ * SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ * SPDX-License-Identifier: MPL-2.0
4
+ */
5
+
6
+ import { afterEach, describe, expect, it } from 'vitest';
7
+ import {
8
+ BASE_STYLE_REGISTRY,
9
+ STYLE_REGISTRY,
10
+ extendStyleRegistry,
11
+ getMergedStyleRegistry,
12
+ getPluginStyleRegistry,
13
+ getRegistryEntry,
14
+ getRegistryEntryAny,
15
+ getStylePluginId,
16
+ isKnownStyleName,
17
+ isRegisteredStyleName,
18
+ _resetPluginStyleRegistry,
19
+ type IStyleRegistryEntry,
20
+ } from '../index';
21
+
22
+ /**
23
+ * Contract coverage for the shared style registry — the runtime catalog the
24
+ * frontend, mobile and plugin renderers all dispatch through. A renderer that
25
+ * mis-resolves a style name (or crashes on an unknown one) breaks every page,
26
+ * so these tests pin: the core catalog is present and well-shaped, lookups are
27
+ * safe for unknown keys, and the plugin extension lifecycle (register → resolve
28
+ * → collision-guard → dispose) behaves.
29
+ */
30
+
31
+ const ALLOWED_CATEGORIES = new Set<IStyleRegistryEntry['category']>([
32
+ 'auth', 'layout', 'typography', 'media', 'interactive', 'forms', 'composite', 'plugin',
33
+ ]);
34
+
35
+ const PLUGIN_ID = 'qa-shp-registry-test';
36
+ const pluginEntry: IStyleRegistryEntry = {
37
+ description: 'QA plugin style for registry tests',
38
+ category: 'plugin',
39
+ frontendOnly: true,
40
+ canHaveChildren: false,
41
+ };
42
+
43
+ describe('shared style registry', () => {
44
+ // Plugin contributions live in module-level mutable state; reset after
45
+ // every test so cases stay isolated and order-independent (Testing Rule 14/15).
46
+ afterEach(() => {
47
+ _resetPluginStyleRegistry();
48
+ });
49
+
50
+ describe('core catalog', () => {
51
+ it('registers the expected representative core styles', () => {
52
+ for (const name of ['login', 'container', 'text', 'button', 'form-record', 'accordion']) {
53
+ expect(isKnownStyleName(name)).toBe(true);
54
+ expect(getRegistryEntry(name)).toBeDefined();
55
+ }
56
+ });
57
+
58
+ it('exposes STYLE_REGISTRY as the same closed core map', () => {
59
+ expect(STYLE_REGISTRY).toBe(BASE_STYLE_REGISTRY);
60
+ });
61
+
62
+ it('keeps every core entry shape stable for renderer consumers', () => {
63
+ for (const [name, entry] of Object.entries(BASE_STYLE_REGISTRY)) {
64
+ expect(typeof entry.description, `${name}.description`).toBe('string');
65
+ expect(entry.description.length, `${name}.description not empty`).toBeGreaterThan(0);
66
+ expect(ALLOWED_CATEGORIES.has(entry.category), `${name}.category`).toBe(true);
67
+ expect(entry.frontendOnly, `${name}.frontendOnly`).toBe(true);
68
+ expect(typeof entry.canHaveChildren, `${name}.canHaveChildren`).toBe('boolean');
69
+ }
70
+ });
71
+
72
+ it('reflects the documented canHaveChildren contract', () => {
73
+ // Leaf style vs container style — renderers short-circuit children
74
+ // traversal on this flag, so a flip is a real behaviour change.
75
+ expect(getRegistryEntry('text')?.canHaveChildren).toBe(false);
76
+ expect(getRegistryEntry('container')?.canHaveChildren).toBe(true);
77
+ });
78
+ });
79
+
80
+ describe('lookups for unknown keys are safe', () => {
81
+ const unknown = 'qa-not-a-real-style';
82
+
83
+ it('returns undefined / false instead of throwing', () => {
84
+ expect(getRegistryEntry(unknown)).toBeUndefined();
85
+ expect(getRegistryEntryAny(unknown)).toBeUndefined();
86
+ expect(isKnownStyleName(unknown)).toBe(false);
87
+ expect(isRegisteredStyleName(unknown)).toBe(false);
88
+ expect(getStylePluginId(unknown)).toBeUndefined();
89
+ });
90
+ });
91
+
92
+ describe('plugin extension lifecycle', () => {
93
+ it('registers, resolves and tags a plugin style without polluting the core map', () => {
94
+ const dispose = extendStyleRegistry(
95
+ { 'qa-survey': pluginEntry },
96
+ { pluginId: PLUGIN_ID, pluginVersion: '1.0.0' },
97
+ );
98
+
99
+ // Resolvable via the merged/any lookups...
100
+ expect(getRegistryEntryAny('qa-survey')).toMatchObject({ pluginId: PLUGIN_ID, category: 'plugin' });
101
+ expect(isRegisteredStyleName('qa-survey')).toBe(true);
102
+ expect(getStylePluginId('qa-survey')).toBe(PLUGIN_ID);
103
+ expect(getPluginStyleRegistry()['qa-survey']).toBeDefined();
104
+ expect(getMergedStyleRegistry()['qa-survey']).toBeDefined();
105
+ // ...but it is NOT a core style.
106
+ expect(isKnownStyleName('qa-survey')).toBe(false);
107
+ expect(getRegistryEntry('qa-survey')).toBeUndefined();
108
+
109
+ dispose();
110
+ expect(isRegisteredStyleName('qa-survey')).toBe(false);
111
+ expect(getPluginStyleRegistry()['qa-survey']).toBeUndefined();
112
+ });
113
+
114
+ it('rejects a plugin trying to shadow a core style', () => {
115
+ expect(() => extendStyleRegistry(
116
+ { container: pluginEntry },
117
+ { pluginId: PLUGIN_ID },
118
+ )).toThrow(/core style/i);
119
+ });
120
+
121
+ it('rejects a second plugin claiming an already-owned style', () => {
122
+ extendStyleRegistry({ 'qa-shared-style': pluginEntry }, { pluginId: PLUGIN_ID });
123
+ expect(() => extendStyleRegistry(
124
+ { 'qa-shared-style': pluginEntry },
125
+ { pluginId: 'qa-other-plugin' },
126
+ )).toThrow(/already owned/i);
127
+ });
128
+
129
+ it('merged registry equals core when no plugins are registered', () => {
130
+ expect(Object.keys(getMergedStyleRegistry()).sort())
131
+ .toEqual(Object.keys(BASE_STYLE_REGISTRY).sort());
132
+ expect(getPluginStyleRegistry()).toEqual({});
133
+ });
134
+ });
135
+ });
@@ -1,94 +1,94 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- import type { TStyleName } from '../types/styles/unknown';
6
- import {
7
- BASE_STYLE_REGISTRY,
8
- getMergedStyleRegistry,
9
- getPluginStyleRegistry,
10
- type IPluginStyleRegistryEntry,
11
- type IStyleRegistryEntry,
12
- } from './styles.registry';
13
-
14
- export * from './styles.registry';
15
-
16
- /**
17
- * Open style-name type: a core `TStyleName` literal OR any plugin style
18
- * name. The `string & {}` keeps autocomplete for core names while still
19
- * accepting plugin keys at type level.
20
- */
21
- export type TAnyStyleName = TStyleName | (string & {});
22
-
23
- /**
24
- * Helper for renderers: produces a typed map shape that requires every
25
- * core registry key to be implemented. Pass this to your dispatcher and
26
- * let TypeScript enforce 100% style coverage for core styles. Plugin
27
- * styles are dispatched through a separate `IPluginStyleImplMap`.
28
- *
29
- * type IStyleImpls = TStyleImplMap<React.FC<{ style: TStyle }>>;
30
- *
31
- * `keyof TStyleImplMap<...>` is exactly the union of *core* style names.
32
- */
33
- export type TStyleImplMap<TImpl> = {
34
- readonly [K in TStyleName]: TImpl;
35
- };
36
-
37
- /**
38
- * Runtime-extensible map: plugin-contributed style implementations
39
- * keyed by plugin style name. Renderers merge this with their
40
- * `TStyleImplMap<TImpl>` at boot.
41
- */
42
- export type TPluginStyleImplMap<TImpl> = Record<string, TImpl>;
43
-
44
- /**
45
- * Get an entry from the **core** registry. Returns undefined if the
46
- * `style_name` is not a known core style. Use `getRegistryEntryAny()`
47
- * to also resolve plugin styles.
48
- */
49
- export function getRegistryEntry(name: string): IStyleRegistryEntry | undefined {
50
- if (Object.prototype.hasOwnProperty.call(BASE_STYLE_REGISTRY, name)) {
51
- return (BASE_STYLE_REGISTRY as Record<string, IStyleRegistryEntry>)[name];
52
- }
53
- return undefined;
54
- }
55
-
56
- /**
57
- * Get an entry from either the core or the plugin registry. Returns
58
- * undefined if the `style_name` is not registered (the renderer should
59
- * fall back to `UnknownStyle`).
60
- */
61
- export function getRegistryEntryAny(
62
- name: string,
63
- ): IStyleRegistryEntry | IPluginStyleRegistryEntry | undefined {
64
- const merged = getMergedStyleRegistry();
65
- return merged[name];
66
- }
67
-
68
- /**
69
- * Type guard to narrow a string to `TStyleName` (core styles only).
70
- */
71
- export function isKnownStyleName(name: string): name is TStyleName {
72
- return Object.prototype.hasOwnProperty.call(BASE_STYLE_REGISTRY, name);
73
- }
74
-
75
- /**
76
- * Type guard for any registered style — core or plugin.
77
- */
78
- export function isRegisteredStyleName(name: string): boolean {
79
- if (Object.prototype.hasOwnProperty.call(BASE_STYLE_REGISTRY, name)) {
80
- return true;
81
- }
82
- return Object.prototype.hasOwnProperty.call(getPluginStyleRegistry(), name);
83
- }
84
-
85
- /**
86
- * For a known plugin style, return the owning `pluginId`. Useful for
87
- * mobile to construct the "Open on web" fallback link when the plugin
88
- * style is not bundled in the current build.
89
- */
90
- export function getStylePluginId(name: string): string | undefined {
91
- const plugin = getPluginStyleRegistry()[name];
92
- return plugin?.pluginId;
93
- }
94
-
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ import type { TStyleName } from '../types/styles/unknown';
6
+ import {
7
+ BASE_STYLE_REGISTRY,
8
+ getMergedStyleRegistry,
9
+ getPluginStyleRegistry,
10
+ type IPluginStyleRegistryEntry,
11
+ type IStyleRegistryEntry,
12
+ } from './styles.registry';
13
+
14
+ export * from './styles.registry';
15
+
16
+ /**
17
+ * Open style-name type: a core `TStyleName` literal OR any plugin style
18
+ * name. The `string & {}` keeps autocomplete for core names while still
19
+ * accepting plugin keys at type level.
20
+ */
21
+ export type TAnyStyleName = TStyleName | (string & {});
22
+
23
+ /**
24
+ * Helper for renderers: produces a typed map shape that requires every
25
+ * core registry key to be implemented. Pass this to your dispatcher and
26
+ * let TypeScript enforce 100% style coverage for core styles. Plugin
27
+ * styles are dispatched through a separate `IPluginStyleImplMap`.
28
+ *
29
+ * type IStyleImpls = TStyleImplMap<React.FC<{ style: TStyle }>>;
30
+ *
31
+ * `keyof TStyleImplMap<...>` is exactly the union of *core* style names.
32
+ */
33
+ export type TStyleImplMap<TImpl> = {
34
+ readonly [K in TStyleName]: TImpl;
35
+ };
36
+
37
+ /**
38
+ * Runtime-extensible map: plugin-contributed style implementations
39
+ * keyed by plugin style name. Renderers merge this with their
40
+ * `TStyleImplMap<TImpl>` at boot.
41
+ */
42
+ export type TPluginStyleImplMap<TImpl> = Record<string, TImpl>;
43
+
44
+ /**
45
+ * Get an entry from the **core** registry. Returns undefined if the
46
+ * `style_name` is not a known core style. Use `getRegistryEntryAny()`
47
+ * to also resolve plugin styles.
48
+ */
49
+ export function getRegistryEntry(name: string): IStyleRegistryEntry | undefined {
50
+ if (Object.prototype.hasOwnProperty.call(BASE_STYLE_REGISTRY, name)) {
51
+ return (BASE_STYLE_REGISTRY as Record<string, IStyleRegistryEntry>)[name];
52
+ }
53
+ return undefined;
54
+ }
55
+
56
+ /**
57
+ * Get an entry from either the core or the plugin registry. Returns
58
+ * undefined if the `style_name` is not registered (the renderer should
59
+ * fall back to `UnknownStyle`).
60
+ */
61
+ export function getRegistryEntryAny(
62
+ name: string,
63
+ ): IStyleRegistryEntry | IPluginStyleRegistryEntry | undefined {
64
+ const merged = getMergedStyleRegistry();
65
+ return merged[name];
66
+ }
67
+
68
+ /**
69
+ * Type guard to narrow a string to `TStyleName` (core styles only).
70
+ */
71
+ export function isKnownStyleName(name: string): name is TStyleName {
72
+ return Object.prototype.hasOwnProperty.call(BASE_STYLE_REGISTRY, name);
73
+ }
74
+
75
+ /**
76
+ * Type guard for any registered style — core or plugin.
77
+ */
78
+ export function isRegisteredStyleName(name: string): boolean {
79
+ if (Object.prototype.hasOwnProperty.call(BASE_STYLE_REGISTRY, name)) {
80
+ return true;
81
+ }
82
+ return Object.prototype.hasOwnProperty.call(getPluginStyleRegistry(), name);
83
+ }
84
+
85
+ /**
86
+ * For a known plugin style, return the owning `pluginId`. Useful for
87
+ * mobile to construct the "Open on web" fallback link when the plugin
88
+ * style is not bundled in the current build.
89
+ */
90
+ export function getStylePluginId(name: string): string | undefined {
91
+ const plugin = getPluginStyleRegistry()[name];
92
+ return plugin?.pluginId;
93
+ }
94
+