@nakafa/aksara-contracts 1.0.0 → 2.0.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 (166) hide show
  1. package/README.md +15 -32
  2. package/dist/{question → assessment}/answer.d.ts +14 -58
  3. package/dist/{question → assessment}/answer.js +13 -60
  4. package/dist/assessment/catalog.d.ts +235 -0
  5. package/dist/assessment/catalog.js +115 -0
  6. package/dist/assessment/chart.d.ts +62 -0
  7. package/dist/assessment/chart.js +64 -0
  8. package/dist/assessment/closure.d.ts +28 -0
  9. package/dist/assessment/closure.js +164 -0
  10. package/dist/assessment/explanation.d.ts +13 -8
  11. package/dist/assessment/explanation.js +14 -8
  12. package/dist/assessment/figure.d.ts +140 -1
  13. package/dist/assessment/figure.js +9 -2
  14. package/dist/assessment/hash.d.ts +348 -208
  15. package/dist/assessment/hash.js +14 -0
  16. package/dist/assessment/inline.d.ts +3 -1
  17. package/dist/assessment/inline.js +20 -5
  18. package/dist/assessment/item.d.ts +8 -4
  19. package/dist/assessment/item.js +6 -2
  20. package/dist/assessment/key.js +6 -6
  21. package/dist/assessment/paper.d.ts +74 -74
  22. package/dist/assessment/snapshot.d.ts +3778 -0
  23. package/dist/assessment/snapshot.js +192 -0
  24. package/dist/cache/content.d.ts +4 -4
  25. package/dist/canonical.d.ts +6 -0
  26. package/dist/canonical.js +8 -1
  27. package/dist/content.d.ts +1 -1
  28. package/dist/content.js +0 -1
  29. package/dist/delivery.d.ts +0 -3
  30. package/dist/delivery.js +0 -5
  31. package/dist/preview/artifact.d.ts +1 -120
  32. package/dist/preview/document.d.ts +0 -586
  33. package/dist/preview/document.js +1 -47
  34. package/dist/preview/spec.d.ts +6 -1984
  35. package/dist/preview/spec.js +7 -47
  36. package/dist/preview/target.d.ts +0 -173
  37. package/dist/preview/target.js +1 -70
  38. package/dist/projection/digest.d.ts +0 -127
  39. package/dist/projection/hash.d.ts +0 -127
  40. package/dist/projection/spec.d.ts +6 -211
  41. package/dist/projection/spec.js +4 -18
  42. package/dist/projection/verify.d.ts +0 -127
  43. package/dist/projection/verify.js +0 -3
  44. package/dist/release/canonical.d.ts +2 -2
  45. package/dist/release/current/evidence.d.ts +24 -24
  46. package/dist/release/current/state.d.ts +28 -56
  47. package/dist/release/current/state.js +1 -6
  48. package/dist/release/digest.d.ts +2 -2
  49. package/dist/release/hash.d.ts +5 -5
  50. package/dist/release/head.d.ts +10 -57
  51. package/dist/release/head.js +3 -14
  52. package/dist/release/identity.d.ts +3 -3
  53. package/dist/release/items.d.ts +2 -2
  54. package/dist/release/lifecycle.d.ts +18 -18
  55. package/dist/release/policy.d.ts +3 -4
  56. package/dist/release/policy.js +2 -11
  57. package/dist/release/result/digest.d.ts +6 -18
  58. package/dist/release/result/digest.js +1 -2
  59. package/dist/release/rollback/digest.d.ts +4 -19
  60. package/dist/release/rollback/spec.d.ts +30 -792
  61. package/dist/release/rollback/spec.js +1 -4
  62. package/dist/release/snapshot/data.d.ts +599 -249
  63. package/dist/release/snapshot/data.js +11 -28
  64. package/dist/release/snapshot/evidence/assessment.d.ts +26 -0
  65. package/dist/release/snapshot/evidence/assessment.js +44 -0
  66. package/dist/release/snapshot/evidence/requirement.d.ts +1 -1
  67. package/dist/release/snapshot/evidence.d.ts +19 -19
  68. package/dist/release/snapshot/evidence.js +2 -2
  69. package/dist/release/snapshot/scope.d.ts +5 -5
  70. package/dist/release/snapshot/scope.js +1 -1
  71. package/dist/release/snapshot/spec.d.ts +15 -15
  72. package/dist/release/snapshot/spec.js +8 -8
  73. package/dist/release/snapshot/verify.d.ts +1278 -268
  74. package/dist/release/snapshot/verify.js +8 -5
  75. package/dist/release/spec.d.ts +33 -33
  76. package/dist/release/verify.d.ts +5 -5
  77. package/dist/runtime/spec.d.ts +38 -38
  78. package/dist/runtime/spec.js +4 -5
  79. package/dist/runtime/verify.d.ts +40 -40
  80. package/dist/signature/spec.d.ts +5 -5
  81. package/dist/signature/spec.js +0 -1
  82. package/dist/signature/trusted.d.ts +4 -1
  83. package/dist/signature/trusted.js +4 -1
  84. package/dist/transport/batch.d.ts +6 -244
  85. package/dist/transport/failure.d.ts +6 -20
  86. package/dist/transport/failure.js +0 -10
  87. package/dist/transport/group.d.ts +1969 -1369
  88. package/dist/transport/group.js +0 -2
  89. package/dist/transport/request.d.ts +10691 -5086
  90. package/dist/transport/request.js +0 -3
  91. package/dist/transport/response.d.ts +219 -1442
  92. package/dist/transport/response.js +0 -4
  93. package/dist/transport/snapshot.d.ts +1187 -487
  94. package/package.json +187 -295
  95. package/dist/projection/question.d.ts +0 -1067
  96. package/dist/projection/question.js +0 -121
  97. package/dist/question/identity.d.ts +0 -175
  98. package/dist/question/identity.js +0 -207
  99. package/dist/question/item.d.ts +0 -450
  100. package/dist/question/item.js +0 -174
  101. package/dist/question/label.d.ts +0 -3
  102. package/dist/question/label.js +0 -3
  103. package/dist/question/points.d.ts +0 -18
  104. package/dist/question/points.js +0 -22
  105. package/dist/question/response.d.ts +0 -289
  106. package/dist/question/response.js +0 -167
  107. package/dist/question/rubric.d.ts +0 -234
  108. package/dist/question/rubric.js +0 -186
  109. package/dist/release/current/runtime.d.ts +0 -4
  110. package/dist/release/current/runtime.js +0 -15
  111. package/dist/release/snapshot/evidence/tryout.d.ts +0 -22
  112. package/dist/release/snapshot/evidence/tryout.js +0 -91
  113. package/dist/runtime/protected/limits.d.ts +0 -10
  114. package/dist/runtime/protected/limits.js +0 -16
  115. package/dist/runtime/protected/spec.d.ts +0 -310
  116. package/dist/runtime/protected/spec.js +0 -80
  117. package/dist/runtime/protected/verify.d.ts +0 -83
  118. package/dist/runtime/protected/verify.js +0 -70
  119. package/dist/transport/runtime.d.ts +0 -77
  120. package/dist/transport/runtime.js +0 -33
  121. package/dist/tryout/canonical.d.ts +0 -12
  122. package/dist/tryout/canonical.js +0 -7
  123. package/dist/tryout/catalog.d.ts +0 -396
  124. package/dist/tryout/catalog.js +0 -155
  125. package/dist/tryout/closure/accounting.d.ts +0 -20
  126. package/dist/tryout/closure/accounting.js +0 -70
  127. package/dist/tryout/closure/locale.d.ts +0 -14
  128. package/dist/tryout/closure/locale.js +0 -113
  129. package/dist/tryout/closure/placement.d.ts +0 -5
  130. package/dist/tryout/closure/placement.js +0 -32
  131. package/dist/tryout/digest.d.ts +0 -34
  132. package/dist/tryout/digest.js +0 -57
  133. package/dist/tryout/hash/catalog.d.ts +0 -364
  134. package/dist/tryout/hash/catalog.js +0 -209
  135. package/dist/tryout/hash/content.d.ts +0 -215
  136. package/dist/tryout/hash/content.js +0 -75
  137. package/dist/tryout/hash/placement.d.ts +0 -242
  138. package/dist/tryout/hash/placement.js +0 -56
  139. package/dist/tryout/identity.d.ts +0 -12
  140. package/dist/tryout/identity.js +0 -47
  141. package/dist/tryout/key.d.ts +0 -4
  142. package/dist/tryout/key.js +0 -8
  143. package/dist/tryout/language.d.ts +0 -24
  144. package/dist/tryout/language.js +0 -30
  145. package/dist/tryout/placement.d.ts +0 -331
  146. package/dist/tryout/placement.js +0 -96
  147. package/dist/tryout/runtime/canonical.d.ts +0 -8
  148. package/dist/tryout/runtime/canonical.js +0 -34
  149. package/dist/tryout/runtime/hash.d.ts +0 -34
  150. package/dist/tryout/runtime/hash.js +0 -13
  151. package/dist/tryout/runtime/source.d.ts +0 -44
  152. package/dist/tryout/runtime/source.js +0 -47
  153. package/dist/tryout/runtime/spec.d.ts +0 -58
  154. package/dist/tryout/runtime/spec.js +0 -21
  155. package/dist/tryout/runtime/verify.d.ts +0 -69
  156. package/dist/tryout/runtime/verify.js +0 -67
  157. package/dist/tryout/scoring.d.ts +0 -148
  158. package/dist/tryout/scoring.js +0 -98
  159. package/dist/tryout/snapshot/canonical.d.ts +0 -7
  160. package/dist/tryout/snapshot/canonical.js +0 -18
  161. package/dist/tryout/snapshot/hash.d.ts +0 -49
  162. package/dist/tryout/snapshot/hash.js +0 -22
  163. package/dist/tryout/snapshot/spec.d.ts +0 -46
  164. package/dist/tryout/snapshot/spec.js +0 -30
  165. package/dist/tryout/spec.d.ts +0 -41
  166. package/dist/tryout/spec.js +0 -46
