@selfhelp/shared 1.2.1 → 1.2.3

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.
@@ -0,0 +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
+ });
@@ -0,0 +1,228 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ import { describe, it, expect } from 'vitest';
6
+ import type { IPluginManifest } from '../../plugin-sdk';
7
+ import {
8
+ CERTIFICATION_CHECKS,
9
+ CERTIFICATION_KIT_VERSION,
10
+ checkCapabilitiesVsTrustLevel,
11
+ checkCompatibilityShape,
12
+ checkDbNaming,
13
+ checkLookupOwnership,
14
+ checkManifestValid,
15
+ definePluginCertification,
16
+ mockMercureHub,
17
+ seedFromLockFile,
18
+ } from '../index';
19
+
20
+ /** A minimal but valid `official` manifest modelled on the SurveyJS plugin. */
21
+ function validManifest(): IPluginManifest {
22
+ return {
23
+ id: 'sh2-shp-survey-js',
24
+ name: 'SurveyJS',
25
+ version: '0.2.20',
26
+ pluginApiVersion: '1.1',
27
+ compatibility: {
28
+ selfhelp: '>=8.0.0-dev <9.0.0',
29
+ php: '^8.4',
30
+ node: '^22',
31
+ react: '^19',
32
+ },
33
+ security: {
34
+ trustLevel: 'official',
35
+ capabilities: [
36
+ 'backendBundle',
37
+ 'databaseMigrations',
38
+ 'readDataTables',
39
+ 'writeDataTables',
40
+ 'lookupOwnGroup',
41
+ 'frontendStyles',
42
+ 'adminPages',
43
+ 'realtimePublish',
44
+ ],
45
+ },
46
+ backend: {
47
+ package: 'humdek/sh2-shp-survey-js',
48
+ bundleClass: 'Humdek\\SurveyJsBundle\\HumdekSurveyJsBundle',
49
+ migrationsNamespace: 'Humdek\\SurveyJsBundle\\Migrations',
50
+ },
51
+ adminPages: [{ slug: 'surveys', label: 'SurveyJS', permission: 'surveyjs.surveys.manage' }],
52
+ realtimeTopics: [{ key: 'surveys/{id}/responses', description: 'responses' }],
53
+ styles: [{ name: 'surveyjs', description: 'survey', canHaveChildren: false }],
54
+ dataAccess: {
55
+ ownedTables: ['surveys', 'survey_runs', 'survey_versions'],
56
+ ownedDataTablePrefix: 'sh2_surveyjs_',
57
+ read: ['data_tables', 'lookups'],
58
+ write: ['data_tables', 'data_cells'],
59
+ },
60
+ lookups: {
61
+ extends: [
62
+ {
63
+ typeCode: 'surveyJsTheme',
64
+ ownership: 'plugin_owned',
65
+ entries: [{ code: 'default', value: 'Default' }],
66
+ },
67
+ ],
68
+ },
69
+ };
70
+ }
71
+
72
+ describe('plugin certification kit (contract)', () => {
73
+ it('exposes a stable 1.0.0 kit version + ordered static check list', () => {
74
+ expect(CERTIFICATION_KIT_VERSION).toBe('1.0.0');
75
+ expect(CERTIFICATION_CHECKS).toEqual([
76
+ 'manifest-valid',
77
+ 'capabilities-vs-trust-level',
78
+ 'compatibility-shape',
79
+ 'lookup-ownership',
80
+ 'db-naming',
81
+ ]);
82
+ });
83
+
84
+ it('definePluginCertification returns a suite that runs against a manifest', () => {
85
+ const suite = definePluginCertification({ pluginId: 'sh2-shp-survey-js' });
86
+ expect(suite.pluginId).toBe('sh2-shp-survey-js');
87
+ expect(suite.kitVersion).toBe(CERTIFICATION_KIT_VERSION);
88
+ expect(suite.checks).toEqual(CERTIFICATION_CHECKS);
89
+
90
+ const report = suite.run(validManifest());
91
+ expect(report.passed).toBe(true);
92
+ expect(report.results.map((r) => r.name)).toEqual(CERTIFICATION_CHECKS);
93
+ expect(report.results.every((r) => r.passed)).toBe(true);
94
+ });
95
+
96
+ it('definePluginCertification rejects a missing pluginId', () => {
97
+ expect(() => definePluginCertification({ pluginId: '' })).toThrow(/pluginId is required/);
98
+ });
99
+ });
100
+
101
+ describe('certification checks produce real, specific findings', () => {
102
+ it('manifest-valid flags missing required fields + bad version', () => {
103
+ const result = checkManifestValid({
104
+ ...validManifest(),
105
+ id: '',
106
+ version: 'not-semver',
107
+ });
108
+ expect(result.passed).toBe(false);
109
+ expect(result.errors.some((e) => e.includes('id is required'))).toBe(true);
110
+ expect(result.errors.some((e) => e.includes('not valid semver'))).toBe(true);
111
+ });
112
+
113
+ it('capabilities-vs-trust-level rejects an untrusted plugin shipping a bundle', () => {
114
+ const manifest = validManifest();
115
+ const result = checkCapabilitiesVsTrustLevel({
116
+ ...manifest,
117
+ security: { trustLevel: 'untrusted', capabilities: ['backendBundle', 'databaseMigrations'] },
118
+ });
119
+ expect(result.passed).toBe(false);
120
+ expect(result.errors.some((e) => e.includes("untrusted plugins may not declare capability 'backendBundle'"))).toBe(
121
+ true,
122
+ );
123
+ });
124
+
125
+ it('capabilities-vs-trust-level flags a declared surface missing its capability', () => {
126
+ const manifest = validManifest();
127
+ const result = checkCapabilitiesVsTrustLevel({
128
+ ...manifest,
129
+ security: { trustLevel: 'official', capabilities: [] },
130
+ });
131
+ expect(result.passed).toBe(false);
132
+ expect(result.errors.some((e) => e.includes("'backendBundle'"))).toBe(true);
133
+ expect(result.errors.some((e) => e.includes("'realtimePublish'"))).toBe(true);
134
+ expect(result.errors.some((e) => e.includes("'lookupOwnGroup'"))).toBe(true);
135
+ });
136
+
137
+ it('capabilities-vs-trust-level flags an unknown capability', () => {
138
+ const manifest = validManifest();
139
+ const result = checkCapabilitiesVsTrustLevel({
140
+ ...manifest,
141
+ security: { trustLevel: 'official', capabilities: [...manifest.security.capabilities, 'timeTravel'] },
142
+ } as unknown as IPluginManifest);
143
+ expect(result.passed).toBe(false);
144
+ expect(result.errors.some((e) => e.includes("unknown capability 'timeTravel'"))).toBe(true);
145
+ });
146
+
147
+ it('compatibility-shape rejects the per-surface object shape', () => {
148
+ const manifest = validManifest();
149
+ const result = checkCompatibilityShape({
150
+ ...manifest,
151
+ compatibility: {
152
+ selfhelp: { backend: '^8.0', shared: '^8.0' },
153
+ } as unknown as IPluginManifest['compatibility'],
154
+ });
155
+ expect(result.passed).toBe(false);
156
+ expect(result.errors.some((e) => e.includes('single-range shape'))).toBe(true);
157
+ });
158
+
159
+ it('compatibility-shape accepts the adopted single-range shape', () => {
160
+ expect(checkCompatibilityShape(validManifest()).passed).toBe(true);
161
+ });
162
+
163
+ it('lookup-ownership rejects an invalid ownership + empty entries', () => {
164
+ const manifest = validManifest();
165
+ const result = checkLookupOwnership({
166
+ ...manifest,
167
+ lookups: {
168
+ extends: [{ typeCode: 'surveyJsTheme', ownership: 'world_owned' as never, entries: [] }],
169
+ },
170
+ });
171
+ expect(result.passed).toBe(false);
172
+ expect(result.errors.some((e) => e.includes('invalid ownership'))).toBe(true);
173
+ expect(result.errors.some((e) => e.includes('at least one entry'))).toBe(true);
174
+ });
175
+
176
+ it('db-naming rejects non snake_case owned tables and a bad prefix', () => {
177
+ const manifest = validManifest();
178
+ const result = checkDbNaming({
179
+ ...manifest,
180
+ dataAccess: {
181
+ ownedTables: ['SurveyRuns', 'survey runs'],
182
+ ownedDataTablePrefix: 'SurveyJS',
183
+ },
184
+ });
185
+ expect(result.passed).toBe(false);
186
+ expect(result.errors.some((e) => e.includes("owned table 'SurveyRuns'"))).toBe(true);
187
+ expect(result.errors.some((e) => e.includes('ownedDataTablePrefix'))).toBe(true);
188
+ });
189
+ });
190
+
191
+ describe('mockMercureHub', () => {
192
+ it('records published topics + payloads and supports counting/reset (no polling)', () => {
193
+ const hub = mockMercureHub();
194
+ hub.publish('plugin/sh2-shp-survey-js/progress', { pct: 10 });
195
+ hub.publish('plugin/sh2-shp-survey-js/progress', { pct: 100 });
196
+ hub.publish('plugin/sh2-shp-survey-js/done');
197
+
198
+ expect(hub.updates).toHaveLength(3);
199
+ expect(hub.publishedTopics()).toEqual([
200
+ 'plugin/sh2-shp-survey-js/progress',
201
+ 'plugin/sh2-shp-survey-js/done',
202
+ ]);
203
+ expect(hub.countForTopic('plugin/sh2-shp-survey-js/progress')).toBe(2);
204
+
205
+ hub.reset();
206
+ expect(hub.updates).toHaveLength(0);
207
+ });
208
+ });
209
+
210
+ describe('seedFromLockFile', () => {
211
+ it('extracts plugin id + version pairs from a lock document', () => {
212
+ const seed = seedFromLockFile({
213
+ plugins: {
214
+ 'sh2-shp-survey-js': { version: '1.1.0' },
215
+ 'sh2-shp-llm': { version: '2.0.1' },
216
+ },
217
+ });
218
+ expect(seed.plugins).toEqual([
219
+ { pluginId: 'sh2-shp-survey-js', version: '1.1.0' },
220
+ { pluginId: 'sh2-shp-llm', version: '2.0.1' },
221
+ ]);
222
+ });
223
+
224
+ it('tolerates a missing/empty plugins map', () => {
225
+ expect(seedFromLockFile({}).plugins).toEqual([]);
226
+ expect(seedFromLockFile(null).plugins).toEqual([]);
227
+ });
228
+ });