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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ 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
4
  export * from './random/seeded-random.js';
5
+ export * from './random/selection.js';
5
6
  export * from './random/shuffle.js';
6
7
  export * from './state/clock.js';
7
8
  export * from './state/commands.js';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './model/test-model.js';
2
2
  export * from './model/item-facts.js';
3
3
  export * from './random/seeded-random.js';
4
+ export * from './random/selection.js';
4
5
  export * from './random/shuffle.js';
5
6
  export * from './state/clock.js';
6
7
  export * from './state/commands.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,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/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,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"]}
@@ -0,0 +1,36 @@
1
+ import type { RandomSource } from './shuffle.js';
2
+ /** A child of a section that selection can pick: an item-ref or a subsection, which counts as one. */
3
+ export interface SelectionUnit {
4
+ /** `required="true"`: always in the selection. */
5
+ required: boolean;
6
+ }
7
+ /** A section's `qti-selection` (QTI 3 info model §5.129). */
8
+ export interface SelectionRule {
9
+ /** How many units to select. */
10
+ select: number;
11
+ /** Whether a unit may be selected more than once (its clones). Defaults to false. */
12
+ withReplacement?: boolean;
13
+ }
14
+ /**
15
+ * Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a
16
+ * unit selected more than once (`with-replacement`) has its index repeated, the clones right after
17
+ * it. Ordering is a later, separate step (implementation guide §4.5.1: selection "should preserve
18
+ * the original order").
19
+ *
20
+ * Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)
21
+ * selects the same units for the same seed:
22
+ *
23
+ * 1. Every required unit is in.
24
+ * 2. The rest of the first `min(select, units.length)` come from the non-required units, each with
25
+ * an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for
26
+ * `i` from 0 while fewer are picked than needed, swapping position `i` with
27
+ * `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.
28
+ * 3. With replacement and `select` above `units.length`, each unit is then in once, and each further
29
+ * clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however
30
+ * often it was cloned before.
31
+ *
32
+ * Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or
33
+ * more, below the number of required units, or above the number of units without replacement (or
34
+ * above zero with no units at all).
35
+ */
36
+ export declare function selectUnits(units: readonly SelectionUnit[], rule: SelectionRule, random: RandomSource): number[];
@@ -0,0 +1,52 @@
1
+ const randomValue = (source) => (typeof source === 'function' ? source() : source.random());
2
+ /**
3
+ * Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a
4
+ * unit selected more than once (`with-replacement`) has its index repeated, the clones right after
5
+ * it. Ordering is a later, separate step (implementation guide §4.5.1: selection "should preserve
6
+ * the original order").
7
+ *
8
+ * Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)
9
+ * selects the same units for the same seed:
10
+ *
11
+ * 1. Every required unit is in.
12
+ * 2. The rest of the first `min(select, units.length)` come from the non-required units, each with
13
+ * an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for
14
+ * `i` from 0 while fewer are picked than needed, swapping position `i` with
15
+ * `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.
16
+ * 3. With replacement and `select` above `units.length`, each unit is then in once, and each further
17
+ * clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however
18
+ * often it was cloned before.
19
+ *
20
+ * Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or
21
+ * more, below the number of required units, or above the number of units without replacement (or
22
+ * above zero with no units at all).
23
+ */
24
+ export function selectUnits(units, rule, random) {
25
+ const { select, withReplacement = false } = rule;
26
+ if (!Number.isInteger(select) || select < 0) {
27
+ throw new RangeError(`qti-selection select="${select}" is not a whole number of zero or more.`);
28
+ }
29
+ const required = units.flatMap((unit, index) => (unit.required ? [index] : []));
30
+ if (select < required.length) {
31
+ throw new RangeError(`qti-selection select="${select}" is less than the ${required.length} required items and subsections.`);
32
+ }
33
+ if (select > units.length && (!withReplacement || units.length === 0)) {
34
+ throw new RangeError(`qti-selection select="${select}" is more than the ${units.length} items and subsections, without with-replacement="true".`);
35
+ }
36
+ const optional = units.flatMap((unit, index) => (unit.required ? [] : [index]));
37
+ const needed = Math.min(select, units.length) - required.length;
38
+ if (needed < optional.length) {
39
+ for (let i = 0; i < needed; i++) {
40
+ const j = i + Math.floor(randomValue(random) * (optional.length - i));
41
+ [optional[i], optional[j]] = [optional[j], optional[i]];
42
+ }
43
+ }
44
+ const occurrences = new Array(units.length).fill(0);
45
+ for (const index of [...required, ...optional.slice(0, needed)])
46
+ occurrences[index] = 1;
47
+ for (let clone = units.length; clone < select; clone++) {
48
+ occurrences[Math.floor(randomValue(random) * units.length)]++;
49
+ }
50
+ return occurrences.flatMap((count, index) => new Array(count).fill(index));
51
+ }
52
+ //# sourceMappingURL=selection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selection.js","sourceRoot":"","sources":["../../src/random/selection.ts"],"names":[],"mappings":"AAgBA,MAAM,WAAW,GAAG,CAAC,MAAoB,EAAU,EAAE,CAAC,CAAC,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAElH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,WAAW,CAAC,KAA+B,EAAE,IAAmB,EAAE,MAAoB;IACpG,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK,EAAE,GAAG,IAAI,CAAC;IACjD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,UAAU,CAAC,yBAAyB,MAAM,0CAA0C,CAAC,CAAC;IAClG,CAAC;IACD,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,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AAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC5D,KAAK,MAAM,KAAK,IAAI,CAAC,GAAG,QAAQ,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QAAE,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAExF,KAAK,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACvD,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;IAChE,CAAC;IAED,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,CAAS,KAAK,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AACrF,CAAC","sourcesContent":["import type { RandomSource } from './shuffle.js';\n\n/** A child of a section that selection can pick: an item-ref or a subsection, which counts as one. */\nexport interface SelectionUnit {\n /** `required=\"true\"`: always in the selection. */\n required: boolean;\n}\n\n/** A section's `qti-selection` (QTI 3 info model §5.129). */\nexport interface SelectionRule {\n /** How many units to select. */\n select: number;\n /** Whether a unit may be selected more than once (its clones). Defaults to false. */\n withReplacement?: boolean;\n}\n\nconst randomValue = (source: RandomSource): number => (typeof source === 'function' ? source() : source.random());\n\n/**\n * Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a\n * unit selected more than once (`with-replacement`) has its index repeated, the clones right after\n * it. Ordering is a later, separate step (implementation guide §4.5.1: selection \"should preserve\n * the original order\").\n *\n * Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)\n * selects the same units for the same seed:\n *\n * 1. Every required unit is in.\n * 2. The rest of the first `min(select, units.length)` come from the non-required units, each with\n * an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for\n * `i` from 0 while fewer are picked than needed, swapping position `i` with\n * `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.\n * 3. With replacement and `select` above `units.length`, each unit is then in once, and each further\n * clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however\n * often it was cloned before.\n *\n * Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or\n * more, below the number of required units, or above the number of units without replacement (or\n * above zero with no units at all).\n */\nexport function selectUnits(units: readonly SelectionUnit[], rule: SelectionRule, random: RandomSource): number[] {\n const { select, withReplacement = false } = rule;\n if (!Number.isInteger(select) || select < 0) {\n throw new RangeError(`qti-selection select=\"${select}\" is not a whole number of zero or more.`);\n }\n const required = units.flatMap((unit, index) => (unit.required ? [index] : []));\n if (select < required.length) {\n throw new RangeError(\n `qti-selection select=\"${select}\" is less than the ${required.length} required items and subsections.`\n );\n }\n if (select > units.length && (!withReplacement || units.length === 0)) {\n throw new RangeError(\n `qti-selection select=\"${select}\" is more than the ${units.length} items and subsections, without with-replacement=\"true\".`\n );\n }\n\n const optional = units.flatMap((unit, index) => (unit.required ? [] : [index]));\n const needed = Math.min(select, units.length) - required.length;\n if (needed < optional.length) {\n for (let i = 0; i < needed; i++) {\n const j = i + Math.floor(randomValue(random) * (optional.length - i));\n [optional[i], optional[j]] = [optional[j], optional[i]];\n }\n }\n const occurrences = new Array<number>(units.length).fill(0);\n for (const index of [...required, ...optional.slice(0, needed)]) occurrences[index] = 1;\n\n for (let clone = units.length; clone < select; clone++) {\n occurrences[Math.floor(randomValue(random) * units.length)]++;\n }\n\n return occurrences.flatMap((count, index) => new Array<number>(count).fill(index));\n}\n"]}
@@ -3,6 +3,14 @@ import type { DeliveryState } from './delivery-state.js';
3
3
  export interface CreateDeliveryStateOptions {
4
4
  clock: Clock;
5
5
  seed?: string | null;
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).
9
+ */
10
+ order?: readonly string[];
6
11
  }