@@ -0,0 +1,115 @@
1
+ import { Array as Arr, Schema } from "effect";
2
+ import { KebabKeySchema, PaperIdSchema } from "#contracts/assessment/identity";
3
+ import { CountryCodeSchema } from "#contracts/country";
4
+ import { LearningGraphIdentitySchema } from "#contracts/graph/spec";
5
+ import { PublicPathSchema, Sha256HashSchema } from "#contracts/ids";
6
+ import { AppLocaleSchema } from "#contracts/locale";
7
+ const PositiveCountSchema = Schema.Int.check(Schema.isGreaterThan(0));
8
+ /** Bounded authored revision shared by one catalog source hierarchy. */
9
+ export const CatalogRevisionSchema = Schema.Trimmed.check(Schema.isNonEmpty(), Schema.isMaxLength(128));
10
+ /**
11
+ * Navigation role of one track: one exam `year`, one `subject`, or the sets of
12
+ * one `institution`, such as a Studienkolleg.
13
+ */
14
+ export const TrackKindSchema = Schema.Literals([
15
+ "institution",
16
+ "subject",
17
+ "year",
18
+ ]);
19
+ /**
20
+ * Public route behavior of one section: `visible` sections have a page of
21
+ * their own, and an `internal-entry` section is entered from its set.
22
+ */
23
+ export const SectionVisibilitySchema = Schema.Literals([
24
+ "internal-entry",
25
+ "visible",
26
+ ]);
27
+ /** What every row of the catalog says in one app locale. */
28
+ const LocalizedFields = {
29
+ appLocale: AppLocaleSchema,
30
+ description: Schema.optionalKey(Schema.String),
31
+ graph: LearningGraphIdentitySchema,
32
+ sourceRevision: CatalogRevisionSchema,
33
+ title: Schema.String,
34
+ };
35
+ const CountryFields = { countryKey: KebabKeySchema };
36
+ const ExamFields = { ...CountryFields, examKey: KebabKeySchema };
37
+ const TrackFields = { ...ExamFields, trackKey: KebabKeySchema };
38
+ const SetFields = { ...TrackFields, setKey: KebabKeySchema };
39
+ /** One localized country of the catalog. */
40
+ export const CatalogCountrySchema = Schema.Struct({
41
+ ...LocalizedFields,
42
+ ...CountryFields,
43
+ countryCode: CountryCodeSchema,
44
+ kind: Schema.Literal("country"),
45
+ order: PositiveCountSchema,
46
+ publicPath: PublicPathSchema,
47
+ });
48
+ /** One localized exam of a country. */
49
+ export const CatalogExamSchema = Schema.Struct({
50
+ ...LocalizedFields,
51
+ ...ExamFields,
52
+ kind: Schema.Literal("exam"),
53
+ order: PositiveCountSchema,
54
+ publicPath: PublicPathSchema,
55
+ });
56
+ /** One localized track of an exam: a year, a subject, or an institution. */
57
+ export const CatalogTrackSchema = Schema.Struct({
58
+ ...LocalizedFields,
59
+ ...TrackFields,
60
+ kind: Schema.Literal("track"),
61
+ order: PositiveCountSchema,
62
+ publicPath: PublicPathSchema,
63
+ trackKind: TrackKindSchema,
64
+ });
65
+ /**
66
+ * One localized set of a track, and the paper a learner sits for it, by
67
+ * identifier and hash. A set whose only section is entered from the set names
68
+ * that section.
69
+ */
70
+ export const CatalogSetSchema = Schema.Struct({
71
+ ...LocalizedFields,
72
+ ...SetFields,
73
+ internalEntrySectionKey: Schema.optionalKey(KebabKeySchema),
74
+ kind: Schema.Literal("set"),
75
+ order: PositiveCountSchema,
76
+ paper: Schema.Struct({ hash: Sha256HashSchema, id: PaperIdSchema }),
77
+ publicPath: PublicPathSchema,
78
+ });
79
+ /**
80
+ * One localized section of a set. Its key is the identifier of a section of
81
+ * the exam profile that the set's paper follows, so the profile owns its time,
82
+ * its items, and its scoring, and the paper owns its places.
83
+ */
84
+ export const CatalogSectionSchema = Schema.Struct({
85
+ ...LocalizedFields,
86
+ ...SetFields,
87
+ kind: Schema.Literal("section"),
88
+ order: PositiveCountSchema,
89
+ publicPath: Schema.optionalKey(PublicPathSchema),
90
+ sectionKey: KebabKeySchema,
91
+ visibility: SectionVisibilitySchema,
92
+ }).check(Schema.makeFilter(({ publicPath, visibility }) => (visibility === "visible") === (publicPath !== undefined), { message: "Expected a public path on a visible section only." }));
93
+ /** One row of the catalog, the navigation of the public try-outs. */
94
+ export const CatalogRowSchema = Schema.Union([
95
+ CatalogCountrySchema,
96
+ CatalogExamSchema,
97
+ CatalogTrackSchema,
98
+ CatalogSetSchema,
99
+ CatalogSectionSchema,
100
+ ]);
101
+ /**
102
+ * The identity of one catalog row, unique within a snapshot: its locale, its
103
+ * kind, and the keys of its node and of every node above it.
104
+ */
105
+ export function catalogRowIdentity(row) {
106
+ return Arr.join([
107
+ row.appLocale,
108
+ row.kind,
109
+ row.countryKey,
110
+ "examKey" in row ? row.examKey : "",
111
+ "trackKey" in row ? row.trackKey : "",
112
+ "setKey" in row ? row.setKey : "",
113
+ "sectionKey" in row ? row.sectionKey : "",
114
+ ], "\0");
115
+ }
@@ -0,0 +1,62 @@
1
+ import { Schema } from "effect";
2
+ /**
3
+ * Props of `Chart`: categories along one axis and one or more series of
4
+ * values drawn as bars or lines over them, the data of a bar chart, a line
5
+ * chart, a histogram, or both marks at once. The data is the figure, so a
6
+ * learner reads the same values that the figure draws. Every series has a
7
+ * name, because no meaning may ride on color alone.
8
+ */
9
+ export declare const ChartPropsSchema: Schema.Struct<{
10
+ /**
11
+ * How the bars of a chart that has bars stand: `apart` leaves a gap between
12
+ * neighboring categories, as a bar chart does, and `touching` closes it, as a
13
+ * histogram of classes does. A chart of lines alone has no bars to place.
14
+ */
15
+ readonly bars: Schema.optionalKey<Schema.Literals<readonly ["apart", "touching"]>>;
16
+ readonly categories: Schema.NonEmptyArray<Schema.Trimmed>;
17
+ readonly description: Schema.NonEmptyArray<Schema.Union<readonly [Schema.Struct<{
18
+ readonly marks: Schema.optionalKey<Schema.NonEmptyArray<Schema.Union<readonly [Schema.Struct<{
19
+ readonly type: Schema.Literals<readonly ["bold", "italic", "underline", "code", "subscript", "superscript"]>;
20
+ }>, Schema.Struct<{
21
+ readonly attrs: Schema.Struct<{
22
+ readonly href: Schema.String;
23
+ }>;
24
+ readonly type: Schema.Literal<"link">;
25
+ }>]>>>;
26
+ readonly text: Schema.String;
27
+ readonly type: Schema.Literal<"text">;
28
+ }>, Schema.Struct<{
29
+ readonly attrs: Schema.Struct<{
30
+ readonly latex: Schema.Trimmed;
31
+ }>;
32
+ readonly type: Schema.Literal<"mathInline">;
33
+ }>]>>;
34
+ readonly series: Schema.NonEmptyArray<Schema.Struct<{
35
+ readonly label: Schema.Trimmed;
36
+ readonly mark: Schema.Literals<readonly ["bar", "line"]>;
37
+ readonly values: Schema.NonEmptyArray<Schema.NullOr<Schema.Finite>>;
38
+ }>>;
39
+ readonly title: Schema.NonEmptyArray<Schema.Union<readonly [Schema.Struct<{
40
+ readonly marks: Schema.optionalKey<Schema.NonEmptyArray<Schema.Union<readonly [Schema.Struct<{
41
+ readonly type: Schema.Literals<readonly ["bold", "italic", "underline", "code", "subscript", "superscript"]>;
42
+ }>, Schema.Struct<{
43
+ readonly attrs: Schema.Struct<{
44
+ readonly href: Schema.String;
45
+ }>;
46
+ readonly type: Schema.Literal<"link">;
47
+ }>]>>>;
48
+ readonly text: Schema.String;
49
+ readonly type: Schema.Literal<"text">;
50
+ }>, Schema.Struct<{
51
+ readonly attrs: Schema.Struct<{
52
+ readonly latex: Schema.Trimmed;
53
+ }>;
54
+ readonly type: Schema.Literal<"mathInline">;
55
+ }>]>>;
56
+ readonly valueLabel: Schema.optionalKey<Schema.Trimmed>;
57
+ readonly valueRange: Schema.optionalKey<Schema.Struct<{
58
+ readonly max: Schema.Finite;
59
+ readonly min: Schema.Finite;
60
+ }>>;
61
+ }>;
62
+ export type ChartProps = typeof ChartPropsSchema.Type;
@@ -0,0 +1,64 @@
1
+ import { Array as Arr, Schema } from "effect";
2
+ import { hasDistinctIds } from "#contracts/assessment/identity";
3
+ import { RichTextSchema } from "#contracts/assessment/inline";
4
+ import { PlainTextSchema } from "#contracts/assessment/scalar";
5
+ /**
6
+ * One series of a chart: its name, whether it is drawn as bars or as a line,
7
+ * and one value for each category. A value is null where the series has no
8
+ * value for that category, as a line of yearly growth that bars mark only in
9
+ * some years.
10
+ */
11
+ const ChartSeriesSchema = Schema.Struct({
12
+ label: PlainTextSchema,
13
+ mark: Schema.Literals(["bar", "line"]),
14
+ values: Schema.NonEmptyArray(Schema.NullOr(Schema.Finite)),
15
+ });
16
+ /** The part of the value axis that a chart shows, from its minimum to its maximum. */
17
+ const ValueRangeSchema = Schema.Struct({
18
+ max: Schema.Finite,
19
+ min: Schema.Finite,
20
+ }).check(Schema.makeFilter(({ max, min }) => min < max, {
21
+ message: "Expected a value range whose minimum is below its maximum.",
22
+ }));
23
+ const ChartFieldsSchema = Schema.Struct({
24
+ /**
25
+ * How the bars of a chart that has bars stand: `apart` leaves a gap between
26
+ * neighboring categories, as a bar chart does, and `touching` closes it, as a
27
+ * histogram of classes does. A chart of lines alone has no bars to place.
28
+ */
29
+ bars: Schema.optionalKey(Schema.Literals(["apart", "touching"])),
30
+ categories: Schema.NonEmptyArray(PlainTextSchema),
31
+ description: RichTextSchema,
32
+ series: Schema.NonEmptyArray(ChartSeriesSchema),
33
+ title: RichTextSchema,
34
+ valueLabel: Schema.optionalKey(PlainTextSchema),
35
+ valueRange: Schema.optionalKey(ValueRangeSchema),
36
+ });
37
+ /** Checks that every series has one value for each category, and at least one that is not null. */
38
+ function hasValuePerCategory(chart) {
39
+ return Arr.every(chart.series, ({ values }) => values.length === chart.categories.length &&
40
+ Arr.some(values, (value) => value !== null));
41
+ }
42
+ /** Checks that no two series share a name, so a legend tells them apart in words. */
43
+ function hasDistinctSeries(chart) {
44
+ return hasDistinctIds(Arr.map(chart.series, ({ label }) => label));
45
+ }
46
+ /** Checks that a chart says how its bars stand exactly when it has bars. */
47
+ function hasBarPlacement(chart) {
48
+ const hasBars = Arr.some(chart.series, ({ mark }) => mark === "bar");
49
+ return hasBars === (chart.bars !== undefined);
50
+ }
51
+ /**
52
+ * Props of `Chart`: categories along one axis and one or more series of
53
+ * values drawn as bars or lines over them, the data of a bar chart, a line
54
+ * chart, a histogram, or both marks at once. The data is the figure, so a
55
+ * learner reads the same values that the figure draws. Every series has a
56
+ * name, because no meaning may ride on color alone.
57
+ */
58
+ export const ChartPropsSchema = ChartFieldsSchema.check(Schema.makeFilter(hasValuePerCategory, {
59
+ message: "Expected each series to have one value for each category, and at least one value.",
60
+ }), Schema.makeFilter(hasDistinctSeries, {
61
+ message: "Expected each series to have a name of its own.",
62
+ }), Schema.makeFilter(hasBarPlacement, {
63
+ message: "Expected a bar placement on a chart with bars, and on no other.",
64
+ }));
@@ -0,0 +1,28 @@
1
+ import { Effect, Schema, Stream } from "effect";
2
+ import type { AssessmentRecord } from "#contracts/assessment/snapshot";
3
+ import type { ActiveAppLocaleList } from "#contracts/locale";
4
+ /**
5
+ * Why a snapshot is not closed: a reference names a row it does not hold, a
6
+ * row is never reached, or a paper does not fit the profile it follows.
7
+ */
8
+ export declare const AssessmentClosureReasonSchema: Schema.Literals<readonly ["explanation", "family", "key", "locale", "paper", "place", "profile", "section", "stimulus", "unplaced"]>;
9
+ declare const AssessmentClosureError_base: Schema.Class<AssessmentClosureError, Schema.TaggedStruct<"AssessmentClosureError", {
10
+ readonly identity: Schema.String;
11
+ readonly reason: Schema.Literals<readonly ["explanation", "family", "key", "locale", "paper", "place", "profile", "section", "stimulus", "unplaced"]>;
12
+ }>, import("effect/Cause").YieldableError & {
13
+ readonly brand: unique symbol;
14
+ }>;
15
+ /** A snapshot holds a reference that leads nowhere, or a row that nothing reaches. */
16
+ export declare class AssessmentClosureError extends AssessmentClosureError_base {
17
+ }
18
+ /**
19
+ * Verifies that an assessment snapshot is closed: the catalog reaches papers it
20
+ * holds, every paper fits the profile it follows and places versions it holds,
21
+ * every version has its family, its reading, its key, and an explanation in
22
+ * every active app locale, and no family, key, or explanation is left over.
23
+ */
24
+ export declare const verifyAssessmentClosure: <E, R>(input: {
25
+ readonly activeAppLocales: ActiveAppLocaleList;
26
+ readonly records: Stream.Stream<AssessmentRecord, E, R>;
27
+ }) => Effect.Effect<void, E | AssessmentClosureError, R>;
28
+ export {};
@@ -0,0 +1,164 @@
1
+ import { Array as Arr, Effect, HashMap, HashSet, Option, Result, Schema, Stream, } from "effect";
2
+ import { catalogRowIdentity } from "#contracts/assessment/catalog";
3
+ import { verifyPaperStimuli } from "#contracts/assessment/paper";
4
+ /**
5
+ * Why a snapshot is not closed: a reference names a row it does not hold, a
6
+ * row is never reached, or a paper does not fit the profile it follows.
7
+ */
8
+ export const AssessmentClosureReasonSchema = Schema.Literals([
9
+ "explanation",
10
+ "family",
11
+ "key",
12
+ "locale",
13
+ "paper",
14
+ "place",
15
+ "profile",
16
+ "section",
17
+ "stimulus",
18
+ "unplaced",
19
+ ]);
20
+ /** A snapshot holds a reference that leads nowhere, or a row that nothing reaches. */
21
+ export class AssessmentClosureError extends Schema.TaggedError()("AssessmentClosureError", {
22
+ identity: Schema.String,
23
+ reason: AssessmentClosureReasonSchema,
24
+ }) {
25
+ }
26
+ /** Gathers the rows of a snapshot by kind for the reference checks. */
27
+ function indexRows(records) {
28
+ const rows = Arr.map(records, ({ row }) => row);
29
+ return {
30
+ catalog: Arr.filterMap(rows, (row) => row.kind === "catalog" ? Result.succeed(row.row) : Result.failVoid),
31
+ explanations: HashMap.fromIterable(Arr.filterMap(rows, (row) => row.kind === "explanation"
32
+ ? Result.succeed([
33
+ explanationIdentity(row.explanation.version, row.explanation.language),
34
+ row.explanation.version,
35
+ ])
36
+ : Result.failVoid)),
37
+ families: HashMap.fromIterable(Arr.filterMap(rows, (row) => row.kind === "family"
38
+ ? Result.succeed([row.family.id, row.family])
39
+ : Result.failVoid)),
40
+ items: HashMap.fromIterable(Arr.filterMap(rows, (row) => row.kind === "item"
41
+ ? Result.succeed([row.version.hash, row.version])
42
+ : Result.failVoid)),
43
+ keys: HashSet.fromIterable(Arr.filterMap(rows, (row) => row.kind === "key" ? Result.succeed(row.key.version) : Result.failVoid)),
44
+ papers: HashMap.fromIterable(Arr.filterMap(rows, (row) => row.kind === "paper"
45
+ ? Result.succeed([row.paper.id, row])
46
+ : Result.failVoid)),
47
+ profiles: HashMap.fromIterable(Arr.filterMap(rows, (row) => row.kind === "profile"
48
+ ? Result.succeed([row.profile.id, row])
49
+ : Result.failVoid)),
50
+ stimuli: HashSet.fromIterable(Arr.filterMap(rows, (row) => row.kind === "stimulus"
51
+ ? Result.succeed(stimulusIdentity(row.version))
52
+ : Result.failVoid)),
53
+ };
54
+ }
55
+ /** The identity of one explanation: the version it explains and its language. */
56
+ function explanationIdentity(version, language) {
57
+ return `${version}\0${language}`;
58
+ }
59
+ /** The identity of one stimulus version: its identifier and its hash. */
60
+ function stimulusIdentity(version) {
61
+ return `${version.id}\0${version.hash}`;
62
+ }
63
+ /** Fails with one closure reason unless the condition holds. */
64
+ function require(holds, reason, identity) {
65
+ return holds
66
+ ? Effect.void
67
+ : Effect.fail(new AssessmentClosureError({ identity, reason }));
68
+ }
69
+ /** Checks that a paper follows its profile: the same sections, in order, each with its count of places. */
70
+ function fitsProfile(paper, profile) {
71
+ return (paper.sections.length === profile.sections.length &&
72
+ Arr.every(Arr.zip(paper.sections, profile.sections), ([placed, section]) => placed.section === section.id &&
73
+ placed.placements.length === section.items));
74
+ }
75
+ /** Verifies the papers: their profiles, their places, and the readings they list. */
76
+ const verifyPapers = Effect.fn("AksaraContracts.verifyAssessmentPapers")(function* (rows) {
77
+ for (const { paper } of HashMap.values(rows.papers)) {
78
+ const profile = HashMap.get(rows.profiles, paper.profile.id);
79
+ yield* require(Option.exists(profile, ({ hash }) => hash === paper.profile.hash), "profile", paper.id);
80
+ yield* require(Option.exists(profile, (entry) => fitsProfile(paper, entry.profile)), "section", paper.id);
81
+ const placed = Arr.flatMap(paper.sections, ({ placements }) => placements);
82
+ const versions = Arr.getSomes(Arr.map(placed, ({ family, hash }) => Option.filter(HashMap.get(rows.items, hash), (version) => version.family === family)));
83
+ yield* require(versions.length === placed.length, "place", paper.id);
84
+ yield* verifyPaperStimuli(paper, versions).pipe(Effect.mapError(() => new AssessmentClosureError({
85
+ identity: paper.id,
86
+ reason: "stimulus",
87
+ })));
88
+ }
89
+ });
90
+ /** Verifies each item version: its family, its reading, its key, its explanations, and a paper that places it. */
91
+ const verifyItems = Effect.fn("AksaraContracts.verifyAssessmentItems")(function* (rows, activeAppLocales) {
92
+ const placed = HashSet.fromIterable(Arr.flatMap(Arr.fromIterable(HashMap.values(rows.papers)), ({ paper }) => Arr.flatMap(paper.sections, ({ placements }) => Arr.map(placements, ({ hash }) => hash))));
93
+ for (const version of HashMap.values(rows.items)) {
94
+ const family = HashMap.get(rows.families, version.family);
95
+ yield* require(Option.exists(family, ({ stimulus }) => stimulus === version.stimulus?.id), "family", version.family);
96
+ yield* require(version.stimulus === undefined ||
97
+ HashSet.has(rows.stimuli, stimulusIdentity(version.stimulus)), "stimulus", version.family);
98
+ yield* require(HashSet.has(rows.keys, version.hash), "key", version.hash);
99
+ yield* require(HashSet.has(placed, version.hash), "unplaced", version.hash);
100
+ for (const language of activeAppLocales) {
101
+ const identity = explanationIdentity(version.hash, language);
102
+ yield* require(HashMap.has(rows.explanations, identity), "explanation", identity);
103
+ }
104
+ }
105
+ const familiesWithVersions = HashSet.fromIterable(Arr.map(Arr.fromIterable(HashMap.values(rows.items)), ({ family }) => family));
106
+ for (const id of HashMap.keys(rows.families)) {
107
+ yield* require(HashSet.has(familiesWithVersions, id), "family", id);
108
+ }
109
+ });
110
+ /** Verifies that keys and explanations name item versions the snapshot holds. */
111
+ function verifyRecordsOfItems(rows) {
112
+ return Effect.gen(function* () {
113
+ for (const version of rows.keys) {
114
+ yield* require(HashMap.has(rows.items, version), "key", version);
115
+ }
116
+ for (const [identity, version] of rows.explanations) {
117
+ yield* require(HashMap.has(rows.items, version), "explanation", identity);
118
+ }
119
+ });
120
+ }
121
+ /**
122
+ * Verifies the catalog: every node in every active app locale, every set's
123
+ * paper, and the sections of each set equal to the sections of its paper.
124
+ */
125
+ const verifyCatalog = Effect.fn("AksaraContracts.verifyAssessmentCatalog")(function* (rows, activeAppLocales) {
126
+ for (const row of rows.catalog) {
127
+ if (row.kind !== "set") {
128
+ continue;
129
+ }
130
+ const paper = HashMap.get(rows.papers, row.paper.id);
131
+ yield* require(Option.exists(paper, ({ hash }) => hash === row.paper.hash), "paper", row.paper.id);
132
+ const sections = Arr.filterMap(rows.catalog, (section) => section.kind === "section" &&
133
+ section.appLocale === row.appLocale &&
134
+ section.countryKey === row.countryKey &&
135
+ section.examKey === row.examKey &&
136
+ section.trackKey === row.trackKey &&
137
+ section.setKey === row.setKey
138
+ ? Result.succeed(section.sectionKey)
139
+ : Result.failVoid);
140
+ yield* require(Option.exists(paper, (entry) => HashSet.size(HashSet.fromIterable(sections)) === sections.length &&
141
+ sections.length === entry.paper.sections.length &&
142
+ Arr.every(entry.paper.sections, ({ section }) => Arr.contains(sections, section))), "section", `${row.appLocale}\0${row.setKey}`);
143
+ }
144
+ const present = HashSet.fromIterable(Arr.map(rows.catalog, catalogRowIdentity));
145
+ for (const row of rows.catalog) {
146
+ for (const appLocale of activeAppLocales) {
147
+ const identity = catalogRowIdentity({ ...row, appLocale });
148
+ yield* require(HashSet.has(present, identity), "locale", identity);
149
+ }
150
+ }
151
+ });
152
+ /**
153
+ * Verifies that an assessment snapshot is closed: the catalog reaches papers it
154
+ * holds, every paper fits the profile it follows and places versions it holds,
155
+ * every version has its family, its reading, its key, and an explanation in
156
+ * every active app locale, and no family, key, or explanation is left over.
157
+ */
158
+ export const verifyAssessmentClosure = Effect.fn("AksaraContracts.verifyAssessmentClosure")(function* (input) {
159
+ const rows = indexRows(yield* Stream.runCollect(input.records));
160
+ yield* verifyCatalog(rows, input.activeAppLocales);
161
+ yield* verifyPapers(rows);
162
+ yield* verifyItems(rows, input.activeAppLocales);
163
+ yield* verifyRecordsOfItems(rows);
164
+ });
@@ -1,19 +1,24 @@
1
1
  import { Schema } from "effect";
