@qti-components/delivery 0.1.0-next.8 → 0.1.0-next.9

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.
@@ -12,7 +12,7 @@ export interface CreateDeliveryStateOptions {
12
12
  order?: readonly string[];
13
13
  }
14
14
  /**
15
- * A fresh delivery state: the item instances in `order`, nothing on screen, no plugin entries,
16
- * started now by the given clock.
15
+ * A fresh delivery state: the item instances in `order`, each `initial` with no attempts and
16
+ * `not_attempted`, nothing on screen, no plugin entries, started now by the given clock.
17
17
  */
18
18
  export declare function createDeliveryState({ clock, seed, order }: CreateDeliveryStateOptions): DeliveryState;
@@ -1,7 +1,7 @@
1
1
  import { DELIVERY_STATE_VERSION, itemInstanceId, parseItemInstanceId } from './delivery-state.js';
2
2
  /**
3
- * A fresh delivery state: the item instances in `order`, nothing on screen, no plugin entries,
4
- * started now by the given clock.
3
+ * A fresh delivery state: the item instances in `order`, each `initial` with no attempts and
4
+ * `not_attempted`, nothing on screen, no plugin entries, started now by the given clock.
5
5
  */
6
6
  export function createDeliveryState({ clock, seed = null, order = [] }) {
7
7
  const parsed = order.map(parseItemInstanceId);
@@ -17,7 +17,13 @@ export function createDeliveryState({ clock, seed = null, order = [] }) {
17
17
  else if (Object.hasOwn(items, id)) {
18
18
  throw new RangeError(`The order lists the item instance ${id} twice.`);
19
19
  }
20
- items[id] = { itemRefIdentifier, occurrence };
20
+ items[id] = {
21
+ itemRefIdentifier,
22
+ occurrence,
23
+ lifecycle: 'initial',
24
+ numAttempts: 0,
25
+ completionStatus: 'not_attempted'
26
+ };
21
27
  return id;
22
28
  });
