@qti-components/test 2.0.0-next.21 → 2.0.0-next.22
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.
|
@@ -63,7 +63,9 @@ export class TestContainer extends LitElement {
|
|
|
63
63
|
return;
|
|
64
64
|
try {
|
|
65
65
|
const explicitSeed = this.qtiContext?.QTI_CONTEXT?.seed;
|
|
66
|
-
let api = (await qtiTransformTest().load(this.testURL))
|
|
66
|
+
let api = (await qtiTransformTest().load(this.testURL))
|
|
67
|
+
.applySelection(explicitSeed)
|
|
68
|
+
.shuffleOrdering(explicitSeed);
|
|
67
69
|
// Apply external transformation if provided
|
|
68
70
|
const qtiTest = this.testHost;
|
|
69
71
|
if (qtiTest?.postLoadTestTransformCallback) {
|
|
@@ -105,6 +107,7 @@ export class TestContainer extends LitElement {
|
|
|
105
107
|
const explicitSeed = this.qtiContext?.QTI_CONTEXT?.seed;
|
|
106
108
|
this.#setTestDoc(qtiTransformTest()
|
|
107
109
|
.parse(this.testXML)
|
|
110
|
+
.applySelection(explicitSeed)
|
|
108
111
|
.shuffleOrdering(explicitSeed)
|
|
109
112
|
.htmlDoc(this.#resolvedCustomElementRegistry ?? undefined));
|
|
110
113
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-container.js","sourceRoot":"","sources":["../../../src/components/test-container/test-container.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAEhD,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAClD,OAAO,EAAE,gBAAgB,EA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AAK,EAAE,CAAC;4DAC4B;AAI3D;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;8CACpB;AAI9B;IADC,KAAK,EAAE;8CACgC;AAIxC;IADC,KAAK,EAAE;8CACsB;AAIpB;IADT,OAAO,CAAC,EAAE,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;+CACzB;AAIpB;IAFT,KAAK,EAAE;IACP,OAAO,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;iDAGhD;AAwBc;IADf,KAAK,CAAC,SAAS,EAAE,EAAE,oBAAoB,EAAE,IAAI,EAAE,CAAC;wDAuBhD;AAuBS;IADT,KAAK,CAAC,SAAS,EAAE,EAAE,oBAAoB,EAAE,IAAI,EAAE,CAAC;wDAchD","sourcesContent":["import { consume } from '@lit/context';\nimport { LitElement, html } from 'lit';\nimport { property, state } from 'lit/decorators.js';\nimport { until } from 'lit/directives/until.js';\n\nimport { qtiContext } from '@qti-components/base';\nimport { watch } from '@qti-components/utilities';\nimport { qtiTransformTest } from '@qti-components/transformers';\nimport itemCss from '@qti-components/theme/item-css';\n\nimport { testHostContext } from '../../internal/test-host.context';\n\nimport type { QtiContext } from '@qti-components/base';\nimport type { TestHost } from '../../internal/test-host.context';\n\n/**\n * `<test-container>` is a custom element designed for hosting the qti-assessment-item.\n * The `qti-assessment-test` will be placed inside the shadow DOM of this element.\n * The element loads the test from the provided URL and renders it inside the shadow DOM.\n *\n * ```html\n * <qti-test>\n * <test-navigation>\n * <test-container class=\"m-4 bg-white\" test-url=\"./path/to/assessmenttest.xml\"></test-container>\n * </test-navigation>\n * </qti-test>\n * ```\n */\n\nexport class TestContainer extends LitElement {\n /** Scoped registry used by the test shadow root. Must be set before connection. */\n @property({ attribute: false })\n customElementRegistry: CustomElementRegistry | null = null;\n\n /** URL of the item to load */\n @property({ type: String, attribute: 'test-url' })\n testURL: string | null = null;\n\n /** A parsed HTML document */\n @state()\n testDoc: DocumentFragment | null = null;\n\n /** The raw XML string */\n @state()\n testXML: string | null = null;\n\n /** The `qti-test` this container is in: where the host's `postLoadTestTransformCallback` lives. */\n @consume({ context: testHostContext, subscribe: true })\n protected testHost?: TestHost;\n\n @state()\n @consume({ context: qtiContext, subscribe: true })\n protected qtiContext: QtiContext = {\n QTI_CONTEXT: { testIdentifier: '', candidateIdentifier: '', environmentIdentifier: 'default' }\n };\n\n /** Template content if provided */\n #templateContent: unknown = null;\n #resolvedCustomElementRegistry: CustomElementRegistry | null = null;\n\n protected override createRenderRoot(): HTMLElement | DocumentFragment {\n const registry = this.customElementRegistry;\n\n this.#resolvedCustomElementRegistry = registry;\n\n return (\n this.shadowRoot ??\n this.attachShadow({\n ...(this.constructor as typeof TestContainer).shadowRootOptions,\n ...(registry ? { customElementRegistry: registry } : {})\n })\n );\n }\n\n /** Callback function to transform the test after loading */\n // @property({ type: Function }) postLoadTestTransformCallback: PostLoadTestTransformCallback | null = null;\n\n @watch('testURL', { waitUntilFirstUpdate: true })\n protected async handleTestURLChange() {\n if (!this.testURL) return;\n try {\n const explicitSeed = this.qtiContext?.QTI_CONTEXT?.seed;\n let api = (await qtiTransformTest().load(this.testURL)).shuffleOrdering(explicitSeed);\n // Apply external transformation if provided\n const qtiTest = this.testHost;\n if (qtiTest?.postLoadTestTransformCallback) {\n // Create a temporary document to get the test element reference\n const tempDoc = api.htmlDoc(this.#resolvedCustomElementRegistry ?? undefined);\n const testElement = tempDoc.querySelector('qti-assessment-test') as any;\n\n if (testElement) {\n // Apply the callback with the test element\n api = await qtiTest.postLoadTestTransformCallback(api, testElement);\n }\n }\n\n this.#setTestDoc(api.htmlDoc(this.#resolvedCustomElementRegistry ?? undefined));\n } catch (error) {\n console.error('Error loading or parsing XML:', error);\n }\n }\n\n /**\n * Assign a new test document and announce it.\n *\n * The announcement has to happen here rather than on\n * `qc-test-loaded`, because by then the new document's\n * children have already connected and registered themselves — a host that\n * resets its context on that event throws those registrations away. Lit's\n * re-render is async, so a handler running now still gets in before any child\n * of the new `qti-assessment-test` connects.\n */\n #setTestDoc(testDoc: DocumentFragment): void {\n this.testDoc = testDoc;\n this.dispatchEvent(\n new CustomEvent('qti-testdoc-loaded', {\n bubbles: true,\n composed: true\n })\n );\n }\n\n @watch('testXML', { waitUntilFirstUpdate: true })\n protected handleTestXMLChange() {\n if (!this.testXML) return;\n try {\n const explicitSeed = this.qtiContext?.QTI_CONTEXT?.seed;\n this.#setTestDoc(\n qtiTransformTest()\n .parse(this.testXML)\n .shuffleOrdering(explicitSeed)\n .htmlDoc(this.#resolvedCustomElementRegistry ?? undefined)\n );\n } catch (error) {\n console.error('Error parsing XML:', error);\n }\n }\n\n override async connectedCallback(): Promise<void> {\n super.connectedCallback();\n this.#initializeTemplateContent();\n this.#applyStyles();\n if (this.testURL) {\n this.handleTestURLChange();\n }\n if (this.testXML) {\n this.handleTestXMLChange();\n }\n }\n\n #initializeTemplateContent() {\n const template = this.querySelector('template') as HTMLTemplateElement;\n this.#templateContent = template ? template.content : html``;\n }\n\n #applyStyles() {\n const sheet = new CSSStyleSheet();\n sheet.replaceSync(itemCss);\n this.shadowRoot.adoptedStyleSheets = [sheet];\n }\n\n override render() {\n return html`\n ${this.#templateContent}\n <slot></slot>\n ${until(this.testDoc, html`<span>Loading...</span>`)}\n `;\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'test-container': TestContainer;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"test-container.js","sourceRoot":"","sources":["../../../src/components/test-container/test-container.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAA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,MAAM;QACb,OAAO,IAAI,CAAA;QACP,IAAI,CAAC,gBAAgB;;QAErB,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAA,yBAAyB,CAAC;KACrD,CAAC;IACJ,CAAC;CACF;AA3IC;IADC,QAAQ,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;4DAC4B;AAI3D;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;8CACpB;AAI9B;IADC,KAAK,EAAE;8CACgC;AAIxC;IADC,KAAK,EAAE;8CACsB;AAIpB;IADT,OAAO,CAAC,EAAE,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;+CACzB;AAIpB;IAFT,KAAK,EAAE;IACP,OAAO,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;iDAGhD;AAwBc;IADf,KAAK,CAAC,SAAS,EAAE,EAAE,oBAAoB,EAAE,IAAI,EAAE,CAAC;wDAyBhD;AAuBS;IADT,KAAK,CAAC,SAAS,EAAE,EAAE,oBAAoB,EAAE,IAAI,EAAE,CAAC;wDAehD","sourcesContent":["import { consume } from '@lit/context';\nimport { LitElement, html } from 'lit';\nimport { property, state } from 'lit/decorators.js';\nimport { until } from 'lit/directives/until.js';\n\nimport { qtiContext } from '@qti-components/base';\nimport { watch } from '@qti-components/utilities';\nimport { qtiTransformTest } from '@qti-components/transformers';\nimport itemCss from '@qti-components/theme/item-css';\n\nimport { testHostContext } from '../../internal/test-host.context';\n\nimport type { QtiContext } from '@qti-components/base';\nimport type { TestHost } from '../../internal/test-host.context';\n\n/**\n * `<test-container>` is a custom element designed for hosting the qti-assessment-item.\n * The `qti-assessment-test` will be placed inside the shadow DOM of this element.\n * The element loads the test from the provided URL and renders it inside the shadow DOM.\n *\n * ```html\n * <qti-test>\n * <test-navigation>\n * <test-container class=\"m-4 bg-white\" test-url=\"./path/to/assessmenttest.xml\"></test-container>\n * </test-navigation>\n * </qti-test>\n * ```\n */\n\nexport class TestContainer extends LitElement {\n /** Scoped registry used by the test shadow root. Must be set before connection. */\n @property({ attribute: false })\n customElementRegistry: CustomElementRegistry | null = null;\n\n /** URL of the item to load */\n @property({ type: String, attribute: 'test-url' })\n testURL: string | null = null;\n\n /** A parsed HTML document */\n @state()\n testDoc: DocumentFragment | null = null;\n\n /** The raw XML string */\n @state()\n testXML: string | null = null;\n\n /** The `qti-test` this container is in: where the host's `postLoadTestTransformCallback` lives. */\n @consume({ context: testHostContext, subscribe: true })\n protected testHost?: TestHost;\n\n @state()\n @consume({ context: qtiContext, subscribe: true })\n protected qtiContext: QtiContext = {\n QTI_CONTEXT: { testIdentifier: '', candidateIdentifier: '', environmentIdentifier: 'default' }\n };\n\n /** Template content if provided */\n #templateContent: unknown = null;\n #resolvedCustomElementRegistry: CustomElementRegistry | null = null;\n\n protected override createRenderRoot(): HTMLElement | DocumentFragment {\n const registry = this.customElementRegistry;\n\n this.#resolvedCustomElementRegistry = registry;\n\n return (\n this.shadowRoot ??\n this.attachShadow({\n ...(this.constructor as typeof TestContainer).shadowRootOptions,\n ...(registry ? { customElementRegistry: registry } : {})\n })\n );\n }\n\n /** Callback function to transform the test after loading */\n // @property({ type: Function }) postLoadTestTransformCallback: PostLoadTestTransformCallback | null = null;\n\n @watch('testURL', { waitUntilFirstUpdate: true })\n protected async handleTestURLChange() {\n if (!this.testURL) return;\n try {\n const explicitSeed = this.qtiContext?.QTI_CONTEXT?.seed;\n let api = (await qtiTransformTest().load(this.testURL))\n .applySelection(explicitSeed)\n .shuffleOrdering(explicitSeed);\n // Apply external transformation if provided\n const qtiTest = this.testHost;\n if (qtiTest?.postLoadTestTransformCallback) {\n // Create a temporary document to get the test element reference\n const tempDoc = api.htmlDoc(this.#resolvedCustomElementRegistry ?? undefined);\n const testElement = tempDoc.querySelector('qti-assessment-test') as any;\n\n if (testElement) {\n // Apply the callback with the test element\n api = await qtiTest.postLoadTestTransformCallback(api, testElement);\n }\n }\n\n this.#setTestDoc(api.htmlDoc(this.#resolvedCustomElementRegistry ?? undefined));\n } catch (error) {\n console.error('Error loading or parsing XML:', error);\n }\n }\n\n /**\n * Assign a new test document and announce it.\n *\n * The announcement has to happen here rather than on\n * `qc-test-loaded`, because by then the new document's\n * children have already connected and registered themselves — a host that\n * resets its context on that event throws those registrations away. Lit's\n * re-render is async, so a handler running now still gets in before any child\n * of the new `qti-assessment-test` connects.\n */\n #setTestDoc(testDoc: DocumentFragment): void {\n this.testDoc = testDoc;\n this.dispatchEvent(\n new CustomEvent('qti-testdoc-loaded', {\n bubbles: true,\n composed: true\n })\n );\n }\n\n @watch('testXML', { waitUntilFirstUpdate: true })\n protected handleTestXMLChange() {\n if (!this.testXML) return;\n try {\n const explicitSeed = this.qtiContext?.QTI_CONTEXT?.seed;\n this.#setTestDoc(\n qtiTransformTest()\n .parse(this.testXML)\n .applySelection(explicitSeed)\n .shuffleOrdering(explicitSeed)\n .htmlDoc(this.#resolvedCustomElementRegistry ?? undefined)\n );\n } catch (error) {\n console.error('Error parsing XML:', error);\n }\n }\n\n override async connectedCallback(): Promise<void> {\n super.connectedCallback();\n this.#initializeTemplateContent();\n this.#applyStyles();\n if (this.testURL) {\n this.handleTestURLChange();\n }\n if (this.testXML) {\n this.handleTestXMLChange();\n }\n }\n\n #initializeTemplateContent() {\n const template = this.querySelector('template') as HTMLTemplateElement;\n this.#templateContent = template ? template.content : html``;\n }\n\n #applyStyles() {\n const sheet = new CSSStyleSheet();\n sheet.replaceSync(itemCss);\n this.shadowRoot.adoptedStyleSheets = [sheet];\n }\n\n override render() {\n return html`\n ${this.#templateContent}\n <slot></slot>\n ${until(this.testDoc, html`<span>Loading...</span>`)}\n `;\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'test-container': TestContainer;\n }\n}\n"]}
|
|
@@ -111,6 +111,7 @@ export const TestStateMixin = (superClass) => {
|
|
|
111
111
|
// updated too.
|
|
112
112
|
for (const saved of state.test.items)
|
|
113
113
|
this.updateItemVariables(saved.identifier, saved.variables, saved.state);
|
|
114
|
+
this.#warnLeftOut(state);
|
|
114
115
|
const savedOutcomes = new Map(state.test.outcomes.map(outcome => [outcome.identifier, outcome.value]));
|
|
115
116
|
this.testContext = {
|
|
116
117
|
...this.testContext,
|
|
@@ -125,6 +126,18 @@ export const TestStateMixin = (superClass) => {
|
|
|
125
126
|
plugins: state.session.plugins && typeof state.session.plugins === 'object' ? { ...state.session.plugins } : {}
|
|
126
127
|
};
|
|
127
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Answers for items this test does not present are not restored, and the next state leaves them
|
|
131
|
+
* out: a selection (`qti-selection`) that leaves an item out, or a revised test. Said, not silent
|
|
132
|
+
* (roadmap 1 T4, `plans/host-api.md` → Decided).
|
|
133
|
+
*/
|
|
134
|
+
#warnLeftOut(state) {
|
|
135
|
+
const presented = new Set(this.testContext.items.map(item => item.identifier));
|
|
136
|
+
const leftOut = state.test.items.map(item => item.identifier).filter(identifier => !presented.has(identifier));
|
|
137
|
+
if (leftOut.length === 0)
|
|
138
|
+
return;
|
|
139
|
+
console.warn(`[qti-test] The state has answers for items this test does not present (not selected, or no longer in it); they are not restored: ${leftOut.join(', ')}.`);
|
|
140
|
+
}
|
|
128
141
|
/** A stored position may point at an item or section a revised test no longer has. */
|
|
129
142
|
#validSession(session) {
|
|
130
143
|
const exists = (selector, id) => !!id && !!this._testElement?.querySelector(`${selector}[identifier="${CSS.escape(id)}"]`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-state.mixin.js","sourceRoot":"","sources":["../../src/mixins/test-state.mixin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AA+B3D,MAA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E,OAAO,CAAC,YAAY,EAAE;oBACxC,CAAC,CAAC,EAAE,CAAC;aACR,CAAC;QACJ,CAAC;QAED,kBAAkB,CAAC,IAAwB,EAAE,EAAU;YACrD,cAAc,CAAC,IAAI,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QACzD,CAAC;KACF;IAED,OAAO,cAAkD,CAAC;AAC5D,CAAC,CAAC","sourcesContent":["import { QTI_TEST_STATE_VERSION } from '../types/qti-test-state';\nimport { dispatchPublic, listenPublic } from '../public-events';\nimport { stateFromContexts } from '../state-from-contexts';\n\nimport type { PropertyValues } from 'lit';\nimport type { StateRefusal } from '../public-events';\nimport type { QtiTestState, QtiTestStateSession } from '../types/qti-test-state';\nimport type { TestBaseInterface } from './test-base';\n\ntype Constructor<T = {}> = abstract new (...args: any[]) => T;\ntype Value = string | string[] | null;\n\nexport interface ITestStateMixin {\n /**\n * The candidate's persistable state: what they answered and where they are.\n *\n * Assign a stored state to resume. It may be assigned at any time: before the test document\n * has loaded it is held and applied as soon as the test connects — before the first item\n * loads, so items and the initial navigation start from it — and after that it is applied\n * immediately and the test navigates to the restored position.\n *\n * Items are matched by item-ref identifier; items the state does not mention start fresh, and\n * stored items, test outcomes or a position the test no longer has are ignored. Restoring is a\n * resume, so linear-mode navigation restrictions do not apply.\n *\n * A value that is not a version 1 `QtiTestState` is ignored and reported with\n * `qc-test-state-refused`: at once when the test has loaded, or when it does if it was assigned\n * before. A state assigned earlier that could be read stays in effect. A stored 9.x\n * `testContext` is turned into a state with `stateFromContexts`.\n */\n state: QtiTestState;\n}\n\nconst copy = (value: Readonly<Value> | undefined): Value =>\n Array.isArray(value) ? [...value] : ((value as string | null | undefined) ?? null);\n\nconst isQtiTestState = (value: unknown): value is QtiTestState => {\n const state = value as QtiTestState | null;\n return (\n state?.version === QTI_TEST_STATE_VERSION &&\n Array.isArray(state.test?.items) &&\n Array.isArray(state.test?.outcomes) &&\n typeof state.session === 'object' &&\n state.session !== null\n );\n};\n\n/**\n * Persist and resume a test session through one value, `state`, and one event,\n * `qc-test-state-changed`.\n *\n * Sits directly on top of `TestBaseMixin`, so its `qc-test-loaded` listener runs\n * after the base has built `testContext.items` from the item-refs, and before the navigation\n * mixin's listener picks the first item to load.\n */\nexport const TestStateMixin = <T extends Constructor<TestBaseInterface>>(superClass: T) => {\n abstract class TestStateClass extends superClass implements ITestStateMixin {\n /** A state assigned before the test connected, applied when it does. */\n #pending: QtiTestState | null = null;\n\n /** A refused state assigned before the test connected, reported when it does. */\n #refused: StateRefusal | null = null;\n\n /**\n * False while a test document is loading. Loading resets both contexts and rebuilds them, so\n * the intermediate states are empty — emitting one would invite a host to save it over the\n * real one.\n */\n #ready = false;\n\n #lastEmitted: string | null = null;\n\n constructor(...args: any[]) {\n super(...args);\n this.addEventListener('qti-testdoc-loaded', () => {\n this.#ready = false;\n this.#lastEmitted = null;\n });\n listenPublic(this, 'qc-test-update-plugin-state', ({ detail }) => this.#updatePluginState(detail));\n listenPublic(this, 'qc-test-loaded', () => {\n if (this.#pending) {\n this.#apply(this.#pending);\n this.#pending = null;\n }\n this.#ready = true;\n if (this.#refused) {\n dispatchPublic(this, 'qc-test-state-refused', this.#refused);\n this.#refused = null;\n }\n });\n }\n\n /** One plugin's entry, as JSON: the test stores it without reading it. `null` removes it. */\n #updatePluginState({ plugin, value }: { plugin: string; value: unknown }): void {\n if (typeof plugin !== 'string' || plugin === '') return;\n const { [plugin]: _removed, ...others } = this.sessionContext.plugins ?? {};\n let next: Record<string, unknown> = others;\n if (value !== null && value !== undefined) {\n try {\n next = { ...others, [plugin]: JSON.parse(JSON.stringify(value)) };\n } catch {\n console.warn(`[qti-test] Ignoring plugin state for \"${plugin}\": it is not JSON.`);\n return;\n }\n }\n this.sessionContext = { ...this.sessionContext, plugins: next };\n }\n\n get state(): QtiTestState {\n return stateFromContexts(this.testContext, this.sessionContext);\n }\n\n set state(value: QtiTestState) {\n if (!isQtiTestState(value)) {\n const message = `Ignoring a state that is not a version ${QTI_TEST_STATE_VERSION} QtiTestState.`;\n console.warn(`[qti-test] ${message}`);\n // Hosts assign state before the test is in the page, where an event reaches no listener.\n if (this.#ready) dispatchPublic(this, 'qc-test-state-refused', { message, state: value });\n else this.#refused = { message, state: value };\n return;\n }\n if (!this.#ready) {\n this.#pending = value;\n return;\n }\n\n const { navItemRefId, navSectionId } = this.sessionContext;\n this.#apply(value);\n\n const restored = this.sessionContext;\n if (restored.navItemRefId && restored.navItemRefId !== navItemRefId) {\n this.#requestNavigation('item', restored.navItemRefId);\n } else if (!restored.navItemRefId && restored.navSectionId && restored.navSectionId !== navSectionId) {\n this.#requestNavigation('section', restored.navSectionId);\n }\n }\n\n protected override updated(changed: PropertyValues): void {\n super.updated(changed);\n if (!this.#ready || !(changed.has('testContext') || changed.has('sessionContext'))) return;\n\n const state = this.state;\n const serialized = JSON.stringify(state);\n if (serialized === this.#lastEmitted) return;\n this.#lastEmitted = serialized;\n dispatchPublic(this, 'qc-test-state-changed', state);\n }\n\n #apply(state: QtiTestState): void {\n // Before the items load this only fills the test context; items already on screen are\n // updated too.\n for (const saved of state.test.items) this.updateItemVariables(saved.identifier, saved.variables, saved.state);\n\n const savedOutcomes = new Map(state.test.outcomes.map(outcome => [outcome.identifier, outcome.value]));\n this.testContext = {\n ...this.testContext,\n testOutcomeVariables: this.testContext.testOutcomeVariables?.map(v =>\n savedOutcomes.has(v.identifier) ? { ...v, value: copy(savedOutcomes.get(v.identifier)) } : v\n )\n };\n\n // Written before any navigation request, so the linear-mode restriction — which compares\n // against the current position — sees the restored item as current and lets it load.\n // Plugins are replaced, not merged: a state without them clears what an earlier one left.\n this.sessionContext = {\n ...this.sessionContext,\n ...this.#validSession(state.session),\n plugins: state.session.plugins && typeof state.session.plugins === 'object' ? { ...state.session.plugins } : {}\n };\n }\n\n /** A stored position may point at an item or section a revised test no longer has. */\n #validSession(session: QtiTestStateSession): QtiTestStateSession {\n const exists = (selector: string, id: string | null | undefined) =>\n !!id && !!this._testElement?.querySelector(`${selector}[identifier=\"${CSS.escape(id)}\"]`);\n return {\n ...(exists('qti-test-part', session.navPartId) ? { navPartId: session.navPartId } : {}),\n ...(exists('qti-assessment-section', session.navSectionId) ? { navSectionId: session.navSectionId } : {}),\n ...(this.testContext.items.some(i => i.identifier === session.navItemRefId)\n ? { navItemRefId: session.navItemRefId }\n : {})\n };\n }\n\n #requestNavigation(type: 'item' | 'section', id: string): void {\n dispatchPublic(this, 'qc-test-navigate', { type, id });\n }\n }\n\n return TestStateClass as Constructor<ITestStateMixin> & T;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"test-state.mixin.js","sourceRoot":"","sources":["../../src/mixins/test-state.mixin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AA+B3D,MAAM,IAAI,GAAG,CAAC,KAAkC,EAAS,EAAE,CACzD,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAE,KAAmC,IAAI,IAAI,CAAC,CAAC;AAErF,MAAM,cAAc,GAAG,CAAC,KAAc,EAAyB,EAAE;IAC/D,MAAM,KAAK,GAAG,KAA4B,CAAC;IAC3C,OAAO,CACL,KAAK,EAAE,OAAO,KAAK,sBAAsB;QACzC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC;QAChC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC;QACnC,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;QACjC,KAAK,CAAC,OAAO,KAAK,IAAI,CACvB,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cA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AE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE;oBACxC,CAAC,CAAC,EAAE,CAAC;aACR,CAAC;QACJ,CAAC;QAED,kBAAkB,CAAC,IAAwB,EAAE,EAAU;YACrD,cAAc,CAAC,IAAI,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QACzD,CAAC;KACF;IAED,OAAO,cAAkD,CAAC;AAC5D,CAAC,CAAC","sourcesContent":["import { QTI_TEST_STATE_VERSION } from '../types/qti-test-state';\nimport { dispatchPublic, listenPublic } from '../public-events';\nimport { stateFromContexts } from '../state-from-contexts';\n\nimport type { PropertyValues } from 'lit';\nimport type { StateRefusal } from '../public-events';\nimport type { QtiTestState, QtiTestStateSession } from '../types/qti-test-state';\nimport type { TestBaseInterface } from './test-base';\n\ntype Constructor<T = {}> = abstract new (...args: any[]) => T;\ntype Value = string | string[] | null;\n\nexport interface ITestStateMixin {\n /**\n * The candidate's persistable state: what they answered and where they are.\n *\n * Assign a stored state to resume. It may be assigned at any time: before the test document\n * has loaded it is held and applied as soon as the test connects — before the first item\n * loads, so items and the initial navigation start from it — and after that it is applied\n * immediately and the test navigates to the restored position.\n *\n * Items are matched by item-ref identifier; items the state does not mention start fresh, and\n * stored items, test outcomes or a position the test no longer has are ignored. Restoring is a\n * resume, so linear-mode navigation restrictions do not apply.\n *\n * A value that is not a version 1 `QtiTestState` is ignored and reported with\n * `qc-test-state-refused`: at once when the test has loaded, or when it does if it was assigned\n * before. A state assigned earlier that could be read stays in effect. A stored 9.x\n * `testContext` is turned into a state with `stateFromContexts`.\n */\n state: QtiTestState;\n}\n\nconst copy = (value: Readonly<Value> | undefined): Value =>\n Array.isArray(value) ? [...value] : ((value as string | null | undefined) ?? null);\n\nconst isQtiTestState = (value: unknown): value is QtiTestState => {\n const state = value as QtiTestState | null;\n return (\n state?.version === QTI_TEST_STATE_VERSION &&\n Array.isArray(state.test?.items) &&\n Array.isArray(state.test?.outcomes) &&\n typeof state.session === 'object' &&\n state.session !== null\n );\n};\n\n/**\n * Persist and resume a test session through one value, `state`, and one event,\n * `qc-test-state-changed`.\n *\n * Sits directly on top of `TestBaseMixin`, so its `qc-test-loaded` listener runs\n * after the base has built `testContext.items` from the item-refs, and before the navigation\n * mixin's listener picks the first item to load.\n */\nexport const TestStateMixin = <T extends Constructor<TestBaseInterface>>(superClass: T) => {\n abstract class TestStateClass extends superClass implements ITestStateMixin {\n /** A state assigned before the test connected, applied when it does. */\n #pending: QtiTestState | null = null;\n\n /** A refused state assigned before the test connected, reported when it does. */\n #refused: StateRefusal | null = null;\n\n /**\n * False while a test document is loading. Loading resets both contexts and rebuilds them, so\n * the intermediate states are empty — emitting one would invite a host to save it over the\n * real one.\n */\n #ready = false;\n\n #lastEmitted: string | null = null;\n\n constructor(...args: any[]) {\n super(...args);\n this.addEventListener('qti-testdoc-loaded', () => {\n this.#ready = false;\n this.#lastEmitted = null;\n });\n listenPublic(this, 'qc-test-update-plugin-state', ({ detail }) => this.#updatePluginState(detail));\n listenPublic(this, 'qc-test-loaded', () => {\n if (this.#pending) {\n this.#apply(this.#pending);\n this.#pending = null;\n }\n this.#ready = true;\n if (this.#refused) {\n dispatchPublic(this, 'qc-test-state-refused', this.#refused);\n this.#refused = null;\n }\n });\n }\n\n /** One plugin's entry, as JSON: the test stores it without reading it. `null` removes it. */\n #updatePluginState({ plugin, value }: { plugin: string; value: unknown }): void {\n if (typeof plugin !== 'string' || plugin === '') return;\n const { [plugin]: _removed, ...others } = this.sessionContext.plugins ?? {};\n let next: Record<string, unknown> = others;\n if (value !== null && value !== undefined) {\n try {\n next = { ...others, [plugin]: JSON.parse(JSON.stringify(value)) };\n } catch {\n console.warn(`[qti-test] Ignoring plugin state for \"${plugin}\": it is not JSON.`);\n return;\n }\n }\n this.sessionContext = { ...this.sessionContext, plugins: next };\n }\n\n get state(): QtiTestState {\n return stateFromContexts(this.testContext, this.sessionContext);\n }\n\n set state(value: QtiTestState) {\n if (!isQtiTestState(value)) {\n const message = `Ignoring a state that is not a version ${QTI_TEST_STATE_VERSION} QtiTestState.`;\n console.warn(`[qti-test] ${message}`);\n // Hosts assign state before the test is in the page, where an event reaches no listener.\n if (this.#ready) dispatchPublic(this, 'qc-test-state-refused', { message, state: value });\n else this.#refused = { message, state: value };\n return;\n }\n if (!this.#ready) {\n this.#pending = value;\n return;\n }\n\n const { navItemRefId, navSectionId } = this.sessionContext;\n this.#apply(value);\n\n const restored = this.sessionContext;\n if (restored.navItemRefId && restored.navItemRefId !== navItemRefId) {\n this.#requestNavigation('item', restored.navItemRefId);\n } else if (!restored.navItemRefId && restored.navSectionId && restored.navSectionId !== navSectionId) {\n this.#requestNavigation('section', restored.navSectionId);\n }\n }\n\n protected override updated(changed: PropertyValues): void {\n super.updated(changed);\n if (!this.#ready || !(changed.has('testContext') || changed.has('sessionContext'))) return;\n\n const state = this.state;\n const serialized = JSON.stringify(state);\n if (serialized === this.#lastEmitted) return;\n this.#lastEmitted = serialized;\n dispatchPublic(this, 'qc-test-state-changed', state);\n }\n\n #apply(state: QtiTestState): void {\n // Before the items load this only fills the test context; items already on screen are\n // updated too.\n for (const saved of state.test.items) this.updateItemVariables(saved.identifier, saved.variables, saved.state);\n this.#warnLeftOut(state);\n\n const savedOutcomes = new Map(state.test.outcomes.map(outcome => [outcome.identifier, outcome.value]));\n this.testContext = {\n ...this.testContext,\n testOutcomeVariables: this.testContext.testOutcomeVariables?.map(v =>\n savedOutcomes.has(v.identifier) ? { ...v, value: copy(savedOutcomes.get(v.identifier)) } : v\n )\n };\n\n // Written before any navigation request, so the linear-mode restriction — which compares\n // against the current position — sees the restored item as current and lets it load.\n // Plugins are replaced, not merged: a state without them clears what an earlier one left.\n this.sessionContext = {\n ...this.sessionContext,\n ...this.#validSession(state.session),\n plugins: state.session.plugins && typeof state.session.plugins === 'object' ? { ...state.session.plugins } : {}\n };\n }\n\n /**\n * Answers for items this test does not present are not restored, and the next state leaves them\n * out: a selection (`qti-selection`) that leaves an item out, or a revised test. Said, not silent\n * (roadmap 1 T4, `plans/host-api.md` → Decided).\n */\n #warnLeftOut(state: QtiTestState): void {\n const presented = new Set(this.testContext.items.map(item => item.identifier));\n const leftOut = state.test.items.map(item => item.identifier).filter(identifier => !presented.has(identifier));\n if (leftOut.length === 0) return;\n console.warn(\n `[qti-test] The state has answers for items this test does not present (not selected, or no longer in it); they are not restored: ${leftOut.join(', ')}.`\n );\n }\n\n /** A stored position may point at an item or section a revised test no longer has. */\n #validSession(session: QtiTestStateSession): QtiTestStateSession {\n const exists = (selector: string, id: string | null | undefined) =>\n !!id && !!this._testElement?.querySelector(`${selector}[identifier=\"${CSS.escape(id)}\"]`);\n return {\n ...(exists('qti-test-part', session.navPartId) ? { navPartId: session.navPartId } : {}),\n ...(exists('qti-assessment-section', session.navSectionId) ? { navSectionId: session.navSectionId } : {}),\n ...(this.testContext.items.some(i => i.identifier === session.navItemRefId)\n ? { navItemRefId: session.navItemRefId }\n : {})\n };\n }\n\n #requestNavigation(type: 'item' | 'section', id: string): void {\n dispatchPublic(this, 'qc-test-navigate', { type, id });\n }\n }\n\n return TestStateClass as Constructor<ITestStateMixin> & T;\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qti-components/test",
|
|
3
3
|
"description": "QTI test utilities",
|
|
4
|
-
"version": "2.0.0-next.
|
|
4
|
+
"version": "2.0.0-next.22",
|
|
5
5
|
"author": "",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@heximal/templates": "^0.1.5",
|
|
8
8
|
"@qti-components/base": "^3.0.0-next.10",
|
|
9
|
-
"@qti-components/delivery": "^0.1.0-next.
|
|
9
|
+
"@qti-components/delivery": "^0.1.0-next.3",
|
|
10
10
|
"@qti-components/elements": "^2.0.0-next.7",
|
|
11
11
|
"@qti-components/interactions": "^1.10.5-next.8",
|
|
12
12
|
"@qti-components/processing": "^1.6.1-next.5",
|
|
13
13
|
"@qti-components/theme": "^3.0.0-next.6",
|
|
14
|
-
"@qti-components/transformers": "^1.8.0-next.
|
|
14
|
+
"@qti-components/transformers": "^1.8.0-next.4",
|
|
15
15
|
"@qti-components/utilities": "^1.3.3"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|