2
2
  /**
3
- * The explanation of one item version: its own schema and its own artifact, as
4
- * the answer key is. A paper bundle carries prompts and stimuli only, never a
5
- * key and never an explanation, so an explanation reaches a learner after
6
- * release, under the release policy that Nakafa applies, from this record. The
7
- * document places no interaction. `version` is the hash of the item version it
8
- * explains, which also fixes the delivery language it is written in. An item
9
- * version holds no explanation, and a version without one has no record. Its
10
- * size is limited by `assessment/record`.
3
+ * The explanation of one item version in one language: its own schema and its
4
+ * own artifact, as the answer key is. A paper bundle carries prompts and
5
+ * stimuli only, never a key and never an explanation, so an explanation
6
+ * reaches a learner after release, under the release policy that Nakafa
7
+ * applies, from this record. The document places no interaction. `version` is
8
+ * the hash of the item version it explains. `language` is the language the
9
+ * explanation is written in, which need not be the delivery language of the
10
+ * version: an item asked in Indonesian is explained to a learner in the
11
+ * language of the learner, and may quote the prompt in its own language. A
12
+ * version has at most one explanation in each language, and a version without
13
+ * one has no record. An item version holds no explanation. Its size is limited
14
+ * by `assessment/record`.
11
15
  */
12
16
  export declare const ItemExplanationSchema: Schema.Struct<{
13
17
  readonly document: Schema.decodeTo<Schema.Struct<{
14
18
  readonly content: Schema.NonEmptyArray<Schema.Codec<import("#contracts/assessment/document").BlockNode, import("#contracts/assessment/document").BlockNode, never, never>>;
15
19
  readonly type: Schema.Literal<"doc">;
16
20
  }>, Schema.Unknown, never, never>;
21
+ readonly language: Schema.brand<Schema.String, "@NakafaAI/AksaraContentLanguage">;
17
22
  readonly version: Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">;
18
23
  }>;
