Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const makeValidDescriptor = (overrides = {}) => ({
convertsFrom: [],
matches: () => false,
getQuestionType: () => null,
getDeclarationSchema: () => ({ baseType: 'string', cardinality: 'single' }),
parse: () => ({}),
validate: () => [],
...overrides,
Expand All @@ -28,6 +29,7 @@ describe('defineInteraction', () => {
'convertsFrom',
'matches',
'getQuestionType',
'getDeclarationSchema',
'parse',
'validate',
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import defineInteraction from '../defineInteraction';
import { QtiInteraction, QuestionType } from '../../constants';
import { QtiInteraction, QuestionType, BaseType, Cardinality } from '../../constants';
import ChoiceInteractionEditor from './ChoiceInteractionEditor.vue';

/**
Expand Down Expand Up @@ -48,6 +48,21 @@ export default defineInteraction({
: QuestionType.MULTI_SELECT;
},

/**
* Defines the structural schema for the response declaration of this interaction.
* Returns baseType and cardinality, which can depend on the selected questionType.
*
* @param {string} questionType
* @returns {{ baseType: string, cardinality: string }}
*/
getDeclarationSchema(questionType) {
return {
baseType: BaseType.IDENTIFIER,
cardinality:
questionType === QuestionType.SINGLE_SELECT ? Cardinality.SINGLE : Cardinality.MULTIPLE,
};
},

/**
* Extracts a structured state object from the interaction element.
* Stub — full parsing is a future task.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const REQUIRED_KEYS = [
'convertsFrom',
'matches',
'getQuestionType',
'getDeclarationSchema',
'parse',
'validate',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ export const descriptors = [choiceDescriptor];
* @type {Object.<string, import('./defineInteraction').InteractionDescriptor>}
*/
export const registry = Object.fromEntries(descriptors.map(d => [d.type, d]));

/**
* Find the interaction descriptor that supports a given question type.
*
* @param {string} questionType
* @returns {import('./defineInteraction').InteractionDescriptor|undefined}
*/
export function getDescriptorForQuestionType(questionType) {
return descriptors.find(d => d.questionTypes.includes(questionType));
}
Comment thread
AlexVelezLl marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Disabled because jest-dom matchers (toHaveAttribute/toHaveTextContent) are designed for
// HTML and do not work reliably on strict XML elements generated by serialization.
/* eslint-disable jest-dom/prefer-to-have-attribute, jest-dom/prefer-to-have-text-content */
import { buildXmlNode } from '../assembleItem.js';

const serializer = new XMLSerializer();

describe('assembleItem', () => {
describe('tag and attributes', () => {
it('creates an element with the given tag name', () => {
const node = buildXmlNode({ tag: 'qti-response-declaration' });
expect(node.tagName).toBe('qti-response-declaration');
});

it('sets string attributes', () => {
const node = buildXmlNode({
tag: 'qti-response-declaration',
attrs: { identifier: 'RESPONSE', cardinality: 'single', 'base-type': 'identifier' },
});
expect(node.getAttribute('identifier')).toBe('RESPONSE');
expect(node.getAttribute('cardinality')).toBe('single');
expect(node.getAttribute('base-type')).toBe('identifier');
});

it('coerces numeric attribute values to strings', () => {
const node = buildXmlNode({ tag: 'qti-map-entry', attrs: { 'mapped-value': 1.5 } });
expect(node.getAttribute('mapped-value')).toBe('1.5');
});

it('skips null attribute values', () => {
const node = buildXmlNode({ tag: 'qti-response-declaration', attrs: { 'base-type': null } });
expect(node.hasAttribute('base-type')).toBe(false);
});

it('skips undefined attribute values', () => {
const node = buildXmlNode({
tag: 'qti-response-declaration',
attrs: { 'base-type': undefined },
});
expect(node.hasAttribute('base-type')).toBe(false);
});

it('produces an element with no attributes when attrs is omitted', () => {
const node = buildXmlNode({ tag: 'qti-correct-response' });
expect(node.attributes.length).toBe(0);
});
});

describe('string children', () => {
it('appends a text node for a string child', () => {
const node = buildXmlNode({ tag: 'qti-value', children: ['ChoiceA'] });
expect(node.textContent).toBe('ChoiceA');
});

it('handles multiple string children as separate text nodes', () => {
const node = buildXmlNode({ tag: 'qti-value', children: ['hello', ' ', 'world'] });
expect(node.textContent).toBe('hello world');
});
});

describe('element children', () => {
it('appends child element nodes', () => {
const child = buildXmlNode({ tag: 'qti-value', children: ['ChoiceA'] });
const parent = buildXmlNode({ tag: 'qti-correct-response', children: [child] });
const childEls = parent.getElementsByTagName('qti-value');
expect(childEls.length).toBe(1);
expect(childEls[0].textContent).toBe('ChoiceA');
});

it('appends multiple element children in order', () => {
const childA = buildXmlNode({ tag: 'qti-value', children: ['A'] });
const childB = buildXmlNode({ tag: 'qti-value', children: ['B'] });
const parent = buildXmlNode({ tag: 'qti-correct-response', children: [childA, childB] });
const values = [...parent.getElementsByTagName('qti-value')].map(n => n.textContent);
expect(values).toEqual(['A', 'B']);
});
});

describe('mixed children', () => {
it('handles a mix of element and string children', () => {
const child = buildXmlNode({ tag: 'qti-value', children: ['X'] });
const parent = buildXmlNode({ tag: 'qti-correct-response', children: ['prefix', child] });
// First child is a text node
expect(parent.childNodes[0].nodeType).toBe(Node.TEXT_NODE);
expect(parent.childNodes[0].textContent).toBe('prefix');
// Second child is the element
expect(parent.childNodes[1].tagName).toBe('qti-value');
});
});

describe('serialization', () => {
it('round-trips through XMLSerializer', () => {
const node = buildXmlNode({
tag: 'qti-response-declaration',
attrs: { identifier: 'RESPONSE', cardinality: 'single', 'base-type': 'identifier' },
children: [
buildXmlNode({
tag: 'qti-correct-response',
children: [buildXmlNode({ tag: 'qti-value', children: ['ChoiceA'] })],
}),
],
});

const xml = serializer.serializeToString(node);
expect(xml).toContain('qti-response-declaration');
expect(xml).toContain('identifier="RESPONSE"');
expect(xml).toContain('qti-correct-response');
expect(xml).toContain('ChoiceA');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* XML node builder for QTI serialization.
*
* Used by QTIDeclaration.getXML() and all declaration strategy classes
* to produce DOM nodes. A module-level XML document is created once so
* all nodes share the same owner document, avoiding adoptNode requirements
* when assembling trees. Callers serialize to a string only at the boundary
* (e.g. XMLSerializer.serializeToString).
*/

const xmlDoc = new DOMParser().parseFromString('<root/>', 'text/xml');

/**
* Build an XML element node.
*
* @param {object} options
* @param {string} options.tag - Element tag name (e.g. 'qti-response-declaration')
* @param {Object.<string,*>} [options.attrs] - Attribute name→value pairs;
* null/undefined values are skipped
* @param {Array.<Node|string>} [options.children] - Child nodes or plain strings
* @returns {Element}
*/
export function buildXmlNode({ tag, attrs = {}, children = [] }) {
const el = xmlDoc.createElement(tag);

for (const [name, value] of Object.entries(attrs)) {
if (value !== null && value !== undefined) {
el.setAttribute(name, String(value));
}
}

for (const child of children) {
if (typeof child === 'string') {
el.appendChild(xmlDoc.createTextNode(child));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may need this to set the innerHTML property at some point when we need to build the prompt nodes, but we can defer this change to when we actually need it :)

} else {
el.appendChild(child);
}
}

return el;
}
Loading
Loading