@qti-components/delivery 0.1.0-next.3 → 0.1.0-next.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export * from './model/test-model.js';
2
2
  export * from './model/item-facts.js';
3
3
  export type { XmlElement, XmlNode } from './model/xml-node.js';
4
+ export * from './lifecycle/item-lifecycle.js';
5
+ export * from './lifecycle/item-rules.js';
4
6
  export * from './random/seeded-random.js';
5
7
  export * from './random/selection.js';
6
8
  export * from './random/shuffle.js';
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './model/test-model.js';
2
2
  export * from './model/item-facts.js';
3
+ export * from './lifecycle/item-lifecycle.js';
4
+ export * from './lifecycle/item-rules.js';
3
5
  export * from './random/seeded-random.js';
4
6
  export * from './random/selection.js';
5
7
  export * from './random/shuffle.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC","sourcesContent":["export * from './model/test-model.js';\nexport * from './model/item-facts.js';\nexport type { XmlElement, XmlNode } from './model/xml-node.js';\nexport * from './random/seeded-random.js';\nexport * from './random/selection.js';\nexport * from './random/shuffle.js';\nexport * from './state/clock.js';\nexport * from './state/commands.js';\nexport * from './state/create-delivery-state.js';\nexport * from './state/delivery-state.js';\nexport * from './processing.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC","sourcesContent":["export * from './model/test-model.js';\nexport * from './model/item-facts.js';\nexport type { XmlElement, XmlNode } from './model/xml-node.js';\nexport * from './lifecycle/item-lifecycle.js';\nexport * from './lifecycle/item-rules.js';\nexport * from './random/seeded-random.js';\nexport * from './random/selection.js';\nexport * from './random/shuffle.js';\nexport * from './state/clock.js';\nexport * from './state/commands.js';\nexport * from './state/create-delivery-state.js';\nexport * from './state/delivery-state.js';\nexport * from './processing.js';\n"]}
