@nakafa/aksara-contracts 0.48.9 → 0.50.0

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.
Files changed (53) hide show
  1. package/README.md +9 -0
  2. package/dist/assessment/choice.d.ts +154 -0
  3. package/dist/assessment/choice.js +87 -0
  4. package/dist/assessment/document.d.ts +219 -0
  5. package/dist/assessment/document.js +162 -0
  6. package/dist/assessment/entry.d.ts +50 -0
  7. package/dist/assessment/entry.js +48 -0
  8. package/dist/assessment/explanation.d.ts +19 -0
  9. package/dist/assessment/explanation.js +18 -0
  10. package/dist/assessment/figure.d.ts +1938 -0
  11. package/dist/assessment/figure.js +236 -0
  12. package/dist/assessment/fit.d.ts +21 -0
  13. package/dist/assessment/fit.js +42 -0
  14. package/dist/assessment/graphic.d.ts +95 -0
  15. package/dist/assessment/graphic.js +106 -0
  16. package/dist/assessment/hash.d.ts +1480 -0
  17. package/dist/assessment/hash.js +66 -0
  18. package/dist/assessment/identity.d.ts +37 -0
  19. package/dist/assessment/identity.js +50 -0
  20. package/dist/assessment/inline.d.ts +124 -0
  21. package/dist/assessment/inline.js +92 -0
  22. package/dist/assessment/interaction.d.ts +219 -0
  23. package/dist/assessment/interaction.js +38 -0
  24. package/dist/assessment/item.d.ts +796 -0
  25. package/dist/assessment/item.js +108 -0
  26. package/dist/assessment/joint.d.ts +62 -0
  27. package/dist/assessment/joint.js +85 -0
  28. package/dist/assessment/key.d.ts +195 -0
  29. package/dist/assessment/key.js +136 -0
  30. package/dist/assessment/paper.d.ts +817 -0
  31. package/dist/assessment/paper.js +69 -0
  32. package/dist/assessment/part.d.ts +47 -0
  33. package/dist/assessment/part.js +32 -0
  34. package/dist/assessment/profile.d.ts +159 -0
  35. package/dist/assessment/profile.js +160 -0
  36. package/dist/assessment/record.d.ts +18 -0
  37. package/dist/assessment/record.js +27 -0
  38. package/dist/assessment/scalar.d.ts +18 -0
  39. package/dist/assessment/scalar.js +18 -0
  40. package/dist/assessment/scoring.d.ts +210 -0
  41. package/dist/assessment/scoring.js +205 -0
  42. package/dist/assessment/stimulus.d.ts +48 -0
  43. package/dist/assessment/stimulus.js +42 -0
  44. package/dist/assessment/structure.d.ts +13 -0
  45. package/dist/assessment/structure.js +29 -0
  46. package/dist/assessment/tools.d.ts +17 -0
  47. package/dist/assessment/tools.js +39 -0
  48. package/dist/locale.d.ts +9 -0
  49. package/dist/locale.js +25 -1
  50. package/dist/projection/verify.d.ts +23 -23
  51. package/dist/runtime/verify.d.ts +46 -46
  52. package/dist/transport/request.d.ts +138 -138
  53. package/package.json +73 -1