7
- /** A fresh delivery state: nothing selected, nothing on screen, started now by the given clock. */
8
- export declare function createDeliveryState({ clock, seed }: CreateDeliveryStateOptions): DeliveryState;
12
+ /**
13
+ * A fresh delivery state: the item instances in `order`, nothing on screen, no plugin entries,
14
+ * started now by the given clock.
15
+ */
16
+ export declare function createDeliveryState({ clock, seed, order }: CreateDeliveryStateOptions): DeliveryState;
@@ -1,6 +1,26 @@
1
- import { DELIVERY_STATE_VERSION } from './delivery-state.js';
2
- /** A fresh delivery state: nothing selected, nothing on screen, started now by the given clock. */
3
- export function createDeliveryState({ clock, seed = null }) {
4
- return { version: DELIVERY_STATE_VERSION, seed, startedAt: clock.now(), current: null, items: {} };
1
+ import { DELIVERY_STATE_VERSION, itemInstanceId } from './delivery-state.js';
2
+ /**
3
+ * A fresh delivery state: the item instances in `order`, nothing on screen, no plugin entries,
4
+ * started now by the given clock.
5
+ */
6
+ export function createDeliveryState({ clock, seed = null, order = [] }) {
7
+ const occurrences = new Map();
8
+ 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);
13
+ items[id] = { itemRefIdentifier, occurrence };
14
+ return id;
15
+ });
16
+ return {
17
+ version: DELIVERY_STATE_VERSION,
18
+ seed,
19
+ startedAt: clock.now(),
20
+ current: null,
21
+ order: instances,
22
+ plugins: {},
23
+ items
24
+ };
5
25
  }