@@ -0,0 +1,94 @@
1
+ import type { SessionControl, TestPartModel } from '../model/test-model.js';
2
+ /**
3
+ * The item session lifecycle (QTI 3 info model §2.2.1, Figure 2.1) as one finite state machine per
4
+ * item instance: every item session is in exactly one of these states, and only the transitions in
5
+ * `ITEM_LIFECYCLE` happen. Each rule of item session control is a guard on one of them.
6
+ *
7
+ * ```mermaid
8
+ * stateDiagram-v2
9
+ * [*] --> initial
10
+ * initial --> interacting: beginAttempt [attemptsLeft]
11
+ * initial --> closed: endItemSession
12
+ * interacting --> closed: endAttempt [individualSubmission, no attemptsLeft]
13
+ * interacting --> modalFeedback: endAttempt [individualSubmission, attemptsLeft, modalFeedback]
14
+ * interacting --> interacting: endAttempt [individualSubmission, attemptsLeft]
15
+ * interacting --> suspended: endCandidateSession
16
+ * interacting --> closed: endItemSession
17
+ * modalFeedback --> interacting: beginAttempt [attemptsLeft]
18
+ * modalFeedback --> suspended: endCandidateSession
19
+ * modalFeedback --> closed: endItemSession
20
+ * suspended --> interacting: beginCandidateSession [mayResume]
21
+ * suspended --> closed: endItemSession
22
+ * closed --> review: beginReview [allowReview]
23
+ * review --> closed: endReview
24
+ * closed --> solution: beginSolution [showSolution]
25
+ * solution --> closed: endSolution
26
+ * ```
27
+ *
28
+ * What QTI ties to the states: interactions are enabled in `interacting` only; in `closed` and
29
+ * `review` the responses show but are fixed; in `solution` the correct responses show instead. An
30
+ * item that goes from `initial` straight to `closed` keeps NULL responses.
31
+ *
32
+ * The text of §2.2.1 names six states; `modalFeedback` comes from its figure (Figure 2.1, as this
33
+ * plan reads it): the item after an attempt whose response processing set modal feedback, with
34
+ * attempts left ("modal feedback is shown to the candidate directly following response
35
+ * processing", §2.6). The candidate reads it before trying again.
36
+ *
37
+ * Experimental (0.x): roadmap 1 phase T3; see `plans/delivery-state.md`.
38
+ */
39
+ export declare const ITEM_LIFECYCLE_STATES: readonly ["initial", "interacting", "modalFeedback", "suspended", "closed", "review", "solution"];
40
+ export type ItemLifecycleState = (typeof ITEM_LIFECYCLE_STATES)[number];
41
+ /**
42
+ * What happens to an item session, in QTI's words (info model §2.2.1). The delivery's commands
43
+ * (`navigate`, `beginAttempt`, `endAttempt`, `endTestPart`, `timeUp`) are turned into these, per item.
44
+ */
45
+ export declare const ITEM_LIFECYCLE_EVENTS: readonly ["beginAttempt", "endAttempt", "endCandidateSession", "beginCandidateSession", "endItemSession", "beginReview", "endReview", "beginSolution", "endSolution"];
46
+ export type ItemLifecycleEvent = (typeof ITEM_LIFECYCLE_EVENTS)[number];
47
+ /** The conditions a transition can depend on; `itemLifecycleGuards` reads them for one item. */
48
+ export declare const ITEM_LIFECYCLE_GUARDS: readonly ["attemptsLeft", "individualSubmission", "modalFeedback", "mayResume", "allowReview", "showSolution"];
49
+ export type ItemLifecycleGuard = (typeof ITEM_LIFECYCLE_GUARDS)[number];
50
+ export type ItemLifecycleGuards = Readonly<Record<ItemLifecycleGuard, boolean>>;
51
+ /** One way out of a state: taken when every `when` guard holds and no `unless` guard does. */
52
+ export interface ItemLifecycleBranch {
53
+ readonly to: ItemLifecycleState;
54
+ readonly when?: readonly ItemLifecycleGuard[];
55
+ readonly unless?: readonly ItemLifecycleGuard[];
56
+ }
57
+ /** A state with no guard, or branches tried in order, the first that holds taken. */
58
+ export type ItemLifecycleTarget = ItemLifecycleState | readonly ItemLifecycleBranch[];
59
+ /** The transition table: a pair it does not list is refused. */
60
+ export declare const ITEM_LIFECYCLE: Readonly<Record<ItemLifecycleState, Readonly<Partial<Record<ItemLifecycleEvent, ItemLifecycleTarget>>>>>;
61
+ /**
62
+ * The state an item session goes to on `event`, or null when the table refuses it: the pair is not
63
+ * listed, or no branch's guards hold. Pure: the same state, event and guards give the same answer
64
+ * on every runtime.
65
+ */
66
+ export declare function transition(state: ItemLifecycleState, event: ItemLifecycleEvent, guards: ItemLifecycleGuards): ItemLifecycleState | null;
67
+ /** What the guards of one item session are read from. */
68
+ export interface ItemLifecycleInput {
69
+ /** The item-ref's resolved session control (`ItemRefModel.sessionControl`). */
70
+ sessionControl: Pick<SessionControl, 'maxAttempts' | 'allowReview' | 'showSolution'>;
71
+ /** From the item's facts (`ItemFacts.adaptive`). */
72
+ adaptive: boolean;
73
+ navigationMode: TestPartModel['navigationMode'];
74
+ submissionMode: TestPartModel['submissionMode'];
75
+ /**
76
+ * The attempts the candidate has ended. QTI's `numAttempts` counts an attempt as it begins; this
77
+ * delivery counts it when it ends, and T3d keeps that (`plans/item-lifecycle.md`).
78
+ */
79
+ numAttempts: number;
80
+ /** The item's `completionStatus` (`not_attempted`, `unknown`, `incomplete`, `completed`). */
81
+ completionStatus: string | null;
82
+ /** The candidate has moved on from the item; in a linear part there is no way back. */
83
+ left: boolean;
84
+ /** The last response processing set modal feedback to show. */
85
+ modalFeedback: boolean;
86
+ }
87
+ /**
88
+ * Whether another attempt is allowed (implementation guide §4.4.2.1): a non-adaptive item until it
89
+ * has used `max-attempts` (0 is unlimited); an adaptive item, which ignores `max-attempts`, until its
90
+ * response processing set `completionStatus` to `completed`.
91
+ */
92
+ export declare function hasAttemptsLeft({ sessionControl: { maxAttempts }, adaptive, numAttempts, completionStatus }: Pick<ItemLifecycleInput, 'sessionControl' | 'adaptive' | 'numAttempts' | 'completionStatus'>): boolean;
93
+ /** The guards of one item session, from its session control, its facts, its part and its variables. */
94
+ export declare function itemLifecycleGuards(input: ItemLifecycleInput): ItemLifecycleGuards;
@@ -0,0 +1,144 @@
1
+ /**
2
+ * The item session lifecycle (QTI 3 info model §2.2.1, Figure 2.1) as one finite state machine per
3
+ * item instance: every item session is in exactly one of these states, and only the transitions in
4
+ * `ITEM_LIFECYCLE` happen. Each rule of item session control is a guard on one of them.
5
+ *
6
+ * ```mermaid
7
+ * stateDiagram-v2
8
+ * [*] --> initial
9
+ * initial --> interacting: beginAttempt [attemptsLeft]
10
+ * initial --> closed: endItemSession
11
+ * interacting --> closed: endAttempt [individualSubmission, no attemptsLeft]
12
+ * interacting --> modalFeedback: endAttempt [individualSubmission, attemptsLeft, modalFeedback]
13
+ * interacting --> interacting: endAttempt [individualSubmission, attemptsLeft]
14
+ * interacting --> suspended: endCandidateSession
15
+ * interacting --> closed: endItemSession
16
+ * modalFeedback --> interacting: beginAttempt [attemptsLeft]
17
+ * modalFeedback --> suspended: endCandidateSession
18
+ * modalFeedback --> closed: endItemSession
19
+ * suspended --> interacting: beginCandidateSession [mayResume]
20
+ * suspended --> closed: endItemSession
21
+ * closed --> review: beginReview [allowReview]
22
+ * review --> closed: endReview
23
+ * closed --> solution: beginSolution [showSolution]
24
+ * solution --> closed: endSolution
25
+ * ```
26
+ *
27
+ * What QTI ties to the states: interactions are enabled in `interacting` only; in `closed` and
28
+ * `review` the responses show but are fixed; in `solution` the correct responses show instead. An
29
+ * item that goes from `initial` straight to `closed` keeps NULL responses.
30
+ *
31
+ * The text of §2.2.1 names six states; `modalFeedback` comes from its figure (Figure 2.1, as this
32
+ * plan reads it): the item after an attempt whose response processing set modal feedback, with
33
+ * attempts left ("modal feedback is shown to the candidate directly following response
34
+ * processing", §2.6). The candidate reads it before trying again.
35
+ *
36
+ * Experimental (0.x): roadmap 1 phase T3; see `plans/delivery-state.md`.
37
+ */
38
+ export const ITEM_LIFECYCLE_STATES = [
39
+ 'initial',
40
+ 'interacting',
41
+ 'modalFeedback',
42
+ 'suspended',
43
+ 'closed',
44
+ 'review',
45
+ 'solution'
46
+ ];
47
+ /**
48
+ * What happens to an item session, in QTI's words (info model §2.2.1). The delivery's commands
49
+ * (`navigate`, `beginAttempt`, `endAttempt`, `endTestPart`, `timeUp`) are turned into these, per item.
50
+ */
51
+ export const ITEM_LIFECYCLE_EVENTS = [
52
+ 'beginAttempt',
53
+ 'endAttempt',
54
+ 'endCandidateSession',
55
+ 'beginCandidateSession',
56
+ 'endItemSession',
57
+ 'beginReview',
58
+ 'endReview',
59
+ 'beginSolution',
60
+ 'endSolution'
61
+ ];
62
+ /** The conditions a transition can depend on; `itemLifecycleGuards` reads them for one item. */
63
+ export const ITEM_LIFECYCLE_GUARDS = [
64
+ 'attemptsLeft',
65
+ 'individualSubmission',
66
+ 'modalFeedback',
67
+ 'mayResume',
68
+ 'allowReview',
69
+ 'showSolution'
70
+ ];
71
+ /** The transition table: a pair it does not list is refused. */
72
+ export const ITEM_LIFECYCLE = {
73
+ initial: {
74
+ beginAttempt: [{ to: 'interacting', when: ['attemptsLeft'] }],
75
+ endItemSession: 'closed'
76
+ },
77
+ interacting: {
78
+ // The guards are read after response processing, with the ended attempt counted.
79
+ endAttempt: [
80
+ { to: 'closed', when: ['individualSubmission'], unless: ['attemptsLeft'] },
81
+ { to: 'modalFeedback', when: ['individualSubmission', 'attemptsLeft', 'modalFeedback'] },
82
+ { to: 'interacting', when: ['individualSubmission', 'attemptsLeft'] }
83
+ ],
84
+ endCandidateSession: 'suspended',
85
+ endItemSession: 'closed'
86
+ },
87
+ modalFeedback: {
88
+ beginAttempt: [{ to: 'interacting', when: ['attemptsLeft'] }],
89
+ endCandidateSession: 'suspended',
90
+ endItemSession: 'closed'
91
+ },
92
+ suspended: {
93
+ beginCandidateSession: [{ to: 'interacting', when: ['mayResume'] }],
94
+ endItemSession: 'closed'
95
+ },
96
+ closed: {
97
+ beginReview: [{ to: 'review', when: ['allowReview'] }],
98
+ beginSolution: [{ to: 'solution', when: ['showSolution'] }]
99
+ },
100
+ review: {
101
+ endReview: 'closed'
102
+ },
103
+ solution: {
104
+ endSolution: 'closed'
105
+ }
106
+ };
107
+ /**
108
+ * The state an item session goes to on `event`, or null when the table refuses it: the pair is not
109
+ * listed, or no branch's guards hold. Pure: the same state, event and guards give the same answer
110
+ * on every runtime.
111
+ */
112
+ export function transition(state, event, guards) {
113
+ const target = ITEM_LIFECYCLE[state][event];
114
+ if (target === undefined)
115
+ return null;
116
+ if (typeof target === 'string')
117
+ return target;
118
+ const branch = target.find(({ when = [], unless = [] }) => when.every(guard => guards[guard]) && !unless.some(guard => guards[guard]));
119
+ return branch?.to ?? null;
120
+ }
121
+ /**
122
+ * Whether another attempt is allowed (implementation guide §4.4.2.1): a non-adaptive item until it
123
+ * has used `max-attempts` (0 is unlimited); an adaptive item, which ignores `max-attempts`, until its
124
+ * response processing set `completionStatus` to `completed`.
125
+ */
126
+ export function hasAttemptsLeft({ sessionControl: { maxAttempts }, adaptive, numAttempts, completionStatus }) {
127
+ if (adaptive)
128
+ return completionStatus !== 'completed';
129
+ return maxAttempts === 0 || numAttempts < maxAttempts;
130
+ }
131
+ /** The guards of one item session, from its session control, its facts, its part and its variables. */
132
+ export function itemLifecycleGuards(input) {
133
+ return {
134
+ attemptsLeft: hasAttemptsLeft(input),
135
+ // In a simultaneous part every response is processed when the part is submitted (§4.1).
136
+ individualSubmission: input.submissionMode === 'individual',
137
+ modalFeedback: input.modalFeedback,
138
+ // A linear part restricts the candidate to each item in turn: once left, never again (§4.1).
139
+ mayResume: input.navigationMode === 'nonlinear' || !input.left,
140
+ allowReview: input.sessionControl.allowReview,
141
+ showSolution: input.sessionControl.showSolution
142
+ };
143
+ }
144
+ //# sourceMappingURL=item-lifecycle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"item-lifecycle.js","sourceRoot":"","sources":["../../src/lifecycle/item-lifecycle.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,SAAS;IACT,aAAa;IACb,eAAe;IACf,WAAW;IACX,QAAQ;IACR,QAAQ;IACR,UAAU;CACF,CAAC;AAIX;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,cAAc;IACd,YAAY;IACZ,qBAAqB;IACrB,uBAAuB;IACvB,gBAAgB;IAChB,aAAa;IACb,WAAW;IACX,eAAe;IACf,aAAa;CACL,CAAC;AAIX,gGAAgG;AAChG,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,cAAc;IACd,sBAAsB;IACtB,eAAe;IACf,WAAW;IACX,aAAa;IACb,cAAc;CACN,CAAC;AAgBX,gEAAgE;AAChE,MAAM,CAAC,MAAM,cAAc,GAEvB;IACF,OAAO,EAAE;QACP,YAAY,EAAE,CAAC,EAAE,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,cAAc,CAAC,EAAE,CAAC;QAC7D,cAAc,EAAE,QAAQ;KACzB;IACD,WAAW,EAAE;QACX,iFAAiF;QACjF,UAAU,EAAE;YACV,EAAE,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,sBAAsB,CAAC,EAAE,MAAM,EAAE,CAAC,cAAc,CAAC,EAAE;YAC1E,EAAE,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,sBAAsB,EAAE,cAAc,EAAE,eAAe,CAAC,EAAE;YACxF,EAAE,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,sBAAsB,EAAE,cAAc,CAAC,EAAE;SACtE;QACD,mBAAmB,EAAE,WAAW;QAChC,cAAc,EAAE,QAAQ;KACzB;IACD,aAAa,EAAE;QACb,YAAY,EAA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AAC5B,CAAC;AAuBD;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,EAC9B,cAAc,EAAE,EAAE,WAAW,EAAE,EAC/B,QAAQ,EACR,WAAW,EACX,gBAAgB,EAC6E;IAC7F,IAAI,QAAQ;QAAE,OAAO,gBAAgB,KAAK,WAAW,CAAC;IACtD,OAAO,WAAW,KAAK,CAAC,IAAI,WAAW,GAAG,WAAW,CAAC;AACxD,CAAC;AAED,uGAAuG;AACvG,MAAM,UAAU,mBAAmB,CAAC,KAAyB;IAC3D,OAAO;QACL,YAAY,EAAE,eAAe,CAAC,KAAK,CAAC;QACpC,wFAAwF;QACxF,oBAAoB,EAAE,KAAK,CAAC,cAAc,KAAK,YAAY;QAC3D,aAAa,EAAE,KAAK,CAAC,aAAa;QAClC,6FAA6F;QAC7F,SAAS,EAAE,KAAK,CAAC,cAAc,KAAK,WAAW,IAAI,CAAC,KAAK,CAAC,IAAI;QAC9D,WAAW,EAAE,KAAK,CAAC,cAAc,CAAC,WAAW;QAC7C,YAAY,EAAE,KAAK,CAAC,cAAc,CAAC,YAAY;KAChD,CAAC;AACJ,CAAC","sourcesContent":["import type { SessionControl, TestPartModel } from '../model/test-model.js';\n\n/**\n * The item session lifecycle (QTI 3 info model §2.2.1, Figure 2.1) as one finite state machine per\n * item instance: every item session is in exactly one of these states, and only the transitions in\n * `ITEM_LIFECYCLE` happen. Each rule of item session control is a guard on one of them.\n *\n * ```mermaid\n * stateDiagram-v2\n * [*] --> initial\n * initial --> interacting: beginAttempt [attemptsLeft]\n * initial --> closed: endItemSession\n * interacting --> closed: endAttempt [individualSubmission, no attemptsLeft]\n * interacting --> modalFeedback: endAttempt [individualSubmission, attemptsLeft, modalFeedback]\n * interacting --> interacting: endAttempt [individualSubmission, attemptsLeft]\n * interacting --> suspended: endCandidateSession\n * interacting --> closed: endItemSession\n * modalFeedback --> interacting: beginAttempt [attemptsLeft]\n * modalFeedback --> suspended: endCandidateSession\n * modalFeedback --> closed: endItemSession\n * suspended --> interacting: beginCandidateSession [mayResume]\n * suspended --> closed: endItemSession\n * closed --> review: beginReview [allowReview]\n * review --> closed: endReview\n * closed --> solution: beginSolution [showSolution]\n * solution --> closed: endSolution\n * ```\n *\n * What QTI ties to the states: interactions are enabled in `interacting` only; in `closed` and\n * `review` the responses show but are fixed; in `solution` the correct responses show instead. An\n * item that goes from `initial` straight to `closed` keeps NULL responses.\n *\n * The text of §2.2.1 names six states; `modalFeedback` comes from its figure (Figure 2.1, as this\n * plan reads it): the item after an attempt whose response processing set modal feedback, with\n * attempts left (\"modal feedback is shown to the candidate directly following response\n * processing\", §2.6). The candidate reads it before trying again.\n *\n * Experimental (0.x): roadmap 1 phase T3; see `plans/delivery-state.md`.\n */\nexport const ITEM_LIFECYCLE_STATES = [\n 'initial',\n 'interacting',\n 'modalFeedback',\n 'suspended',\n 'closed',\n 'review',\n 'solution'\n] as const;\n\nexport type ItemLifecycleState = (typeof ITEM_LIFECYCLE_STATES)[number];\n\n/**\n * What happens to an item session, in QTI's words (info model §2.2.1). The delivery's commands\n * (`navigate`, `beginAttempt`, `endAttempt`, `endTestPart`, `timeUp`) are turned into these, per item.\n */\nexport const ITEM_LIFECYCLE_EVENTS = [\n 'beginAttempt',\n 'endAttempt',\n 'endCandidateSession',\n 'beginCandidateSession',\n 'endItemSession',\n 'beginReview',\n 'endReview',\n 'beginSolution',\n 'endSolution'\n] as const;\n\nexport type ItemLifecycleEvent = (typeof ITEM_LIFECYCLE_EVENTS)[number];\n\n/** The conditions a transition can depend on; `itemLifecycleGuards` reads them for one item. */\nexport const ITEM_LIFECYCLE_GUARDS = [\n 'attemptsLeft',\n 'individualSubmission',\n 'modalFeedback',\n 'mayResume',\n 'allowReview',\n 'showSolution'\n] as const;\n\nexport type ItemLifecycleGuard = (typeof ITEM_LIFECYCLE_GUARDS)[number];\n\nexport type ItemLifecycleGuards = Readonly<Record<ItemLifecycleGuard, boolean>>;\n\n/** One way out of a state: taken when every `when` guard holds and no `unless` guard does. */\nexport interface ItemLifecycleBranch {\n readonly to: ItemLifecycleState;\n readonly when?: readonly ItemLifecycleGuard[];\n readonly unless?: readonly ItemLifecycleGuard[];\n}\n\n/** A state with no guard, or branches tried in order, the first that holds taken. */\nexport type ItemLifecycleTarget = ItemLifecycleState | readonly ItemLifecycleBranch[];\n\n/** The transition table: a pair it does not list is refused. */\nexport const ITEM_LIFECYCLE: Readonly<\n Record<ItemLifecycleState, Readonly<Partial<Record<ItemLifecycleEvent, ItemLifecycleTarget>>>>\n> = {\n initial: {\n beginAttempt: [{ to: 'interacting', when: ['attemptsLeft'] }],\n endItemSession: 'closed'\n },\n interacting: {\n // The guards are read after response processing, with the ended attempt counted.\n endAttempt: [\n { to: 'closed', when: ['individualSubmission'], unless: ['attemptsLeft'] },\n { to: 'modalFeedback', when: ['individualSubmission', 'attemptsLeft', 'modalFeedback'] },\n { to: 'interacting', when: ['individualSubmission', 'attemptsLeft'] }\n ],\n endCandidateSession: 'suspended',\n endItemSession: 'closed'\n },\n modalFeedback: {\n beginAttempt: [{ to: 'interacting', when: ['attemptsLeft'] }],\n endCandidateSession: 'suspended',\n endItemSession: 'closed'\n },\n suspended: {\n beginCandidateSession: [{ to: 'interacting', when: ['mayResume'] }],\n endItemSession: 'closed'\n },\n closed: {\n beginReview: [{ to: 'review', when: ['allowReview'] }],\n beginSolution: [{ to: 'solution', when: ['showSolution'] }]\n },\n review: {\n endReview: 'closed'\n },\n solution: {\n endSolution: 'closed'\n }\n};\n\n/**\n * The state an item session goes to on `event`, or null when the table refuses it: the pair is not\n * listed, or no branch's guards hold. Pure: the same state, event and guards give the same answer\n * on every runtime.\n */\nexport function transition(\n state: ItemLifecycleState,\n event: ItemLifecycleEvent,\n guards: ItemLifecycleGuards\n): ItemLifecycleState | null {\n const target = ITEM_LIFECYCLE[state][event];\n if (target === undefined) return null;\n if (typeof target === 'string') return target;\n const branch = target.find(\n ({ when = [], unless = [] }) => when.every(guard => guards[guard]) && !unless.some(guard => guards[guard])\n );\n return branch?.to ?? null;\n}\n\n/** What the guards of one item session are read from. */\nexport interface ItemLifecycleInput {\n /** The item-ref's resolved session control (`ItemRefModel.sessionControl`). */\n sessionControl: Pick<SessionControl, 'maxAttempts' | 'allowReview' | 'showSolution'>;\n /** From the item's facts (`ItemFacts.adaptive`). */\n adaptive: boolean;\n navigationMode: TestPartModel['navigationMode'];\n submissionMode: TestPartModel['submissionMode'];\n /**\n * The attempts the candidate has ended. QTI's `numAttempts` counts an attempt as it begins; this\n * delivery counts it when it ends, and T3d keeps that (`plans/item-lifecycle.md`).\n */\n numAttempts: number;\n /** The item's `completionStatus` (`not_attempted`, `unknown`, `incomplete`, `completed`). */\n completionStatus: string | null;\n /** The candidate has moved on from the item; in a linear part there is no way back. */\n left: boolean;\n /** The last response processing set modal feedback to show. */\n modalFeedback: boolean;\n}\n\n/**\n * Whether another attempt is allowed (implementation guide §4.4.2.1): a non-adaptive item until it\n * has used `max-attempts` (0 is unlimited); an adaptive item, which ignores `max-attempts`, until its\n * response processing set `completionStatus` to `completed`.\n */\nexport function hasAttemptsLeft({\n sessionControl: { maxAttempts },\n adaptive,\n numAttempts,\n completionStatus\n}: Pick<ItemLifecycleInput, 'sessionControl' | 'adaptive' | 'numAttempts' | 'completionStatus'>): boolean {\n if (adaptive) return completionStatus !== 'completed';\n return maxAttempts === 0 || numAttempts < maxAttempts;\n}\n\n/** The guards of one item session, from its session control, its facts, its part and its variables. */\nexport function itemLifecycleGuards(input: ItemLifecycleInput): ItemLifecycleGuards {\n return {\n attemptsLeft: hasAttemptsLeft(input),\n // In a simultaneous part every response is processed when the part is submitted (§4.1).\n individualSubmission: input.submissionMode === 'individual',\n modalFeedback: input.modalFeedback,\n // A linear part restricts the candidate to each item in turn: once left, never again (§4.1).\n mayResume: input.navigationMode === 'nonlinear' || !input.left,\n allowReview: input.sessionControl.allowReview,\n showSolution: input.sessionControl.showSolution\n };\n}\n"]}
@@ -0,0 +1,95 @@
1
+ import type { SessionControl, TestPartModel } from '../model/test-model.js';
2
+ import type { ItemLifecycleInput, ItemLifecycleState } from './item-lifecycle.js';
3
+ /**
4
+ * What one item session may do now, answered from its lifecycle state, its session control and its
5
+ * responses. Every reader of attempts, skipping, feedback, review or the solution asks here instead
6
+ * of working the rule out itself (roadmap 1, T3b).
7
+ *
8
+ * The answers follow QTI 3 (`qti-item-session-control`, info model §7.19; implementation guide
9
+ * §4.1 and §4.4.2) and nothing else: whether the last attempt was optimal decides none of them
10
+ * (decided 2026-11-27, `plans/host-api.md`).
11
+ *
12
+ * Experimental (0.x).
13
+ */
14
+ export interface ItemRules {
15
+ /**
16
+ * Another attempt is allowed: `max-attempts` not reached (0 is unlimited), or, for an adaptive
17
+ * item, which ignores `max-attempts`, `completionStatus` not `completed`.
18
+ */
19
+ readonly attemptsLeft: boolean;
20
+ /** No further attempt is allowed: the negation of `attemptsLeft`. */
21
+ readonly attemptsUsed: boolean;
22
+ /** The responses can change: only while `interacting`. In every other state they are fixed or not shown. */
23
+ readonly interactive: boolean;
24
+ /** The item session is over: `closed`, or in `review` or `solution`, which come back to `closed`. */
25
+ readonly done: boolean;
26
+ /**
27
+ * The candidate may end the attempt now: the item is `interacting` in a part with individual
28
+ * submission, and `maySubmit` holds.
29
+ */
30
+ readonly mayEndAttempt: boolean;
31
+ /**
32
+ * The responses may be submitted as they are: `allow-skipping="false"` refuses an item with no
33
+ * response, `validate-responses` an invalid one. Two separate rules, both for individual
34
+ * submission only (§7.19).
35
+ */
36
+ readonly maySubmit: boolean;
37
+ /** Submitting no response is allowed: `allow-skipping`, or a response given, or simultaneous submission. */
38
+ readonly maySkip: boolean;
39
+ /**
40
+ * Feedback, modal and integrated, may show: always while attempts are left, after the last
41
+ * attempt only with `show-feedback`. In `review`, an adaptive item with `allow-review` shows its
42
+ * feedback whatever `show-feedback` says (§7.19, `allow-review`).
43
+ */
44
+ readonly mayShowFeedback: boolean;
45
+ /**
46
+ * The item session may enter `review` now: it is `closed`, with `allow-review`. False in `review`
47
+ * itself; whether review shows is the state.
48
+ */
49
+ readonly mayBeginReview: boolean;
50
+ /**
51
+ * The item session may enter `solution`, which shows the correct responses: it is `closed`, with
52
+ * `show-solution`. False in `solution` itself; whether the solution shows is the state.
53
+ */
54
+ readonly mayBeginSolution: boolean;
55
+ }
56
+ /** What the rules of one item session are read from: its lifecycle input, its state and its responses. */
57
+ export interface ItemRulesInput extends Omit<ItemLifecycleInput, 'sessionControl'> {
58
+ /** The item-ref's resolved session control (`ItemRefModel.sessionControl`). */
59
+ sessionControl: Omit<SessionControl, 'allowComment'>;
60
+ /** The item session's lifecycle state. */
61
+ state: ItemLifecycleState;
62
+ /**
63
+ * The candidate gave no response: every response variable holds its default or NULL. False for an
64
+ * item with no response variables, which "by definition cannot be skipped" (§7.19).
65
+ */
66
+ skipped: boolean;
67
+ /** Every interaction's response satisfies its constraints (the item's `valid`). */
68
+ valid: boolean;
69
+ }
70
+ /** The rules of one item session. Pure: the same input gives the same answers on every runtime. */
71
+ export declare function itemRules(input: ItemRulesInput): ItemRules;
72
+ /** Where the candidate is in a part, for its navigation answers. */
73
+ export interface NavigationRulesInput {
74
+ navigationMode: TestPartModel['navigationMode'];
75
+ submissionMode: TestPartModel['submissionMode'];
76
+ /** The current item's position among the part's items in order, from 0; -1 when none is current. */
77
+ index: number;
78
+ /** The number of items in the part. */
79
+ count: number;
80
+ /** The rules of the current item. */
81
+ current: Pick<ItemRules, 'maySubmit' | 'interactive'>;
82
+ }
83
+ /** Whether the candidate may move to the next or the previous item of a part. */
84
+ export interface NavigationRules {
85
+ /**
86
+ * There is a next item and, in a linear part with individual submission, the current item may be
87
+ * left: Next ends an attempt that was not ended and closes the item (decided 2026-11-27), so its
88
+ * responses must be ones the candidate may submit.
89
+ */
90
+ readonly mayGoNext: boolean;
91
+ /** There is a previous item, in a nonlinear part: a linear part never goes back (§4.1). */
92
+ readonly mayGoPrevious: boolean;
93
+ }
94
+ /** The navigation answers of a part. Pure. */
95
+ export declare function navigationRules({ navigationMode, submissionMode, index, count, current }: NavigationRulesInput): NavigationRules;
@@ -0,0 +1,33 @@
1
+ import { itemLifecycleGuards, transition } from './item-lifecycle.js';
2
+ /** The rules of one item session. Pure: the same input gives the same answers on every runtime. */
3
+ export function itemRules(input) {
4
+ const { state, sessionControl, adaptive } = input;
5
+ const guards = itemLifecycleGuards(input);
6
+ const individual = guards.individualSubmission;
7
+ const maySkip = !individual || sessionControl.allowSkipping || !input.skipped;
8
+ const maySubmit = maySkip && (!individual || !sessionControl.validateResponses || input.valid);
9
+ return {
10
+ attemptsLeft: guards.attemptsLeft,
11
+ attemptsUsed: !guards.attemptsLeft,
12
+ interactive: state === 'interacting',
13
+ done: state === 'closed' || state === 'review' || state === 'solution',
14
+ mayEndAttempt: maySubmit && transition(state, 'endAttempt', guards) !== null,
15
+ maySubmit,
16
+ maySkip,
17
+ mayShowFeedback: guards.attemptsLeft ||
18
+ sessionControl.showFeedback ||
19
+ (state === 'review' && adaptive && sessionControl.allowReview),
20
+ mayBeginReview: transition(state, 'beginReview', guards) !== null,
21
+ mayBeginSolution: transition(state, 'beginSolution', guards) !== null
22
+ };
23
+ }
24
+ /** The navigation answers of a part. Pure. */
25
+ export function navigationRules({ navigationMode, submissionMode, index, count, current }) {
26
+ const onItem = index >= 0 && index < count;
27
+ const endsAttempt = navigationMode === 'linear' && submissionMode === 'individual' && current.interactive;
28
+ return {
29
+ mayGoNext: onItem && index < count - 1 && (!endsAttempt || current.maySubmit),
30
+ mayGoPrevious: onItem && index > 0 && navigationMode === 'nonlinear'
31
+ };
32
+ }
33
+ //# sourceMappingURL=item-rules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"item-rules.js","sourceRoot":"","sources":["../../src/lifecycle/item-rules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AA0EtE,mGAAmG;AACnG,MAAM,UAAU,SAAS,CAAC,KAAqB;IAC7C,MAAM,EAAE,KAAK,EAAE,cAAc,EAAE,Q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I,cAAc,KAAK,YAAY,IAAI,OAAO,CAAC,WAAW,CAAC;IAC1G,OAAO;QACL,SAAS,EAAE,MAAM,IAAI,KAAK,GAAG,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC,WAAW,IAAI,OAAO,CAAC,SAAS,CAAC;QAC7E,aAAa,EAAE,MAAM,IAAI,KAAK,GAAG,CAAC,IAAI,cAAc,KAAK,WAAW;KACrE,CAAC;AACJ,CAAC","sourcesContent":["import { itemLifecycleGuards, transition } from './item-lifecycle.js';\n\nimport type { SessionControl, TestPartModel } from '../model/test-model.js';\nimport type { ItemLifecycleInput, ItemLifecycleState } from './item-lifecycle.js';\n\n/**\n * What one item session may do now, answered from its lifecycle state, its session control and its\n * responses. Every reader of attempts, skipping, feedback, review or the solution asks here instead\n * of working the rule out itself (roadmap 1, T3b).\n *\n * The answers follow QTI 3 (`qti-item-session-control`, info model §7.19; implementation guide\n * §4.1 and §4.4.2) and nothing else: whether the last attempt was optimal decides none of them\n * (decided 2026-11-27, `plans/host-api.md`).\n *\n * Experimental (0.x).\n */\nexport interface ItemRules {\n /**\n * Another attempt is allowed: `max-attempts` not reached (0 is unlimited), or, for an adaptive\n * item, which ignores `max-attempts`, `completionStatus` not `completed`.\n */\n readonly attemptsLeft: boolean;\n /** No further attempt is allowed: the negation of `attemptsLeft`. */\n readonly attemptsUsed: boolean;\n /** The responses can change: only while `interacting`. In every other state they are fixed or not shown. */\n readonly interactive: boolean;\n /** The item session is over: `closed`, or in `review` or `solution`, which come back to `closed`. */\n readonly done: boolean;\n /**\n * The candidate may end the attempt now: the item is `interacting` in a part with individual\n * submission, and `maySubmit` holds.\n */\n readonly mayEndAttempt: boolean;\n /**\n * The responses may be submitted as they are: `allow-skipping=\"false\"` refuses an item with no\n * response, `validate-responses` an invalid one. Two separate rules, both for individual\n * submission only (§7.19).\n */\n readonly maySubmit: boolean;\n /** Submitting no response is allowed: `allow-skipping`, or a response given, or simultaneous submission. */\n readonly maySkip: boolean;\n /**\n * Feedback, modal and integrated, may show: always while attempts are left, after the last\n * attempt only with `show-feedback`. In `review`, an adaptive item with `allow-review` shows its\n * feedback whatever `show-feedback` says (§7.19, `allow-review`).\n */\n readonly mayShowFeedback: boolean;\n /**\n * The item session may enter `review` now: it is `closed`, with `allow-review`. False in `review`\n * itself; whether review shows is the state.\n */\n readonly mayBeginReview: boolean;\n /**\n * The item session may enter `solution`, which shows the correct responses: it is `closed`, with\n * `show-solution`. False in `solution` itself; whether the solution shows is the state.\n */\n readonly mayBeginSolution: boolean;\n}\n\n/** What the rules of one item session are read from: its lifecycle input, its state and its responses. */\nexport interface ItemRulesInput extends Omit<ItemLifecycleInput, 'sessionControl'> {\n /** The item-ref's resolved session control (`ItemRefModel.sessionControl`). */\n sessionControl: Omit<SessionControl, 'allowComment'>;\n /** The item session's lifecycle state. */\n state: ItemLifecycleState;\n /**\n * The candidate gave no response: every response variable holds its default or NULL. False for an\n * item with no response variables, which \"by definition cannot be skipped\" (§7.19).\n */\n skipped: boolean;\n /** Every interaction's response satisfies its constraints (the item's `valid`). */\n valid: boolean;\n}\n\n/** The rules of one item session. Pure: the same input gives the same answers on every runtime. */\nexport function itemRules(input: ItemRulesInput): ItemRules {\n const { state, sessionControl, adaptive } = input;\n const guards = itemLifecycleGuards(input);\n const individual = guards.individualSubmission;\n const maySkip = !individual || sessionControl.allowSkipping || !input.skipped;\n const maySubmit = maySkip && (!individual || !sessionControl.validateResponses || input.valid);\n return {\n attemptsLeft: guards.attemptsLeft,\n attemptsUsed: !guards.attemptsLeft,\n interactive: state === 'interacting',\n done: state === 'closed' || state === 'review' || state === 'solution',\n mayEndAttempt: maySubmit && transition(state, 'endAttempt', guards) !== null,\n maySubmit,\n maySkip,\n mayShowFeedback:\n guards.attemptsLeft ||\n sessionControl.showFeedback ||\n (state === 'review' && adaptive && sessionControl.allowReview),\n mayBeginReview: transition(state, 'beginReview', guards) !== null,\n mayBeginSolution: transition(state, 'beginSolution', guards) !== null\n };\n}\n\n/** Where the candidate is in a part, for its navigation answers. */\nexport interface NavigationRulesInput {\n navigationMode: TestPartModel['navigationMode'];\n submissionMode: TestPartModel['submissionMode'];\n /** The current item's position among the part's items in order, from 0; -1 when none is current. */\n index: number;\n /** The number of items in the part. */\n count: number;\n /** The rules of the current item. */\n current: Pick<ItemRules, 'maySubmit' | 'interactive'>;\n}\n\n/** Whether the candidate may move to the next or the previous item of a part. */\nexport interface NavigationRules {\n /**\n * There is a next item and, in a linear part with individual submission, the current item may be\n * left: Next ends an attempt that was not ended and closes the item (decided 2026-11-27), so its\n * responses must be ones the candidate may submit.\n */\n readonly mayGoNext: boolean;\n /** There is a previous item, in a nonlinear part: a linear part never goes back (§4.1). */\n readonly mayGoPrevious: boolean;\n}\n\n/** The navigation answers of a part. Pure. */\nexport function navigationRules({\n navigationMode,\n submissionMode,\n index,\n count,\n current\n}: NavigationRulesInput): NavigationRules {\n const onItem = index >= 0 && index < count;\n const endsAttempt = navigationMode === 'linear' && submissionMode === 'individual' && current.interactive;\n return {\n mayGoNext: onItem && index < count - 1 && (!endsAttempt || current.maySubmit),\n mayGoPrevious: onItem && index > 0 && navigationMode === 'nonlinear'\n };\n}\n"]}
@@ -4,8 +4,10 @@ export interface CreateDeliveryStateOptions {
4
4
  clock: Clock;
5
5
  seed?: string | null;
6
6
  /**
7
- * The item-ref identifiers after selection and ordering, in the order the candidate meets them.
8
- * An item-ref listed again is its next occurrence (`with-replacement`, roadmap 1 T4).
7
+ * The items after selection and ordering, in the order the candidate meets them: the rendered
8
+ * test's item-ref identifiers, where a `with-replacement` clone is its instance id (`item-3#2`,
9
+ * roadmap 1 T4) and keeps that occurrence wherever ordering put it. An item-ref listed again
10
+ * without a suffix is its next free occurrence.
9
11
  */
10
12
  order?: readonly string[];
11
13
  }
