@pie-qti/transform-core 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -0
- package/CHANGELOG.md +10 -0
- package/VALIDATION.md +217 -0
- package/dist/engine/index.d.ts +2 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +2 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/transform-engine.d.ts +58 -0
- package/dist/engine/transform-engine.d.ts.map +1 -0
- package/dist/engine/transform-engine.js +173 -0
- package/dist/engine/transform-engine.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/registry/index.d.ts +2 -0
- package/dist/registry/index.d.ts.map +1 -0
- package/dist/registry/index.js +2 -0
- package/dist/registry/index.js.map +1 -0
- package/dist/registry/plugin-registry.d.ts +54 -0
- package/dist/registry/plugin-registry.d.ts.map +1 -0
- package/dist/registry/plugin-registry.js +86 -0
- package/dist/registry/plugin-registry.js.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/logger.d.ts +40 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +61 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/validator.d.ts +60 -0
- package/dist/utils/validator.d.ts.map +1 -0
- package/dist/utils/validator.js +161 -0
- package/dist/utils/validator.js.map +1 -0
- package/package.json +38 -0
- package/src/engine/index.ts +1 -0
- package/src/engine/transform-engine.ts +257 -0
- package/src/index.ts +18 -0
- package/src/registry/index.ts +1 -0
- package/src/registry/plugin-registry.ts +116 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/logger.ts +79 -0
- package/src/utils/validator.ts +193 -0
- package/tests/plugin-registry.test.ts +154 -0
- package/tests/transform-engine.test.ts +168 -0
- package/tests/validator.test.ts +234 -0
- package/tsconfig.json +13 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin Registry Tests
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { beforeEach, describe, expect, test } from 'bun:test';
|
|
6
|
+
import type { TransformPlugin } from '@pie-qti/transform-types';
|
|
7
|
+
import { PluginRegistry } from '../src/registry/plugin-registry';
|
|
8
|
+
|
|
9
|
+
// Simple mock plugins
|
|
10
|
+
const createMockPlugin = (
|
|
11
|
+
id: string,
|
|
12
|
+
sourceFormat: 'qti22' | 'pie',
|
|
13
|
+
targetFormat: 'pie' | 'qti22'
|
|
14
|
+
): TransformPlugin => ({
|
|
15
|
+
id,
|
|
16
|
+
version: '1.0.0',
|
|
17
|
+
name: `Mock ${id}`,
|
|
18
|
+
sourceFormat,
|
|
19
|
+
targetFormat,
|
|
20
|
+
async canHandle() { return true; },
|
|
21
|
+
async transform() {
|
|
22
|
+
return {
|
|
23
|
+
items: [],
|
|
24
|
+
format: targetFormat,
|
|
25
|
+
metadata: {
|
|
26
|
+
sourceFormat,
|
|
27
|
+
targetFormat,
|
|
28
|
+
pluginId: id,
|
|
29
|
+
timestamp: new Date(),
|
|
30
|
+
itemCount: 0,
|
|
31
|
+
processingTime: 0,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('PluginRegistry', () => {
|
|
38
|
+
let registry: PluginRegistry;
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
registry = new PluginRegistry();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('should register a plugin', () => {
|
|
45
|
+
const plugin = createMockPlugin('test-plugin', 'qti22', 'pie');
|
|
46
|
+
registry.register(plugin);
|
|
47
|
+
|
|
48
|
+
expect(registry.has('test-plugin')).toBe(true);
|
|
49
|
+
expect(registry.size).toBe(1);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('should throw error when registering duplicate plugin', () => {
|
|
53
|
+
const plugin = createMockPlugin('test-plugin', 'qti22', 'pie');
|
|
54
|
+
registry.register(plugin);
|
|
55
|
+
|
|
56
|
+
expect(() => registry.register(plugin)).toThrow('already registered');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('should unregister a plugin', () => {
|
|
60
|
+
const plugin = createMockPlugin('test-plugin', 'qti22', 'pie');
|
|
61
|
+
registry.register(plugin);
|
|
62
|
+
|
|
63
|
+
const removed = registry.unregister('test-plugin');
|
|
64
|
+
expect(removed).toBe(true);
|
|
65
|
+
expect(registry.has('test-plugin')).toBe(false);
|
|
66
|
+
expect(registry.size).toBe(0);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('should get plugin by id', () => {
|
|
70
|
+
const plugin = createMockPlugin('test-plugin', 'qti22', 'pie');
|
|
71
|
+
registry.register(plugin);
|
|
72
|
+
|
|
73
|
+
const found = registry.get('test-plugin');
|
|
74
|
+
expect(found).toBeDefined();
|
|
75
|
+
expect(found?.id).toBe('test-plugin');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('should get all plugins', () => {
|
|
79
|
+
const plugin1 = createMockPlugin('plugin-1', 'qti22', 'pie');
|
|
80
|
+
const plugin2 = createMockPlugin('plugin-2', 'pie', 'qti22');
|
|
81
|
+
|
|
82
|
+
registry.register(plugin1);
|
|
83
|
+
registry.register(plugin2);
|
|
84
|
+
|
|
85
|
+
const all = registry.getAll();
|
|
86
|
+
expect(all.length).toBe(2);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('should find plugin by source and target format', () => {
|
|
90
|
+
const plugin1 = createMockPlugin('qti22-to-pie', 'qti22', 'pie');
|
|
91
|
+
const plugin2 = createMockPlugin('pie-to-qti22', 'pie', 'qti22');
|
|
92
|
+
|
|
93
|
+
registry.register(plugin1);
|
|
94
|
+
registry.register(plugin2);
|
|
95
|
+
|
|
96
|
+
const found = registry.findPlugin('qti22', 'pie');
|
|
97
|
+
expect(found).toBeDefined();
|
|
98
|
+
expect(found?.id).toBe('qti22-to-pie');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('should get plugins by source format', () => {
|
|
102
|
+
const plugin1 = createMockPlugin('qti22-to-pie', 'qti22', 'pie');
|
|
103
|
+
const plugin2 = createMockPlugin('pie-to-qti22', 'pie', 'qti22');
|
|
104
|
+
|
|
105
|
+
registry.register(plugin1);
|
|
106
|
+
registry.register(plugin2);
|
|
107
|
+
|
|
108
|
+
const qtiPlugins = registry.getBySourceFormat('qti22');
|
|
109
|
+
expect(qtiPlugins.length).toBe(1);
|
|
110
|
+
expect(qtiPlugins[0].id).toBe('qti22-to-pie');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('should get plugins by target format', () => {
|
|
114
|
+
const plugin1 = createMockPlugin('qti22-to-pie', 'qti22', 'pie');
|
|
115
|
+
const plugin2 = createMockPlugin('pie-to-qti22', 'pie', 'qti22');
|
|
116
|
+
|
|
117
|
+
registry.register(plugin1);
|
|
118
|
+
registry.register(plugin2);
|
|
119
|
+
|
|
120
|
+
const piePlugins = registry.getByTargetFormat('pie');
|
|
121
|
+
expect(piePlugins.length).toBe(1);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('should find compatible plugins', async () => {
|
|
125
|
+
const plugin1 = createMockPlugin('plugin-1', 'qti22', 'pie');
|
|
126
|
+
const plugin2 = createMockPlugin('plugin-2', 'pie', 'qti22');
|
|
127
|
+
|
|
128
|
+
// Override canHandle for plugin2 to return false
|
|
129
|
+
plugin2.canHandle = async () => false;
|
|
130
|
+
|
|
131
|
+
registry.register(plugin1);
|
|
132
|
+
registry.register(plugin2);
|
|
133
|
+
|
|
134
|
+
const compatible = await registry.findCompatiblePlugins({
|
|
135
|
+
content: '<assessmentItem></assessmentItem>',
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
expect(compatible.length).toBe(1);
|
|
139
|
+
expect(compatible[0].id).toBe('plugin-1');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test('should clear all plugins', () => {
|
|
143
|
+
const plugin1 = createMockPlugin('plugin-1', 'qti22', 'pie');
|
|
144
|
+
const plugin2 = createMockPlugin('plugin-2', 'pie', 'qti22');
|
|
145
|
+
|
|
146
|
+
registry.register(plugin1);
|
|
147
|
+
registry.register(plugin2);
|
|
148
|
+
expect(registry.size).toBe(2);
|
|
149
|
+
|
|
150
|
+
registry.clear();
|
|
151
|
+
expect(registry.size).toBe(0);
|
|
152
|
+
expect(registry.getAll().length).toBe(0);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transform Engine Tests
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { beforeEach, describe, expect, test } from 'bun:test';
|
|
6
|
+
import type { TransformContext, TransformInput, TransformOutput, TransformPlugin } from '@pie-qti/transform-types';
|
|
7
|
+
import { TransformEngine } from '../src/engine/transform-engine';
|
|
8
|
+
|
|
9
|
+
// Mock plugin for testing
|
|
10
|
+
class MockPlugin implements TransformPlugin {
|
|
11
|
+
dispose?: () => Promise<void>;
|
|
12
|
+
|
|
13
|
+
readonly id = 'mock-qti22-to-pie';
|
|
14
|
+
readonly version = '1.0.0';
|
|
15
|
+
readonly name = 'Mock QTI 2.2 to PIE Plugin';
|
|
16
|
+
readonly sourceFormat = 'qti22' as const;
|
|
17
|
+
readonly targetFormat = 'pie' as const;
|
|
18
|
+
|
|
19
|
+
async canHandle(input: TransformInput): Promise<boolean> {
|
|
20
|
+
if (typeof input.content === 'string') {
|
|
21
|
+
return input.content.includes('assessmentItem');
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async transform(_input: TransformInput, context: TransformContext): Promise<TransformOutput> {
|
|
27
|
+
context.logger?.info('Mock transformation', 'test-item');
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
items: [
|
|
31
|
+
{
|
|
32
|
+
id: 'test-item',
|
|
33
|
+
uuid: 'test-uuid',
|
|
34
|
+
config: { id: 'test-uuid', models: [], elements: {} },
|
|
35
|
+
searchMetaData: {},
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
format: 'pie',
|
|
39
|
+
metadata: {
|
|
40
|
+
sourceFormat: 'qti22',
|
|
41
|
+
targetFormat: 'pie',
|
|
42
|
+
pluginId: this.id,
|
|
43
|
+
timestamp: new Date(),
|
|
44
|
+
itemCount: 1,
|
|
45
|
+
processingTime: 0,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
describe('TransformEngine', () => {
|
|
52
|
+
let engine: TransformEngine;
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
engine = new TransformEngine();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('should register a plugin', () => {
|
|
59
|
+
const plugin = new MockPlugin();
|
|
60
|
+
engine.use(plugin);
|
|
61
|
+
|
|
62
|
+
const plugins = engine.getPlugins();
|
|
63
|
+
expect(plugins.length).toBe(1);
|
|
64
|
+
expect(plugins[0].id).toBe('mock-qti22-to-pie');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('should get plugin by id', () => {
|
|
68
|
+
const plugin = new MockPlugin();
|
|
69
|
+
engine.use(plugin);
|
|
70
|
+
|
|
71
|
+
const found = engine.getPlugin('mock-qti22-to-pie');
|
|
72
|
+
expect(found).toBeDefined();
|
|
73
|
+
expect(found?.id).toBe('mock-qti22-to-pie');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('should transform QTI to PIE', async () => {
|
|
77
|
+
const plugin = new MockPlugin();
|
|
78
|
+
engine.use(plugin);
|
|
79
|
+
|
|
80
|
+
const qtiXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
81
|
+
<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2"
|
|
82
|
+
identifier="test-item"
|
|
83
|
+
title="Test Item">
|
|
84
|
+
<itemBody>
|
|
85
|
+
<p>Test question</p>
|
|
86
|
+
</itemBody>
|
|
87
|
+
</assessmentItem>`;
|
|
88
|
+
|
|
89
|
+
const output = await engine.transform(qtiXml, {
|
|
90
|
+
sourceFormat: 'qti22',
|
|
91
|
+
targetFormat: 'pie',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expect(output).toBeDefined();
|
|
95
|
+
expect(output.items.length).toBe(1);
|
|
96
|
+
expect(output.items[0].id).toBe('test-item');
|
|
97
|
+
expect(output.format).toBe('pie');
|
|
98
|
+
expect(output.metadata.sourceFormat).toBe('qti22');
|
|
99
|
+
expect(output.metadata.targetFormat).toBe('pie');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('should auto-detect QTI 2.2 format', async () => {
|
|
103
|
+
const plugin = new MockPlugin();
|
|
104
|
+
engine.use(plugin);
|
|
105
|
+
|
|
106
|
+
const qtiXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
107
|
+
<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">
|
|
108
|
+
<itemBody><p>Test</p></itemBody>
|
|
109
|
+
</assessmentItem>`;
|
|
110
|
+
|
|
111
|
+
const output = await engine.transform(qtiXml, {
|
|
112
|
+
targetFormat: 'pie',
|
|
113
|
+
// sourceFormat not specified - should auto-detect
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(output.metadata.sourceFormat).toBe('qti22');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('should throw error when no plugin found', async () => {
|
|
120
|
+
// Don't register any plugins
|
|
121
|
+
const qtiXml = '<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2"></assessmentItem>';
|
|
122
|
+
|
|
123
|
+
await expect(
|
|
124
|
+
engine.transform(qtiXml, {
|
|
125
|
+
sourceFormat: 'qti22',
|
|
126
|
+
targetFormat: 'pie',
|
|
127
|
+
})
|
|
128
|
+
).rejects.toThrow('No plugin found');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('should transform batch of inputs', async () => {
|
|
132
|
+
const plugin = new MockPlugin();
|
|
133
|
+
engine.use(plugin);
|
|
134
|
+
|
|
135
|
+
const inputs = [
|
|
136
|
+
'<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">1</assessmentItem>',
|
|
137
|
+
'<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">2</assessmentItem>',
|
|
138
|
+
'<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">3</assessmentItem>',
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
const outputs = await engine.transformBatch(inputs, {
|
|
142
|
+
sourceFormat: 'qti22',
|
|
143
|
+
targetFormat: 'pie',
|
|
144
|
+
parallel: 2,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
expect(outputs.length).toBe(3);
|
|
148
|
+
outputs.forEach((output) => {
|
|
149
|
+
expect(output.items.length).toBe(1);
|
|
150
|
+
expect(output.format).toBe('pie');
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('should clean up resources on dispose', async () => {
|
|
155
|
+
const plugin = new MockPlugin();
|
|
156
|
+
let disposeCalled = false;
|
|
157
|
+
|
|
158
|
+
plugin.dispose = async () => {
|
|
159
|
+
disposeCalled = true;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
engine.use(plugin);
|
|
163
|
+
await engine.dispose();
|
|
164
|
+
|
|
165
|
+
expect(disposeCalled).toBe(true);
|
|
166
|
+
expect(engine.getPlugins().length).toBe(0);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PIE Item Validator Tests
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, expect, test } from 'bun:test';
|
|
6
|
+
import { PieItemValidator, validatePieItemStructure } from '../src/utils/validator.js';
|
|
7
|
+
|
|
8
|
+
describe('validatePieItemStructure', () => {
|
|
9
|
+
test('should validate a valid PIE item structure', () => {
|
|
10
|
+
const item = {
|
|
11
|
+
id: 'test-001',
|
|
12
|
+
uuid: '123e4567-e89b-12d3-a456-426614174000',
|
|
13
|
+
config: {
|
|
14
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
15
|
+
models: [],
|
|
16
|
+
elements: {},
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const result = validatePieItemStructure(item);
|
|
21
|
+
expect(result.valid).toBe(true);
|
|
22
|
+
expect(result.errors).toBeUndefined();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should fail when id is missing', () => {
|
|
26
|
+
const item = {
|
|
27
|
+
uuid: '123e4567-e89b-12d3-a456-426614174000',
|
|
28
|
+
config: {
|
|
29
|
+
models: [],
|
|
30
|
+
elements: {},
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const result = validatePieItemStructure(item);
|
|
35
|
+
expect(result.valid).toBe(false);
|
|
36
|
+
expect(result.errors).toBeDefined();
|
|
37
|
+
expect(result.detailedErrors?.find(e => e.path === 'id')).toBeDefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('should fail when config.models is not an array', () => {
|
|
41
|
+
const item = {
|
|
42
|
+
id: 'test-001',
|
|
43
|
+
uuid: '123e4567-e89b-12d3-a456-426614174000',
|
|
44
|
+
config: {
|
|
45
|
+
models: 'not-an-array',
|
|
46
|
+
elements: {},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const result = validatePieItemStructure(item);
|
|
51
|
+
expect(result.valid).toBe(false);
|
|
52
|
+
expect(result.detailedErrors?.find(e => e.path === 'config.models')).toBeDefined();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('should fail when item is not an object', () => {
|
|
56
|
+
const result = validatePieItemStructure('not an object');
|
|
57
|
+
expect(result.valid).toBe(false);
|
|
58
|
+
expect(result.errors?.[0]).toBe('Item must be an object');
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('PieItemValidator', () => {
|
|
63
|
+
test('should create a validator instance', () => {
|
|
64
|
+
const validator = new PieItemValidator();
|
|
65
|
+
expect(validator).toBeDefined();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('should register a schema', () => {
|
|
69
|
+
const validator = new PieItemValidator();
|
|
70
|
+
const schema = {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
id: { type: 'string' },
|
|
74
|
+
element: { type: 'string' },
|
|
75
|
+
prompt: { type: 'string' },
|
|
76
|
+
},
|
|
77
|
+
required: ['id', 'element'],
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
validator.registerSchema('@pie-element/test', schema);
|
|
81
|
+
// No error means success
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('should validate against registered schema', () => {
|
|
85
|
+
const validator = new PieItemValidator();
|
|
86
|
+
|
|
87
|
+
// Register a simple schema
|
|
88
|
+
const schema = {
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: {
|
|
91
|
+
id: { type: 'string' },
|
|
92
|
+
element: { type: 'string' },
|
|
93
|
+
prompt: { type: 'string' },
|
|
94
|
+
choices: {
|
|
95
|
+
type: 'array',
|
|
96
|
+
items: {
|
|
97
|
+
type: 'object',
|
|
98
|
+
properties: {
|
|
99
|
+
label: { type: 'string' },
|
|
100
|
+
value: { type: 'string' },
|
|
101
|
+
},
|
|
102
|
+
required: ['label', 'value'],
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
required: ['id', 'element', 'choices'],
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
validator.registerSchema('@pie-element/test', schema);
|
|
110
|
+
|
|
111
|
+
const item = {
|
|
112
|
+
id: 'test-001',
|
|
113
|
+
uuid: '123e4567-e89b-12d3-a456-426614174000',
|
|
114
|
+
config: {
|
|
115
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
116
|
+
models: [
|
|
117
|
+
{
|
|
118
|
+
id: 'model-001',
|
|
119
|
+
element: '@pie-element/test',
|
|
120
|
+
prompt: 'Test question',
|
|
121
|
+
choices: [
|
|
122
|
+
{ label: 'Option A', value: 'a' },
|
|
123
|
+
{ label: 'Option B', value: 'b' },
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
elements: {
|
|
128
|
+
'test': '@pie-element/test@1.0.0',
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const result = validator.validate(item);
|
|
134
|
+
expect(result.valid).toBe(true);
|
|
135
|
+
expect(result.errors).toBeUndefined();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('should detect schema violations', () => {
|
|
139
|
+
const validator = new PieItemValidator();
|
|
140
|
+
|
|
141
|
+
const schema = {
|
|
142
|
+
type: 'object',
|
|
143
|
+
properties: {
|
|
144
|
+
id: { type: 'string' },
|
|
145
|
+
element: { type: 'string' },
|
|
146
|
+
choices: {
|
|
147
|
+
type: 'array',
|
|
148
|
+
items: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
properties: {
|
|
151
|
+
label: { type: 'string' },
|
|
152
|
+
value: { type: 'string' },
|
|
153
|
+
},
|
|
154
|
+
required: ['label', 'value'],
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
required: ['id', 'element', 'choices'],
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
validator.registerSchema('@pie-element/test', schema);
|
|
162
|
+
|
|
163
|
+
const item = {
|
|
164
|
+
id: 'test-001',
|
|
165
|
+
uuid: '123e4567-e89b-12d3-a456-426614174000',
|
|
166
|
+
config: {
|
|
167
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
168
|
+
models: [
|
|
169
|
+
{
|
|
170
|
+
id: 'model-001',
|
|
171
|
+
element: '@pie-element/test',
|
|
172
|
+
// Missing required 'choices' field
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
elements: {},
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const result = validator.validate(item);
|
|
180
|
+
expect(result.valid).toBe(false);
|
|
181
|
+
expect(result.errors).toBeDefined();
|
|
182
|
+
expect(result.errors!.length).toBeGreaterThan(0);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('should validate multiple items', () => {
|
|
186
|
+
const validator = new PieItemValidator();
|
|
187
|
+
|
|
188
|
+
const items = [
|
|
189
|
+
{
|
|
190
|
+
id: 'test-001',
|
|
191
|
+
uuid: 'uuid-001',
|
|
192
|
+
config: { id: 'uuid-001', models: [], elements: {} },
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
id: 'test-002',
|
|
196
|
+
uuid: 'uuid-002',
|
|
197
|
+
config: { id: 'uuid-002', models: [], elements: {} },
|
|
198
|
+
},
|
|
199
|
+
];
|
|
200
|
+
|
|
201
|
+
const results = validator.validateMany(items);
|
|
202
|
+
expect(results.length).toBe(2);
|
|
203
|
+
expect(results[0].valid).toBe(true);
|
|
204
|
+
expect(results[1].valid).toBe(true);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test('should load schema from schemas package', async () => {
|
|
208
|
+
const validator = new PieItemValidator();
|
|
209
|
+
|
|
210
|
+
// Load schema from schemas package
|
|
211
|
+
await validator.loadSchemaForElement('@pie-element/multiple-choice');
|
|
212
|
+
|
|
213
|
+
const item = {
|
|
214
|
+
id: 'test-001',
|
|
215
|
+
uuid: 'uuid-001',
|
|
216
|
+
config: {
|
|
217
|
+
id: 'uuid-001',
|
|
218
|
+
models: [
|
|
219
|
+
{
|
|
220
|
+
id: 'model-001',
|
|
221
|
+
element: '@pie-element/multiple-choice',
|
|
222
|
+
prompt: 'Test question',
|
|
223
|
+
choices: [{ label: 'A', value: 'a' }],
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
elements: {},
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const result = validator.validate(item);
|
|
231
|
+
// The actual schema validation will vary based on the real schema
|
|
232
|
+
expect(result).toBeDefined();
|
|
233
|
+
});
|
|
234
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"lib": ["ES2022", "DOM"]
|
|
7
|
+
},
|
|
8
|
+
"include": ["src/**/*"],
|
|
9
|
+
"exclude": ["node_modules", "dist", "tests"],
|
|
10
|
+
"references": [
|
|
11
|
+
{ "path": "../types" }
|
|
12
|
+
]
|
|
13
|
+
}
|