19
24
  export type ItemExplanation = typeof ItemExplanationSchema.Type;
@@ -2,17 +2,23 @@ import { Schema } from "effect";
2
2
  import { ContentDocumentSchema } from "#contracts/assessment/document";
3
3
  import { isWithinRecordLimit } from "#contracts/assessment/record";
4
4
  import { Sha256HashSchema } from "#contracts/ids";
5
+ import { ContentLanguageSchema } from "#contracts/locale";
5
6
  /**
6
- * The explanation of one item version: its own schema and its own artifact, as
7
- * the answer key is. A paper bundle carries prompts and stimuli only, never a
8
- * key and never an explanation, so an explanation reaches a learner after
9
- * release, under the release policy that Nakafa applies, from this record. The
10
- * document places no interaction. `version` is the hash of the item version it
11
- * explains, which also fixes the delivery language it is written in. An item
12
- * version holds no explanation, and a version without one has no record. Its
13
- * size is limited by `assessment/record`.
7
+ * The explanation of one item version in one language: its own schema and its
8
+ * own artifact, as the answer key is. A paper bundle carries prompts and
9
+ * stimuli only, never a key and never an explanation, so an explanation
10
+ * reaches a learner after release, under the release policy that Nakafa
11
+ * applies, from this record. The document places no interaction. `version` is
12
+ * the hash of the item version it explains. `language` is the language the
13
+ * explanation is written in, which need not be the delivery language of the
14
+ * version: an item asked in Indonesian is explained to a learner in the
15
+ * language of the learner, and may quote the prompt in its own language. A
16
+ * version has at most one explanation in each language, and a version without
17
+ * one has no record. An item version holds no explanation. Its size is limited
18
+ * by `assessment/record`.
14
19
  */
15
20
  export const ItemExplanationSchema = Schema.Struct({
16
21
  document: ContentDocumentSchema,
22
+ language: ContentLanguageSchema,
17
23
  version: Sha256HashSchema,
18
24
  }).check(isWithinRecordLimit);