@@ -0,0 +1,236 @@
1
+ import { Array as Arr, Record as Rec, Schema } from "effect";
2
+ import { AssetIdSchema } from "#contracts/assessment/identity";
3
+ import { RichTextSchema } from "#contracts/assessment/inline";
4
+ import { LatexSchema, NonNegativeIntSchema, PlainTextSchema, PositiveIntSchema, } from "#contracts/assessment/scalar";
5
+ import { MathVisualKeySchema, PositiveMeasureSchema, SpacePointSchema, } from "#contracts/math/base";
6
+ import { MathVisualSchema, mathVisualLabelKeys } from "#contracts/math/visual";
7
+ /**
8
+ * One uploaded image. The text alternative is required, and the pixel size is
9
+ * required so a page can reserve the space before the file loads.
10
+ */
11
+ export const ImageSchema = Schema.Struct({
12
+ alt: PlainTextSchema,
13
+ asset: AssetIdSchema,
14
+ height: PositiveIntSchema,
15
+ width: PositiveIntSchema,
16
+ });
17
+ /** Three finite numbers, such as a camera position or an offset: x, y, and z. */
18
+ const TripleSchema = Schema.Tuple([
19
+ Schema.Finite,
20
+ Schema.Finite,
21
+ Schema.Finite,
22
+ ]);
23
+ /**
24
+ * The width of a line on screen. Zero is a width: the renderer draws no stroke,
25
+ * so a series with `showPoints` shows its points alone, as the answer to TKA
26
+ * compulsory mathematics set 3, question 8, shows its two fixed points.
27
+ */
28
+ const LineWidthSchema = Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0));
29
+ /** The header every figure card shows: a title and a description. */
30
+ const CardFields = {
31
+ description: RichTextSchema,
32
+ title: RichTextSchema,
33
+ };
34
+ /** Checks that the rich labels match the anchors of the scene, key for key. */
35
+ function hasSceneLabels(props) {
36
+ const anchors = mathVisualLabelKeys(props.scene);
37
+ const keys = Rec.keys(props.labels);
38
+ return (anchors.length === keys.length &&
39
+ Arr.every(anchors, (anchor) => Arr.contains(keys, anchor)));
40
+ }
41
+ /** Props of `MathVisual`: a plane or space scene and the rich labels it anchors. */
42
+ export const MathVisualPropsSchema = Schema.Struct({
43
+ ...CardFields,
44
+ labels: Schema.Record(MathVisualKeySchema, RichTextSchema),
45
+ scene: MathVisualSchema,
46
+ }).check(Schema.makeFilter(hasSceneLabels, {
47
+ message: "Expected one rich label for each label anchor of the scene.",
48
+ }));
49
+ /** A segment start: a number, or minus infinity, which JSON cannot hold. */
50
+ const SegmentStartSchema = Schema.Union([
51
+ Schema.Finite,
52
+ Schema.Literal("-infinity"),
53
+ ]);
54
+ /** A segment end: a number, or plus infinity, which JSON cannot hold. */
55
+ const SegmentEndSchema = Schema.Union([
56
+ Schema.Finite,
57
+ Schema.Literal("infinity"),
58
+ ]);
59
+ /** Checks that a segment that has two numbers does not end before it starts. */
60
+ function isOrderedSegment(segment) {
61
+ return (typeof segment.start !== "number" ||
62
+ typeof segment.end !== "number" ||
63
+ segment.start <= segment.end);
64
+ }
65
+ /**
66
+ * One segment of a number line. The corpus sets a background only to
67
+ * transparent, and an arbitrary color is not part of this contract, because no
68
+ * meaning may ride on color.
69
+ */
70
+ const NumberLineSegmentSchema = Schema.Struct({
71
+ backgroundColor: Schema.optionalKey(Schema.Literal("transparent")),
72
+ end: SegmentEndSchema,
73
+ endInclusive: Schema.optionalKey(Schema.Boolean),
74
+ endLabel: Schema.optionalKey(RichTextSchema),
75
+ label: Schema.optionalKey(RichTextSchema),
76
+ shaded: Schema.optionalKey(Schema.Boolean),
77
+ showPoints: Schema.optionalKey(Schema.Boolean),
78
+ start: SegmentStartSchema,
79
+ startInclusive: Schema.optionalKey(Schema.Boolean),
80
+ startLabel: Schema.optionalKey(RichTextSchema),
81
+ }).check(Schema.makeFilter(isOrderedSegment, {
82
+ message: "Expected a segment that does not end before it starts.",
83
+ }));
84
+ /** Checks that the visible range of a number line runs upward. */
85
+ function isOrderedRange(props) {
86
+ return (props.min === undefined || props.max === undefined || props.min < props.max);
87
+ }
88
+ /** Props of `NumberLine`: segments on a line, with an optional visible range. */
89
+ export const NumberLinePropsSchema = Schema.Struct({
90
+ ...CardFields,
91
+ max: Schema.optionalKey(Schema.Finite),
92
+ min: Schema.optionalKey(Schema.Finite),
93
+ segments: Schema.NonEmptyArray(NumberLineSegmentSchema),
94
+ }).check(Schema.makeFilter(isOrderedRange, {
95
+ message: "Expected a visible range whose minimum is below its maximum.",
96
+ }));
97
+ /** Whether the first or last point of a line belongs to it. */
98
+ const EndpointStateSchema = Schema.Literals(["open", "closed"]);
99
+ /** A label on a line: rich text at one of its points, nudged by an offset. */
100
+ const LineLabelSchema = Schema.Struct({
101
+ anchorX: Schema.optionalKey(Schema.Literals(["center", "left", "right"])),
102
+ at: Schema.optionalKey(NonNegativeIntSchema),
103
+ offset: Schema.optionalKey(TripleSchema),
104
+ text: RichTextSchema,
105
+ });
106
+ /** One line or curve through points, with the markers and labels it shows. */
107
+ const LineFieldsSchema = Schema.Struct({
108
+ color: PlainTextSchema,
109
+ cone: Schema.optionalKey(Schema.Struct({
110
+ position: Schema.Literals(["start", "end", "both"]),
111
+ size: Schema.optionalKey(PositiveMeasureSchema),
112
+ })),
113
+ endpoints: Schema.optionalKey(Schema.Struct({
114
+ end: Schema.optionalKey(EndpointStateSchema),
115
+ start: Schema.optionalKey(EndpointStateSchema),
116
+ })),
117
+ labels: Schema.optionalKey(Schema.NonEmptyArray(LineLabelSchema)),
118
+ lineWidth: Schema.optionalKey(LineWidthSchema),
119
+ pointIndices: Schema.optionalKey(Schema.NonEmptyArray(NonNegativeIntSchema)),
120
+ points: Schema.NonEmptyArray(SpacePointSchema),
121
+ showPoints: Schema.optionalKey(Schema.Boolean),
122
+ smooth: Schema.optionalKey(Schema.Boolean),
123
+ });
124
+ /** Checks that every index of a line names one of its points. */
125
+ function hasIndexedPoints(line) {
126
+ const count = line.points.length;
127
+ return (Arr.every(line.pointIndices ?? [], (index) => index < count) &&
128
+ Arr.every(line.labels ?? [], ({ at }) => at === undefined || at < count));
129
+ }
130
+ /** One line whose point indices and label places all name a point of it. */
131
+ const LineSchema = LineFieldsSchema.check(Schema.makeFilter(hasIndexedPoints, {
132
+ message: "Expected every point index to name a point of the line.",
133
+ }));
134
+ /**
135
+ * Where the `LineEquation` card puts the camera when `cameraPosition` is left
136
+ * out: `DEFAULT_CAMERA_POSITION_X`, `_Y`, and `_Z` in
137
+ * `components/contents/mathematics/line/equation.tsx` of Nakafa's design system
138
+ * (read on 13 October 2026).
139
+ */
140
+ const DEFAULT_CAMERA_POSITION = [10, 6, 10];
141
+ /**
142
+ * Where the scene looks when `cameraTarget` is left out: the origin, as
143
+ * `DEFAULT_CAMERA_TARGET` in `components/three/camera-controls.tsx` of the
144
+ * same package says (read on 13 October 2026).
145
+ */
146
+ const DEFAULT_CAMERA_TARGET = [0, 0, 0];
147
+ const LineEquationFieldsSchema = Schema.Struct({
148
+ ...CardFields,
149
+ cameraPosition: Schema.optionalKey(TripleSchema),
150
+ cameraTarget: Schema.optionalKey(TripleSchema),
151
+ data: Schema.NonEmptyArray(LineSchema),
152
+ showZAxis: Schema.optionalKey(Schema.Boolean),
153
+ });
154
+ /**
155
+ * Checks that the camera sits away from the point it looks at, with the
156
+ * renderer's defaults for a pose that is left out. A camera on its target has
157
+ * no direction to look in, and the renderer measures its zoom and its distance
158
+ * limits from that distance, which is then zero.
159
+ */
160
+ function hasCameraDistance({ cameraPosition = DEFAULT_CAMERA_POSITION, cameraTarget = DEFAULT_CAMERA_TARGET, }) {
161
+ const [x, y, z] = cameraPosition;
162
+ const [targetX, targetY, targetZ] = cameraTarget;
163
+ return x !== targetX || y !== targetY || z !== targetZ;
164
+ }
165
+ /**
166
+ * Props of `LineEquation`: lines in three dimensions, as the question bank uses
167
+ * them. Lessons also draw circle shapes (`kind` set to `circle-arc`,
168
+ * `circle-chord`, `circle-outline`, `circle-radius`, or `circle-segment`); no
169
+ * item uses one, so they are not part of this contract.
170
+ */
171
+ export const LineEquationPropsSchema = LineEquationFieldsSchema.check(Schema.makeFilter(hasCameraDistance, {
172
+ message: "Expected a camera that sits away from the point it looks at.",
173
+ }));
174
+ /** The exact sine, cosine, and tangent of the angle, each as LaTeX, such as `5/13`. */
175
+ const TrigValuesSchema = Schema.Struct({
176
+ cos: Schema.optionalKey(LatexSchema),
177
+ sin: Schema.optionalKey(LatexSchema),
178
+ tan: Schema.optionalKey(LatexSchema),
179
+ });
180
+ /** Props of `UnitCircle`: the angle in degrees and its exact values in LaTeX. */
181
+ export const UnitCirclePropsSchema = Schema.Struct({
182
+ ...CardFields,
183
+ angle: Schema.optionalKey(Schema.Finite),
184
+ trigValues: Schema.optionalKey(TrigValuesSchema),
185
+ });
186
+ /** Props of `Mermaid`: a diagram source with a plain title and description. */
187
+ export const MermaidPropsSchema = Schema.Struct({
188
+ chart: Schema.String.check(Schema.isNonEmpty()),
189
+ description: PlainTextSchema,
190
+ title: PlainTextSchema,
191
+ });
192
+ /**
193
+ * What every figure carries besides its props. The text alternative is
194
+ * required, because a figure must say in words what it shows, and the caption
195
+ * is rich text or null, as ProseMirror writes an unset attribute.
196
+ */
197
+ const FigureFields = {
198
+ alt: PlainTextSchema,
199
+ caption: Schema.NullOr(RichTextSchema),
200
+ };
201
+ /**
202
+ * The attributes of a figure node: one of the five renderer components and the
203
+ * typed props that component takes.
204
+ */
205
+ export const FigureAttrsSchema = Schema.Union([
206
+ Schema.Struct({
207
+ ...FigureFields,
208
+ component: Schema.Literal("MathVisual"),
209
+ props: MathVisualPropsSchema,
210
+ }),
211
+ Schema.Struct({
212
+ ...FigureFields,
213
+ component: Schema.Literal("NumberLine"),
214
+ props: NumberLinePropsSchema,
215
+ }),
216
+ Schema.Struct({
217
+ ...FigureFields,
218
+ component: Schema.Literal("LineEquation"),
219
+ props: LineEquationPropsSchema,
220
+ }),
221
+ Schema.Struct({
222
+ ...FigureFields,
223
+ component: Schema.Literal("Mermaid"),
224
+ props: MermaidPropsSchema,
225
+ }),
226
+ Schema.Struct({
227
+ ...FigureFields,
228
+ component: Schema.Literal("UnitCircle"),
229
+ props: UnitCirclePropsSchema,
230
+ }),
231
+ ]);
232
+ /** A figure block: an interactive visual with a required text alternative. */
233
+ export const FigureNodeSchema = Schema.Struct({
234
+ attrs: FigureAttrsSchema,
235
+ type: Schema.Literal("figure"),
236
+ });
@@ -0,0 +1,21 @@
1
+ import { type AssessmentDocument } from "#contracts/assessment/document";
2
+ import { type Interaction } from "#contracts/assessment/interaction";
3
+ import { type ScoringOverride } from "#contracts/assessment/scoring";
4
+ /**
5
+ * Checks that the interactions of a version and its prompt agree: each
6
+ * interaction has its own identifier and sits in the prompt as its kind
7
+ * allows, and the prompt places no interaction that the version does not have.
8
+ */
9
+ export declare function hasPlacedInteractions(version: {
10
+ readonly interactions: readonly Interaction[];
11
+ readonly prompt: AssessmentDocument;
12
+ }): boolean;
13
+ /**
14
+ * Checks the scoring rules that a version sets itself: each names one of its
15
+ * interactions once, and fits its kind. An extended text has no default, so it
16
+ * must carry its rubric.
17
+ */
18
+ export declare function hasFittingRules(version: {
19
+ readonly interactions: readonly Interaction[];
20
+ readonly scoring: readonly ScoringOverride[];
21
+ }): boolean;
@@ -0,0 +1,42 @@
1
+ import { Array as Arr, HashSet } from "effect";
2
+ import { documentPlacements, } from "#contracts/assessment/document";
3
+ import { hasDistinctIds, hasUniqueIds } from "#contracts/assessment/identity";
4
+ import { canSitInText, } from "#contracts/assessment/interaction";
5
+ import { supportsRule, } from "#contracts/assessment/scoring";
6
+ /**
7
+ * Checks that the prompt places one interaction where its kind belongs: a
8
+ * field exactly once, in text or as a block; a gap match once for each gap, in
9
+ * the text; every other kind exactly once, as a block.
10
+ */
11
+ function isPlaced(interaction, places) {
12
+ const own = Arr.filter(places, ({ attrs }) => attrs.interaction === interaction.id);
13
+ if (interaction.kind === "gapMatch") {
14
+ return (own.length === interaction.gaps.length &&
15
+ Arr.every(interaction.gaps, ({ id }) => Arr.some(own, (place) => place.type === "inlineInteraction" && place.attrs.gap === id)));
16
+ }
17
+ return (own.length === 1 &&
18
+ Arr.every(own, (place) => place.type === "interaction" ||
19
+ (canSitInText(interaction.kind) && place.attrs.gap === null)));
20
+ }
21
+ /**
22
+ * Checks that the interactions of a version and its prompt agree: each
23
+ * interaction has its own identifier and sits in the prompt as its kind
24
+ * allows, and the prompt places no interaction that the version does not have.
25
+ */
26
+ export function hasPlacedInteractions(version) {
27
+ const places = documentPlacements(version.prompt);
28
+ const known = HashSet.fromIterable(Arr.map(version.interactions, ({ id }) => id));
29
+ return (hasUniqueIds(version.interactions) &&
30
+ Arr.every(places, ({ attrs }) => HashSet.has(known, attrs.interaction)) &&
31
+ Arr.every(version.interactions, (interaction) => isPlaced(interaction, places)));
32
+ }
33
+ /**
34
+ * Checks the scoring rules that a version sets itself: each names one of its
35
+ * interactions once, and fits its kind. An extended text has no default, so it
36
+ * must carry its rubric.
37
+ */
38
+ export function hasFittingRules(version) {
39
+ return (hasDistinctIds(Arr.map(version.scoring, ({ interaction }) => interaction)) &&
40
+ Arr.every(version.scoring, ({ interaction, rule }) => Arr.some(version.interactions, (candidate) => candidate.id === interaction && supportsRule(candidate.kind, rule))) &&
41
+ Arr.every(Arr.filter(version.interactions, ({ kind }) => kind === "extendedText"), ({ id }) => Arr.some(version.scoring, ({ interaction, rule }) => interaction === id && rule.kind === "rubric")));
42
+ }
@@ -0,0 +1,95 @@
1
+ import { Schema } from "effect";
2
+ /** One selectable area, in one of the five shapes that QTI allows for a hotspot. */
3
+ export declare const HotspotAreaSchema: Schema.Union<readonly [Schema.Struct<{
4
+ readonly id: Schema.String;
5
+ readonly label: Schema.Trimmed;
6
+ readonly coords: Schema.Tuple<readonly [Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite]>;
7
+ readonly shape: Schema.Literal<"rect">;
8
+ }>, Schema.Struct<{
9
+ readonly id: Schema.String;
10
+ readonly label: Schema.Trimmed;
11
+ readonly coords: Schema.Tuple<readonly [Schema.Finite, Schema.Finite, Schema.Finite]>;
12
+ readonly shape: Schema.Literal<"circle">;
13
+ }>, Schema.Struct<{
14
+ readonly id: Schema.String;
15
+ readonly label: Schema.Trimmed;
16
+ readonly coords: Schema.Tuple<readonly [Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite]>;
17
+ readonly shape: Schema.Literal<"ellipse">;
18
+ }>, Schema.Struct<{
19
+ readonly id: Schema.String;
20
+ readonly label: Schema.Trimmed;
21
+ readonly coords: Schema.TupleWithRest<Schema.Tuple<readonly [Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite]>, readonly [Schema.Finite]>;
22
+ readonly shape: Schema.Literal<"poly">;
23
+ }>, Schema.Struct<{
24
+ readonly id: Schema.String;
25
+ readonly label: Schema.Trimmed;
26
+ readonly shape: Schema.Literal<"default">;
27
+ }>]>;
28
+ /**
29
+ * Select areas of an image. Maps to `qti-hotspot-interaction` with one
30
+ * `qti-hotspot-choice` (`shape`, `coords`) for each area.
31
+ */
32
+ export declare const HotspotInteractionSchema: Schema.Struct<{
33
+ readonly id: Schema.String;
34
+ readonly label: Schema.Trimmed;
35
+ readonly areas: Schema.$Array<Schema.Union<readonly [Schema.Struct<{
36
+ readonly id: Schema.String;
37
+ readonly label: Schema.Trimmed;
38
+ readonly coords: Schema.Tuple<readonly [Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite]>;
39
+ readonly shape: Schema.Literal<"rect">;
40
+ }>, Schema.Struct<{
41
+ readonly id: Schema.String;
42
+ readonly label: Schema.Trimmed;
43
+ readonly coords: Schema.Tuple<readonly [Schema.Finite, Schema.Finite, Schema.Finite]>;
44
+ readonly shape: Schema.Literal<"circle">;
45
+ }>, Schema.Struct<{
46
+ readonly id: Schema.String;
47
+ readonly label: Schema.Trimmed;
48
+ readonly coords: Schema.Tuple<readonly [Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite]>;
49
+ readonly shape: Schema.Literal<"ellipse">;
50
+ }>, Schema.Struct<{
51
+ readonly id: Schema.String;
52
+ readonly label: Schema.Trimmed;
53
+ readonly coords: Schema.TupleWithRest<Schema.Tuple<readonly [Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite, Schema.Finite]>, readonly [Schema.Finite]>;
54
+ readonly shape: Schema.Literal<"poly">;
55
+ }>, Schema.Struct<{
56
+ readonly id: Schema.String;
57
+ readonly label: Schema.Trimmed;
58
+ readonly shape: Schema.Literal<"default">;
59
+ }>]>>;
60
+ readonly image: Schema.Struct<{
61
+ readonly alt: Schema.Trimmed;
62
+ readonly asset: Schema.String;
63
+ readonly height: Schema.Int;
64
+ readonly width: Schema.Int;
65
+ }>;
66
+ readonly kind: Schema.Literal<"hotspot">;
67
+ readonly maxChoices: Schema.Int;
68
+ readonly minChoices: Schema.Int;
69
+ }>;
70
+ /**
71
+ * Place points, or draw lines through two points, on a plane of Cartesian
72
+ * axes. `count` is how many points or lines the learner places, and `snap`,
73
+ * when present, is the step that the plane rounds a position to. Maps to
74
+ * `qti-select-point-interaction` over the rendered plane for points, and to
75
+ * `qti-portable-custom-interaction` for lines.
76
+ */
77
+ export declare const GraphPointInteractionSchema: Schema.Struct<{
78
+ readonly id: Schema.String;
79
+ readonly label: Schema.Trimmed;
80
+ readonly count: Schema.Int;
81
+ readonly frame: Schema.Struct<{
82
+ readonly kind: Schema.Literal<"cartesian">;
83
+ readonly x: Schema.Struct<{
84
+ readonly max: Schema.Finite;
85
+ readonly min: Schema.Finite;
86
+ }>;
87
+ readonly y: Schema.Struct<{
88
+ readonly max: Schema.Finite;
89
+ readonly min: Schema.Finite;
90
+ }>;
91
+ }>;
92
+ readonly kind: Schema.Literal<"graphPoint">;
93
+ readonly placing: Schema.Literals<readonly ["points", "lines"]>;
94
+ readonly snap: Schema.optionalKey<Schema.Finite>;
95
+ }>;
@@ -0,0 +1,106 @@
1
+ import { Schema } from "effect";
2
+ import { ImageSchema } from "#contracts/assessment/figure";
3
+ import { hasUniqueIds, LocalIdSchema } from "#contracts/assessment/identity";
4
+ import { hasSelectionBounds, InteractionFields, } from "#contracts/assessment/part";
5
+ import { NonNegativeIntSchema, PlainTextSchema, PositiveIntSchema, } from "#contracts/assessment/scalar";
6
+ import { PositiveMeasureSchema } from "#contracts/math/base";
7
+ import { PlaneMathFrameSchema } from "#contracts/math/plane";
8
+ /** A pixel position or length in the image's own space. */
9
+ const CoordinateSchema = Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0));
10
+ /**
11
+ * What every area has: an identifier, and a text label so that no area is told
12
+ * apart by its looks alone.
13
+ */
14
+ const AreaFields = {
15
+ id: LocalIdSchema,
16
+ label: PlainTextSchema,
17
+ };
18
+ /** A rectangle by its left, top, right, and bottom edges. */
19
+ const RectAreaSchema = Schema.Struct({
20
+ ...AreaFields,
21
+ coords: Schema.Tuple([
22
+ CoordinateSchema,
23
+ CoordinateSchema,
24
+ CoordinateSchema,
25
+ CoordinateSchema,
26
+ ]),
27
+ shape: Schema.Literal("rect"),
28
+ }).check(Schema.makeFilter(({ coords: [left, top, right, bottom] }) => left < right && top < bottom, { message: "Expected a rectangle with its left and top edges first." }));
29
+ /** A circle by its center and radius. */
30
+ const CircleAreaSchema = Schema.Struct({
31
+ ...AreaFields,
32
+ coords: Schema.Tuple([CoordinateSchema, CoordinateSchema, CoordinateSchema]),
33
+ shape: Schema.Literal("circle"),
34
+ }).check(Schema.makeFilter(({ coords: [, , radius] }) => radius > 0, {
35
+ message: "Expected a circle with a radius above zero.",
36
+ }));
37
+ /** An ellipse by its center and its horizontal and vertical radius. */
38
+ const EllipseAreaSchema = Schema.Struct({
39
+ ...AreaFields,
40
+ coords: Schema.Tuple([
41
+ CoordinateSchema,
42
+ CoordinateSchema,
43
+ CoordinateSchema,
44
+ CoordinateSchema,
45
+ ]),
46
+ shape: Schema.Literal("ellipse"),
47
+ }).check(Schema.makeFilter(({ coords: [, , horizontal, vertical] }) => horizontal > 0 && vertical > 0, { message: "Expected an ellipse with radii above zero." }));
48
+ /** A polygon by at least three corners, each an x and a y. */
49
+ const PolyAreaSchema = Schema.Struct({
50
+ ...AreaFields,
51
+ coords: Schema.TupleWithRest(Schema.Tuple([
52
+ CoordinateSchema,
53
+ CoordinateSchema,
54
+ CoordinateSchema,
55
+ CoordinateSchema,
56
+ CoordinateSchema,
57
+ CoordinateSchema,
58
+ ]), [CoordinateSchema]),
59
+ shape: Schema.Literal("poly"),
60
+ }).check(Schema.makeFilter(({ coords }) => coords.length % 2 === 0, {
61
+ message: "Expected a polygon whose corners each have an x and a y.",
62
+ }));
63
+ /** The whole image, which needs no coordinates. */
64
+ const WholeAreaSchema = Schema.Struct({
65
+ ...AreaFields,
66
+ shape: Schema.Literal("default"),
67
+ });
68
+ /** One selectable area, in one of the five shapes that QTI allows for a hotspot. */
69
+ export const HotspotAreaSchema = Schema.Union([
70
+ RectAreaSchema,
71
+ CircleAreaSchema,
72
+ EllipseAreaSchema,
73
+ PolyAreaSchema,
74
+ WholeAreaSchema,
75
+ ]);
76
+ /**
77
+ * Select areas of an image. Maps to `qti-hotspot-interaction` with one
78
+ * `qti-hotspot-choice` (`shape`, `coords`) for each area.
79
+ */
80
+ export const HotspotInteractionSchema = Schema.Struct({
81
+ ...InteractionFields,
82
+ areas: Schema.Array(HotspotAreaSchema).check(Schema.isMinLength(2)),
83
+ image: ImageSchema,
84
+ kind: Schema.Literal("hotspot"),
85
+ maxChoices: PositiveIntSchema,
86
+ minChoices: NonNegativeIntSchema,
87
+ }).check(Schema.makeFilter(({ areas }) => hasUniqueIds(areas), {
88
+ message: "Expected each area to have its own identifier.",
89
+ }), Schema.makeFilter((hotspot) => hasSelectionBounds(hotspot, hotspot.areas.length), {
90
+ message: "Expected the fewest areas to be at most the most areas, and the most areas to be at most the number of areas.",
91
+ }));
92
+ /**
93
+ * Place points, or draw lines through two points, on a plane of Cartesian
94
+ * axes. `count` is how many points or lines the learner places, and `snap`,
95
+ * when present, is the step that the plane rounds a position to. Maps to
96
+ * `qti-select-point-interaction` over the rendered plane for points, and to
97
+ * `qti-portable-custom-interaction` for lines.
98
+ */
99
+ export const GraphPointInteractionSchema = Schema.Struct({
100
+ ...InteractionFields,
101
+ count: PositiveIntSchema,
102
+ frame: PlaneMathFrameSchema,
103
+ kind: Schema.Literal("graphPoint"),
104
+ placing: Schema.Literals(["points", "lines"]),
105
+ snap: Schema.optionalKey(PositiveMeasureSchema),
106
+ });