6
26
  //# sourceMappingURL=create-delivery-state.js.map
@@ -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,MAAM,qBAAqB,CAAC;AAU7D,mGAAmG;AACnG,MAAM,UAAU,mBAAmB,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,EAA8B;IACpF,OAAO,EAAE,OAAO,EAAE,sBAAsB,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;AACrG,CAAC","sourcesContent":["import { DELIVERY_STATE_VERSION } from './delivery-state.js';\n\nimport type { Clock } from './clock.js';\nimport type { DeliveryState } from './delivery-state.js';\n\nexport interface CreateDeliveryStateOptions {\n clock: Clock;\n seed?: string | null;\n}\n\n/** A fresh delivery state: nothing selected, nothing on screen, started now by the given clock. */\nexport function createDeliveryState({ clock, seed = null }: CreateDeliveryStateOptions): DeliveryState {\n return { version: DELIVERY_STATE_VERSION, seed, startedAt: clock.now(), current: null, items: {} };\n}\n"]}
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"]}
@@ -31,12 +31,25 @@ export interface DeliveryState {
31
31
  startedAt: number;
32
32
  /** The item instance on screen, or null before the first navigation and after the end. */
33
33
  current: ItemInstanceId | null;
34
+ /**
35
+ * Every item instance of the test after selection and ordering, in the order the candidate meets
36
+ * them; written once, when the test starts. Each one is a key of `items`.
37
+ */
38
+ order: ItemInstanceId[];
39
+ /**
40
+ * What test plugins keep, by plugin name: JSON, stored and restored without being read (a
41
+ * plugin reads its own entry).
42
+ */
43
+ plugins: Record<string, unknown>;
44
+ /** One entry per instance in `order`. */
34
45
  items: Record<ItemInstanceId, ItemDeliveryState>;
35
46
  }
36
47
  /** The instance id of the `occurrence`th selection of an item-ref: `item-3`, `item-3#2`. */
37
48
  export declare const itemInstanceId: (itemRefIdentifier: string, occurrence?: number) => ItemInstanceId;
38
49
  /**
39
- * Read a stored state back. Throws on a state this release cannot read (another version, or not a
40
- * state at all) rather than guessing, so a host never resumes from something half understood.
50
+ * Read a stored state back. Throws on a state this release cannot read rather than guessing, so a
51
+ * host never resumes from something half understood: a `RangeError` for another version, a
52
+ * `TypeError` for anything that is not a version 1 state (a missing or mistyped field, an `order`
53
+ * or `current` that names an instance `items` does not have).
41
54
  */
