@pie-qti/item-player 0.1.4 → 0.1.5

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 (148) hide show
  1. package/README.md +21 -18
  2. package/dist/catalog/applyGlossaryTriggers.d.ts +15 -0
  3. package/dist/catalog/applyGlossaryTriggers.d.ts.map +1 -0
  4. package/dist/catalog/applyGlossaryTriggers.js +193 -0
  5. package/dist/catalog/catalogExtractor.d.ts +18 -0
  6. package/dist/catalog/catalogExtractor.d.ts.map +1 -0
  7. package/dist/catalog/catalogExtractor.js +114 -0
  8. package/dist/catalog/catalogLookup.d.ts +18 -0
  9. package/dist/catalog/catalogLookup.d.ts.map +1 -0
  10. package/dist/catalog/catalogLookup.js +44 -0
  11. package/dist/catalog/glossaryAction.d.ts +17 -0
  12. package/dist/catalog/glossaryAction.d.ts.map +1 -0
  13. package/dist/catalog/glossaryAction.js +21 -0
  14. package/dist/catalog/types.d.ts +26 -0
  15. package/dist/catalog/types.d.ts.map +1 -0
  16. package/dist/catalog/types.js +6 -0
  17. package/dist/core/ComponentRegistry.d.ts +9 -0
  18. package/dist/core/ComponentRegistry.d.ts.map +1 -1
  19. package/dist/core/ComponentRegistry.js +14 -0
  20. package/dist/core/ItemRenderer.d.ts.map +1 -1
  21. package/dist/core/ItemRenderer.js +3 -0
  22. package/dist/core/Player.d.ts +52 -0
  23. package/dist/core/Player.d.ts.map +1 -1
  24. package/dist/core/Player.js +158 -4
  25. package/dist/core/rolePolicy.d.ts +16 -0
  26. package/dist/core/rolePolicy.d.ts.map +1 -0
  27. package/dist/core/rolePolicy.js +15 -0
  28. package/dist/core/urlPolicy.d.ts.map +1 -1
  29. package/dist/core/urlPolicy.js +5 -2
  30. package/dist/element.d.ts +22 -0
  31. package/dist/element.js +5327 -0
  32. package/dist/element.js.map +1 -0
  33. package/dist/extraction/ExtractionRegistry.d.ts.map +1 -1
  34. package/dist/extraction/ExtractionRegistry.js +3 -1
  35. package/dist/extraction/extractors/associateExtractor.d.ts +1 -0
  36. package/dist/extraction/extractors/associateExtractor.d.ts.map +1 -1
  37. package/dist/extraction/extractors/associateExtractor.js +3 -0
  38. package/dist/extraction/extractors/choiceExtractor.d.ts +2 -0
  39. package/dist/extraction/extractors/choiceExtractor.d.ts.map +1 -1
  40. package/dist/extraction/extractors/choiceExtractor.js +10 -0
  41. package/dist/extraction/extractors/extendedTextExtractor.d.ts.map +1 -1
  42. package/dist/extraction/extractors/extendedTextExtractor.js +6 -0
  43. package/dist/extraction/extractors/gapMatchExtractor.d.ts +1 -0
  44. package/dist/extraction/extractors/gapMatchExtractor.d.ts.map +1 -1
  45. package/dist/extraction/extractors/gapMatchExtractor.js +33 -20
  46. package/dist/extraction/extractors/graphicAssociateExtractor.d.ts +1 -0
  47. package/dist/extraction/extractors/graphicAssociateExtractor.d.ts.map +1 -1
  48. package/dist/extraction/extractors/graphicAssociateExtractor.js +3 -0
  49. package/dist/extraction/extractors/graphicGapMatchExtractor.d.ts +11 -0
  50. package/dist/extraction/extractors/graphicGapMatchExtractor.d.ts.map +1 -1
  51. package/dist/extraction/extractors/graphicGapMatchExtractor.js +71 -10
  52. package/dist/extraction/extractors/hotspotExtractor.d.ts.map +1 -1
  53. package/dist/extraction/extractors/hotspotExtractor.js +27 -8
  54. package/dist/extraction/extractors/hottextExtractor.d.ts +1 -1
  55. package/dist/extraction/extractors/hottextExtractor.d.ts.map +1 -1
  56. package/dist/extraction/extractors/hottextExtractor.js +12 -4
  57. package/dist/extraction/extractors/index.d.ts +1 -0
  58. package/dist/extraction/extractors/index.d.ts.map +1 -1
  59. package/dist/extraction/extractors/index.js +4 -0
  60. package/dist/extraction/extractors/inlineChoiceExtractor.d.ts +3 -0
  61. package/dist/extraction/extractors/inlineChoiceExtractor.d.ts.map +1 -1
  62. package/dist/extraction/extractors/inlineChoiceExtractor.js +17 -4
  63. package/dist/extraction/extractors/matchExtractor.d.ts +5 -0
  64. package/dist/extraction/extractors/matchExtractor.d.ts.map +1 -1
  65. package/dist/extraction/extractors/matchExtractor.js +26 -0
  66. package/dist/extraction/extractors/orderExtractor.d.ts +2 -0
  67. package/dist/extraction/extractors/orderExtractor.d.ts.map +1 -1
  68. package/dist/extraction/extractors/orderExtractor.js +4 -0
  69. package/dist/extraction/extractors/portableCustomExtractor.d.ts +14 -0
  70. package/dist/extraction/extractors/portableCustomExtractor.d.ts.map +1 -0
  71. package/dist/extraction/extractors/portableCustomExtractor.js +96 -0
  72. package/dist/extraction/extractors/selectPointExtractor.js +5 -5
  73. package/dist/extraction/extractors/textEntryExtractor.d.ts +1 -0
  74. package/dist/extraction/extractors/textEntryExtractor.d.ts.map +1 -1
  75. package/dist/extraction/extractors/textEntryExtractor.js +6 -0
  76. package/dist/extraction/extractors/uploadExtractor.d.ts.map +1 -1
  77. package/dist/extraction/extractors/uploadExtractor.js +9 -2
  78. package/dist/index.d.ts +11 -1
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/index.js +8 -1
  81. package/dist/pci/PciHost.d.ts +53 -0
  82. package/dist/pci/PciHost.d.ts.map +1 -0
  83. package/dist/pci/PciHost.js +116 -0
  84. package/dist/pci/types.d.ts +57 -0
  85. package/dist/pci/types.d.ts.map +1 -0
  86. package/dist/pci/types.js +19 -0
  87. package/dist/pnp/applyPnp.d.ts +9 -0
  88. package/dist/pnp/applyPnp.d.ts.map +1 -0
  89. package/dist/pnp/applyPnp.js +19 -0
  90. package/dist/pnp/parsePnpXml.d.ts +9 -0
  91. package/dist/pnp/parsePnpXml.d.ts.map +1 -0
  92. package/dist/pnp/parsePnpXml.js +101 -0
  93. package/dist/pnp/types.d.ts +40 -0
  94. package/dist/pnp/types.d.ts.map +1 -0
  95. package/dist/pnp/types.js +1 -0
  96. package/dist/types/index.d.ts +22 -8
  97. package/dist/types/index.d.ts.map +1 -1
  98. package/dist/types/interactions.d.ts +90 -1
  99. package/dist/types/interactions.d.ts.map +1 -1
  100. package/package.json +19 -9
  101. package/src/__tests__/catalog.test.ts +267 -0
  102. package/src/__tests__/fixtures/pci/echo-pci.js +49 -0
  103. package/src/__tests__/glossaryTriggers.test.ts +411 -0
  104. package/src/__tests__/pci.test.ts +353 -0
  105. package/src/__tests__/pnp.test.ts +250 -0
  106. package/src/__tests__/qti3-extraction.test.ts +5 -5
  107. package/src/catalog/applyGlossaryTriggers.ts +232 -0
  108. package/src/catalog/catalogExtractor.ts +130 -0
  109. package/src/catalog/catalogLookup.ts +53 -0
  110. package/src/catalog/glossaryAction.ts +28 -0
  111. package/src/catalog/types.ts +37 -0
  112. package/src/components/ItemBody.svelte +134 -26
  113. package/src/components/ItemPlayer.svelte +6 -2
  114. package/src/components/index.ts +3 -0
  115. package/src/core/ComponentRegistry.ts +14 -0
  116. package/src/core/ItemRenderer.ts +4 -0
  117. package/src/core/Player.ts +174 -4
  118. package/src/core/rolePolicy.ts +34 -0
  119. package/src/core/urlPolicy.ts +5 -2
  120. package/src/element.ts +128 -0
  121. package/src/extraction/ExtractionRegistry.ts +5 -1
  122. package/src/extraction/extractors/associateExtractor.ts +4 -0
  123. package/src/extraction/extractors/choiceExtractor.ts +12 -1
  124. package/src/extraction/extractors/conformance-qti3-advanced.test.ts +239 -0
  125. package/src/extraction/extractors/conformance-qti3-basic.test.ts +82 -0
  126. package/src/extraction/extractors/conformance.test.ts +208 -0
  127. package/src/extraction/extractors/extendedTextExtractor.ts +6 -0
  128. package/src/extraction/extractors/gapMatchExtractor.ts +36 -21
  129. package/src/extraction/extractors/graphicAssociateExtractor.ts +4 -0
  130. package/src/extraction/extractors/graphicGapMatchExtractor.ts +82 -10
  131. package/src/extraction/extractors/hotspotExtractor.ts +26 -8
  132. package/src/extraction/extractors/hottextExtractor.ts +15 -5
  133. package/src/extraction/extractors/index.ts +4 -0
  134. package/src/extraction/extractors/inlineChoiceExtractor.ts +22 -5
  135. package/src/extraction/extractors/matchExtractor.ts +31 -4
  136. package/src/extraction/extractors/orderExtractor.ts +6 -0
  137. package/src/extraction/extractors/portableCustomExtractor.ts +122 -0
  138. package/src/extraction/extractors/selectPointExtractor.ts +5 -5
  139. package/src/extraction/extractors/textEntryExtractor.ts +7 -0
  140. package/src/extraction/extractors/uploadExtractor.ts +10 -2
  141. package/src/index.ts +17 -1
  142. package/src/pci/PciHost.ts +138 -0
  143. package/src/pci/types.ts +70 -0
  144. package/src/pnp/applyPnp.ts +23 -0
  145. package/src/pnp/parsePnpXml.ts +118 -0
  146. package/src/pnp/types.ts +33 -0
  147. package/src/types/index.ts +22 -8
  148. package/src/types/interactions.ts +81 -6
