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

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.
@@ -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,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,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I,CAAC;QACzB,CAAC,WAAW,CAAC,MAAM,CAAC,SAAS,CAAC;QAC9B,CAAC,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,EAC7B,CAAC;QACD,IAAI,CAAC,QAAQ,EAAE,+FAA+F,CAAC,CAAC;IAClH,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 */\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\n/**\n * Time measured, never counted (roadmap 1 T5): the time of the stretches already ended, plus the\n * moment the running stretch began. A timer only checks limits and repaints; it never adds time, so\n * a throttled tab or a reload neither loses nor doubles any. Milliseconds, from the injected clock.\n */\nexport interface Stopwatch {\n /** The time of every stretch already ended, in milliseconds. */\n spent: number;\n /** When the running stretch began (milliseconds since the epoch), or null when stopped. */\n since: number | null;\n}\n\n/** A stopwatch that has not run. */\nexport const stoppedStopwatch = (): Stopwatch => ({ spent: 0, since: null });\n\n/**\n * The time spent in the test and in each test part and section (QTI 3 info model §2.8.5): all the\n * time the candidate session was in it, navigation included. A part or section has an entry from\n * the first time it is entered.\n */\nexport interface DeliveryTiming {\n test: Stopwatch;\n testParts: Record<string, Stopwatch>;\n sections: Record<string, Stopwatch>;\n}\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/delivery-state.md`).\n */\n numAttempts: number;\n /** QTI's built-in `completionStatus`: `not_attempted` until the item is shown. */\n completionStatus: CompletionStatus;\n /**\n * QTI's built-in `duration` (info model §2.2.2.2): the time the session was `interacting`, over\n * all attempts and candidate sessions (decided 2026-12-01, roadmap 1 T5).\n */\n duration: Stopwatch;\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 /** The time spent in the test, its parts and its sections (roadmap 1 T5). */\n timing: DeliveryTiming;\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\nconst isTime = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value) && value >= 0;\n\nconst isStopwatch = (value: unknown): value is Stopwatch =>\n isRecord(value) && isTime(value.spent) && (value.since === null || isTime(value.since));\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, timing, 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 if (!isStopwatch(session.duration)) fail(`items[\"${id}\"].duration`, 'a stopwatch');\n }\n const stopwatches = (record: unknown) => isRecord(record) && Object.values(record).every(isStopwatch);\n if (\n !isRecord(timing) ||\n !isStopwatch(timing.test) ||\n !stopwatches(timing.testParts) ||\n !stopwatches(timing.sections)\n ) {\n fail('timing', 'an object with a stopwatch for the test and records of stopwatches for testParts and sections');\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"]}
@@ -0,0 +1,50 @@
1
+ import type { TestModel } from '../model/test-model.js';
2
+ import type { DeliveryState } from '../state/delivery-state.js';
3
+ /**
4
+ * Where one level stands against its `qti-time-limits` (QTI 3 info model §7.40, §2.8.5), in seconds.
5
+ * Derived from the model and the state at a moment, never stored.
6
+ *
7
+ * Experimental (0.x): roadmap 1 T5.
8
+ */
9
+ export interface TimeLimitState {
10
+ /**
11
+ * The minimum time, or null when none is declared or it does not apply: on a section or an
12
+ * item-ref only in a linear part (§2.8.5). Reported only; nothing is refused (decided 2026-12-01).
13
+ */
14
+ minTime: number | null;
15
+ maxTime: number | null;
16
+ allowLateSubmission: boolean;
17
+ /** The time spent so far: an item's `duration`, or all the time spent in a section, part or test. */
18
+ spent: number;
19
+ /** Seconds left before `maxTime`, never below 0; null without `maxTime`. */
20
+ remaining: number | null;
21
+ /** `spent` has reached `maxTime`. */
22
+ expired: boolean;
23
+ /**
24
+ * Expired without `allow-late-submission`: what it limits ends (the item session, or every item
25
+ * session in the section, part or test).
26
+ */
27
+ timeUp: boolean;
28
+ /**
29
+ * Expired with `allow-late-submission`: nothing ends, and a response given now is late (decided
30
+ * 2026-12-01); the host decides what that means.
31
+ */
32
+ late: boolean;
33
+ /** `spent` has reached `minTime`, or there is no `minTime` that applies. */
34
+ minTimeMet: boolean;
35
+ }
36
+ /** The answers for each level that declares `qti-time-limits`; a level without one has no entry. */
37
+ export interface TimeLimitAnswers {
38
+ test?: TimeLimitState;
39
+ /** By test part identifier. */
40
+ testParts: Record<string, TimeLimitState>;
41
+ /** By section identifier. */
42
+ sections: Record<string, TimeLimitState>;
43
+ /** By item instance: each instance of an item-ref selected more than once has a limit of its own. */
44
+ items: Record<string, TimeLimitState>;
45
+ }
46
+ /**
47
+ * Each declared time limit against the time spent at `now`. `min-time` applies always on the test
48
+ * and a part, and on a section or item-ref only in a linear part (info model §2.8.5). Pure.
49
+ */
50
+ export declare function timeLimitRules(model: TestModel, state: DeliveryState, now: number): TimeLimitAnswers;
@@ -0,0 +1,51 @@
1
+ import { itemRefsInOrder, sectionsInOrder } from '../model/test-model.js';
2
+ import { elapsed } from './timing.js';
3
+ const NOT_RUN = { spent: 0, since: null };
4
+ function answer(limits, stopwatch, now, minTimeApplies) {
5
+ const spentMs = elapsed(stopwatch, now);
6
+ const minTime = minTimeApplies ? limits.minTime : null;
7
+ const { maxTime, allowLateSubmission } = limits;
8
+ const expired = maxTime !== null && spentMs >= maxTime * 1000;
9
+ return {
10
+ minTime,
11
+ maxTime,
12
+ allowLateSubmission,
13
+ spent: spentMs / 1000,
14
+ remaining: maxTime === null ? null : Math.max(0, maxTime * 1000 - spentMs) / 1000,
15
+ expired,
16
+ timeUp: expired && !allowLateSubmission,
17
+ late: expired && allowLateSubmission,
18
+ minTimeMet: minTime === null || spentMs >= minTime * 1000
19
+ };
20
+ }
21
+ /**
22
+ * Each declared time limit against the time spent at `now`. `min-time` applies always on the test
23
+ * and a part, and on a section or item-ref only in a linear part (info model §2.8.5). Pure.
24
+ */
25
+ export function timeLimitRules(model, state, now) {
26
+ const answers = { testParts: {}, sections: {}, items: {} };
27
+ if (model.timeLimits)
28
+ answers.test = answer(model.timeLimits, state.timing.test, now, true);
29
+ for (const testPart of model.testParts) {
30
+ const linear = testPart.navigationMode === 'linear';
31
+ if (testPart.timeLimits) {
32
+ const stopwatch = state.timing.testParts[testPart.identifier] ?? NOT_RUN;
33
+ answers.testParts[testPart.identifier] = answer(testPart.timeLimits, stopwatch, now, true);
34
+ }
35
+ for (const section of sectionsInOrder(testPart.sections)) {
36
+ if (!section.timeLimits)
37
+ continue;
38
+ const stopwatch = state.timing.sections[section.identifier] ?? NOT_RUN;
39
+ answers.sections[section.identifier] = answer(section.timeLimits, stopwatch, now, linear);
40
+ }
41
+ const limitedItemRefs = new Map(itemRefsInOrder(testPart.sections).flatMap(itemRef => itemRef.timeLimits ? [[itemRef.identifier, itemRef.timeLimits]] : []));
42
+ for (const id of state.order) {
43
+ const item = state.items[id];
44
+ const limits = item && limitedItemRefs.get(item.itemRefIdentifier);
45
+ if (limits)
46
+ answers.items[id] = answer(limits, item.duration, now, linear);
47
+ }
48
+ }
49
+ return answers;
50
+ }
51
+ //# sourceMappingURL=time-limits.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time-limits.js","sourceRoot":"","sources":["../../src/timing/time-limits.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAkDtC,MAAM,OAAO,GAAc,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAErD,SAAS,MAAM,CAAC,MAAkB,EAAE,SAAoB,EAAE,GAAW,EAAE,cAAuB;IAC5F,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IACxC,MAAM,OAAO,GAAG,cAAc,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;IACvD,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,GAAG,MAAM,CAAC;IAChD,MAAM,OAAO,GAAG,OAAO,KAAK,IAAI,IAAI,OAAO,IAAI,OAAO,GAAG,IAAI,CAAC;IAC9D,OAAO;QACL,OAAO;QACP,OAAO;QACP,mBAAmB;QACnB,KAAK,EAAE,OAAO,GAAG,IAAI;QACrB,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,OAAO,CAAC,GAAG,IAAI;QACjF,OAAO;QAC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,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;QAC5F,CAAC;QACD,MAAM,eAAe,GAAG,IAAI,GAAG,CAC7B,eAAe,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CACnD,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,UAAU,CAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAC9E,CACF,CAAC;QACF,KAAK,MAAM,EAAE,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;YAC7B,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YAC7B,MAAM,MAAM,GAAG,IAAI,IAAI,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;YACnE,IAAI,MAAM;gBAAE,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;QAC7E,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { itemRefsInOrder, sectionsInOrder } from '../model/test-model.js';\nimport { elapsed } from './timing.js';\n\nimport type { TestModel, TimeLimits } from '../model/test-model.js';\nimport type { DeliveryState, Stopwatch } from '../state/delivery-state.js';\n\n/**\n * Where one level stands against its `qti-time-limits` (QTI 3 info model §7.40, §2.8.5), in seconds.\n * Derived from the model and the state at a moment, never stored.\n *\n * Experimental (0.x): roadmap 1 T5.\n */\nexport interface TimeLimitState {\n /**\n * The minimum time, or null when none is declared or it does not apply: on a section or an\n * item-ref only in a linear part (§2.8.5). Reported only; nothing is refused (decided 2026-12-01).\n */\n minTime: number | null;\n maxTime: number | null;\n allowLateSubmission: boolean;\n /** The time spent so far: an item's `duration`, or all the time spent in a section, part or test. */\n spent: number;\n /** Seconds left before `maxTime`, never below 0; null without `maxTime`. */\n remaining: number | null;\n /** `spent` has reached `maxTime`. */\n expired: boolean;\n /**\n * Expired without `allow-late-submission`: what it limits ends (the item session, or every item\n * session in the section, part or test).\n */\n timeUp: boolean;\n /**\n * Expired with `allow-late-submission`: nothing ends, and a response given now is late (decided\n * 2026-12-01); the host decides what that means.\n */\n late: boolean;\n /** `spent` has reached `minTime`, or there is no `minTime` that applies. */\n minTimeMet: boolean;\n}\n\n/** The answers for each level that declares `qti-time-limits`; a level without one has no entry. */\nexport interface TimeLimitAnswers {\n test?: TimeLimitState;\n /** By test part identifier. */\n testParts: Record<string, TimeLimitState>;\n /** By section identifier. */\n sections: Record<string, TimeLimitState>;\n /** By item instance: each instance of an item-ref selected more than once has a limit of its own. */\n items: Record<string, TimeLimitState>;\n}\n\nconst NOT_RUN: Stopwatch = { spent: 0, since: null };\n\nfunction answer(limits: TimeLimits, stopwatch: Stopwatch, now: number, minTimeApplies: boolean): TimeLimitState {\n const spentMs = elapsed(stopwatch, now);\n const minTime = minTimeApplies ? limits.minTime : null;\n const { maxTime, allowLateSubmission } = limits;\n const expired = maxTime !== null && spentMs >= maxTime * 1000;\n return {\n minTime,\n maxTime,\n allowLateSubmission,\n spent: spentMs / 1000,\n remaining: maxTime === null ? null : Math.max(0, maxTime * 1000 - spentMs) / 1000,\n expired,\n timeUp: expired && !allowLateSubmission,\n late: expired && allowLateSubmission,\n minTimeMet: minTime === null || spentMs >= minTime * 1000\n };\n}\n\n/**\n * Each declared time limit against the time spent at `now`. `min-time` applies always on the test\n * and a part, and on a section or item-ref only in a linear part (info model §2.8.5). Pure.\n */\nexport function timeLimitRules(model: TestModel, state: DeliveryState, now: number): TimeLimitAnswers {\n const answers: TimeLimitAnswers = { testParts: {}, sections: {}, items: {} };\n if (model.timeLimits) answers.test = answer(model.timeLimits, state.timing.test, now, true);\n for (const testPart of model.testParts) {\n const linear = testPart.navigationMode === 'linear';\n if (testPart.timeLimits) {\n const stopwatch = state.timing.testParts[testPart.identifier] ?? NOT_RUN;\n answers.testParts[testPart.identifier] = answer(testPart.timeLimits, stopwatch, now, true);\n }\n for (const section of sectionsInOrder(testPart.sections)) {\n if (!section.timeLimits) continue;\n const stopwatch = state.timing.sections[section.identifier] ?? NOT_RUN;\n answers.sections[section.identifier] = answer(section.timeLimits, stopwatch, now, linear);\n }\n const limitedItemRefs = new Map(\n itemRefsInOrder(testPart.sections).flatMap(itemRef =>\n itemRef.timeLimits ? [[itemRef.identifier, itemRef.timeLimits] as const] : []\n )\n );\n for (const id of state.order) {\n const item = state.items[id];\n const limits = item && limitedItemRefs.get(item.itemRefIdentifier);\n if (limits) answers.items[id] = answer(limits, item.duration, now, linear);\n }\n }\n return answers;\n}\n"]}
@@ -0,0 +1,48 @@
1
+ import type { SectionModel, TestModel, TestPartModel } from '../model/test-model.js';
2
+ import type { DeliveryState, Stopwatch } from '../state/delivery-state.js';
3
+ /**
4
+ * The time spent in the test, its parts, its sections and each item session (roadmap 1 T5; QTI 3
5
+ * info model §2.8.5 and §2.2.2.2), measured, never counted: each level keeps a `Stopwatch`, and a
6
+ * stretch is added from the injected clock when it ends. A timer only checks the limits and repaints
7
+ * the time left, so a throttled tab or a reload neither loses nor doubles time.
8
+ *
9
+ * Experimental (0.x): see `plans/delivery-state.md`.
10
+ */
11
+ /** The time on a stopwatch at `now`, in milliseconds; a clock that went back adds nothing. */
12
+ export declare const elapsed: ({ spent, since }: Stopwatch, now: number) => number;
13
+ /** Where an item-ref sits: its test part and its sections, the outermost first. */
14
+ export interface ItemRefPlace {
15
+ testPart: TestPartModel;
16
+ sections: SectionModel[];
17
+ }
18
+ /** The part and the sections around an item-ref, or null when the model has no such item-ref. */
19
+ export declare function placeOfItemRef(model: TestModel, itemRefIdentifier: string): ItemRefPlace | null;
20
+ /** What a delivery knows at an update, besides its state. */
21
+ export interface FollowTimingInput {
22
+ /** Milliseconds since the epoch, from the injected clock. */
23
+ now: number;
24
+ /**
25
+ * The candidate session runs: the test is open in front of the candidate. False once it ends
26
+ * (the window closed, the test suspended); test, part and section time pause then, as an item's
27
+ * `duration` does (decided 2026-12-01).
28
+ */
29
+ running: boolean;
30
+ }
31
+ /**
32
+ * The state with each stopwatch running exactly when it should, at `now`: the test, the current
33
+ * item's test part and every section around it while the candidate session runs and an item is
34
+ * current; each item's `duration` while its session is `interacting` (decided 2026-12-01: not in
35
+ * `modalFeedback`). A stopwatch that should stop gets its stretch added; one that should run starts
36
+ * at `now`; the rest are untouched, so following twice changes nothing. Pure.
37
+ */
38
+ export declare function followTiming(state: DeliveryState, model: TestModel, { now, running }: FollowTimingInput): DeliveryState;
39
+ /**
40
+ * The state to store at `now`: every running stretch added to its time and started again at `now`.
41
+ * Read back with `resumeTiming`, a state stored this way loses at most the time since it was stored.
42
+ */
43
+ export declare function checkpointTiming(state: DeliveryState, now: number): DeliveryState;
44
+ /**
45
+ * A stored state, read back: each running stretch ends where it was stored, so the time the test was
46
+ * closed never counts (decided 2026-12-01). `followTiming` starts what runs now.
47
+ */
48
+ export declare function resumeTiming(state: DeliveryState): DeliveryState;
@@ -0,0 +1,100 @@
1
+ /**
2
+ * The time spent in the test, its parts, its sections and each item session (roadmap 1 T5; QTI 3
3
+ * info model §2.8.5 and §2.2.2.2), measured, never counted: each level keeps a `Stopwatch`, and a
4
+ * stretch is added from the injected clock when it ends. A timer only checks the limits and repaints
5
+ * the time left, so a throttled tab or a reload neither loses nor doubles time.
6
+ *
7
+ * Experimental (0.x): see `plans/delivery-state.md`.
8
+ */
9
+ /** The time on a stopwatch at `now`, in milliseconds; a clock that went back adds nothing. */
10
+ export const elapsed = ({ spent, since }, now) => since === null ? spent : spent + Math.max(0, now - since);
11
+ const start = (stopwatch, now) => stopwatch.since === null ? { spent: stopwatch.spent, since: now } : stopwatch;
12
+ const stop = (stopwatch, now) => stopwatch.since === null ? stopwatch : { spent: elapsed(stopwatch, now), since: null };
13
+ const NOT_RUN = Object.freeze({ spent: 0, since: null });
14
+ /** The part and the sections around an item-ref, or null when the model has no such item-ref. */
15
+ export function placeOfItemRef(model, itemRefIdentifier) {
16
+ const find = (content, around) => {
17
+ for (const child of content) {
18
+ if (child.kind === 'itemRef') {
19
+ if (child.identifier === itemRefIdentifier)
20
+ return around;
21
+ }
22
+ else {
23
+ const found = find(child.content, [...around, child]);
24
+ if (found)
25
+ return found;
26
+ }
27
+ }
28
+ return null;
29
+ };
30
+ for (const testPart of model.testParts) {
31
+ const sections = find(testPart.sections, []);
32
+ if (sections)
33
+ return { testPart, sections };
34
+ }
35
+ return null;
36
+ }
37
+ /**
38
+ * The state with each stopwatch running exactly when it should, at `now`: the test, the current
39
+ * item's test part and every section around it while the candidate session runs and an item is
40
+ * current; each item's `duration` while its session is `interacting` (decided 2026-12-01: not in
41
+ * `modalFeedback`). A stopwatch that should stop gets its stretch added; one that should run starts
42
+ * at `now`; the rest are untouched, so following twice changes nothing. Pure.
43
+ */
44
+ export function followTiming(state, model, { now, running }) {
45
+ const current = state.current === null ? undefined : state.items[state.current];
46
+ const place = running && current ? placeOfItemRef(model, current.itemRefIdentifier) : null;
47
+ const inTest = running && current !== undefined;
48
+ const runningParts = new Set(place ? [place.testPart.identifier] : []);
49
+ const runningSections = new Set(place?.sections.map(section => section.identifier) ?? []);
50
+ const follow = (stopwatch, run) => (run ? start(stopwatch, now) : stop(stopwatch, now));
51
+ const followAll = (record, run) => {
52
+ const followed = {};
53
+ for (const [identifier, stopwatch] of Object.entries(record))
54
+ followed[identifier] = follow(stopwatch, run.has(identifier));
55
+ for (const identifier of run)
56
+ followed[identifier] ??= start(NOT_RUN, now);
57
+ return followed;
58
+ };
59
+ const timing = {
60
+ test: follow(state.timing.test, inTest),
61
+ testParts: followAll(state.timing.testParts, runningParts),
62
+ sections: followAll(state.timing.sections, runningSections)
63
+ };
64
+ const items = mapItems(state, item => ({
65
+ ...item,
66
+ duration: follow(item.duration, running && item.lifecycle === 'interacting')
67
+ }));
68
+ return { ...state, timing, items };
69
+ }
70
+ /**
71
+ * The state to store at `now`: every running stretch added to its time and started again at `now`.
72
+ * Read back with `resumeTiming`, a state stored this way loses at most the time since it was stored.
73
+ */
74
+ export function checkpointTiming(state, now) {
75
+ const checkpoint = (stopwatch) => stopwatch.since === null ? stopwatch : { spent: elapsed(stopwatch, now), since: now };
76
+ return mapStopwatches(state, checkpoint);
77
+ }
78
+ /**
79
+ * A stored state, read back: each running stretch ends where it was stored, so the time the test was
80
+ * closed never counts (decided 2026-12-01). `followTiming` starts what runs now.
81
+ */
82
+ export function resumeTiming(state) {
83
+ return mapStopwatches(state, ({ spent }) => ({ spent, since: null }));
84
+ }
85
+ function mapStopwatches(state, map) {
86
+ const mapRecord = (record) => Object.fromEntries(Object.entries(record).map(([identifier, stopwatch]) => [identifier, map(stopwatch)]));
87
+ return {
88
+ ...state,
89
+ timing: {
90
+ test: map(state.timing.test),
91
+ testParts: mapRecord(state.timing.testParts),
92
+ sections: mapRecord(state.timing.sections)
93
+ },
94
+ items: mapItems(state, item => ({ ...item, duration: map(item.duration) }))
95
+ };
96
+ }
97
+ function mapItems(state, map) {
98
+ return Object.fromEntries(Object.entries(state.items).map(([id, item]) => [id, map(item)]));
99
+ }
100
+ //# sourceMappingURL=timing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timing.js","sourceRoot":"","sources":["../../src/timing/timing.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AAEH,8FAA8F;AAC9F,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,EAAE,KAAK,EAAE,KAAK,EAAa,EAAE,GAAW,EAAU,EAAE,CAC1E,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,GAAG,KAAK,CAAC,CAAC;AAE5D,MAAM,KAAK,GAAG,CAAC,SAAoB,EAAE,GAAW,EAAa,EAAE,CAC7D,SAAS,CAAC,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';\nimport type { DeliveryState, DeliveryTiming, ItemInstanceId, Stopwatch } from '../state/delivery-state.js';\n\n/**\n * The time spent in the test, its parts, its sections and each item session (roadmap 1 T5; QTI 3\n * info model §2.8.5 and §2.2.2.2), measured, never counted: each level keeps a `Stopwatch`, and a\n * stretch is added from the injected clock when it ends. A timer only checks the limits and repaints\n * the time left, so a throttled tab or a reload neither loses nor doubles time.\n *\n * Experimental (0.x): see `plans/delivery-state.md`.\n */\n\n/** The time on a stopwatch at `now`, in milliseconds; a clock that went back adds nothing. */\nexport const elapsed = ({ spent, since }: Stopwatch, now: number): number =>\n since === null ? spent : spent + Math.max(0, now - since);\n\nconst start = (stopwatch: Stopwatch, now: number): Stopwatch =>\n stopwatch.since === null ? { spent: stopwatch.spent, since: now } : stopwatch;\n\nconst stop = (stopwatch: Stopwatch, now: number): Stopwatch =>\n stopwatch.since === null ? stopwatch : { spent: elapsed(stopwatch, now), since: null };\n\nconst NOT_RUN: Stopwatch = Object.freeze({ spent: 0, since: null });\n\n/** Where an item-ref sits: its test part and its sections, the outermost first. */\nexport interface ItemRefPlace {\n testPart: TestPartModel;\n sections: SectionModel[];\n}\n\n/** The part and the sections around an item-ref, or null when the model has no such item-ref. */\nexport function placeOfItemRef(model: TestModel, itemRefIdentifier: string): ItemRefPlace | null {\n const find = (content: SectionContent[], around: SectionModel[]): SectionModel[] | null => {\n for (const child of content) {\n if (child.kind === 'itemRef') {\n if (child.identifier === itemRefIdentifier) return around;\n } else {\n const found = find(child.content, [...around, child]);\n if (found) return found;\n }\n }\n return null;\n };\n for (const testPart of model.testParts) {\n const sections = find(testPart.sections, []);\n if (sections) return { testPart, sections };\n }\n return null;\n}\n\n/** What a delivery knows at an update, besides its state. */\nexport interface FollowTimingInput {\n /** Milliseconds since the epoch, from the injected clock. */\n now: number;\n /**\n * The candidate session runs: the test is open in front of the candidate. False once it ends\n * (the window closed, the test suspended); test, part and section time pause then, as an item's\n * `duration` does (decided 2026-12-01).\n */\n running: boolean;\n}\n\n/**\n * The state with each stopwatch running exactly when it should, at `now`: the test, the current\n * item's test part and every section around it while the candidate session runs and an item is\n * current; each item's `duration` while its session is `interacting` (decided 2026-12-01: not in\n * `modalFeedback`). A stopwatch that should stop gets its stretch added; one that should run starts\n * at `now`; the rest are untouched, so following twice changes nothing. Pure.\n */\nexport function followTiming(\n state: DeliveryState,\n model: TestModel,\n { now, running }: FollowTimingInput\n): DeliveryState {\n const current = state.current === null ? undefined : state.items[state.current];\n const place = running && current ? placeOfItemRef(model, current.itemRefIdentifier) : null;\n const inTest = running && current !== undefined;\n const runningParts = new Set(place ? [place.testPart.identifier] : []);\n const runningSections = new Set(place?.sections.map(section => section.identifier) ?? []);\n\n const follow = (stopwatch: Stopwatch, run: boolean) => (run ? start(stopwatch, now) : stop(stopwatch, now));\n const followAll = (record: Record<string, Stopwatch>, run: ReadonlySet<string>) => {\n const followed: Record<string, Stopwatch> = {};\n for (const [identifier, stopwatch] of Object.entries(record))\n followed[identifier] = follow(stopwatch, run.has(identifier));\n for (const identifier of run) followed[identifier] ??= start(NOT_RUN, now);\n return followed;\n };\n\n const timing: DeliveryTiming = {\n test: follow(state.timing.test, inTest),\n testParts: followAll(state.timing.testParts, runningParts),\n sections: followAll(state.timing.sections, runningSections)\n };\n const items = mapItems(state, item => ({\n ...item,\n duration: follow(item.duration, running && item.lifecycle === 'interacting')\n }));\n return { ...state, timing, items };\n}\n\n/**\n * The state to store at `now`: every running stretch added to its time and started again at `now`.\n * Read back with `resumeTiming`, a state stored this way loses at most the time since it was stored.\n */\nexport function checkpointTiming(state: DeliveryState, now: number): DeliveryState {\n const checkpoint = (stopwatch: Stopwatch) =>\n stopwatch.since === null ? stopwatch : { spent: elapsed(stopwatch, now), since: now };\n return mapStopwatches(state, checkpoint);\n}\n\n/**\n * A stored state, read back: each running stretch ends where it was stored, so the time the test was\n * closed never counts (decided 2026-12-01). `followTiming` starts what runs now.\n */\nexport function resumeTiming(state: DeliveryState): DeliveryState {\n return mapStopwatches(state, ({ spent }) => ({ spent, since: null }));\n}\n\nfunction mapStopwatches(state: DeliveryState, map: (stopwatch: Stopwatch) => Stopwatch): DeliveryState {\n const mapRecord = (record: Record<string, Stopwatch>) =>\n Object.fromEntries(Object.entries(record).map(([identifier, stopwatch]) => [identifier, map(stopwatch)]));\n return {\n ...state,\n timing: {\n test: map(state.timing.test),\n testParts: mapRecord(state.timing.testParts),\n sections: mapRecord(state.timing.sections)\n },\n items: mapItems(state, item => ({ ...item, duration: map(item.duration) }))\n };\n}\n\nfunction mapItems(\n state: DeliveryState,\n map: (item: DeliveryState['items'][ItemInstanceId]) => DeliveryState['items'][ItemInstanceId]\n): DeliveryState['items'] {\n return Object.fromEntries(Object.entries(state.items).map(([id, item]) => [id, map(item)]));\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.10",
5
5
  "author": "",
6
6
  "keywords": [
7
7
  "qti",