42
55
  export declare function parseDeliveryState(value: unknown): DeliveryState;
@@ -11,18 +11,43 @@
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
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
14
15
  /**
15
- * Read a stored state back. Throws on a state this release cannot read (another version, or not a
16
- * state at all) rather than guessing, so a host never resumes from something half understood.
16
+ * Read a stored state back. Throws on a state this release cannot read rather than guessing, so a
17
+ * host never resumes from something half understood: a `RangeError` for another version, a
18
+ * `TypeError` for anything that is not a version 1 state (a missing or mistyped field, an `order`
19
+ * or `current` that names an instance `items` does not have).
17
20
  */
18
21
  export function parseDeliveryState(value) {
19
- if (typeof value !== 'object' || value === null) {
22
+ if (!isRecord(value)) {
20
23
  throw new TypeError('A delivery state is an object.');
21
24
  }
22
25
  const { version } = value;
23
26
  if (version !== DELIVERY_STATE_VERSION) {
24
27
  throw new RangeError(`Delivery state version ${String(version)} is not supported; expected ${DELIVERY_STATE_VERSION}.`);
25
28
  }
29
+ const { seed, startedAt, current, order, plugins, items } = value;
30
+ const fail = (field, expected) => {
31
+ throw new TypeError(`A delivery state's ${field} must be ${expected}.`);
32
+ };
33
+ if (seed !== null && typeof seed !== 'string')
34
+ fail('seed', 'a string or null');
35
+ if (typeof startedAt !== 'number' || !Number.isFinite(startedAt))
36
+ fail('startedAt', 'a number');
37
+ if (!isRecord(items))
38
+ fail('items', 'an object');
39
+ for (const [id, item] of Object.entries(items)) {
40
+ if (!isRecord(item) || typeof item.itemRefIdentifier !== 'string' || !Number.isInteger(item.occurrence)) {
41
+ fail(`items["${id}"]`, 'an object with an itemRefIdentifier and an integer occurrence');
42
+ }
43
+ }
44
+ const known = (id) => typeof id === 'string' && Object.hasOwn(items, id);
45
+ if (!Array.isArray(order) || !order.every(known))
46
+ fail('order', 'an array of instance ids in items');
47
+ if (current !== null && !known(current))
48
+ fail('current', 'null or an instance id in items');
49
+ if (!isRecord(plugins))
50
+ fail('plugins', 'an object');
26
51
  return value;
27
52
  }
28
53
  //# sourceMappingURL=delivery-state.js.map
@@ -1 +1 @@
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;AA6BxC,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;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,MAAM,IAAI,SAAS,CAAC,gCAAgC,CAAC,CAAC;IACxD,CAAC;IACD,MAAM,EAAE,OAAO,EAAE,GAAG,KAA8B,CAAC;IACnD,IAAI,OAAO,KAAK,sBAAsB,EAAE,CAAC;QACvC,MAAM,IAAI,UAAU,CAClB,0BAA0B,MAAM,CAAC,OAAO,CAAC,+BAA+B,sBAAsB,GAAG,CAClG,CAAC;IACJ,CAAC;IACD,OAAO,KAAsB,CAAC;AAChC,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 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 * Read a stored state back. Throws on a state this release cannot read (another version, or not a\n * state at all) rather than guessing, so a host never resumes from something half understood.\n */\nexport function parseDeliveryState(value: unknown): DeliveryState {\n if (typeof value !== 'object' || value === null) {\n throw new TypeError('A delivery state is an object.');\n }\n const { version } = value as { version?: unknown };\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 return value 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,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAoC,EAAE,CACpE,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAEvE;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACrB,MAAM,IAAI,SAAS,CAAC,gCAAgC,CAAC,CAAC;IACxD,CAAC;IACD,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC;IAC1B,IAAI,OAAO,KAAK,sBAAsB,EAAE,CAAC;QACvC,MAAM,IAAI,UAAU,CAClB,0BAA0B,MAAM,CAAC,OAAO,CAAC,+BAA+B,sBAAsB,GAAG,CAClG,CAAC;IACJ,CAAC;IACD,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;IAClE,MAAM,IAAI,GAAG,CAAC,KAAa,EA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AC,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"]}
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.1",
4
+ "version": "0.1.0-next.3",
5
5
  "author": "",
6
6
  "keywords": [
7
7
  "qti",