@@ -0,0 +1,353 @@
1
+ import { describe, it, expect, mock } from 'bun:test';
2
+ import { PciHost } from '../pci/PciHost.js';
3
+ import { PciLoadError } from '../pci/types.js';
4
+ import type { ExtractedPci, PciModule } from '../pci/types.js';
5
+ import { portableCustomExtractor } from '../extraction/extractors/portableCustomExtractor.js';
6
+ import { createExtractionUtils } from '../extraction/index.js';
7
+ import { Qti3AttributeNameMapper, Qti3ElementNameMapper } from '@pie-qti/qti-common';
8
+ import { parse } from 'node-html-parser';
9
+
10
+ // ---------------------------------------------------------------------------
11
+ // Helpers
12
+ // ---------------------------------------------------------------------------
13
+
14
+ function makeData(overrides: Partial<ExtractedPci> = {}): ExtractedPci {
15
+ return {
16
+ responseIdentifier: 'RESPONSE',
17
+ customInteractionTypeIdentifier: 'urn:test:echo-pci',
18
+ primaryPath: 'https://cdn.example.com/pci/echo.js',
19
+ fallbackPath: 'https://cdn.example.com/pci/echo-fallback.js',
20
+ markup: '<div class="pci-container"></div>',
21
+ config: { color: 'blue', maxLength: '200' },
22
+ ...overrides,
23
+ };
24
+ }
25
+
26
+ function makeModule(overrides: Partial<PciModule> = {}): PciModule {
27
+ return {
28
+ initialize: mock(() => {}),
29
+ getResponse: mock(() => 'test-response'),
30
+ setResponse: mock(() => {}),
31
+ disable: mock(() => {}),
32
+ enable: mock(() => {}),
33
+ destroy: mock(() => {}),
34
+ ...overrides,
35
+ };
36
+ }
37
+
38
+ /** Fake DOM node (minimal — just needs appendChild) */
39
+ function makeDomNode() {
40
+ return {
41
+ appendChild: mock((_el: any) => {}),
42
+ } as any as HTMLElement;
43
+ }
44
+
45
+ // ---------------------------------------------------------------------------
46
+ // PciHost — constructor
47
+ // ---------------------------------------------------------------------------
48
+
49
+ describe('PciHost', () => {
50
+ it('instantiates without throwing', () => {
51
+ expect(() => new PciHost(makeData(), 'https://host.example.com')).not.toThrow();
52
+ });
53
+
54
+ // ---------------------------------------------------------------------------
55
+ // PciHost.load()
56
+ // ---------------------------------------------------------------------------
57
+
58
+ it('load() resolves when dynamic import succeeds with default export', async () => {
59
+ const module = makeModule();
60
+ const host = new PciHost(makeData({ primaryPath: 'https://cdn.example.com/good.js' }), '');
61
+
62
+ // Patch import to resolve with default export
63
+ (host as any).resolveUrl = () => 'mocked';
64
+ (host as any).dynamicImport = mock(async (_url: string) => ({ default: module }));
65
+
66
+ await host.load();
67
+ // Should not throw — module is loaded
68
+ });
69
+
70
+ it('load() falls back to fallbackPath when primary import throws', async () => {
71
+ const module = makeModule();
72
+ const host = new PciHost(makeData(), '');
73
+
74
+ let callCount = 0;
75
+ (host as any).dynamicImport = mock(async (_url: string) => {
76
+ callCount++;
77
+ if (callCount === 1) throw new Error('primary failed');
78
+ return { default: module };
79
+ });
80
+
81
+ await host.load();
82
+ expect(callCount).toBe(2);
83
+ });
84
+
85
+ it('load() throws PciLoadError when both primary and fallback fail', async () => {
86
+ const host = new PciHost(makeData(), '');
87
+
88
+ (host as any).dynamicImport = mock(async () => {
89
+ throw new Error('not found');
90
+ });
91
+
92
+ await expect(host.load()).rejects.toBeInstanceOf(PciLoadError);
93
+ });
94
+
95
+ it('load() throws PciLoadError when only primary fails and no fallback', async () => {
96
+ const host = new PciHost(makeData({ fallbackPath: undefined }), '');
97
+
98
+ (host as any).dynamicImport = mock(async () => {
99
+ throw new Error('not found');
100
+ });
101
+
102
+ await expect(host.load()).rejects.toBeInstanceOf(PciLoadError);
103
+ });
104
+
105
+ it('PciLoadError includes primaryPath and fallbackPath', async () => {
106
+ const data = makeData();
107
+ const host = new PciHost(data, '');
108
+
109
+ (host as any).dynamicImport = mock(async () => {
110
+ throw new Error('not found');
111
+ });
112
+
113
+ let err: PciLoadError | null = null;
114
+ try {
115
+ await host.load();
116
+ } catch (e) {
117
+ err = e as PciLoadError;
118
+ }
119
+
120
+ expect(err).toBeInstanceOf(PciLoadError);
121
+ expect(err!.primaryPath).toBe(data.primaryPath);
122
+ expect(err!.fallbackPath).toBe(data.fallbackPath);
123
+ });
124
+
125
+ // ---------------------------------------------------------------------------
126
+ // PciHost.initialize()
127
+ // ---------------------------------------------------------------------------
128
+
129
+ it('initialize() calls module.initialize with dom, config, and boundTo', () => {
130
+ const module = makeModule();
131
+ const host = new PciHost(makeData(), '');
132
+ (host as any).module = module;
133
+
134
+ const dom = makeDomNode();
135
+ host.initialize(dom);
136
+
137
+ expect(module.initialize).toHaveBeenCalledTimes(1);
138
+ const [calledDom, calledConfig, calledBoundTo] = (module.initialize as any).mock.calls[0];
139
+ expect(calledDom).toBe(dom);
140
+ expect(calledConfig).toEqual({ color: 'blue', maxLength: '200' });
141
+ expect(typeof calledBoundTo.onReady).toBe('function');
142
+ expect(typeof calledBoundTo.onResponseChange).toBe('function');
143
+ });
144
+
145
+ it('initialize() is a no-op when module has not been loaded', () => {
146
+ const host = new PciHost(makeData(), '');
147
+ // Should not throw even though module is null
148
+ expect(() => host.initialize(makeDomNode())).not.toThrow();
149
+ });
150
+
151
+ // ---------------------------------------------------------------------------
152
+ // PciHost response handling
153
+ // ---------------------------------------------------------------------------
154
+
155
+ it('getResponse() delegates to module.getResponse()', () => {
156
+ const module = makeModule({ getResponse: mock(() => 'hello') });
157
+ const host = new PciHost(makeData(), '');
158
+ (host as any).module = module;
159
+
160
+ expect(host.getResponse()).toBe('hello');
161
+ expect(module.getResponse).toHaveBeenCalledTimes(1);
162
+ });
163
+
164
+ it('setResponse() delegates to module.setResponse()', () => {
165
+ const module = makeModule();
166
+ const host = new PciHost(makeData(), '');
167
+ (host as any).module = module;
168
+
169
+ host.setResponse('world');
170
+ expect(module.setResponse).toHaveBeenCalledWith('world');
171
+ });
172
+
173
+ it('onResponseChange callback fires when boundTo.onResponseChange is called', () => {
174
+ const module = makeModule();
175
+ const host = new PciHost(makeData(), '');
176
+ (host as any).module = module;
177
+
178
+ const received: Array<[string, unknown]> = [];
179
+ host.onResponseChange((id, val) => received.push([id, val]));
180
+
181
+ const dom = makeDomNode();
182
+ host.initialize(dom);
183
+
184
+ // Simulate PCI calling boundTo.onResponseChange
185
+ const boundTo = (module.initialize as any).mock.calls[0][2];
186
+ boundTo.onResponseChange('answer-42');
187
+
188
+ expect(received).toEqual([['RESPONSE', 'answer-42']]);
189
+ });
190
+
191
+ // ---------------------------------------------------------------------------
192
+ // PciHost lifecycle
193
+ // ---------------------------------------------------------------------------
194
+
195
+ it('disable() delegates to module.disable()', () => {
196
+ const module = makeModule();
197
+ const host = new PciHost(makeData(), '');
198
+ (host as any).module = module;
199
+
200
+ host.disable();
201
+ expect(module.disable).toHaveBeenCalledTimes(1);
202
+ });
203
+
204
+ it('enable() delegates to module.enable()', () => {
205
+ const module = makeModule();
206
+ const host = new PciHost(makeData(), '');
207
+ (host as any).module = module;
208
+
209
+ host.enable();
210
+ expect(module.enable).toHaveBeenCalledTimes(1);
211
+ });
212
+
213
+ it('destroy() calls module.destroy() and nullifies module', () => {
214
+ const module = makeModule();
215
+ const host = new PciHost(makeData(), '');
216
+ (host as any).module = module;
217
+
218
+ host.destroy();
219
+ expect(module.destroy).toHaveBeenCalledTimes(1);
220
+ expect((host as any).module).toBeNull();
221
+ });
222
+
223
+ it('disable/enable/destroy are no-ops before load()', () => {
224
+ const host = new PciHost(makeData(), '');
225
+ expect(() => { host.disable(); host.enable(); host.destroy(); }).not.toThrow();
226
+ });
227
+
228
+ // ---------------------------------------------------------------------------
229
+ // URL resolution
230
+ // ---------------------------------------------------------------------------
231
+
232
+ it('resolveUrl returns absolute URLs unchanged', () => {
233
+ const host = new PciHost(makeData(), 'https://base.example.com/items/');
234
+ expect((host as any).resolveUrl('https://cdn.example.com/pci.js')).toBe('https://cdn.example.com/pci.js');
235
+ expect((host as any).resolveUrl('/absolute/path.js')).toBe('/absolute/path.js');
236
+ });
237
+
238
+ it('resolveUrl prepends baseUrl for relative paths', () => {
239
+ const host = new PciHost(makeData(), 'https://base.example.com/items/');
240
+ expect((host as any).resolveUrl('pci/echo.js')).toBe('https://base.example.com/items/pci/echo.js');
241
+ });
242
+
243
+ it('resolveUrl adds trailing slash to baseUrl if missing', () => {
244
+ const host = new PciHost(makeData(), 'https://base.example.com/items');
245
+ expect((host as any).resolveUrl('pci/echo.js')).toBe('https://base.example.com/items/pci/echo.js');
246
+ });
247
+ });
248
+
249
+ // ---------------------------------------------------------------------------
250
+ // portableCustomExtractor
251
+ // ---------------------------------------------------------------------------
252
+
253
+ const PCI_XML = `
254
+ <qti-portable-custom-interaction
255
+ response-identifier="PCI_RESPONSE"
256
+ custom-interaction-type-identifier="urn:example:echo-pci">
257
+ <qti-interaction-modules>
258
+ <qti-interaction-module
259
+ id="echoModule"
260
+ primary-path="pci/echo.js"
261
+ fallback-path="pci/echo-fallback.js"/>
262
+ </qti-interaction-modules>
263
+ <qti-interaction-markup>
264
+ <div class="echo-container">Enter text here</div>
265
+ </qti-interaction-markup>
266
+ <qti-pci-properties>
267
+ <qti-pci-property key="theme" value="light"/>
268
+ <qti-pci-property key="maxLength" value="500"/>
269
+ </qti-pci-properties>
270
+ </qti-portable-custom-interaction>
271
+ `;
272
+
273
+ function parseEl(xml: string) {
274
+ const doc = parse(xml.trim(), { lowerCaseTagName: false, comment: false });
275
+ return (doc.firstChild ?? doc) as any;
276
+ }
277
+
278
+ function makeContext(el: any) {
279
+ const utils = createExtractionUtils(undefined, new Qti3ElementNameMapper(), new Qti3AttributeNameMapper());
280
+ return { element: el, responseId: 'PCI_RESPONSE', dom: el, declarations: new Map(), utils, config: {} as any };
281
+ }
282
+
283
+ describe('portableCustomExtractor', () => {
284
+ it('canHandle returns true for qti-portable-custom-interaction', () => {
285
+ const el = parseEl(PCI_XML);
286
+ expect(portableCustomExtractor.canHandle(el, makeContext(el))).toBe(true);
287
+ });
288
+
289
+ it('canHandle returns false for qti-choice-interaction', () => {
290
+ const el = parseEl('<qti-choice-interaction response-identifier="R" max-choices="1"/>');
291
+ expect(portableCustomExtractor.canHandle(el, makeContext(el))).toBe(false);
292
+ });
293
+
294
+ it('extracts responseIdentifier', () => {
295
+ const el = parseEl(PCI_XML);
296
+ const data = portableCustomExtractor.extract(el, makeContext(el));
297
+ expect(data.responseIdentifier).toBe('PCI_RESPONSE');
298
+ });
299
+
300
+ it('extracts customInteractionTypeIdentifier', () => {
301
+ const el = parseEl(PCI_XML);
302
+ const data = portableCustomExtractor.extract(el, makeContext(el));
303
+ expect(data.customInteractionTypeIdentifier).toBe('urn:example:echo-pci');
304
+ });
305
+
306
+ it('extracts primaryPath from qti-interaction-module', () => {
307
+ const el = parseEl(PCI_XML);
308
+ const data = portableCustomExtractor.extract(el, makeContext(el));
309
+ expect(data.primaryPath).toBe('pci/echo.js');
310
+ });
311
+
312
+ it('extracts fallbackPath from qti-interaction-module', () => {
313
+ const el = parseEl(PCI_XML);
314
+ const data = portableCustomExtractor.extract(el, makeContext(el));
315
+ expect(data.fallbackPath).toBe('pci/echo-fallback.js');
316
+ });
317
+
318
+ it('extracts markup from qti-interaction-markup', () => {
319
+ const el = parseEl(PCI_XML);
320
+ const data = portableCustomExtractor.extract(el, makeContext(el));
321
+ expect(data.markup).toContain('echo-container');
322
+ });
323
+
324
+ it('extracts config key/value pairs from qti-pci-properties', () => {
325
+ const el = parseEl(PCI_XML);
326
+ const data = portableCustomExtractor.extract(el, makeContext(el));
327
+ expect(data.config.theme).toBe('light');
328
+ expect(data.config.maxLength).toBe('500');
329
+ });
330
+
331
+ it('validate returns invalid when primaryPath is empty', () => {
332
+ const result = portableCustomExtractor.validate!({
333
+ responseIdentifier: 'R',
334
+ customInteractionTypeIdentifier: 'urn:x',
335
+ primaryPath: '',
336
+ markup: '',
337
+ config: {},
338
+ });
339
+ expect(result.valid).toBe(false);
340
+ expect(result.errors).toBeDefined();
341
+ });
342
+
343
+ it('validate returns valid for a well-formed ExtractedPci', () => {
344
+ const el = parseEl(PCI_XML);
345
+ const data = portableCustomExtractor.extract(el, makeContext(el));
346
+ const result = portableCustomExtractor.validate!(data);
347
+ expect(result.valid).toBe(true);
348
+ });
349
+
350
+ it('has higher priority than standardCustomExtractor (10)', () => {
351
+ expect(portableCustomExtractor.priority).toBeGreaterThan(10);
352
+ });
353
+ });
@@ -0,0 +1,250 @@
1
+ import { describe, it, expect } from 'bun:test';
2
+ import { parsePnpXml } from '../pnp/parsePnpXml.js';
3
+ import { applyPnpToRoot } from '../pnp/applyPnp.js';
4
+ import { Player } from '../core/Player.js';
5
+ import type { PnpProfile } from '../pnp/types.js';
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // parsePnpXml
9
+ // ---------------------------------------------------------------------------
10
+
11
+ describe('parsePnpXml', () => {
12
+ it('parses default color scheme', () => {
13
+ const profile = parsePnpXml(`
14
+ <personalNeedsProfile>
15
+ <display>
16
+ <colorScheme value="default"/>
17
+ </display>
18
+ </personalNeedsProfile>
19
+ `);
20
+ expect(profile.display?.colorScheme).toBe('default');
21
+ });
22
+
23
+ it.each(['blackwhite', 'whitenav', 'blackcream', 'yellowblue', 'medgray'] as const)(
24
+ 'parses color scheme "%s"',
25
+ (scheme) => {
26
+ const profile = parsePnpXml(`
27
+ <personalNeedsProfile>
28
+ <display>
29
+ <colorScheme value="${scheme}"/>
30
+ </display>
31
+ </personalNeedsProfile>
32
+ `);
33
+ expect(profile.display?.colorScheme).toBe(scheme);
34
+ }
35
+ );
36
+
37
+ it('ignores unknown color scheme values', () => {
38
+ const profile = parsePnpXml(`
39
+ <personalNeedsProfile>
40
+ <display>
41
+ <colorScheme value="neon-pink"/>
42
+ </display>
43
+ </personalNeedsProfile>
44
+ `);
45
+ expect(profile.display?.colorScheme).toBeUndefined();
46
+ });
47
+
48
+ it('parses extendedTime', () => {
49
+ const profile = parsePnpXml(`
50
+ <personalNeedsProfile>
51
+ <content>
52
+ <extendedTime active="true" multiplier="1.5"/>
53
+ </content>
54
+ </personalNeedsProfile>
55
+ `);
56
+ expect(profile.content?.extendedTime).toEqual({ active: true, multiplier: 1.5 });
57
+ });
58
+
59
+ it('parses extendedTime with Infinity multiplier', () => {
60
+ const profile = parsePnpXml(`
61
+ <personalNeedsProfile>
62
+ <content>
63
+ <extendedTime active="true" multiplier="Infinity"/>
64
+ </content>
65
+ </personalNeedsProfile>
66
+ `);
67
+ expect(profile.content?.extendedTime?.multiplier).toBe(Infinity);
68
+ });
69
+
70
+ it('parses glossaryOnScreen', () => {
71
+ const profile = parsePnpXml(`
72
+ <personalNeedsProfile>
73
+ <content>
74
+ <glossaryOnScreen enabled="true"/>
75
+ </content>
76
+ </personalNeedsProfile>
77
+ `);
78
+ expect(profile.content?.glossaryOnScreen).toBe(true);
79
+ });
80
+
81
+ it('parses keywordTranslation', () => {
82
+ const profile = parsePnpXml(`
83
+ <personalNeedsProfile>
84
+ <content>
85
+ <keywordTranslation active="true" languageCode="es"/>
86
+ </content>
87
+ </personalNeedsProfile>
88
+ `);
89
+ expect(profile.content?.keywordTranslation).toEqual({ active: true, languageCode: 'es' });
90
+ });
91
+
92
+ it('parses eliminationTool', () => {
93
+ const profile = parsePnpXml(`
94
+ <personalNeedsProfile>
95
+ <cognitive>
96
+ <eliminationTool enabled="true"/>
97
+ </cognitive>
98
+ </personalNeedsProfile>
99
+ `);
100
+ expect(profile.cognitive?.eliminationTool).toBe(true);
101
+ });
102
+
103
+ it('silently ignores unknown child elements', () => {
104
+ expect(() =>
105
+ parsePnpXml(`
106
+ <personalNeedsProfile>
107
+ <display>
108
+ <unknownFutureThing value="42"/>
109
+ </display>
110
+ <futureSection>
111
+ <anotherThing/>
112
+ </futureSection>
113
+ </personalNeedsProfile>
114
+ `)
115
+ ).not.toThrow();
116
+ });
117
+
118
+ it('returns empty profile for empty input', () => {
119
+ const profile = parsePnpXml('');
120
+ expect(profile).toEqual({});
121
+ });
122
+
123
+ it('returns empty profile when no recognized root element', () => {
124
+ // parsePnpXml with an unrecognised root gracefully returns {}
125
+ const profile = parsePnpXml('<somethingElse><display/></somethingElse>');
126
+ expect(profile).toEqual({});
127
+ });
128
+ });
129
+
130
+ // ---------------------------------------------------------------------------
131
+ // applyPnpToRoot — uses a minimal object that satisfies the HTMLElement contract
132
+ // ---------------------------------------------------------------------------
133
+
134
+ function makeEl(): HTMLElement {
135
+ const attrs: Record<string, string> = {};
136
+ return {
137
+ setAttribute(k: string, v: string) { attrs[k] = v; },
138
+ removeAttribute(k: string) { delete attrs[k]; },
139
+ hasAttribute(k: string) { return k in attrs; },
140
+ getAttribute(k: string) { return attrs[k] ?? null; },
141
+ } as unknown as HTMLElement;
142
+ }
143
+
144
+ describe('applyPnpToRoot', () => {
145
+ it('sets data-qti-colorscheme for a non-default scheme', () => {
146
+ const el = makeEl();
147
+ applyPnpToRoot(el, { display: { colorScheme: 'blackwhite' } });
148
+ expect(el.getAttribute('data-qti-colorscheme')).toBe('blackwhite');
149
+ });
150
+
151
+ it('removes the attribute for "default" scheme', () => {
152
+ const el = makeEl();
153
+ el.setAttribute('data-qti-colorscheme', 'blackwhite');
154
+ applyPnpToRoot(el, { display: { colorScheme: 'default' } });
155
+ expect(el.hasAttribute('data-qti-colorscheme')).toBe(false);
156
+ });
157
+
158
+ it('removes the attribute when pnp is undefined', () => {
159
+ const el = makeEl();
160
+ el.setAttribute('data-qti-colorscheme', 'blackwhite');
161
+ applyPnpToRoot(el, undefined);
162
+ expect(el.hasAttribute('data-qti-colorscheme')).toBe(false);
163
+ });
164
+
165
+ it('removes the attribute when colorScheme is undefined', () => {
166
+ const el = makeEl();
167
+ el.setAttribute('data-qti-colorscheme', 'blackwhite');
168
+ applyPnpToRoot(el, { display: {} });
169
+ expect(el.hasAttribute('data-qti-colorscheme')).toBe(false);
170
+ });
171
+
172
+ it.each(['blackwhite', 'whitenav', 'blackcream', 'yellowblue', 'medgray'] as const)(
173
+ 'sets correct attribute value for scheme "%s"',
174
+ (scheme) => {
175
+ const el = makeEl();
176
+ applyPnpToRoot(el, { display: { colorScheme: scheme } });
177
+ expect(el.getAttribute('data-qti-colorscheme')).toBe(scheme);
178
+ }
179
+ );
180
+ });
181
+
182
+ // ---------------------------------------------------------------------------
183
+ // Player.applyPnp / Player.updatePnp
184
+ // ---------------------------------------------------------------------------
185
+
186
+ const MINIMAL_ITEM_XML = `<?xml version="1.0" encoding="UTF-8"?>
187
+ <assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2"
188
+ identifier="test" title="Test" adaptive="false" timeDependent="false">
189
+ <responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier"/>
190
+ <outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float"/>
191
+ <itemBody><choiceInteraction responseIdentifier="RESPONSE" maxChoices="1">
192
+ <simpleChoice identifier="A">Alpha</simpleChoice>
193
+ <simpleChoice identifier="B">Beta</simpleChoice>
194
+ </choiceInteraction></itemBody>
195
+ </assessmentItem>`;
196
+
197
+ describe('Player PNP integration', () => {
198
+ it('applyPnp sets the color scheme attribute on the root element', () => {
199
+ const player = new Player({
200
+ itemXml: MINIMAL_ITEM_XML,
201
+ pnp: { display: { colorScheme: 'yellowblue' } },
202
+ });
203
+ const root = makeEl();
204
+ player.applyPnp(root);
205
+ expect(root.getAttribute('data-qti-colorscheme')).toBe('yellowblue');
206
+ });
207
+
208
+ it('updatePnp changes the attribute without re-parsing the item', () => {
209
+ const player = new Player({ itemXml: MINIMAL_ITEM_XML });
210
+ const root = makeEl();
211
+ player.applyPnp(root);
212
+
213
+ expect(root.hasAttribute('data-qti-colorscheme')).toBe(false);
214
+
215
+ player.updatePnp({ display: { colorScheme: 'blackwhite' } });
216
+ expect(root.getAttribute('data-qti-colorscheme')).toBe('blackwhite');
217
+ });
218
+
219
+ it('updatePnp deep-merges without losing other fields', () => {
220
+ const player = new Player({
221
+ itemXml: MINIMAL_ITEM_XML,
222
+ pnp: { cognitive: { eliminationTool: true }, display: { colorScheme: 'blackwhite' } },
223
+ });
224
+ const root = makeEl();
225
+ player.applyPnp(root);
226
+
227
+ player.updatePnp({ display: { colorScheme: 'medgray' } });
228
+
229
+ expect(root.getAttribute('data-qti-colorscheme')).toBe('medgray');
230
+ expect(player.getPnp()?.cognitive?.eliminationTool).toBe(true);
231
+ });
232
+
233
+ it('updatePnp to default removes the attribute', () => {
234
+ const player = new Player({
235
+ itemXml: MINIMAL_ITEM_XML,
236
+ pnp: { display: { colorScheme: 'blackwhite' } },
237
+ });
238
+ const root = makeEl();
239
+ player.applyPnp(root);
240
+
241
+ player.updatePnp({ display: { colorScheme: 'default' } });
242
+ expect(root.hasAttribute('data-qti-colorscheme')).toBe(false);
243
+ });
244
+
245
+ it('getPnp returns the current profile', () => {
246
+ const pnp: PnpProfile = { cognitive: { eliminationTool: true } };
247
+ const player = new Player({ itemXml: MINIMAL_ITEM_XML, pnp });
248
+ expect(player.getPnp()?.cognitive?.eliminationTool).toBe(true);
249
+ });
250
+ });
@@ -177,7 +177,7 @@ describe('QTI 3.0 Extraction', () => {
177
177
  expect(interactions).toHaveLength(1);
178
178
  const interaction = interactions[0];
179
179
 
180
- expect(interaction.type).toBe('qti-choice-interaction'); // QTI 3.0 uses kebab-case
180
+ expect(interaction.type).toBe('choiceInteraction'); // normalized to QTI 2.x camelCase
181
181
  expect(interaction.responseId).toBe('RESPONSE');
182
182
  expect(interaction.maxChoices).toBe(1);
183
183
  expect(interaction.shuffle).toBe(false);
@@ -194,7 +194,7 @@ describe('QTI 3.0 Extraction', () => {
194
194
  const result = player.processResponses();
195
195
 
196
196
  expect(result.score).toBe(1.0);
197
- expect(result.outcomeValues.SCORE).toBe('1.0'); // outcomeValues are strings
197
+ expect(result.outcomeValues.SCORE).toBe(1.0);
198
198
  });
199
199
  });
