@longsightgroup/qti3-core 0.9.1 → 0.9.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.
Files changed (244) hide show
  1. package/README.md +59 -3
  2. package/dist/adaptive-turn-materializer.d.ts +14 -0
  3. package/dist/adaptive-turn-materializer.d.ts.map +1 -0
  4. package/dist/adaptive-turn-materializer.js +56 -0
  5. package/dist/adaptive-turn-materializer.js.map +1 -0
  6. package/dist/adaptive-turn.d.ts +23 -0
  7. package/dist/adaptive-turn.d.ts.map +1 -0
  8. package/dist/adaptive-turn.js +60 -0
  9. package/dist/adaptive-turn.js.map +1 -0
  10. package/dist/assert-never.d.ts +3 -0
  11. package/dist/assert-never.d.ts.map +1 -0
  12. package/dist/assert-never.js +5 -0
  13. package/dist/assert-never.js.map +1 -0
  14. package/dist/attempt-state-constants.d.ts +6 -0
  15. package/dist/attempt-state-constants.d.ts.map +1 -0
  16. package/dist/attempt-state-constants.js +6 -0
  17. package/dist/attempt-state-constants.js.map +1 -0
  18. package/dist/attempt-state.d.ts +5 -0
  19. package/dist/attempt-state.d.ts.map +1 -0
  20. package/dist/attempt-state.js +190 -0
  21. package/dist/attempt-state.js.map +1 -0
  22. package/dist/catalog.js +1 -1
  23. package/dist/catalog.js.map +1 -1
  24. package/dist/content-text.d.ts.map +1 -1
  25. package/dist/content-text.js +3 -0
  26. package/dist/content-text.js.map +1 -1
  27. package/dist/custom-operators.d.ts +13 -0
  28. package/dist/custom-operators.d.ts.map +1 -0
  29. package/dist/custom-operators.js +2 -0
  30. package/dist/custom-operators.js.map +1 -0
  31. package/dist/delivery-redaction.d.ts +50 -0
  32. package/dist/delivery-redaction.d.ts.map +1 -0
  33. package/dist/delivery-redaction.js +176 -0
  34. package/dist/delivery-redaction.js.map +1 -0
  35. package/dist/delivery-security.d.ts +3 -18
  36. package/dist/delivery-security.d.ts.map +1 -1
  37. package/dist/delivery-security.js +35 -189
  38. package/dist/delivery-security.js.map +1 -1
  39. package/dist/index.d.ts +4 -1
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +3 -0
  42. package/dist/index.js.map +1 -1
  43. package/dist/parser-custom-interactions.js +1 -1
  44. package/dist/parser-custom-interactions.js.map +1 -1
  45. package/dist/parser-declarations.d.ts.map +1 -1
  46. package/dist/parser-declarations.js +4 -4
  47. package/dist/parser-declarations.js.map +1 -1
  48. package/dist/parser-item-metadata.d.ts.map +1 -1
  49. package/dist/parser-item-metadata.js +8 -7
  50. package/dist/parser-item-metadata.js.map +1 -1
  51. package/dist/parser-processing.js +2 -2
  52. package/dist/parser-processing.js.map +1 -1
  53. package/dist/parser-values.d.ts +5 -1
  54. package/dist/parser-values.d.ts.map +1 -1
  55. package/dist/parser-values.js +33 -0
  56. package/dist/parser-values.js.map +1 -1
  57. package/dist/processing-evaluator-boolean.d.ts +8 -0
  58. package/dist/processing-evaluator-boolean.d.ts.map +1 -0
  59. package/dist/processing-evaluator-boolean.js +47 -0
  60. package/dist/processing-evaluator-boolean.js.map +1 -0
  61. package/dist/processing-evaluator-collection.d.ts +12 -0
  62. package/dist/processing-evaluator-collection.d.ts.map +1 -0
  63. package/dist/processing-evaluator-collection.js +35 -0
  64. package/dist/processing-evaluator-collection.js.map +1 -0
  65. package/dist/processing-evaluator-comparison.d.ts +8 -0
  66. package/dist/processing-evaluator-comparison.d.ts.map +1 -0
  67. package/dist/processing-evaluator-comparison.js +88 -0
  68. package/dist/processing-evaluator-comparison.js.map +1 -0
  69. package/dist/processing-evaluator-custom.d.ts +8 -0
  70. package/dist/processing-evaluator-custom.d.ts.map +1 -0
  71. package/dist/processing-evaluator-custom.js +14 -0
  72. package/dist/processing-evaluator-custom.js.map +1 -0
  73. package/dist/processing-evaluator-geometry.d.ts +8 -0
  74. package/dist/processing-evaluator-geometry.d.ts.map +1 -0
  75. package/dist/processing-evaluator-geometry.js +21 -0
  76. package/dist/processing-evaluator-geometry.js.map +1 -0
  77. package/dist/processing-evaluator-numeric.d.ts +8 -0
  78. package/dist/processing-evaluator-numeric.d.ts.map +1 -0
  79. package/dist/processing-evaluator-numeric.js +112 -0
  80. package/dist/processing-evaluator-numeric.js.map +1 -0
  81. package/dist/processing-evaluator-string.d.ts +8 -0
  82. package/dist/processing-evaluator-string.d.ts.map +1 -0
  83. package/dist/processing-evaluator-string.js +33 -0
  84. package/dist/processing-evaluator-string.js.map +1 -0
  85. package/dist/processing-evaluator-variable.d.ts +8 -0
  86. package/dist/processing-evaluator-variable.d.ts.map +1 -0
  87. package/dist/processing-evaluator-variable.js +39 -0
  88. package/dist/processing-evaluator-variable.js.map +1 -0
  89. package/dist/processing-evaluator.d.ts +22 -0
  90. package/dist/processing-evaluator.d.ts.map +1 -0
  91. package/dist/processing-evaluator.js +137 -0
  92. package/dist/processing-evaluator.js.map +1 -0
  93. package/dist/processing-expression-children.d.ts +3 -0
  94. package/dist/processing-expression-children.d.ts.map +1 -0
  95. package/dist/processing-expression-children.js +68 -0
  96. package/dist/processing-expression-children.js.map +1 -0
  97. package/dist/processing-mapping.d.ts +13 -0
  98. package/dist/processing-mapping.d.ts.map +1 -0
  99. package/dist/processing-mapping.js +120 -0
  100. package/dist/processing-mapping.js.map +1 -0
  101. package/dist/processing-operators.d.ts +11 -0
  102. package/dist/processing-operators.d.ts.map +1 -0
  103. package/dist/processing-operators.js +178 -0
  104. package/dist/processing-operators.js.map +1 -0
  105. package/dist/processing-random.d.ts +2 -0
  106. package/dist/processing-random.d.ts.map +1 -0
  107. package/dist/processing-random.js +16 -0
  108. package/dist/processing-random.js.map +1 -0
  109. package/dist/processing-state.d.ts +8 -0
  110. package/dist/processing-state.d.ts.map +1 -0
  111. package/dist/processing-state.js +44 -0
  112. package/dist/processing-state.js.map +1 -0
  113. package/dist/processing-templates.d.ts +3 -0
  114. package/dist/processing-templates.d.ts.map +1 -0
  115. package/dist/processing-templates.js +12 -0
  116. package/dist/processing-templates.js.map +1 -0
  117. package/dist/processing-values.d.ts +15 -0
  118. package/dist/processing-values.d.ts.map +1 -0
  119. package/dist/processing-values.js +150 -0
  120. package/dist/processing-values.js.map +1 -0
  121. package/dist/processing-variables.d.ts +7 -0
  122. package/dist/processing-variables.d.ts.map +1 -0
  123. package/dist/processing-variables.js +40 -0
  124. package/dist/processing-variables.js.map +1 -0
  125. package/dist/serializer-processing-expressions.d.ts.map +1 -1
  126. package/dist/serializer-processing-expressions.js +2 -8
  127. package/dist/serializer-processing-expressions.js.map +1 -1
  128. package/dist/serializer-processing-xml.js +2 -2
  129. package/dist/serializer-processing-xml.js.map +1 -1
  130. package/dist/serializer-processing.d.ts.map +1 -1
  131. package/dist/serializer-processing.js +3 -0
  132. package/dist/serializer-processing.js.map +1 -1
  133. package/dist/server-scoring.d.ts +4 -14
  134. package/dist/server-scoring.d.ts.map +1 -1
  135. package/dist/server-scoring.js +19 -157
  136. package/dist/server-scoring.js.map +1 -1
  137. package/dist/session.d.ts +5 -14
  138. package/dist/session.d.ts.map +1 -1
  139. package/dist/session.js +76 -1158
  140. package/dist/session.js.map +1 -1
  141. package/dist/shared-vocabulary-coverage-policy.js +1 -1
  142. package/dist/shared-vocabulary-coverage-policy.js.map +1 -1
  143. package/dist/shared-vocabulary-generated-families.js +1 -1
  144. package/dist/shared-vocabulary-generated-families.js.map +1 -1
  145. package/dist/shared-vocabulary-support.js +3 -3
  146. package/dist/shared-vocabulary-support.js.map +1 -1
  147. package/dist/shared-vocabulary.d.ts.map +1 -1
  148. package/dist/shared-vocabulary.js +4 -3
  149. package/dist/shared-vocabulary.js.map +1 -1
  150. package/dist/support-evidence.d.ts +22 -0
  151. package/dist/support-evidence.d.ts.map +1 -0
  152. package/dist/support-evidence.js +93 -0
  153. package/dist/support-evidence.js.map +1 -0
  154. package/dist/support.d.ts.map +1 -1
  155. package/dist/support.js +38 -47
  156. package/dist/support.js.map +1 -1
  157. package/dist/trusted-item-session.d.ts +49 -0
  158. package/dist/trusted-item-session.d.ts.map +1 -0
  159. package/dist/trusted-item-session.js +261 -0
  160. package/dist/trusted-item-session.js.map +1 -0
  161. package/dist/tts.d.ts.map +1 -1
  162. package/dist/tts.js +16 -2
  163. package/dist/tts.js.map +1 -1
  164. package/dist/validation-geometry.d.ts +8 -0
  165. package/dist/validation-geometry.d.ts.map +1 -0
  166. package/dist/validation-geometry.js +187 -0
  167. package/dist/validation-geometry.js.map +1 -0
  168. package/dist/validation-interactions.d.ts +3 -0
  169. package/dist/validation-interactions.d.ts.map +1 -0
  170. package/dist/validation-interactions.js +644 -0
  171. package/dist/validation-interactions.js.map +1 -0
  172. package/dist/validation-primitives.d.ts +11 -0
  173. package/dist/validation-primitives.d.ts.map +1 -0
  174. package/dist/validation-primitives.js +48 -0
  175. package/dist/validation-primitives.js.map +1 -0
  176. package/dist/validation-processing.d.ts +4 -0
  177. package/dist/validation-processing.d.ts.map +1 -0
  178. package/dist/validation-processing.js +569 -0
  179. package/dist/validation-processing.js.map +1 -0
  180. package/dist/validation-shared-vocabulary-layout.d.ts +10 -0
  181. package/dist/validation-shared-vocabulary-layout.d.ts.map +1 -0
  182. package/dist/validation-shared-vocabulary-layout.js +102 -0
  183. package/dist/validation-shared-vocabulary-layout.js.map +1 -0
  184. package/dist/validation.d.ts.map +1 -1
  185. package/dist/validation.js +27 -1641
  186. package/dist/validation.js.map +1 -1
  187. package/dist/xml.js +1 -1
  188. package/dist/xml.js.map +1 -1
  189. package/package.json +4 -2
  190. package/src/adaptive-turn-materializer.ts +85 -0
  191. package/src/adaptive-turn.ts +109 -0
  192. package/src/assert-never.ts +4 -0
  193. package/src/attempt-state-constants.ts +5 -0
  194. package/src/attempt-state.ts +245 -0
  195. package/src/catalog.ts +1 -1
  196. package/src/content-text.ts +3 -0
  197. package/src/custom-operators.ts +12 -0
  198. package/src/delivery-redaction.ts +268 -0
  199. package/src/delivery-security.ts +59 -245
  200. package/src/index.ts +9 -0
  201. package/src/parser-custom-interactions.ts +1 -1
  202. package/src/parser-declarations.ts +4 -3
  203. package/src/parser-item-metadata.ts +8 -7
  204. package/src/parser-processing.ts +2 -2
  205. package/src/parser-values.ts +36 -0
  206. package/src/processing-evaluator-boolean.ts +48 -0
  207. package/src/processing-evaluator-collection.ts +50 -0
  208. package/src/processing-evaluator-comparison.ts +106 -0
  209. package/src/processing-evaluator-custom.ts +20 -0
  210. package/src/processing-evaluator-geometry.ts +26 -0
  211. package/src/processing-evaluator-numeric.ts +155 -0
  212. package/src/processing-evaluator-string.ts +60 -0
  213. package/src/processing-evaluator-variable.ts +81 -0
  214. package/src/processing-evaluator.ts +173 -0
  215. package/src/processing-expression-children.ts +69 -0
  216. package/src/processing-mapping.ts +153 -0
  217. package/src/processing-operators.ts +184 -0
  218. package/src/processing-random.ts +16 -0
  219. package/src/processing-state.ts +69 -0
  220. package/src/processing-templates.ts +12 -0
  221. package/src/processing-values.ts +162 -0
  222. package/src/processing-variables.ts +83 -0
  223. package/src/serializer-processing-expressions.ts +2 -12
  224. package/src/serializer-processing-xml.ts +2 -2
  225. package/src/serializer-processing.ts +3 -0
  226. package/src/server-scoring.ts +30 -226
  227. package/src/session.ts +128 -2006
  228. package/src/shared-vocabulary-coverage-policy.ts +1 -1
  229. package/src/shared-vocabulary-generated-families.ts +1 -1
  230. package/src/shared-vocabulary-support.ts +3 -3
  231. package/src/shared-vocabulary.ts +3 -2
  232. package/src/support-evidence.ts +107 -0
  233. package/src/support.ts +68 -54
  234. package/src/trusted-item-session.ts +429 -0
  235. package/src/tts.ts +29 -15
  236. package/src/validation-geometry.ts +204 -0
  237. package/src/validation-interactions.ts +831 -0
  238. package/src/validation-primitives.ts +63 -0
  239. package/src/validation-processing.ts +785 -0
  240. package/src/validation-shared-vocabulary-layout.ts +117 -0
  241. package/src/validation.ts +44 -2077
  242. package/src/xml.ts +1 -1
  243. package/src/interaction-test-fixtures.ts +0 -44
  244. package/src/serializer-processing.fixtures.ts +0 -469