@@ -1,15 +1,22 @@
1
- import { DELIVERY_STATE_VERSION, itemInstanceId } from './delivery-state.js';
1
+ import { DELIVERY_STATE_VERSION, itemInstanceId, parseItemInstanceId } from './delivery-state.js';
2
2
  /**
3
3
  * A fresh delivery state: the item instances in `order`, nothing on screen, no plugin entries,
4
4
  * started now by the given clock.
5
5
  */
6
6
  export function createDeliveryState({ clock, seed = null, order = [] }) {
7
- const occurrences = new Map();
7
+ const parsed = order.map(parseItemInstanceId);
8
+ // Occurrences the order names itself, so a bare repeat never takes a clone's number.
9
+ const taken = new Set(parsed.filter(p => p.occurrence > 1).map(p => itemInstanceId(p.itemRefIdentifier, p.occurrence)));
8
10
  const items = {};
9
- const instances = order.map(itemRefIdentifier => {
10
- const occurrence = (occurrences.get(itemRefIdentifier) ?? 0) + 1;
11
- occurrences.set(itemRefIdentifier, occurrence);
12
- const id = itemInstanceId(itemRefIdentifier, occurrence);
11
+ const instances = parsed.map(({ itemRefIdentifier, occurrence }) => {
12
+ let id = itemInstanceId(itemRefIdentifier, occurrence);
13
+ if (occurrence === 1) {
14
+ while (Object.hasOwn(items, id) || taken.has(id))
15
+ id = itemInstanceId(itemRefIdentifier, ++occurrence);
16
+ }
17
+ else if (Object.hasOwn(items, id)) {
18
+ throw new RangeError(`The order lists the item instance ${id} twice.`);
19
+ }
13
20
  items[id] = { itemRefIdentifier, occurrence };
14
21
  return id;
15
22
  });
@@ -1 +1 @@
1
- {"version":3,"file":"create-delivery-state.js","sourceRoot":"","sources":["../../src/state/create-delivery-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAe7E;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,EAAE,KAAK,GAAG,EAAE,EAA8B;IAChG,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC9C,MAAM,KAAK,GAA8C,EAAE,CAAC;IAC5D,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,iBAAiB,CAAC,EAAE;QAC9C,MAAM,UAAU,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;QACjE,WAAW,CAAC,GAAG,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;QAC/C,MAAM,EAAE,GAAG,cAAc,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;QACzD,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,EAAE,UAAU,EAAE,CAAC;QAC9C,OAAO,EAAE,CAAC;IACZ,CAAC,CAAC,CAAC;IACH,OAAO;QACL,OAAO,EAAE,sBAAsB;QAC/B,IAAI;QACJ,SAAS,EAAE,KAAK,CAAC,GAAG,EAAE;QACtB,OAAO,EAAE,IAAI;QACb,KAAK,EAAE,SAAS;QAChB,OAAO,EAAE,EAAE;QACX,KAAK;KACN,CAAC;AACJ,CAAC","sourcesContent":["import { DELIVERY_STATE_VERSION, itemInstanceId } from './delivery-state.js';\n\nimport type { Clock } from './clock.js';\nimport type { DeliveryState, ItemDeliveryState, ItemInstanceId } from './delivery-state.js';\n\nexport interface CreateDeliveryStateOptions {\n clock: Clock;\n seed?: string | null;\n /**\n * The item-ref identifiers after selection and ordering, in the order the candidate meets them.\n * An item-ref listed again is its next occurrence (`with-replacement`, roadmap 1 T4).\n */\n order?: readonly string[];\n}\n\n/**\n * A fresh delivery state: the item instances in `order`, nothing on screen, no plugin entries,\n * started now by the given clock.\n */\nexport function createDeliveryState({ clock, seed = null, order = [] }: CreateDeliveryStateOptions): DeliveryState {\n const occurrences = new Map<string, number>();\n const items: Record<ItemInstanceId, ItemDeliveryState> = {};\n const instances = order.map(itemRefIdentifier => {\n const occurrence = (occurrences.get(itemRefIdentifier) ?? 0) + 1;\n occurrences.set(itemRefIdentifier, occurrence);\n const id = itemInstanceId(itemRefIdentifier, occurrence);\n items[id] = { itemRefIdentifier, occurrence };\n return id;\n });\n return {\n version: DELIVERY_STATE_VERSION,\n seed,\n startedAt: clock.now(),\n current: null,\n order: instances,\n plugins: {},\n items\n };\n}\n"]}
1
+ {"version":3,"file":"create-delivery-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C;QAC9C,OAAO,EAAE,CAAC;IACZ,CAAC,CAAC,CAAC;IACH,OAAO;QACL,OAAO,EAAE,sBAAsB;QAC/B,IAAI;QACJ,SAAS,EAAE,KAAK,CAAC,GAAG,EAAE;QACtB,OAAO,EAAE,IAAI;QACb,KAAK,EAAE,SAAS;QAChB,OAAO,EAAE,EAAE;QACX,KAAK;KACN,CAAC;AACJ,CAAC","sourcesContent":["import { DELIVERY_STATE_VERSION, itemInstanceId, parseItemInstanceId } from './delivery-state.js';\n\nimport type { Clock } from './clock.js';\nimport type { DeliveryState, ItemDeliveryState, ItemInstanceId } from './delivery-state.js';\n\nexport interface CreateDeliveryStateOptions {\n clock: Clock;\n seed?: string | null;\n /**\n * The items after selection and ordering, in the order the candidate meets them: the rendered\n * test's item-ref identifiers, where a `with-replacement` clone is its instance id (`item-3#2`,\n * roadmap 1 T4) and keeps that occurrence wherever ordering put it. An item-ref listed again\n * without a suffix is its next free occurrence.\n */\n order?: readonly string[];\n}\n\n/**\n * A fresh delivery state: the item instances in `order`, nothing on screen, no plugin entries,\n * started now by the given clock.\n */\nexport function createDeliveryState({ clock, seed = null, order = [] }: CreateDeliveryStateOptions): DeliveryState {\n const parsed = order.map(parseItemInstanceId);\n // Occurrences the order names itself, so a bare repeat never takes a clone's number.\n const taken = new Set(\n parsed.filter(p => p.occurrence > 1).map(p => itemInstanceId(p.itemRefIdentifier, p.occurrence))\n );\n const items: Record<ItemInstanceId, ItemDeliveryState> = {};\n const instances = parsed.map(({ itemRefIdentifier, occurrence }) => {\n let id = itemInstanceId(itemRefIdentifier, occurrence);\n if (occurrence === 1) {\n while (Object.hasOwn(items, id) || taken.has(id)) id = itemInstanceId(itemRefIdentifier, ++occurrence);\n } else if (Object.hasOwn(items, id)) {\n throw new RangeError(`The order lists the item instance ${id} twice.`);\n }\n items[id] = { itemRefIdentifier, occurrence };\n return id;\n });\n return {\n version: DELIVERY_STATE_VERSION,\n seed,\n startedAt: clock.now(),\n current: null,\n order: instances,\n plugins: {},\n items\n };\n}\n"]}
@@ -46,6 +46,15 @@ export interface DeliveryState {
46
46
  }
47
47
  /** The instance id of the `occurrence`th selection of an item-ref: `item-3`, `item-3#2`. */
48
48
  export declare const itemInstanceId: (itemRefIdentifier: string, occurrence?: number) => ItemInstanceId;
49
+ /**
50
+ * The item-ref and occurrence an instance id names, the inverse of `itemInstanceId`: `item-3#2` is
51
+ * the second selection of `item-3`. Only a suffix `itemInstanceId` writes (`#2` and up) counts; `#`
52
+ * cannot occur in a QTI identifier, so no authored identifier reads as a clone.
53
+ */
54
+ export declare function parseItemInstanceId(id: ItemInstanceId): {
55
+ itemRefIdentifier: string;
56
+ occurrence: number;
57
+ };
49
58
  /**
50
59
  * Read a stored state back. Throws on a state this release cannot read rather than guessing, so a
51
60
  * host never resumes from something half understood: a `RangeError` for another version, a
@@ -11,6 +11,18 @@
11
11
  export const DELIVERY_STATE_VERSION = 1;
12
12
  /** The instance id of the `occurrence`th selection of an item-ref: `item-3`, `item-3#2`. */
13
13
  export const itemInstanceId = (itemRefIdentifier, occurrence = 1) => occurrence === 1 ? itemRefIdentifier : `${itemRefIdentifier}#${occurrence}`;
14
+ /**
15
+ * The item-ref and occurrence an instance id names, the inverse of `itemInstanceId`: `item-3#2` is
16
+ * the second selection of `item-3`. Only a suffix `itemInstanceId` writes (`#2` and up) counts; `#`
17
+ * cannot occur in a QTI identifier, so no authored identifier reads as a clone.
18
+ */
19
+ export function parseItemInstanceId(id) {
20
+ const match = /^(.+)#([1-9]\d*)$/.exec(id);
21
+ const occurrence = match ? Number(match[2]) : 1;
22
+ return match && occurrence >= 2
23
+ ? { itemRefIdentifier: match[1], occurrence }
24
+ : { itemRefIdentifier: id, occurrence: 1 };
25
+ }
14
26
  const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
15
27
  /**
16
28
  * Read a stored state back. Throws on a state this release cannot read rather than guessing, so a
@@ -1 +1 @@
1
- {"version":3,"file":"delivery-state.js","sourceRoot":"","sources":["../../src/state/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,OAAO,SAAS,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC;QAAE,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IAChG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;IACjD,KAAK,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,EAAE,CAAC;QAC1E,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,iBAAiB,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YACxG,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,+DAA+D,CAAC,CAAC;QAC1F,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,CAAC,EAAW,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,CAAC,KAAe,EAAE,EAAE,CAAC,CAAC;IAC5F,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,OAAO,EAAE,mCAAmC,CAAC,CAAC;IACrG,IAAI,OAAO,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;QAAE,IAAI,CAAC,SAAS,EAAE,iCAAiC,CAAC,CAAC;IAC5F,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,IAAI,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;IACrD,OAAO,KAAiC,CAAC;AAC3C,CAAC","sourcesContent":["/**\n * The one source of truth for one candidate in one test: what a resumed test, or a server taking\n * over, needs to carry on. Written only through commands; every context is an input or a view\n * derived from it, and anything derivable (done, can*, scores) is never stored here. Plain JSON,\n * versioned, so a stored state can be read back by a later release (or a .NET port) and refused\n * when it cannot.\n *\n * Experimental (0.x): roadmap 1 fills in the target fields, phase by phase; see\n * `plans/delivery-state.md`.\n */\nexport const DELIVERY_STATE_VERSION = 1;\n\n/**\n * One item session. With `with-replacement` selection a single item-ref appears more than once,\n * so a session is keyed by its instance, never by the item-ref identifier alone.\n */\nexport type ItemInstanceId = string;\n\nexport interface ItemDeliveryState {\n /** The `identifier` of the `qti-assessment-item-ref` this instance was selected from. */\n itemRefIdentifier: string;\n /** 1 for the first time the item-ref was selected, 2 for its first clone, and so on. */\n occurrence: number;\n}\n\nexport interface DeliveryState {\n version: typeof DELIVERY_STATE_VERSION;\n /**\n * The seed actually used for selection and ordering; the same seed gives the same test on every\n * runtime. On a first start it comes from the host (`qtiContext.seed`); on resume this one wins.\n */\n seed: string | null;\n /** When the test started, in milliseconds since the epoch, read from the injected clock. */\n startedAt: number;\n /** The item instance on screen, or null before the first navigation and after the end. */\n current: ItemInstanceId | null;\n /**\n * Every item instance of the test after selection and ordering, in the order the candidate meets\n * them; written once, when the test starts. Each one is a key of `items`.\n */\n order: ItemInstanceId[];\n /**\n * What test plugins keep, by plugin name: JSON, stored and restored without being read (a\n * plugin reads its own entry).\n */\n plugins: Record<string, unknown>;\n /** One entry per instance in `order`. */\n items: Record<ItemInstanceId, ItemDeliveryState>;\n}\n\n/** The instance id of the `occurrence`th selection of an item-ref: `item-3`, `item-3#2`. */\nexport const itemInstanceId = (itemRefIdentifier: string, occurrence = 1): ItemInstanceId =>\n occurrence === 1 ? itemRefIdentifier : `${itemRefIdentifier}#${occurrence}`;\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/**\n * Read a stored state back. Throws on a state this release cannot read rather than guessing, so a\n * host never resumes from something half understood: a `RangeError` for another version, a\n * `TypeError` for anything that is not a version 1 state (a missing or mistyped field, an `order`\n * or `current` that names an instance `items` does not have).\n */\nexport function parseDeliveryState(value: unknown): DeliveryState {\n if (!isRecord(value)) {\n throw new TypeError('A delivery state is an object.');\n }\n const { version } = value;\n if (version !== DELIVERY_STATE_VERSION) {\n throw new RangeError(\n `Delivery state version ${String(version)} is not supported; expected ${DELIVERY_STATE_VERSION}.`\n );\n }\n const { seed, startedAt, current, order, plugins, items } = value;\n const fail = (field: string, expected: string): never => {\n throw new TypeError(`A delivery state's ${field} must be ${expected}.`);\n };\n if (seed !== null && typeof seed !== 'string') fail('seed', 'a string or null');\n if (typeof startedAt !== 'number' || !Number.isFinite(startedAt)) fail('startedAt', 'a number');\n if (!isRecord(items)) fail('items', 'an object');\n for (const [id, item] of Object.entries(items as Record<string, unknown>)) {\n if (!isRecord(item) || typeof item.itemRefIdentifier !== 'string' || !Number.isInteger(item.occurrence)) {\n fail(`items[\"${id}\"]`, 'an object with an itemRefIdentifier and an integer occurrence');\n }\n }\n const known = (id: unknown) => typeof id === 'string' && Object.hasOwn(items as object, id);\n if (!Array.isArray(order) || !order.every(known)) fail('order', 'an array of instance ids in items');\n if (current !== null && !known(current)) fail('current', 'null or an instance id in items');\n if (!isRecord(plugins)) fail('plugins', 'an object');\n return value as unknown as DeliveryState;\n}\n"]}
1
+ {"version":3,"file":"delivery-state.js","sourceRoot":"","sources":["../../src/state/delivery-state.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,CAAC;AAwCxC,4FAA4F;AAC5F,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,iBAAyB,EAAE,UAAU,GAAG,CAAC,EAAkB,EAAE,CAC1F,UAAU,KAAK,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,GAAG,iBAAiB,IAAI,UAAU,EAAE,CAAC;AAE9E;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAAkB;IACpD,MAAM,KAAK,GAAG,mBAAmB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC3C,MA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C,CAAC;IAChF,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC;QAAE,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IAChG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;IACjD,KAAK,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,EAAE,CAAC;QAC1E,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,iBAAiB,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YACxG,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,+DAA+D,CAAC,CAAC;QAC1F,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,CAAC,EAAW,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,CAAC,KAAe,EAAE,EAAE,CAAC,CAAC;IAC5F,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,OAAO,EAAE,mCAAmC,CAAC,CAAC;IACrG,IAAI,OAAO,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;QAAE,IAAI,CAAC,SAAS,EAAE,iCAAiC,CAAC,CAAC;IAC5F,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,IAAI,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;IACrD,OAAO,KAAiC,CAAC;AAC3C,CAAC","sourcesContent":["/**\n * The one source of truth for one candidate in one test: what a resumed test, or a server taking\n * over, needs to carry on. Written only through commands; every context is an input or a view\n * derived from it, and anything derivable (done, can*, scores) is never stored here. Plain JSON,\n * versioned, so a stored state can be read back by a later release (or a .NET port) and refused\n * when it cannot.\n *\n * Experimental (0.x): roadmap 1 fills in the target fields, phase by phase; see\n * `plans/delivery-state.md`.\n */\nexport const DELIVERY_STATE_VERSION = 1;\n\n/**\n * One item session. With `with-replacement` selection a single item-ref appears more than once,\n * so a session is keyed by its instance, never by the item-ref identifier alone.\n */\nexport type ItemInstanceId = string;\n\nexport interface ItemDeliveryState {\n /** The `identifier` of the `qti-assessment-item-ref` this instance was selected from. */\n itemRefIdentifier: string;\n /** 1 for the first time the item-ref was selected, 2 for its first clone, and so on. */\n occurrence: number;\n}\n\nexport interface DeliveryState {\n version: typeof DELIVERY_STATE_VERSION;\n /**\n * The seed actually used for selection and ordering; the same seed gives the same test on every\n * runtime. On a first start it comes from the host (`qtiContext.seed`); on resume this one wins.\n */\n seed: string | null;\n /** When the test started, in milliseconds since the epoch, read from the injected clock. */\n startedAt: number;\n /** The item instance on screen, or null before the first navigation and after the end. */\n current: ItemInstanceId | null;\n /**\n * Every item instance of the test after selection and ordering, in the order the candidate meets\n * them; written once, when the test starts. Each one is a key of `items`.\n */\n order: ItemInstanceId[];\n /**\n * What test plugins keep, by plugin name: JSON, stored and restored without being read (a\n * plugin reads its own entry).\n */\n plugins: Record<string, unknown>;\n /** One entry per instance in `order`. */\n items: Record<ItemInstanceId, ItemDeliveryState>;\n}\n\n/** The instance id of the `occurrence`th selection of an item-ref: `item-3`, `item-3#2`. */\nexport const itemInstanceId = (itemRefIdentifier: string, occurrence = 1): ItemInstanceId =>\n occurrence === 1 ? itemRefIdentifier : `${itemRefIdentifier}#${occurrence}`;\n\n/**\n * The item-ref and occurrence an instance id names, the inverse of `itemInstanceId`: `item-3#2` is\n * the second selection of `item-3`. Only a suffix `itemInstanceId` writes (`#2` and up) counts; `#`\n * cannot occur in a QTI identifier, so no authored identifier reads as a clone.\n */\nexport function parseItemInstanceId(id: ItemInstanceId): { itemRefIdentifier: string; occurrence: number } {\n const match = /^(.+)#([1-9]\\d*)$/.exec(id);\n const occurrence = match ? Number(match[2]) : 1;\n return match && occurrence >= 2\n ? { itemRefIdentifier: match[1], occurrence }\n : { itemRefIdentifier: id, occurrence: 1 };\n}\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/**\n * Read a stored state back. Throws on a state this release cannot read rather than guessing, so a\n * host never resumes from something half understood: a `RangeError` for another version, a\n * `TypeError` for anything that is not a version 1 state (a missing or mistyped field, an `order`\n * or `current` that names an instance `items` does not have).\n */\nexport function parseDeliveryState(value: unknown): DeliveryState {\n if (!isRecord(value)) {\n throw new TypeError('A delivery state is an object.');\n }\n const { version } = value;\n if (version !== DELIVERY_STATE_VERSION) {\n throw new RangeError(\n `Delivery state version ${String(version)} is not supported; expected ${DELIVERY_STATE_VERSION}.`\n );\n }\n const { seed, startedAt, current, order, plugins, items } = value;\n const fail = (field: string, expected: string): never => {\n throw new TypeError(`A delivery state's ${field} must be ${expected}.`);\n };\n if (seed !== null && typeof seed !== 'string') fail('seed', 'a string or null');\n if (typeof startedAt !== 'number' || !Number.isFinite(startedAt)) fail('startedAt', 'a number');\n if (!isRecord(items)) fail('items', 'an object');\n for (const [id, item] of Object.entries(items as Record<string, unknown>)) {\n if (!isRecord(item) || typeof item.itemRefIdentifier !== 'string' || !Number.isInteger(item.occurrence)) {\n fail(`items[\"${id}\"]`, 'an object with an itemRefIdentifier and an integer occurrence');\n }\n }\n const known = (id: unknown) => typeof id === 'string' && Object.hasOwn(items as object, id);\n if (!Array.isArray(order) || !order.every(known)) fail('order', 'an array of instance ids in items');\n if (current !== null && !known(current)) fail('current', 'null or an instance id in items');\n if (!isRecord(plugins)) fail('plugins', 'an object');\n return value as unknown as DeliveryState;\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qti-components/delivery",
3
3
  "description": "The QTI 3 delivery engine: test model, item session lifecycle, sequencing and session state, with no browser globals",
4
- "version": "0.1.0-next.3",
4
+ "version": "0.1.0-next.5",
5
5
  "author": "",
6
6
  "keywords": [
7
7
  "qti",