@longsightgroup/qti3-core 0.11.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- 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-items.d.ts.map +1 -1
- package/dist/qti-package-items.js +22 -26
- package/dist/qti-package-items.js.map +1 -1
- 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/qti-package-types.d.ts +1 -0
- package/dist/qti-package-types.d.ts.map +1 -1
- package/dist/support.d.ts.map +1 -1
- package/dist/support.js +3 -1
- 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-items.ts +28 -37
- package/src/qti-package-paths.ts +9 -0
- package/src/qti-package-types.ts +1 -0
- package/src/support.ts +3 -1
- 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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { assertNever } from "./assert-never.js";
|
|
2
|
+
import type { QtiTestExpression } from "./test-expression.js";
|
|
3
|
+
import type { QtiExecutableTest } from "./test-model.js";
|
|
4
|
+
|
|
5
|
+
/** Trusted server-scored submission. Item scores are finite scalar numbers. */
|
|
6
|
+
export interface QtiTestSubmission {
|
|
7
|
+
readonly itemRef: string;
|
|
8
|
+
readonly score: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type TestValue = number | boolean | string | number[] | null;
|
|
12
|
+
|
|
13
|
+
/** Data used by the deterministic test interpreter; no item session state or hooks. */
|
|
14
|
+
export interface TestExpressionValues {
|
|
15
|
+
readonly outcomes: Readonly<Record<string, TestValue>>;
|
|
16
|
+
readonly submissions: readonly QtiTestSubmission[];
|
|
17
|
+
readonly categories: ReadonlyMap<string, readonly string[]>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Evaluate a validated test expression using QTI null and three-valued boolean semantics. */
|
|
21
|
+
export function evaluateTestExpression(
|
|
22
|
+
expression: QtiTestExpression,
|
|
23
|
+
values: TestExpressionValues,
|
|
24
|
+
): TestValue {
|
|
25
|
+
const evaluate = (child: QtiTestExpression) => evaluateTestExpression(child, values);
|
|
26
|
+
switch (expression.type) {
|
|
27
|
+
case "baseValue":
|
|
28
|
+
return expression.value;
|
|
29
|
+
case "variable":
|
|
30
|
+
return values.outcomes[expression.identifier] ?? null;
|
|
31
|
+
case "testVariables": {
|
|
32
|
+
const scores = values.submissions
|
|
33
|
+
.filter(
|
|
34
|
+
(submission) =>
|
|
35
|
+
expression.includeCategory === undefined ||
|
|
36
|
+
values.categories.get(submission.itemRef)?.includes(expression.includeCategory),
|
|
37
|
+
)
|
|
38
|
+
.map((submission) => submission.score);
|
|
39
|
+
return scores.length ? scores : null;
|
|
40
|
+
}
|
|
41
|
+
case "sum": {
|
|
42
|
+
let sum = 0;
|
|
43
|
+
for (const child of expression.expressions) {
|
|
44
|
+
const value = evaluate(child);
|
|
45
|
+
if (value === null) return null;
|
|
46
|
+
if (typeof value === "number") sum += value;
|
|
47
|
+
else if (typeof value === "object") for (const number of value) sum += number;
|
|
48
|
+
else return null;
|
|
49
|
+
}
|
|
50
|
+
return sum;
|
|
51
|
+
}
|
|
52
|
+
case "numericCompare": {
|
|
53
|
+
const left = evaluate(expression.left);
|
|
54
|
+
const right = evaluate(expression.right);
|
|
55
|
+
if (typeof left !== "number" || typeof right !== "number") return null;
|
|
56
|
+
switch (expression.operator) {
|
|
57
|
+
case "gt":
|
|
58
|
+
return left > right;
|
|
59
|
+
case "gte":
|
|
60
|
+
return left >= right;
|
|
61
|
+
case "lt":
|
|
62
|
+
return left < right;
|
|
63
|
+
case "lte":
|
|
64
|
+
return left <= right;
|
|
65
|
+
default:
|
|
66
|
+
return assertNever(expression.operator);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
case "and":
|
|
70
|
+
case "or": {
|
|
71
|
+
const decisive = expression.type === "or";
|
|
72
|
+
let sawNull = false;
|
|
73
|
+
for (const child of expression.expressions) {
|
|
74
|
+
const value = evaluate(child);
|
|
75
|
+
if (value === decisive) return decisive;
|
|
76
|
+
if (value === null) sawNull = true;
|
|
77
|
+
}
|
|
78
|
+
return sawNull ? null : !decisive;
|
|
79
|
+
}
|
|
80
|
+
case "not": {
|
|
81
|
+
const value = evaluate(expression.expression);
|
|
82
|
+
return value === null ? null : !value;
|
|
83
|
+
}
|
|
84
|
+
default:
|
|
85
|
+
return assertNever(expression);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Recompute outcomes from defaults and committed scores, in assignment order. */
|
|
90
|
+
export function processTestOutcomes(
|
|
91
|
+
test: QtiExecutableTest,
|
|
92
|
+
submissions: readonly QtiTestSubmission[],
|
|
93
|
+
): { readonly outcomes: Record<string, TestValue> } {
|
|
94
|
+
const outcomes: Record<string, TestValue> = Object.fromEntries(
|
|
95
|
+
test.outcomeDeclarations.map((d) => [d.identifier, d.defaultValue]),
|
|
96
|
+
);
|
|
97
|
+
const categories = new Map(
|
|
98
|
+
test.sections.flatMap((s) => s.items.map((i) => [i.identifier, i.categories] as const)),
|
|
99
|
+
);
|
|
100
|
+
for (const rule of test.outcomeProcessing)
|
|
101
|
+
outcomes[rule.identifier] = evaluateTestExpression(rule.expression, {
|
|
102
|
+
outcomes,
|
|
103
|
+
submissions,
|
|
104
|
+
categories,
|
|
105
|
+
});
|
|
106
|
+
return { outcomes };
|
|
107
|
+
}
|