@@ -0,0 +1,831 @@
1
+ import type {
2
+ QtiAssessmentItem,
3
+ QtiBaseType,
4
+ QtiCardinality,
5
+ QtiChoice,
6
+ QtiDiagnostic,
7
+ QtiInteraction,
8
+ QtiResponseDeclaration,
9
+ } from "./types.js";
10
+ import { assertNever } from "./assert-never.js";
11
+ import { isValidQtiPatternMask } from "./pattern-mask.js";
12
+ import { validateInteractionSharedVocabulary } from "./shared-vocabulary-interaction-validation.js";
13
+ import { qtiValueToStringList } from "./value-format.js";
14
+ import {
15
+ validateGraphicHotspotObjectDimensions,
16
+ validateHotspotGeometry,
17
+ } from "./validation-geometry.js";
18
+ import {
19
+ isBooleanAttribute,
20
+ isFiniteNumber,
21
+ isNonNegativeInteger,
22
+ requireIdentifier,
23
+ } from "./validation-primitives.js";
24
+
25
+ export function validateInteractions(item: QtiAssessmentItem, diagnostics: QtiDiagnostic[]): void {
26
+ const responseDeclarations = new Map(
27
+ item.responseDeclarations.map((declaration) => [declaration.identifier, declaration]),
28
+ );
29
+ const responseIdentifiers = new Set(responseDeclarations.keys());
30
+ for (const interaction of item.interactions) {
31
+ validateInteractionResponseReference(interaction, responseIdentifiers, diagnostics);
32
+ validateInteractionResponseShape(interaction, diagnostics);
33
+ validateInteractionSharedVocabulary(interaction, diagnostics);
34
+ validateInteractionChoices(interaction, diagnostics);
35
+ validateInteractionChildren(interaction, diagnostics);
36
+ validateInteractionRequiredAttributes(interaction, diagnostics);
37
+ validatePortableCustomInteraction(interaction, item, diagnostics);
38
+ validateInteractionLimitAttributes(interaction, diagnostics);
39
+ validatePatternMaskAttribute(interaction, diagnostics);
40
+ validateGraphicHotspotObjectDimensions(interaction, diagnostics);
41
+ validateCorrectResponseReferences(
42
+ interaction,
43
+ interaction.responseIdentifier
44
+ ? responseDeclarations.get(interaction.responseIdentifier)
45
+ : undefined,
46
+ diagnostics,
47
+ );
48
+ validateMappingReferences(
49
+ interaction,
50
+ interaction.responseIdentifier
51
+ ? responseDeclarations.get(interaction.responseIdentifier)
52
+ : undefined,
53
+ diagnostics,
54
+ );
55
+ }
56
+ }
57
+
58
+ function validateInteractionResponseReference(
59
+ interaction: QtiInteraction,
60
+ responseIdentifiers: Set<string>,
61
+ diagnostics: QtiDiagnostic[],
62
+ ): void {
63
+ if (!interaction.responseIdentifier) {
64
+ if (interaction.type !== "endAttempt") {
65
+ diagnostics.push({
66
+ code: "interaction.responseIdentifier",
67
+ severity: "error",
68
+ message: `${interaction.qtiName} is missing response-identifier.`,
69
+ path: interaction.source?.path,
70
+ source: interaction.source,
71
+ });
72
+ }
73
+ return;
74
+ }
75
+
76
+ if (!responseIdentifiers.has(interaction.responseIdentifier)) {
77
+ diagnostics.push({
78
+ code: "interaction.responseIdentifier.reference",
79
+ severity: "error",
80
+ message: `${interaction.qtiName} references missing response declaration ${interaction.responseIdentifier}.`,
81
+ path: interaction.source?.path,
82
+ source: interaction.source,
83
+ });
84
+ }
85
+ }
86
+
87
+ function validateInteractionResponseShape(
88
+ interaction: QtiInteraction,
89
+ diagnostics: QtiDiagnostic[],
90
+ ): void {
91
+ const expected = expectedResponseShape(interaction);
92
+ if (!expected) return;
93
+
94
+ if (
95
+ interaction.responseCardinality &&
96
+ !expected.cardinalities.includes(interaction.responseCardinality)
97
+ ) {
98
+ diagnostics.push({
99
+ code: "interaction.cardinality",
100
+ severity: "error",
101
+ message: `${interaction.qtiName} expects ${expected.cardinalities.join(" or ")} cardinality, got ${interaction.responseCardinality}.`,
102
+ path: interaction.source?.path,
103
+ source: interaction.source,
104
+ });
105
+ }
106
+
107
+ if (interaction.responseBaseType && !expected.baseTypes.includes(interaction.responseBaseType)) {
108
+ diagnostics.push({
109
+ code: "interaction.baseType",
110
+ severity: "error",
111
+ message: `${interaction.qtiName} expects ${expected.baseTypes.join(" or ")} base type, got ${interaction.responseBaseType}.`,
112
+ path: interaction.source?.path,
113
+ source: interaction.source,
114
+ });
115
+ }
116
+ }
117
+
118
+ function validateInteractionChoices(
119
+ interaction: QtiInteraction,
120
+ diagnostics: QtiDiagnostic[],
121
+ ): void {
122
+ const identifiers = new Set<string>();
123
+ for (const choice of interaction.choices) {
124
+ requireIdentifier(choice.qtiName, choice.attributes.identifier, diagnostics, choice.source);
125
+ if (!choice.identifier) continue;
126
+ if (identifiers.has(choice.identifier)) {
127
+ diagnostics.push({
128
+ code: "choice.identifier.duplicate",
129
+ severity: "error",
130
+ message: `${interaction.qtiName} has duplicate choice identifier ${choice.identifier}.`,
131
+ path: choice.source?.path ?? interaction.source?.path,
132
+ source: choice.source ?? interaction.source,
133
+ });
134
+ }
135
+ identifiers.add(choice.identifier);
136
+ }
137
+
138
+ if (
139
+ needsChoices(interaction) &&
140
+ interaction.choices.filter((choice) => choice.role !== "gap").length === 0
141
+ ) {
142
+ diagnostics.push({
143
+ code: "interaction.choices.required",
144
+ severity: "error",
145
+ message: `${interaction.qtiName} requires at least one choice.`,
146
+ path: interaction.source?.path,
147
+ source: interaction.source,
148
+ });
149
+ }
150
+
151
+ for (const choice of interaction.choices) {
152
+ validateChoiceLimitAttributes(choice, diagnostics);
153
+ }
154
+ }
155
+
156
+ function validateCorrectResponseReferences(
157
+ interaction: QtiInteraction,
158
+ declaration: QtiResponseDeclaration | undefined,
159
+ diagnostics: QtiDiagnostic[],
160
+ ): void {
161
+ if (!declaration || declaration.correctResponse === null) return;
162
+ if (
163
+ declaration.baseType !== "identifier" &&
164
+ declaration.baseType !== "pair" &&
165
+ declaration.baseType !== "directedPair"
166
+ ) {
167
+ return;
168
+ }
169
+
170
+ const identifiers = new Set(
171
+ interaction.choices
172
+ .map((choice) => choice.identifier)
173
+ .filter((identifier) => identifier.length > 0),
174
+ );
175
+ if (identifiers.size === 0) return;
176
+
177
+ for (const value of responseValues(declaration.correctResponse)) {
178
+ if (declaration.baseType === "identifier") {
179
+ if (identifiers.has(value)) continue;
180
+ invalidCorrectResponseReference(interaction, declaration, value, diagnostics);
181
+ continue;
182
+ }
183
+
184
+ const parts = value.trim().split(/\s+/);
185
+ if (parts.length !== 2 || parts.some((part) => !identifiers.has(part))) {
186
+ invalidCorrectResponseReference(interaction, declaration, value, diagnostics);
187
+ }
188
+ }
189
+ }
190
+
191
+ function responseValues(value: QtiResponseDeclaration["correctResponse"]): string[] {
192
+ return qtiValueToStringList(value);
193
+ }
194
+
195
+ function invalidCorrectResponseReference(
196
+ interaction: QtiInteraction,
197
+ declaration: QtiResponseDeclaration,
198
+ value: string,
199
+ diagnostics: QtiDiagnostic[],
200
+ ): void {
201
+ diagnostics.push({
202
+ code: "response.correctResponse.reference",
203
+ severity: "error",
204
+ message: `Response declaration ${declaration.identifier} correct response ${value} does not reference choices in ${interaction.qtiName}.`,
205
+ path: declaration.source?.path,
206
+ source: declaration.source,
207
+ });
208
+ }
209
+
210
+ function validateMappingReferences(
211
+ interaction: QtiInteraction,
212
+ declaration: QtiResponseDeclaration | undefined,
213
+ diagnostics: QtiDiagnostic[],
214
+ ): void {
215
+ if (!declaration?.mapping) return;
216
+ if (
217
+ declaration.baseType !== "identifier" &&
218
+ declaration.baseType !== "pair" &&
219
+ declaration.baseType !== "directedPair"
220
+ ) {
221
+ return;
222
+ }
223
+
224
+ const identifiers = new Set(
225
+ interaction.choices
226
+ .map((choice) => choice.identifier)
227
+ .filter((identifier) => identifier.length > 0),
228
+ );
229
+ if (identifiers.size === 0) return;
230
+
231
+ for (const entry of declaration.mapping.entries) {
232
+ const mapKey = entry.mapKey;
233
+ if (!mapKey) continue;
234
+ if (declaration.baseType === "identifier") {
235
+ if (identifiers.has(mapKey)) continue;
236
+ invalidMappingReference(interaction, declaration, entry, diagnostics);
237
+ continue;
238
+ }
239
+
240
+ const parts = mapKey.trim().split(/\s+/);
241
+ if (parts.length !== 2 || parts.some((part) => !identifiers.has(part))) {
242
+ invalidMappingReference(interaction, declaration, entry, diagnostics);
243
+ }
244
+ }
245
+ }
246
+
247
+ function invalidMappingReference(
248
+ interaction: QtiInteraction,
249
+ declaration: QtiResponseDeclaration,
250
+ entry: NonNullable<QtiResponseDeclaration["mapping"]>["entries"][number],
251
+ diagnostics: QtiDiagnostic[],
252
+ ): void {
253
+ diagnostics.push({
254
+ code: "mapping.mapKey.reference",
255
+ severity: "error",
256
+ message: `Response declaration ${declaration.identifier} map-key ${entry.mapKey ?? ""} does not reference choices in ${interaction.qtiName}.`,
257
+ path: entry.source?.path ?? declaration.source?.path,
258
+ source: entry.source ?? declaration.source,
259
+ });
260
+ }
261
+
262
+ function validateInteractionChildren(
263
+ interaction: QtiInteraction,
264
+ diagnostics: QtiDiagnostic[],
265
+ ): void {
266
+ const allowed = allowedInteractionChildren(interaction);
267
+ if (!allowed) return;
268
+
269
+ for (const child of interaction.childElements) {
270
+ if (allowed.has(child.qtiName)) continue;
271
+ diagnostics.push({
272
+ code: "interaction.child.unsupported",
273
+ severity: "error",
274
+ message: `${interaction.qtiName} does not allow ${child.qtiName} as a direct child.`,
275
+ path: child.source?.path ?? interaction.source?.path,
276
+ source: child.source ?? interaction.source,
277
+ });
278
+ }
279
+ }
280
+
281
+ function validateInteractionRequiredAttributes(
282
+ interaction: QtiInteraction,
283
+ diagnostics: QtiDiagnostic[],
284
+ ): void {
285
+ if (requiresObject(interaction) && !hasRequiredObjectAsset(interaction)) {
286
+ diagnostics.push({
287
+ code: "interaction.object.required",
288
+ severity: "error",
289
+ message:
290
+ interaction.type === "drawing"
291
+ ? `${interaction.qtiName} requires an object, img, or picture canvas with a data/src attribute.`
292
+ : `${interaction.qtiName} requires an object, img, audio, or video child with a data/src attribute or media sources.`,
293
+ path: interaction.source?.path,
294
+ source: interaction.source,
295
+ });
296
+ }
297
+
298
+ if (interaction.type === "portableCustom") {
299
+ requireInteractionAttribute(
300
+ interaction,
301
+ "custom-interaction-type-identifier",
302
+ "interaction.portableCustom.typeIdentifier",
303
+ diagnostics,
304
+ );
305
+ }
306
+
307
+ if (interaction.type === "slider") {
308
+ const lower = interaction.attributes["lower-bound"];
309
+ const upper = interaction.attributes["upper-bound"];
310
+ requireInteractionAttribute(
311
+ interaction,
312
+ "lower-bound",
313
+ "interaction.slider.lowerBound",
314
+ diagnostics,
315
+ );
316
+ requireInteractionAttribute(
317
+ interaction,
318
+ "upper-bound",
319
+ "interaction.slider.upperBound",
320
+ diagnostics,
321
+ );
322
+ if (lower !== undefined && !isFiniteNumber(lower)) {
323
+ invalidNumber(interaction, "lower-bound", lower, diagnostics);
324
+ }
325
+ if (upper !== undefined && !isFiniteNumber(upper)) {
326
+ invalidNumber(interaction, "upper-bound", upper, diagnostics);
327
+ }
328
+ if (
329
+ lower !== undefined &&
330
+ upper !== undefined &&
331
+ isFiniteNumber(lower) &&
332
+ isFiniteNumber(upper)
333
+ ) {
334
+ if (Number(lower) >= Number(upper)) {
335
+ diagnostics.push({
336
+ code: "interaction.slider.bounds",
337
+ severity: "error",
338
+ message: `${interaction.qtiName} requires lower-bound to be less than upper-bound.`,
339
+ path: interaction.source?.path,
340
+ source: interaction.source,
341
+ });
342
+ }
343
+ }
344
+ const step = interaction.attributes.step;
345
+ if (step !== undefined && (!isFiniteNumber(step) || Number(step) <= 0)) {
346
+ invalidNumber(interaction, "step", step, diagnostics);
347
+ }
348
+ }
349
+ }
350
+
351
+ function requiresObject(interaction: QtiInteraction): boolean {
352
+ return (
353
+ interaction.type === "graphicOrder" ||
354
+ interaction.type === "graphicAssociate" ||
355
+ interaction.type === "graphicGapMatch" ||
356
+ interaction.type === "hotspot" ||
357
+ interaction.type === "selectPoint" ||
358
+ interaction.type === "positionObject" ||
359
+ interaction.type === "media" ||
360
+ interaction.type === "drawing"
361
+ );
362
+ }
363
+
364
+ function hasRequiredObjectAsset(interaction: QtiInteraction): boolean {
365
+ if (interaction.type === "media") {
366
+ return Boolean(
367
+ interaction.object?.data || interaction.object?.sources.some((source) => Boolean(source.src)),
368
+ );
369
+ }
370
+ if (interaction.type === "drawing") return Boolean(interaction.object?.data);
371
+ return Boolean(interaction.object?.data);
372
+ }
373
+
374
+ function requireInteractionAttribute(
375
+ interaction: QtiInteraction,
376
+ attribute: string,
377
+ code: string,
378
+ diagnostics: QtiDiagnostic[],
379
+ ): void {
380
+ if (interaction.attributes[attribute]) return;
381
+ diagnostics.push({
382
+ code,
383
+ severity: "error",
384
+ message: `${interaction.qtiName} requires ${attribute}.`,
385
+ path: interaction.source?.path,
386
+ source: interaction.source,
387
+ });
388
+ }
389
+
390
+ function validatePortableCustomInteraction(
391
+ interaction: QtiInteraction,
392
+ item: QtiAssessmentItem,
393
+ diagnostics: QtiDiagnostic[],
394
+ ): void {
395
+ if (interaction.type !== "portableCustom") return;
396
+ const definition = interaction.portableCustom;
397
+ if (!definition) return;
398
+
399
+ const configuredModules = definition.interactionModules?.modules ?? [];
400
+ const hasModuleAttribute = Boolean(definition.module?.trim());
401
+ const hasConfiguredModule = configuredModules.some((module) => Boolean(module.id?.trim()));
402
+ if (!hasModuleAttribute && !hasConfiguredModule) {
403
+ diagnostics.push({
404
+ code: "interaction.portableCustom.module",
405
+ severity: "error",
406
+ message: `${interaction.qtiName} requires a module attribute or at least one qti-interaction-module id.`,
407
+ path: interaction.source?.path,
408
+ source: interaction.source,
409
+ });
410
+ }
411
+
412
+ for (const module of configuredModules) {
413
+ if (!module.id?.trim()) {
414
+ diagnostics.push({
415
+ code: "interaction.portableCustom.moduleId",
416
+ severity: "error",
417
+ message: "qti-interaction-module requires a non-empty id.",
418
+ path: module.source?.path,
419
+ source: module.source,
420
+ });
421
+ }
422
+ warnExternalPortableCustomUrl(module.primaryPath, module.source, diagnostics);
423
+ warnExternalPortableCustomUrl(module.fallbackPath, module.source, diagnostics);
424
+ }
425
+
426
+ warnExternalPortableCustomUrl(
427
+ definition.interactionModules?.primaryConfiguration,
428
+ definition.interactionModules?.source,
429
+ diagnostics,
430
+ );
431
+ warnExternalPortableCustomUrl(
432
+ definition.interactionModules?.secondaryConfiguration,
433
+ definition.interactionModules?.source,
434
+ diagnostics,
435
+ );
436
+
437
+ const templateIdentifiers = new Set(
438
+ item.templateDeclarations.map((declaration) => declaration.identifier),
439
+ );
440
+ for (const variable of definition.templateVariables) {
441
+ if (!variable.identifier?.trim()) {
442
+ diagnostics.push({
443
+ code: "interaction.portableCustom.templateVariable",
444
+ severity: "error",
445
+ message: "qti-template-variable requires template-identifier or identifier.",
446
+ path: variable.source?.path,
447
+ source: variable.source,
448
+ });
449
+ continue;
450
+ }
451
+ if (!templateIdentifiers.has(variable.identifier)) {
452
+ diagnostics.push({
453
+ code: "interaction.portableCustom.templateVariable.reference",
454
+ severity: "error",
455
+ message: `qti-template-variable references missing template declaration ${variable.identifier}.`,
456
+ path: variable.source?.path,
457
+ source: variable.source,
458
+ });
459
+ }
460
+ }
461
+
462
+ for (const variable of definition.contextVariables) {
463
+ if (variable.identifier?.trim()) continue;
464
+ diagnostics.push({
465
+ code: "interaction.portableCustom.contextVariable",
466
+ severity: "error",
467
+ message: "qti-context-variable requires identifier.",
468
+ path: variable.source?.path,
469
+ source: variable.source,
470
+ });
471
+ }
472
+
473
+ for (const stylesheet of definition.stylesheets) {
474
+ if (stylesheet.href.trim().length > 0) continue;
475
+ diagnostics.push({
476
+ code: "stylesheet.href.required",
477
+ severity: "error",
478
+ message: "qti-stylesheet requires a non-empty href attribute.",
479
+ path: stylesheet.source?.path,
480
+ source: stylesheet.source,
481
+ });
482
+ }
483
+ }
484
+
485
+ function warnExternalPortableCustomUrl(
486
+ url: string | undefined,
487
+ source: QtiDiagnostic["source"],
488
+ diagnostics: QtiDiagnostic[],
489
+ ): void {
490
+ if (!url || !/^https?:\/\//i.test(url)) return;
491
+ diagnostics.push({
492
+ code: "interaction.portableCustom.externalModuleUrl",
493
+ severity: "warning",
494
+ message: `Portable custom interaction module URL ${url} requires host delivery policy approval.`,
495
+ path: source?.path,
496
+ source,
497
+ });
498
+ }
499
+
500
+ function invalidNumber(
501
+ interaction: QtiInteraction,
502
+ attribute: string,
503
+ value: string,
504
+ diagnostics: QtiDiagnostic[],
505
+ ): void {
506
+ diagnostics.push({
507
+ code: "interaction.numericAttribute",
508
+ severity: "error",
509
+ message: `${interaction.qtiName} requires numeric ${attribute}, got ${value}.`,
510
+ path: interaction.source?.path,
511
+ source: interaction.source,
512
+ });
513
+ }
514
+
515
+ function validatePatternMaskAttribute(
516
+ interaction: QtiInteraction,
517
+ diagnostics: QtiDiagnostic[],
518
+ ): void {
519
+ if (interaction.type !== "textEntry" && interaction.type !== "extendedText") return;
520
+ const patternMask = interaction.attributes["pattern-mask"];
521
+ if (patternMask === undefined || isValidQtiPatternMask(patternMask)) return;
522
+ diagnostics.push({
523
+ code: "interaction.patternMask.invalid",
524
+ severity: "error",
525
+ message: `${interaction.qtiName} pattern-mask is not a valid regular expression.`,
526
+ path: interaction.source?.path,
527
+ source: interaction.source,
528
+ });
529
+ }
530
+
531
+ function validateInteractionLimitAttributes(
532
+ interaction: QtiInteraction,
533
+ diagnostics: QtiDiagnostic[],
534
+ ): void {
535
+ validateNonNegativeIntegerAttribute(interaction, "max-choices", diagnostics);
536
+ validateNonNegativeIntegerAttribute(interaction, "min-choices", diagnostics);
537
+ validateNonNegativeIntegerAttribute(interaction, "max-associations", diagnostics);
538
+ validateNonNegativeIntegerAttribute(interaction, "min-associations", diagnostics);
539
+ validateNonNegativeIntegerAttribute(interaction, "expected-length", diagnostics);
540
+ validateNonNegativeIntegerAttribute(interaction, "expected-lines", diagnostics);
541
+
542
+ validateMinMaxPair(interaction, "min-choices", "max-choices", diagnostics);
543
+ validateMinMaxPair(interaction, "min-associations", "max-associations", diagnostics);
544
+ if (interaction.type === "media") {
545
+ validateNonNegativeIntegerAttribute(interaction, "max-plays", diagnostics);
546
+ validateNonNegativeIntegerAttribute(interaction, "min-plays", diagnostics);
547
+ validateBooleanAttribute(interaction, "autostart", diagnostics);
548
+ validateBooleanAttribute(interaction, "loop", diagnostics);
549
+ validateMinMaxPair(interaction, "min-plays", "max-plays", diagnostics);
550
+ }
551
+ }
552
+
553
+ function validateChoiceLimitAttributes(choice: QtiChoice, diagnostics: QtiDiagnostic[]): void {
554
+ if (requiresMatchMax(choice) && !choice.attributes["match-max"]) {
555
+ diagnostics.push({
556
+ code: "choice.matchMax.required",
557
+ severity: "error",
558
+ message: `${choice.qtiName} ${choice.identifier} requires match-max.`,
559
+ path: choice.source?.path,
560
+ source: choice.source,
561
+ });
562
+ }
563
+
564
+ validateChoiceNonNegativeIntegerAttribute(choice, "match-max", diagnostics);
565
+ validateChoiceNonNegativeIntegerAttribute(choice, "match-min", diagnostics);
566
+ validateChoiceMinMaxPair(choice, "match-min", "match-max", diagnostics);
567
+ validateGapImageAsset(choice, diagnostics);
568
+ validateHotspotGeometry(choice, diagnostics);
569
+ }
570
+
571
+ function validateGapImageAsset(choice: QtiChoice, diagnostics: QtiDiagnostic[]): void {
572
+ if (choice.qtiName !== "qti-gap-img" || choice.asset?.data) return;
573
+ diagnostics.push({
574
+ code: "choice.gapImg.media.required",
575
+ severity: "error",
576
+ message: `qti-gap-img ${choice.identifier} requires an img, object, or picture child with a usable src or data attribute.`,
577
+ path: choice.source?.path,
578
+ source: choice.source,
579
+ });
580
+ }
581
+
582
+ function requiresMatchMax(choice: QtiChoice): boolean {
583
+ return (
584
+ choice.qtiName === "qti-simple-associable-choice" ||
585
+ choice.qtiName === "qti-associable-hotspot" ||
586
+ choice.qtiName === "qti-gap-text" ||
587
+ choice.qtiName === "qti-gap-img"
588
+ );
589
+ }
590
+
591
+ function validateNonNegativeIntegerAttribute(
592
+ interaction: QtiInteraction,
593
+ attribute: string,
594
+ diagnostics: QtiDiagnostic[],
595
+ ): void {
596
+ const value = interaction.attributes[attribute];
597
+ if (value === undefined || isNonNegativeInteger(value)) return;
598
+ diagnostics.push({
599
+ code: "interaction.integerAttribute",
600
+ severity: "error",
601
+ message: `${interaction.qtiName} requires non-negative integer ${attribute}, got ${value}.`,
602
+ path: interaction.source?.path,
603
+ source: interaction.source,
604
+ });
605
+ }
606
+
607
+ function validateBooleanAttribute(
608
+ interaction: QtiInteraction,
609
+ attribute: string,
610
+ diagnostics: QtiDiagnostic[],
611
+ ): void {
612
+ const value = interaction.attributes[attribute];
613
+ if (value === undefined || isBooleanAttribute(value)) return;
614
+ diagnostics.push({
615
+ code: "interaction.booleanAttribute",
616
+ severity: "error",
617
+ message: `${interaction.qtiName} requires boolean ${attribute}, got ${value}.`,
618
+ path: interaction.source?.path,
619
+ source: interaction.source,
620
+ });
621
+ }
622
+
623
+ function validateChoiceNonNegativeIntegerAttribute(
624
+ choice: QtiChoice,
625
+ attribute: string,
626
+ diagnostics: QtiDiagnostic[],
627
+ ): void {
628
+ const value = choice.attributes[attribute];
629
+ if (value === undefined || isNonNegativeInteger(value)) return;
630
+ diagnostics.push({
631
+ code: "choice.integerAttribute",
632
+ severity: "error",
633
+ message: `${choice.qtiName} ${choice.identifier} requires non-negative integer ${attribute}, got ${value}.`,
634
+ path: choice.source?.path,
635
+ source: choice.source,
636
+ });
637
+ }
638
+
639
+ function validateChoiceMinMaxPair(
640
+ choice: QtiChoice,
641
+ minAttribute: string,
642
+ maxAttribute: string,
643
+ diagnostics: QtiDiagnostic[],
644
+ ): void {
645
+ const min = choice.attributes[minAttribute];
646
+ const max = choice.attributes[maxAttribute];
647
+ if (
648
+ min === undefined ||
649
+ max === undefined ||
650
+ !isNonNegativeInteger(min) ||
651
+ !isNonNegativeInteger(max) ||
652
+ max === "0"
653
+ ) {
654
+ return;
655
+ }
656
+ if (Number(min) <= Number(max)) return;
657
+ diagnostics.push({
658
+ code: "choice.minMax",
659
+ severity: "error",
660
+ message: `${choice.qtiName} ${choice.identifier} requires ${minAttribute} to be less than or equal to ${maxAttribute}, unless ${maxAttribute} is 0 for unlimited.`,
661
+ path: choice.source?.path,
662
+ source: choice.source,
663
+ });
664
+ }
665
+
666
+ function validateMinMaxPair(
667
+ interaction: QtiInteraction,
668
+ minAttribute: string,
669
+ maxAttribute: string,
670
+ diagnostics: QtiDiagnostic[],
671
+ ): void {
672
+ const min = interaction.attributes[minAttribute];
673
+ const max = interaction.attributes[maxAttribute];
674
+ if (
675
+ min === undefined ||
676
+ max === undefined ||
677
+ !isNonNegativeInteger(min) ||
678
+ !isNonNegativeInteger(max) ||
679
+ max === "0"
680
+ ) {
681
+ return;
682
+ }
683
+ if (Number(min) <= Number(max)) return;
684
+ diagnostics.push({
685
+ code: "interaction.minMax",
686
+ severity: "error",
687
+ message: `${interaction.qtiName} requires ${minAttribute} to be less than or equal to ${maxAttribute}, unless ${maxAttribute} is 0 for unlimited.`,
688
+ path: interaction.source?.path,
689
+ source: interaction.source,
690
+ });
691
+ }
692
+
693
+ function allowedInteractionChildren(interaction: QtiInteraction): Set<string> | undefined {
694
+ const common = ["qti-prompt"];
695
+ switch (interaction.type) {
696
+ case "choice":
697
+ return setOf(common, ["qti-simple-choice"]);
698
+ case "order":
699
+ return setOf(common, ["qti-simple-choice"]);
700
+ case "associate":
701
+ return setOf(common, ["qti-simple-match-set", "qti-simple-associable-choice"]);
702
+ case "match":
703
+ return setOf(common, ["qti-simple-match-set"]);
704
+ case "gapMatch":
705
+ return setOf(common, ["qti-gap-text", "qti-gap-img", ...staticContentNames()]);
706
+ case "inlineChoice":
707
+ return setOf(common, ["qti-inline-choice"]);
708
+ case "hottext":
709
+ return setOf(common, staticContentNames());
710
+ case "graphicOrder":
711
+ return setOf(common, ["object", "qti-hotspot-choice"]);
712
+ case "graphicAssociate":
713
+ return setOf(common, ["object", "qti-associable-hotspot"]);
714
+ case "graphicGapMatch":
715
+ return setOf(common, [
716
+ "object",
717
+ "qti-gap-text",
718
+ "qti-gap-img",
719
+ "qti-associable-hotspot",
720
+ ...staticContentNames(),
721
+ ]);
722
+ case "hotspot":
723
+ return setOf(common, ["object", "qti-hotspot-choice"]);
724
+ case "positionObject":
725
+ return setOf(common, ["object", "img", "qti-position-object-stage"]);
726
+ case "selectPoint":
727
+ case "media":
728
+ return setOf(common, ["audio", "video", "object", "img"]);
729
+ case "drawing":
730
+ return setOf(common, ["object", "img", "picture"]);
731
+ case "extendedText":
732
+ return new Set(common);
733
+ case "portableCustom":
734
+ return setOf(common, [
735
+ "qti-interaction-markup",
736
+ "qti-interaction-modules",
737
+ "qti-template-variable",
738
+ "qti-context-variable",
739
+ "qti-stylesheet",
740
+ "qti-catalog-info",
741
+ ]);
742
+ case "slider":
743
+ case "textEntry":
744
+ case "upload":
745
+ case "endAttempt":
746
+ return new Set(common);
747
+ case "custom":
748
+ return undefined;
749
+ default:
750
+ return assertNever(interaction.type);
751
+ }
752
+ }
753
+
754
+ function staticContentNames(): string[] {
755
+ return ["p", "div", "span", "ul", "ol", "li", "table", "tbody", "thead", "tr", "td", "th"];
756
+ }
757
+
758
+ function setOf(...items: string[][]): Set<string> {
759
+ return new Set(items.flat());
760
+ }
761
+
762
+ function expectedResponseShape(
763
+ interaction: QtiInteraction,
764
+ ): { cardinalities: QtiCardinality[]; baseTypes: QtiBaseType[] } | undefined {
765
+ if (interaction.type === "endAttempt") {
766
+ return { cardinalities: ["single"], baseTypes: ["boolean"] };
767
+ }
768
+ if (interaction.type === "media") return { cardinalities: ["single"], baseTypes: ["integer"] };
769
+ if (interaction.type === "custom") return undefined;
770
+ if (interaction.type === "order" || interaction.type === "graphicOrder") {
771
+ return { cardinalities: ["ordered"], baseTypes: ["identifier"] };
772
+ }
773
+ if (interaction.type === "associate" || interaction.type === "graphicAssociate") {
774
+ return { cardinalities: ["multiple"], baseTypes: ["pair", "directedPair"] };
775
+ }
776
+ if (
777
+ interaction.type === "match" ||
778
+ interaction.type === "gapMatch" ||
779
+ interaction.type === "graphicGapMatch"
780
+ ) {
781
+ return { cardinalities: ["multiple"], baseTypes: ["directedPair"] };
782
+ }
783
+ if (interaction.type === "selectPoint" || interaction.type === "positionObject") {
784
+ return { cardinalities: ["single", "multiple"], baseTypes: ["point"] };
785
+ }
786
+ if (interaction.type === "slider") {
787
+ return { cardinalities: ["single"], baseTypes: ["integer", "float"] };
788
+ }
789
+ if (interaction.type === "upload") {
790
+ return { cardinalities: ["single"], baseTypes: ["file"] };
791
+ }
792
+ if (interaction.type === "textEntry" || interaction.type === "extendedText") {
793
+ return { cardinalities: ["single"], baseTypes: ["string"] };
794
+ }
795
+ if (interaction.type === "drawing") return { cardinalities: ["single"], baseTypes: ["file"] };
796
+ if (interaction.type === "portableCustom") {
797
+ return {
798
+ cardinalities: ["single", "multiple", "ordered", "record"],
799
+ baseTypes: [
800
+ "identifier",
801
+ "boolean",
802
+ "integer",
803
+ "float",
804
+ "string",
805
+ "point",
806
+ "pair",
807
+ "directedPair",
808
+ "duration",
809
+ "file",
810
+ "uri",
811
+ ],
812
+ };
813
+ }
814
+ return { cardinalities: ["single", "multiple"], baseTypes: ["identifier"] };
815
+ }
816
+
817
+ function needsChoices(interaction: QtiInteraction): boolean {
818
+ return (
819
+ interaction.type === "choice" ||
820
+ interaction.type === "order" ||
821
+ interaction.type === "associate" ||
822
+ interaction.type === "match" ||
823
+ interaction.type === "gapMatch" ||
824
+ interaction.type === "inlineChoice" ||
825
+ interaction.type === "hottext" ||
826
+ interaction.type === "graphicOrder" ||
827
+ interaction.type === "graphicAssociate" ||
828
+ interaction.type === "graphicGapMatch" ||
829
+ interaction.type === "hotspot"
830
+ );
831
+ }