23
29
  return {
@@ -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,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAiBlG;;;GAGG;AACH,MAAM,UAAU,mBAA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AAK,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"]}
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,mBAAmB,EAAE,MAAM,qBAAq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AAO;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`, each `initial` with no attempts and\n * `not_attempted`, nothing on screen, no plugin entries, 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] = {\n itemRefIdentifier,\n occurrence,\n lifecycle: 'initial',\n numAttempts: 0,\n completionStatus: 'not_attempted'\n };\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,14 +1,8 @@
1
- /**
2
- * The one source of truth for one candidate in one test: what a resumed test, or a server taking
3
- * over, needs to carry on. Written only through commands; every context is an input or a view
4
- * derived from it, and anything derivable (done, can*, scores) is never stored here. Plain JSON,
5
- * versioned, so a stored state can be read back by a later release (or a .NET port) and refused
6
- * when it cannot.
7
- *
8
- * Experimental (0.x): roadmap 1 fills in the target fields, phase by phase; see
9
- * `plans/delivery-state.md`.
10
- */
1
+ import type { ItemLifecycleState } from '../lifecycle/item-lifecycle.js';
11
2
  export declare const DELIVERY_STATE_VERSION = 1;
3
+ /** QTI's values of the built-in `completionStatus` outcome (info model §2.2.2). */
4
+ export declare const COMPLETION_STATUSES: readonly ["not_attempted", "unknown", "incomplete", "completed"];
5
+ export type CompletionStatus = (typeof COMPLETION_STATUSES)[number];
12
6
  /**
13
7
  * One item session. With `with-replacement` selection a single item-ref appears more than once,
14
8
  * so a session is keyed by its instance, never by the item-ref identifier alone.
@@ -19,6 +13,18 @@ export interface ItemDeliveryState {
19
13
  itemRefIdentifier: string;
20
14
  /** 1 for the first time the item-ref was selected, 2 for its first clone, and so on. */
21
15
  occurrence: number;
16
+ /**
17
+ * The item session's state in QTI's lifecycle (`ITEM_LIFECYCLE_STATES`, roadmap 1 T3d). What the
18
+ * session may do (`itemRules`: the can* answers) is derived from it, never stored.
19
+ */
20
+ lifecycle: ItemLifecycleState;
21
+ /**
22
+ * The attempts the candidate has ended. QTI's `numAttempts` counts an attempt as it begins; this
23
+ * delivery counts it when it ends, as a version 1 `QtiTestState` stores it (`plans/item-lifecycle.md`).
24
+ */
25
+ numAttempts: number;
26
+ /** QTI's built-in `completionStatus`: `not_attempted` until the item is shown. */
27
+ completionStatus: CompletionStatus;
22
28
  }
23
29
  export interface DeliveryState {
24
30
  version: typeof DELIVERY_STATE_VERSION;
@@ -8,7 +8,10 @@
8
8
  * Experimental (0.x): roadmap 1 fills in the target fields, phase by phase; see
9
9
  * `plans/delivery-state.md`.
10
10
  */
11
+ import { ITEM_LIFECYCLE_STATES } from '../lifecycle/item-lifecycle.js';
11
12
  export const DELIVERY_STATE_VERSION = 1;
13
+ /** QTI's values of the built-in `completionStatus` outcome (info model §2.2.2). */
14
+ export const COMPLETION_STATUSES = ['not_attempted', 'unknown', 'incomplete', 'completed'];
12
15
  /** The instance id of the `occurrence`th selection of an item-ref: `item-3`, `item-3#2`. */
13
16
  export const itemInstanceId = (itemRefIdentifier, occurrence = 1) => occurrence === 1 ? itemRefIdentifier : `${itemRefIdentifier}#${occurrence}`;
14
17
  /**
@@ -52,6 +55,16 @@ export function parseDeliveryState(value) {
52
55
  if (!isRecord(item) || typeof item.itemRefIdentifier !== 'string' || !Number.isInteger(item.occurrence)) {
53
56
  fail(`items["${id}"]`, 'an object with an itemRefIdentifier and an integer occurrence');
54
57
  }
58
+ const session = item;
59
+ if (!ITEM_LIFECYCLE_STATES.includes(session.lifecycle)) {
60
+ fail(`items["${id}"].lifecycle`, `one of ${ITEM_LIFECYCLE_STATES.join(', ')}`);
61
+ }
62
+ if (!Number.isInteger(session.numAttempts) || session.numAttempts < 0) {
63
+ fail(`items["${id}"].numAttempts`, 'a whole number, 0 or more');
64
+ }
65
+ if (!COMPLETION_STATUSES.includes(session.completionStatus)) {
66
+ fail(`items["${id}"].completionStatus`, `one of ${COMPLETION_STATUSES.join(', ')}`);
67
+ }
55
68
  }
56
69
  const known = (id) => typeof id === 'string' && Object.hasOwn(items, id);
57
70
  if (!Array.isArray(order) || !order.every(known))
@@ -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;AAwCxC,4FAA4F;AAC5F,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,iBAAyB,EAAE,UAAU,GAAG,CAAC,EAAkB,EAAE,CAC1F,UA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5F,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"]}
1
+ {"version":3,"file":"delivery-state.js","sourceRoot":"","sources":["../../src/state/delivery-state.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAIvE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,CAAC;AAExC,mFAAmF;AACnF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,CAAU,CAAC;AAsDpG,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,MAAM,UAAU,GAAG,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,OAAO,KAAK,IAAI,UAAU,IAAI,CAAC;QAC7B,CAAC,CAAC,EAAE,iBAAiB,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE;QAC7C,CAAC,CAAC,EAAE,iBAAiB,EAAE,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;AAC/C,CAAC;AAED,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAoC,EAAE,CACpE,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OA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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 */\nimport { ITEM_LIFECYCLE_STATES } from '../lifecycle/item-lifecycle.js';\n\nimport type { ItemLifecycleState } from '../lifecycle/item-lifecycle.js';\n\nexport const DELIVERY_STATE_VERSION = 1;\n\n/** QTI's values of the built-in `completionStatus` outcome (info model §2.2.2). */\nexport const COMPLETION_STATUSES = ['not_attempted', 'unknown', 'incomplete', 'completed'] as const;\n\nexport type CompletionStatus = (typeof COMPLETION_STATUSES)[number];\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 * The item session's state in QTI's lifecycle (`ITEM_LIFECYCLE_STATES`, roadmap 1 T3d). What the\n * session may do (`itemRules`: the can* answers) is derived from it, never stored.\n */\n lifecycle: ItemLifecycleState;\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, as a version 1 `QtiTestState` stores it (`plans/item-lifecycle.md`).\n */\n numAttempts: number;\n /** QTI's built-in `completionStatus`: `not_attempted` until the item is shown. */\n completionStatus: CompletionStatus;\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 const session = item as Record<string, unknown>;\n if (!(ITEM_LIFECYCLE_STATES as readonly unknown[]).includes(session.lifecycle)) {\n fail(`items[\"${id}\"].lifecycle`, `one of ${ITEM_LIFECYCLE_STATES.join(', ')}`);\n }\n if (!Number.isInteger(session.numAttempts) || (session.numAttempts as number) < 0) {\n fail(`items[\"${id}\"].numAttempts`, 'a whole number, 0 or more');\n }\n if (!(COMPLETION_STATUSES as readonly unknown[]).includes(session.completionStatus)) {\n fail(`items[\"${id}\"].completionStatus`, `one of ${COMPLETION_STATUSES.join(', ')}`);\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.8",
4
+ "version": "0.1.0-next.9",
5
5
  "author": "",
6
6
  "keywords": [
7
7
  "qti",