200
200
 
@@ -206,7 +206,7 @@ describe('QTI 3.0 Extraction', () => {
206
206
  expect(interactions).toHaveLength(1);
207
207
  const interaction = interactions[0];
208
208
 
209
- expect(interaction.type).toBe('qti-text-entry-interaction'); // QTI 3.0 uses kebab-case
209
+ expect(interaction.type).toBe('textEntryInteraction'); // normalized to QTI 2.x camelCase
210
210
  expect(interaction.responseId).toBe('RESPONSE');
211
211
  expect(interaction.expectedLength).toBe(15);
212
212
  });
@@ -229,7 +229,7 @@ describe('QTI 3.0 Extraction', () => {
229
229
  expect(interactions).toHaveLength(1);
230
230
  const interaction = interactions[0];
231
231
 
232
- expect(interaction.type).toBe('qti-extended-text-interaction'); // QTI 3.0 uses kebab-case
232
+ expect(interaction.type).toBe('extendedTextInteraction'); // normalized to QTI 2.x camelCase
233
233
  expect(interaction.responseId).toBe('RESPONSE');
234
234
  expect(interaction.expectedLines).toBe(5);
235
235
  expect(interaction.expectedLength).toBe(500);
@@ -245,7 +245,7 @@ describe('QTI 3.0 Extraction', () => {
245
245
  expect(interactions).toHaveLength(1);
246
246
  const interaction = interactions[0];
247
247
 
248
- expect(interaction.type).toBe('qti-match-interaction'); // QTI 3.0 uses kebab-case
248
+ expect(interaction.type).toBe('matchInteraction'); // normalized to QTI 2.x camelCase
249
249
  expect(interaction.responseId).toBe('RESPONSE');
250
250
  expect(interaction.maxAssociations).toBe(3);
251
251
  expect(interaction.shuffle).toBe(false);