@longsightgroup/qti3-core 0.11.0 → 0.12.0
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.
- package/dist/binary-choice.d.ts +9 -0
- package/dist/binary-choice.d.ts.map +1 -0
- package/dist/binary-choice.js +81 -0
- package/dist/binary-choice.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/qti-identifier.d.ts +3 -0
- package/dist/qti-identifier.d.ts.map +1 -0
- package/dist/qti-identifier.js +5 -0
- package/dist/qti-identifier.js.map +1 -0
- package/dist/qti-package-paths.d.ts +2 -0
- package/dist/qti-package-paths.d.ts.map +1 -1
- package/dist/qti-package-paths.js +6 -0
- package/dist/qti-package-paths.js.map +1 -1
- package/dist/support.d.ts.map +1 -1
- package/dist/support.js +2 -0
- package/dist/support.js.map +1 -1
- package/dist/test-expression-parser.d.ts +6 -0
- package/dist/test-expression-parser.d.ts.map +1 -0
- package/dist/test-expression-parser.js +76 -0
- package/dist/test-expression-parser.js.map +1 -0
- package/dist/test-expression-serializer.d.ts +5 -0
- package/dist/test-expression-serializer.d.ts.map +1 -0
- package/dist/test-expression-serializer.js +54 -0
- package/dist/test-expression-serializer.js.map +1 -0
- package/dist/test-expression-validation.d.ts +14 -0
- package/dist/test-expression-validation.d.ts.map +1 -0
- package/dist/test-expression-validation.js +101 -0
- package/dist/test-expression-validation.js.map +1 -0
- package/dist/test-expression.d.ts +99 -0
- package/dist/test-expression.d.ts.map +1 -0
- package/dist/test-expression.js +28 -0
- package/dist/test-expression.js.map +1 -0
- package/dist/test-model.d.ts +62 -0
- package/dist/test-model.d.ts.map +1 -0
- package/dist/test-model.js +5 -0
- package/dist/test-model.js.map +1 -0
- package/dist/test-parser.d.ts +13 -0
- package/dist/test-parser.d.ts.map +1 -0
- package/dist/test-parser.js +144 -0
- package/dist/test-parser.js.map +1 -0
- package/dist/test-processing.d.ts +22 -0
- package/dist/test-processing.d.ts.map +1 -0
- package/dist/test-processing.js +84 -0
- package/dist/test-processing.js.map +1 -0
- package/dist/test-session.d.ts +31 -0
- package/dist/test-session.d.ts.map +1 -0
- package/dist/test-session.js +102 -0
- package/dist/test-session.js.map +1 -0
- package/dist/test-support.d.ts +4 -0
- package/dist/test-support.d.ts.map +1 -0
- package/dist/test-support.js +30 -0
- package/dist/test-support.js.map +1 -0
- package/dist/test-validation.d.ts +4 -0
- package/dist/test-validation.d.ts.map +1 -0
- package/dist/test-validation.js +133 -0
- package/dist/test-validation.js.map +1 -0
- package/dist/test-xml.d.ts +7 -0
- package/dist/test-xml.d.ts.map +1 -0
- package/dist/test-xml.js +23 -0
- package/dist/test-xml.js.map +1 -0
- package/dist/types.d.ts +5 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/binary-choice.ts +96 -0
- package/src/index.ts +29 -0
- package/src/qti-identifier.ts +4 -0
- package/src/qti-package-paths.ts +9 -0
- package/src/support.ts +2 -0
- package/src/test-expression-parser.ts +89 -0
- package/src/test-expression-serializer.ts +71 -0
- package/src/test-expression-validation.ts +139 -0
- package/src/test-expression.ts +66 -0
- package/src/test-model.ts +68 -0
- package/src/test-parser.ts +201 -0
- package/src/test-processing.ts +107 -0
- package/src/test-session.ts +160 -0
- package/src/test-support.ts +31 -0
- package/src/test-validation.ts +190 -0
- package/src/test-xml.ts +37 -0
- package/src/types.ts +6 -0
package/src/qti-package-paths.ts
CHANGED
|
@@ -54,3 +54,12 @@ export function normalizePackagePath(
|
|
|
54
54
|
}
|
|
55
55
|
return parts.join("/");
|
|
56
56
|
}
|
|
57
|
+
|
|
58
|
+
/** Whether an item href is a normalized package-local path, without URI routing or traversal. */
|
|
59
|
+
export function isQtiPackageItemHref(href: string): boolean {
|
|
60
|
+
return (
|
|
61
|
+
href.length > 0 &&
|
|
62
|
+
!/^[a-z][a-z0-9+.-]*:|[\\?#\s]/i.test(href) &&
|
|
63
|
+
href.split("/").every((part) => part.length > 0 && part !== "." && part !== "..")
|
|
64
|
+
);
|
|
65
|
+
}
|
package/src/support.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
QtiProcessingElementSupport,
|
|
9
9
|
QtiSourceLocation,
|
|
10
10
|
} from "./types.js";
|
|
11
|
+
import { testExecutionSupport } from "./test-support.js";
|
|
11
12
|
import {
|
|
12
13
|
browserFeedbackTests,
|
|
13
14
|
browserHarnessTests,
|
|
@@ -397,6 +398,7 @@ export const itemMetadataSupport: QtiItemMetadataElementSupport[] = [
|
|
|
397
398
|
];
|
|
398
399
|
|
|
399
400
|
export const elementSupport: QtiElementSupport[] = [
|
|
401
|
+
...testExecutionSupport,
|
|
400
402
|
...interactionSupport,
|
|
401
403
|
...deprecatedInteractionSupport,
|
|
402
404
|
...processingSupport,
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { assertNever } from "./assert-never.js";
|
|
2
|
+
import { coerceValue } from "./parser-values.js";
|
|
3
|
+
import { isTestBaseType, testExpressionSyntax, type QtiTestExpression } from "./test-expression.js";
|
|
4
|
+
import { checkTestXml, rejectTestXml } from "./test-xml.js";
|
|
5
|
+
import type { QtiDiagnostic } from "./types.js";
|
|
6
|
+
import { textContent, type XmlNode } from "./xml.js";
|
|
7
|
+
|
|
8
|
+
/** Read the single expression owned by a branch or outcome assignment. */
|
|
9
|
+
export function parseTestExpressionChild(
|
|
10
|
+
node: XmlNode,
|
|
11
|
+
diagnostics: QtiDiagnostic[],
|
|
12
|
+
): QtiTestExpression | undefined {
|
|
13
|
+
const first = node.children[0];
|
|
14
|
+
if (node.children.length !== 1 || !first) {
|
|
15
|
+
rejectTestXml(node, diagnostics);
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return parseTestExpression(first, diagnostics);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function parseTestExpression(
|
|
22
|
+
node: XmlNode,
|
|
23
|
+
diagnostics: QtiDiagnostic[],
|
|
24
|
+
): QtiTestExpression | undefined {
|
|
25
|
+
const syntax = testExpressionSyntax.find((entry) => entry.name === node.localName);
|
|
26
|
+
if (!syntax) {
|
|
27
|
+
rejectTestXml(node, diagnostics);
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
checkTestXml(
|
|
31
|
+
node,
|
|
32
|
+
testExpressionSyntax.map((entry) => entry.name),
|
|
33
|
+
syntax.attributes,
|
|
34
|
+
diagnostics,
|
|
35
|
+
syntax.type === "baseValue",
|
|
36
|
+
);
|
|
37
|
+
const arityValid =
|
|
38
|
+
syntax.arity === "oneOrMore" ? node.children.length > 0 : node.children.length === syntax.arity;
|
|
39
|
+
if (!arityValid) {
|
|
40
|
+
rejectTestXml(node, diagnostics);
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const expressions: QtiTestExpression[] = [];
|
|
44
|
+
for (const child of node.children) {
|
|
45
|
+
const parsed = parseTestExpression(child, diagnostics);
|
|
46
|
+
if (!parsed) return undefined;
|
|
47
|
+
expressions.push(parsed);
|
|
48
|
+
}
|
|
49
|
+
const source = node.source;
|
|
50
|
+
switch (syntax.type) {
|
|
51
|
+
case "baseValue": {
|
|
52
|
+
const baseType = node.attributes["base-type"];
|
|
53
|
+
if (!isTestBaseType(baseType)) {
|
|
54
|
+
rejectTestXml(node, diagnostics);
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
type: "baseValue",
|
|
59
|
+
baseType,
|
|
60
|
+
value: coerceValue(textContent(node), baseType),
|
|
61
|
+
source,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
case "variable":
|
|
65
|
+
return { type: "variable", identifier: node.attributes.identifier ?? "", source };
|
|
66
|
+
case "testVariables":
|
|
67
|
+
return {
|
|
68
|
+
type: "testVariables",
|
|
69
|
+
variableIdentifier: node.attributes["variable-identifier"] ?? "",
|
|
70
|
+
includeCategory: node.attributes["include-category"],
|
|
71
|
+
source,
|
|
72
|
+
};
|
|
73
|
+
case "sum":
|
|
74
|
+
case "and":
|
|
75
|
+
case "or":
|
|
76
|
+
return { type: syntax.type, expressions, source };
|
|
77
|
+
case "numericCompare": {
|
|
78
|
+
const [left, right] = expressions;
|
|
79
|
+
if (!left || !right) return undefined;
|
|
80
|
+
return { type: "numericCompare", operator: syntax.operator, left, right, source };
|
|
81
|
+
}
|
|
82
|
+
case "not": {
|
|
83
|
+
const expression = expressions[0];
|
|
84
|
+
return expression ? { type: "not", expression, source } : undefined;
|
|
85
|
+
}
|
|
86
|
+
default:
|
|
87
|
+
return assertNever(syntax);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { assertNever } from "./assert-never.js";
|
|
2
|
+
import { renderElement, type XmlAttribute } from "./serializer-processing-xml.js";
|
|
3
|
+
import { testExpressionSyntax, type QtiTestExpression } from "./test-expression.js";
|
|
4
|
+
import { testFailure, type QtiTestResult } from "./test-model.js";
|
|
5
|
+
import { escapeXmlText } from "./xml.js";
|
|
6
|
+
|
|
7
|
+
/** Serialize the closed test expression language without involving item processing. */
|
|
8
|
+
export function serializeTestExpression(expression: QtiTestExpression): QtiTestResult<string> {
|
|
9
|
+
const result = serializeExpression(expression, 0);
|
|
10
|
+
return result.ok ? { ok: true, value: result.value.join("\n") } : result;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function serializeExpression(
|
|
14
|
+
expression: QtiTestExpression,
|
|
15
|
+
indent: number,
|
|
16
|
+
): QtiTestResult<readonly string[]> {
|
|
17
|
+
const syntax = testExpressionSyntax.find(
|
|
18
|
+
(entry) =>
|
|
19
|
+
entry.type === expression.type &&
|
|
20
|
+
(expression.type !== "numericCompare" ||
|
|
21
|
+
(entry.type === "numericCompare" && entry.operator === expression.operator)),
|
|
22
|
+
);
|
|
23
|
+
if (!syntax)
|
|
24
|
+
return testFailure(
|
|
25
|
+
"expression.unsupported",
|
|
26
|
+
"Cannot serialize an unsupported test expression.",
|
|
27
|
+
);
|
|
28
|
+
const attributes: XmlAttribute[] = [];
|
|
29
|
+
let children: readonly QtiTestExpression[] = [];
|
|
30
|
+
switch (expression.type) {
|
|
31
|
+
case "baseValue":
|
|
32
|
+
return {
|
|
33
|
+
ok: true,
|
|
34
|
+
value: renderElement(
|
|
35
|
+
syntax.name,
|
|
36
|
+
[["base-type", expression.baseType]],
|
|
37
|
+
escapeXmlText(String(expression.value)),
|
|
38
|
+
indent,
|
|
39
|
+
),
|
|
40
|
+
};
|
|
41
|
+
case "variable":
|
|
42
|
+
attributes.push(["identifier", expression.identifier]);
|
|
43
|
+
break;
|
|
44
|
+
case "testVariables":
|
|
45
|
+
attributes.push(
|
|
46
|
+
["variable-identifier", expression.variableIdentifier],
|
|
47
|
+
["include-category", expression.includeCategory],
|
|
48
|
+
);
|
|
49
|
+
break;
|
|
50
|
+
case "sum":
|
|
51
|
+
case "and":
|
|
52
|
+
case "or":
|
|
53
|
+
children = expression.expressions;
|
|
54
|
+
break;
|
|
55
|
+
case "numericCompare":
|
|
56
|
+
children = [expression.left, expression.right];
|
|
57
|
+
break;
|
|
58
|
+
case "not":
|
|
59
|
+
children = [expression.expression];
|
|
60
|
+
break;
|
|
61
|
+
default:
|
|
62
|
+
return assertNever(expression);
|
|
63
|
+
}
|
|
64
|
+
const content: string[] = [];
|
|
65
|
+
for (const child of children) {
|
|
66
|
+
const result = serializeExpression(child, indent + 1);
|
|
67
|
+
if (!result.ok) return result;
|
|
68
|
+
content.push(...result.value);
|
|
69
|
+
}
|
|
70
|
+
return { ok: true, value: renderElement(syntax.name, attributes, content, indent) };
|
|
71
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { assertNever } from "./assert-never.js";
|
|
2
|
+
import {
|
|
3
|
+
isTestBaseType,
|
|
4
|
+
testExpressionSyntax,
|
|
5
|
+
type QtiTestBaseType,
|
|
6
|
+
type QtiTestExpression,
|
|
7
|
+
} from "./test-expression.js";
|
|
8
|
+
import type { QtiDiagnostic, QtiOutcomeDeclaration, QtiValue } from "./types.js";
|
|
9
|
+
|
|
10
|
+
/** The declared QTI type of an expression, including its cardinality. */
|
|
11
|
+
export interface TestExpressionType {
|
|
12
|
+
readonly baseType: QtiTestBaseType;
|
|
13
|
+
readonly cardinality: "single" | "multiple";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Check scalar literals and defaults against the supported base types. */
|
|
17
|
+
export function isTestScalarValue(baseType: string | undefined, value: QtiValue): boolean {
|
|
18
|
+
if (baseType === undefined) return false;
|
|
19
|
+
switch (baseType) {
|
|
20
|
+
case "integer":
|
|
21
|
+
return typeof value === "number" && Number.isSafeInteger(value);
|
|
22
|
+
case "float":
|
|
23
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
24
|
+
case "boolean":
|
|
25
|
+
return typeof value === "boolean";
|
|
26
|
+
case "string":
|
|
27
|
+
case "identifier":
|
|
28
|
+
return typeof value === "string";
|
|
29
|
+
default:
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Infer a test expression's QTI type and diagnose invalid operands and scope. */
|
|
35
|
+
export function checkTestExpression(
|
|
36
|
+
expression: QtiTestExpression,
|
|
37
|
+
declarations: ReadonlyMap<string, QtiOutcomeDeclaration>,
|
|
38
|
+
categories: ReadonlySet<string>,
|
|
39
|
+
scope: "outcome" | "branch",
|
|
40
|
+
diagnostics: QtiDiagnostic[],
|
|
41
|
+
): TestExpressionType | undefined {
|
|
42
|
+
const reject = (code: string, message: string) => {
|
|
43
|
+
diagnostics.push({
|
|
44
|
+
code: `test.${code}`,
|
|
45
|
+
severity: "error",
|
|
46
|
+
message,
|
|
47
|
+
source: expression.source,
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
const check = (child: QtiTestExpression) =>
|
|
51
|
+
checkTestExpression(child, declarations, categories, scope, diagnostics);
|
|
52
|
+
if (!testExpressionSyntax.some((syntax) => syntax.type === expression.type)) {
|
|
53
|
+
reject("expression.unsupported", "Expression is outside the supported test execution profile.");
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
switch (expression.type) {
|
|
57
|
+
case "baseValue":
|
|
58
|
+
if (!isTestScalarValue(expression.baseType, expression.value)) {
|
|
59
|
+
reject("expression.value", "Expression values must match a supported scalar base type.");
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
return { baseType: expression.baseType, cardinality: "single" };
|
|
63
|
+
case "variable": {
|
|
64
|
+
const declaration = declarations.get(expression.identifier);
|
|
65
|
+
if (!declaration || !isTestBaseType(declaration.baseType)) {
|
|
66
|
+
reject(
|
|
67
|
+
"expression.variable",
|
|
68
|
+
"Test expression references an undeclared or unsupported outcome.",
|
|
69
|
+
);
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
return { baseType: declaration.baseType, cardinality: "single" };
|
|
73
|
+
}
|
|
74
|
+
case "testVariables":
|
|
75
|
+
if (scope === "branch")
|
|
76
|
+
reject(
|
|
77
|
+
"branch.scope",
|
|
78
|
+
"Test-variable aggregation belongs in outcome processing, not a branch rule.",
|
|
79
|
+
);
|
|
80
|
+
if (
|
|
81
|
+
expression.variableIdentifier !== "SCORE" ||
|
|
82
|
+
(expression.includeCategory !== undefined && !categories.has(expression.includeCategory))
|
|
83
|
+
)
|
|
84
|
+
reject(
|
|
85
|
+
"expression.testVariables",
|
|
86
|
+
"This test profile aggregates item SCORE values from declared categories.",
|
|
87
|
+
);
|
|
88
|
+
return { baseType: "float", cardinality: "multiple" };
|
|
89
|
+
case "sum": {
|
|
90
|
+
const operands = expression.expressions.map(check);
|
|
91
|
+
if (!operands.length || operands.some((type) => !isNumeric(type)))
|
|
92
|
+
reject("expression.sum", "Test sums require numeric operands.");
|
|
93
|
+
return {
|
|
94
|
+
baseType: operands.every((type) => type?.baseType === "integer") ? "integer" : "float",
|
|
95
|
+
cardinality: "single",
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
case "numericCompare": {
|
|
99
|
+
const left = check(expression.left);
|
|
100
|
+
const right = check(expression.right);
|
|
101
|
+
if (
|
|
102
|
+
!testExpressionSyntax.some(
|
|
103
|
+
(syntax) => syntax.type === "numericCompare" && syntax.operator === expression.operator,
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
reject("expression.compare", "Unsupported test comparison operator.");
|
|
107
|
+
if (
|
|
108
|
+
!isNumeric(left) ||
|
|
109
|
+
left?.cardinality !== "single" ||
|
|
110
|
+
!isNumeric(right) ||
|
|
111
|
+
right?.cardinality !== "single"
|
|
112
|
+
)
|
|
113
|
+
reject("expression.compare", "Test comparisons require two scalar numbers.");
|
|
114
|
+
return { baseType: "boolean", cardinality: "single" };
|
|
115
|
+
}
|
|
116
|
+
case "and":
|
|
117
|
+
case "or": {
|
|
118
|
+
const operands = expression.expressions.map(check);
|
|
119
|
+
if (!operands.length || operands.some((type) => !isBoolean(type)))
|
|
120
|
+
reject("expression.boolean", "Test logic requires boolean operands.");
|
|
121
|
+
return { baseType: "boolean", cardinality: "single" };
|
|
122
|
+
}
|
|
123
|
+
case "not":
|
|
124
|
+
if (!isBoolean(check(expression.expression)))
|
|
125
|
+
reject("expression.boolean", "Test logic requires a boolean operand.");
|
|
126
|
+
return { baseType: "boolean", cardinality: "single" };
|
|
127
|
+
default:
|
|
128
|
+
return assertNever(expression);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isNumeric(type: TestExpressionType | undefined): boolean {
|
|
133
|
+
return type?.baseType === "integer" || type?.baseType === "float";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Whether a branch expression has the required single boolean type. */
|
|
137
|
+
export function isBoolean(type: TestExpressionType | undefined): boolean {
|
|
138
|
+
return type?.baseType === "boolean" && type.cardinality === "single";
|
|
139
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { QtiSourceLocation } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/** Scalar types supported by deterministic test outcome processing. */
|
|
4
|
+
export type QtiTestBaseType = "integer" | "float" | "boolean" | "string" | "identifier";
|
|
5
|
+
|
|
6
|
+
/** Test expressions form a closed language, independent of item processing. */
|
|
7
|
+
export type QtiTestExpression = (
|
|
8
|
+
| {
|
|
9
|
+
readonly type: "baseValue";
|
|
10
|
+
readonly baseType: QtiTestBaseType;
|
|
11
|
+
readonly value: number | boolean | string;
|
|
12
|
+
}
|
|
13
|
+
| { readonly type: "variable"; readonly identifier: string }
|
|
14
|
+
| {
|
|
15
|
+
readonly type: "testVariables";
|
|
16
|
+
readonly variableIdentifier: string;
|
|
17
|
+
readonly includeCategory?: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
| { readonly type: "sum"; readonly expressions: readonly QtiTestExpression[] }
|
|
20
|
+
| { readonly type: "and"; readonly expressions: readonly QtiTestExpression[] }
|
|
21
|
+
| { readonly type: "or"; readonly expressions: readonly QtiTestExpression[] }
|
|
22
|
+
| {
|
|
23
|
+
readonly type: "numericCompare";
|
|
24
|
+
readonly operator: "gt" | "gte" | "lt" | "lte";
|
|
25
|
+
readonly left: QtiTestExpression;
|
|
26
|
+
readonly right: QtiTestExpression;
|
|
27
|
+
}
|
|
28
|
+
| { readonly type: "not"; readonly expression: QtiTestExpression }
|
|
29
|
+
) & { readonly source?: QtiSourceLocation | undefined };
|
|
30
|
+
|
|
31
|
+
/** XML names, AST variants, arities and attributes owned by the test language. */
|
|
32
|
+
export const testExpressionSyntax = [
|
|
33
|
+
{ name: "qti-base-value", type: "baseValue", arity: 0, attributes: ["base-type"] },
|
|
34
|
+
{ name: "qti-variable", type: "variable", arity: 0, attributes: ["identifier"] },
|
|
35
|
+
{
|
|
36
|
+
name: "qti-test-variables",
|
|
37
|
+
type: "testVariables",
|
|
38
|
+
arity: 0,
|
|
39
|
+
attributes: ["variable-identifier", "include-category"],
|
|
40
|
+
},
|
|
41
|
+
{ name: "qti-sum", type: "sum", arity: "oneOrMore", attributes: [] },
|
|
42
|
+
{ name: "qti-gt", type: "numericCompare", operator: "gt", arity: 2, attributes: [] },
|
|
43
|
+
{ name: "qti-gte", type: "numericCompare", operator: "gte", arity: 2, attributes: [] },
|
|
44
|
+
{ name: "qti-lt", type: "numericCompare", operator: "lt", arity: 2, attributes: [] },
|
|
45
|
+
{ name: "qti-lte", type: "numericCompare", operator: "lte", arity: 2, attributes: [] },
|
|
46
|
+
{ name: "qti-and", type: "and", arity: "oneOrMore", attributes: [] },
|
|
47
|
+
{ name: "qti-or", type: "or", arity: "oneOrMore", attributes: [] },
|
|
48
|
+
{ name: "qti-not", type: "not", arity: 1, attributes: [] },
|
|
49
|
+
] as const satisfies readonly {
|
|
50
|
+
name: string;
|
|
51
|
+
type: QtiTestExpression["type"];
|
|
52
|
+
operator?: "gt" | "gte" | "lt" | "lte";
|
|
53
|
+
arity: number | "oneOrMore";
|
|
54
|
+
attributes: readonly string[];
|
|
55
|
+
}[];
|
|
56
|
+
|
|
57
|
+
/** Refine a supported test scalar type without accepting item-only base types. */
|
|
58
|
+
export function isTestBaseType(value: string | undefined): value is QtiTestBaseType {
|
|
59
|
+
return (
|
|
60
|
+
value === "integer" ||
|
|
61
|
+
value === "float" ||
|
|
62
|
+
value === "boolean" ||
|
|
63
|
+
value === "string" ||
|
|
64
|
+
value === "identifier"
|
|
65
|
+
);
|
|
66
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { QtiDiagnostic, QtiOutcomeDeclaration } from "./types.js";
|
|
2
|
+
import type { QtiTestBaseType, QtiTestExpression } from "./test-expression.js";
|
|
3
|
+
|
|
4
|
+
/** An ordered branch evaluated after the section's final item submission. */
|
|
5
|
+
export interface QtiTestBranch {
|
|
6
|
+
readonly target: string;
|
|
7
|
+
readonly expression: QtiTestExpression;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** A fixed item reference; categories participate in test-variable aggregation. */
|
|
11
|
+
export interface QtiTestItemRef {
|
|
12
|
+
readonly identifier: string;
|
|
13
|
+
readonly href: string;
|
|
14
|
+
readonly categories: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A flat section in the supported linear, individually submitted test profile. */
|
|
18
|
+
export interface QtiTestSection {
|
|
19
|
+
readonly identifier: string;
|
|
20
|
+
readonly title: string;
|
|
21
|
+
readonly items: readonly QtiTestItemRef[];
|
|
22
|
+
readonly branches: readonly QtiTestBranch[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Framework-neutral QTI test definition. Validation establishes executable support. */
|
|
26
|
+
export interface QtiTestDefinition {
|
|
27
|
+
readonly identifier: string;
|
|
28
|
+
readonly title: string;
|
|
29
|
+
readonly partIdentifier: string;
|
|
30
|
+
readonly sections: readonly QtiTestSection[];
|
|
31
|
+
readonly outcomeDeclarations: readonly QtiOutcomeDeclaration[];
|
|
32
|
+
readonly outcomeProcessing: readonly QtiTestOutcomeRule[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Sequential assignment of a test expression to a declared test outcome. */
|
|
36
|
+
export interface QtiTestOutcomeRule {
|
|
37
|
+
readonly type: "setOutcomeValue";
|
|
38
|
+
readonly identifier: string;
|
|
39
|
+
readonly expression: QtiTestExpression;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type ExecutableSection = QtiTestSection & {
|
|
43
|
+
readonly items: readonly [QtiTestItemRef, ...QtiTestItemRef[]];
|
|
44
|
+
};
|
|
45
|
+
type ExecutableOutcome = Readonly<QtiOutcomeDeclaration> & {
|
|
46
|
+
readonly cardinality: "single";
|
|
47
|
+
readonly baseType: QtiTestBaseType;
|
|
48
|
+
readonly defaultValue: number | boolean | string | null;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
declare const executableTest: unique symbol;
|
|
52
|
+
|
|
53
|
+
/** A test checked for supported expressions, unique identifiers and forward routing. */
|
|
54
|
+
export type QtiExecutableTest = Omit<QtiTestDefinition, "sections" | "outcomeDeclarations"> & {
|
|
55
|
+
readonly [executableTest]: true;
|
|
56
|
+
readonly sections: readonly [ExecutableSection, ...ExecutableSection[]];
|
|
57
|
+
readonly outcomeDeclarations: readonly ExecutableOutcome[];
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Typed failure channel shared by test construction, parsing and transitions. */
|
|
61
|
+
export type QtiTestResult<T> =
|
|
62
|
+
| { readonly ok: true; readonly value: T }
|
|
63
|
+
| { readonly ok: false; readonly diagnostics: readonly QtiDiagnostic[] };
|
|
64
|
+
|
|
65
|
+
/** Construct a safe test diagnostic without including submitted content. */
|
|
66
|
+
export function testFailure(code: string, message: string): QtiTestResult<never> {
|
|
67
|
+
return { ok: false, diagnostics: [{ code: `test.${code}`, severity: "error", message }] };
|
|
68
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { parseOutcomeDeclaration } from "./parser-declarations.js";
|
|
2
|
+
import { QTI_ASI_NAMESPACE } from "./qti-namespaces.js";
|
|
3
|
+
import { parseXmlTree, type XmlNode } from "./xml.js";
|
|
4
|
+
import { validateQtiTest } from "./test-validation.js";
|
|
5
|
+
import { parseTestExpressionChild } from "./test-expression-parser.js";
|
|
6
|
+
import { testExpressionSyntax } from "./test-expression.js";
|
|
7
|
+
import { checkTestXml, rejectTestXml } from "./test-xml.js";
|
|
8
|
+
import {
|
|
9
|
+
testFailure,
|
|
10
|
+
type QtiExecutableTest,
|
|
11
|
+
type QtiTestBranch,
|
|
12
|
+
type QtiTestOutcomeRule,
|
|
13
|
+
type QtiTestResult,
|
|
14
|
+
type QtiTestSection,
|
|
15
|
+
} from "./test-model.js";
|
|
16
|
+
import type { QtiDiagnostic, QtiOutcomeDeclaration } from "./types.js";
|
|
17
|
+
|
|
18
|
+
/** Whether a test requires runtime selection rather than ordinary fixed navigation. */
|
|
19
|
+
export type QtiTestExecution =
|
|
20
|
+
| { readonly kind: "fixed" }
|
|
21
|
+
| { readonly kind: "sequenced"; readonly test: QtiExecutableTest };
|
|
22
|
+
|
|
23
|
+
/** Classify sequencing on the parsed tree and reject unsupported executable routes. */
|
|
24
|
+
export function parseQtiTestExecution(xml: string): QtiTestResult<QtiTestExecution> {
|
|
25
|
+
const parsed = parseTestRoot(xml);
|
|
26
|
+
if (!parsed.ok) return parsed;
|
|
27
|
+
if (!requiresSequence(parsed.value)) return { ok: true, value: { kind: "fixed" } };
|
|
28
|
+
const result = parseTestDefinition(parsed.value);
|
|
29
|
+
return result.ok ? { ok: true, value: { kind: "sequenced", test: result.value } } : result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Parse the supported executable test profile; never silently ignore routing features. */
|
|
33
|
+
export function parseQtiTest(xml: string): QtiTestResult<QtiExecutableTest> {
|
|
34
|
+
const parsed = parseTestRoot(xml);
|
|
35
|
+
return parsed.ok ? parseTestDefinition(parsed.value) : parsed;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function parseTestRoot(xml: string): QtiTestResult<XmlNode> {
|
|
39
|
+
const { root, errors } = parseXmlTree(xml);
|
|
40
|
+
if (
|
|
41
|
+
errors.length ||
|
|
42
|
+
!root ||
|
|
43
|
+
root.localName !== "qti-assessment-test" ||
|
|
44
|
+
root.uri !== QTI_ASI_NAMESPACE
|
|
45
|
+
)
|
|
46
|
+
return testFailure("xml", "Expected a well-formed QTI 3 assessment test.");
|
|
47
|
+
return { ok: true, value: root };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function requiresSequence(node: XmlNode): boolean {
|
|
51
|
+
return (
|
|
52
|
+
(node.uri === QTI_ASI_NAMESPACE &&
|
|
53
|
+
[
|
|
54
|
+
"qti-branch-rule",
|
|
55
|
+
"qti-pre-condition",
|
|
56
|
+
"qti-selection",
|
|
57
|
+
"qti-ordering",
|
|
58
|
+
"qti-adaptive-selection",
|
|
59
|
+
].includes(node.localName)) ||
|
|
60
|
+
node.children.some(requiresSequence)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function parseTestDefinition(root: XmlNode): QtiTestResult<QtiExecutableTest> {
|
|
65
|
+
const diagnostics: QtiDiagnostic[] = [];
|
|
66
|
+
checkTestXml(
|
|
67
|
+
root,
|
|
68
|
+
["qti-outcome-declaration", "qti-test-part", "qti-outcome-processing"],
|
|
69
|
+
["identifier", "title", "tool-name", "tool-version"],
|
|
70
|
+
diagnostics,
|
|
71
|
+
);
|
|
72
|
+
const part = parseTestPart(root, diagnostics);
|
|
73
|
+
if (!part.ok) return part;
|
|
74
|
+
const declarations = root.children
|
|
75
|
+
.filter((node) => node.localName === "qti-outcome-declaration")
|
|
76
|
+
.map((node) => parseTestDeclaration(node, diagnostics));
|
|
77
|
+
const sections = part.value.children
|
|
78
|
+
.filter((node) => node.localName === "qti-assessment-section")
|
|
79
|
+
.map((node) => parseTestSection(node, diagnostics));
|
|
80
|
+
const rules = parseTestOutcomeProcessing(root, diagnostics);
|
|
81
|
+
if (diagnostics.length) return { ok: false, diagnostics };
|
|
82
|
+
return validateQtiTest({
|
|
83
|
+
identifier: root.attributes.identifier ?? "",
|
|
84
|
+
title: root.attributes.title ?? "",
|
|
85
|
+
partIdentifier: part.value.attributes.identifier ?? "",
|
|
86
|
+
sections,
|
|
87
|
+
outcomeDeclarations: declarations,
|
|
88
|
+
outcomeProcessing: rules,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function parseTestPart(root: XmlNode, diagnostics: QtiDiagnostic[]): QtiTestResult<XmlNode> {
|
|
93
|
+
const parts = root.children.filter((node) => node.localName === "qti-test-part");
|
|
94
|
+
const part = parts[0];
|
|
95
|
+
if (
|
|
96
|
+
parts.length !== 1 ||
|
|
97
|
+
!part ||
|
|
98
|
+
part.attributes["navigation-mode"] !== "linear" ||
|
|
99
|
+
part.attributes["submission-mode"] !== "individual"
|
|
100
|
+
)
|
|
101
|
+
return testFailure(
|
|
102
|
+
"mode",
|
|
103
|
+
"Executable tests require one linear part with individual submission.",
|
|
104
|
+
);
|
|
105
|
+
checkTestXml(
|
|
106
|
+
part,
|
|
107
|
+
["qti-assessment-section"],
|
|
108
|
+
["identifier", "navigation-mode", "submission-mode"],
|
|
109
|
+
diagnostics,
|
|
110
|
+
);
|
|
111
|
+
return { ok: true, value: part };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function parseTestDeclaration(node: XmlNode, diagnostics: QtiDiagnostic[]): QtiOutcomeDeclaration {
|
|
115
|
+
if (node.attributes.cardinality !== "single" || node.children.length > 1)
|
|
116
|
+
rejectTestXml(node, diagnostics);
|
|
117
|
+
checkTestXml(
|
|
118
|
+
node,
|
|
119
|
+
["qti-default-value"],
|
|
120
|
+
["identifier", "cardinality", "base-type"],
|
|
121
|
+
diagnostics,
|
|
122
|
+
);
|
|
123
|
+
for (const value of node.children) {
|
|
124
|
+
checkTestXml(value, ["qti-value"], [], diagnostics);
|
|
125
|
+
if (value.children.length !== 1) rejectTestXml(value, diagnostics);
|
|
126
|
+
for (const scalar of value.children) checkTestXml(scalar, [], [], diagnostics, true);
|
|
127
|
+
}
|
|
128
|
+
return parseOutcomeDeclaration(node, diagnostics);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function parseTestSection(section: XmlNode, diagnostics: QtiDiagnostic[]): QtiTestSection {
|
|
132
|
+
checkTestXml(
|
|
133
|
+
section,
|
|
134
|
+
["qti-assessment-item-ref", "qti-branch-rule"],
|
|
135
|
+
["identifier", "title", "visible", "keep-together"],
|
|
136
|
+
diagnostics,
|
|
137
|
+
);
|
|
138
|
+
if (
|
|
139
|
+
!["true", "1"].includes(section.attributes.visible ?? "") ||
|
|
140
|
+
(section.attributes["keep-together"] !== undefined &&
|
|
141
|
+
!["true", "1"].includes(section.attributes["keep-together"]))
|
|
142
|
+
)
|
|
143
|
+
rejectTestXml(section, diagnostics);
|
|
144
|
+
const branches: QtiTestBranch[] = [];
|
|
145
|
+
for (const branch of section.children.filter((node) => node.localName === "qti-branch-rule")) {
|
|
146
|
+
checkTestXml(
|
|
147
|
+
branch,
|
|
148
|
+
testExpressionSyntax.map((syntax) => syntax.name),
|
|
149
|
+
["target"],
|
|
150
|
+
diagnostics,
|
|
151
|
+
);
|
|
152
|
+
const expression = parseTestExpressionChild(branch, diagnostics);
|
|
153
|
+
if (expression) branches.push({ target: branch.attributes.target ?? "", expression });
|
|
154
|
+
}
|
|
155
|
+
const items = section.children
|
|
156
|
+
.filter((node) => node.localName === "qti-assessment-item-ref")
|
|
157
|
+
.map((item) => {
|
|
158
|
+
checkTestXml(item, [], ["identifier", "href", "category"], diagnostics);
|
|
159
|
+
return {
|
|
160
|
+
identifier: item.attributes.identifier ?? "",
|
|
161
|
+
href: item.attributes.href ?? "",
|
|
162
|
+
categories: (item.attributes.category ?? "")
|
|
163
|
+
.split(/\s+/)
|
|
164
|
+
.filter((category) => category.length > 0),
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
return {
|
|
168
|
+
identifier: section.attributes.identifier ?? "",
|
|
169
|
+
title: section.attributes.title ?? "",
|
|
170
|
+
branches,
|
|
171
|
+
items,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function parseTestOutcomeProcessing(
|
|
176
|
+
root: XmlNode,
|
|
177
|
+
diagnostics: QtiDiagnostic[],
|
|
178
|
+
): QtiTestOutcomeRule[] {
|
|
179
|
+
const processing = root.children.filter((node) => node.localName === "qti-outcome-processing");
|
|
180
|
+
if (processing.length > 1) rejectTestXml(root, diagnostics);
|
|
181
|
+
const rules: QtiTestOutcomeRule[] = [];
|
|
182
|
+
for (const node of processing) {
|
|
183
|
+
checkTestXml(node, ["qti-set-outcome-value"], [], diagnostics);
|
|
184
|
+
for (const rule of node.children) {
|
|
185
|
+
checkTestXml(
|
|
186
|
+
rule,
|
|
187
|
+
testExpressionSyntax.map((syntax) => syntax.name),
|
|
188
|
+
["identifier"],
|
|
189
|
+
diagnostics,
|
|
190
|
+
);
|
|
191
|
+
const expression = parseTestExpressionChild(rule, diagnostics);
|
|
192
|
+
if (expression)
|
|
193
|
+
rules.push({
|
|
194
|
+
type: "setOutcomeValue",
|
|
195
|
+
identifier: rule.attributes.identifier ?? "",
|
|
196
|
+
expression,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return rules;
|
|
201
|
+
}
|