tessera-learn 0.0.3 → 0.0.5

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.
@@ -18,6 +18,8 @@ export interface SCORM2004API {
18
18
  const SCORM2004_DIALECT: ScormDialect<SCORM2004API> = {
19
19
  sessionTimeKey: 'cmi.session_time',
20
20
  formatDuration: formatISO8601Duration,
21
+ suspendDataLimit: 64000,
22
+ suspendDataLimitLabel: 'SCORM 2004 4E cmi.suspend_data 64000-char',
21
23
  interactionFields: {
22
24
  responseField: 'learner_response',
23
25
  timestampField: 'timestamp',
@@ -191,6 +191,45 @@ export function useProgress() {
191
191
  };
192
192
  }
193
193
 
194
+ // One dev warning per session, regardless of caller count.
195
+ let warnedNonManualCompletion = false;
196
+
197
+ /** Test-only: reset the once-per-session warning latch. */
198
+ export function __resetUseCompletionWarning(): void {
199
+ warnedNonManualCompletion = false;
200
+ }
201
+
202
+ /**
203
+ * Trigger course completion from any component, and reactively read the
204
+ * current completion status. Active under `completion.mode: "manual"`; a
205
+ * no-op (with a one-shot dev warning) under any other mode.
206
+ */
207
+ export function useCompletion(): {
208
+ markComplete(): void;
209
+ readonly completionStatus: 'incomplete' | 'complete';
210
+ } {
211
+ const { progress, manifest, config } = requireNavContext('useCompletion()');
212
+ return {
213
+ markComplete() {
214
+ if (config.completion.mode !== 'manual') {
215
+ if (import.meta.env?.DEV && !warnedNonManualCompletion) {
216
+ warnedNonManualCompletion = true;
217
+ console.warn(
218
+ "Tessera: useCompletion().markComplete() ignored — completion.mode is not 'manual'. " +
219
+ '(This warning is shown once per session.)'
220
+ );
221
+ }
222
+ return;
223
+ }
224
+ progress.markCompleteManually();
225
+ progress.recalculateSuccess(manifest, config);
226
+ },
227
+ get completionStatus() {
228
+ return progress.completionStatus;
229
+ },
230
+ };
231
+ }
232
+
194
233
  /**
195
234
  * Scoped persistence — save and restore per-widget state that survives reload.
196
235
  * Routes to whichever adapter the course is running under (localStorage, SCORM
@@ -53,4 +53,6 @@ export interface SavedState {
53
53
  s?: Record<string, Record<string, number>>;
54
54
  /** Graded standalone page indices — pages with at least one graded standalone question */
55
55
  gs?: number[];
56
+ /** Manual completion latch. 1 if the learner triggered manual completion. Absent otherwise. */
57
+ m?: 1;
56
58
  }
@@ -24,6 +24,21 @@ export class ProgressState {
24
24
  completionStatus = $state<'incomplete' | 'complete'>('incomplete');
25
25
  successStatus = $state<'unknown' | 'passed' | 'failed'>('unknown');
26
26
 
27
+ // Latch for manual completion. Monotonic; recalc methods bail when set.
28
+ #manuallyCompleted = $state(false);
29
+
30
+ get manuallyCompleted(): boolean {
31
+ return this.#manuallyCompleted;
32
+ }
33
+
34
+ /** Idempotent — only the first call per session has an effect. */
35
+ markCompleteManually(): void {
36
+ if (this.#manuallyCompleted) return;
37
+ this.#manuallyCompleted = true;
38
+ this.completionStatus = 'complete';
39
+ this.version++;
40
+ }
41
+
27
42
  /**
28
43
  * Monotonic counter incremented on every persistable state mutation
29
44
  * (visited/scores/chunks/standalone). Callers that need to react to *any*
@@ -100,6 +115,8 @@ export class ProgressState {
100
115
  }
101
116
 
102
117
  recalculateCompletion(manifest: Manifest, config: CourseConfig) {
118
+ if (this.#manuallyCompleted) return;
119
+ if (config.completion.mode === 'manual') return;
103
120
  if (config.completion.mode === 'percentage') {
104
121
  const threshold = config.completion.percentageThreshold ?? 100;
105
122
  const percent = manifest.totalPages > 0
@@ -118,6 +135,14 @@ export class ProgressState {
118
135
  }
119
136
 
120
137
  recalculateSuccess(manifest: Manifest, config: CourseConfig) {
138
+ if (config.completion.mode === 'manual') {
139
+ const want = config.completion.requireSuccessStatus;
140
+ // Stay 'unknown' until manual mark fires, so a learner who never
141
+ // finishes isn't reported as passed.
142
+ this.successStatus = this.#manuallyCompleted && want !== undefined ? want : 'unknown';
143
+ return;
144
+ }
145
+
121
146
  const { indices, attempted } = this.#gradedPages(manifest);
122
147
 
123
148
  if (indices.length === 0) {
@@ -29,10 +29,8 @@ export interface CourseConfig {
29
29
  mode: 'free' | 'sequential';
30
30
  canAccess?: AccessFn;
31
31
  };
32
- completion: {
33
- mode: 'quiz' | 'percentage';
34
- percentageThreshold?: number;
35
- };
32
+ completion: ManualCompletion | QuizCompletion | PercentageCompletion;
33
+ /** Optional under "manual"; required under "quiz". */
36
34
  scoring: {
37
35
  passingScore: number;
38
36
  };
@@ -48,6 +46,27 @@ export interface CourseConfig {
48
46
  xapi?: XAPIConfig | XAPIConfig[];
49
47
  }
50
48
 
49
+ export interface ManualCompletion {
50
+ mode: 'manual';
51
+ /**
52
+ * Set to "page" to opt into a build-time check that at least one page
53
+ * declares `completesOn: "view"`. Omit to skip the check; both completion
54
+ * paths still work at runtime.
55
+ */
56
+ trigger?: 'page';
57
+ /** When set, markComplete() also flips successStatus. Omit for unknown. */
58
+ requireSuccessStatus?: 'passed' | 'failed';
59
+ }
60
+
61
+ export interface QuizCompletion {
62
+ mode: 'quiz';
63
+ }
64
+
65
+ export interface PercentageCompletion {
66
+ mode: 'percentage';
67
+ percentageThreshold?: number;
68
+ }
69
+
51
70
  /**
52
71
  * cmi5 launch-inherited destination. Only valid under `export.standard:
53
72
  * 'cmi5'`. Auth, actor, activityId, and registration are taken from the
@@ -568,11 +568,30 @@ export class XAPIPublisher {
568
568
  })
569
569
  );
570
570
  }
571
- return {
572
- ok: false,
573
- status: resp.status,
574
- error: new Error(`LRS responded ${resp.status}`),
575
- };
571
+ // Append the LRS body to the error message so callers see the
572
+ // specific reason (e.g. "Forbidden cmi5 defined statement: ...").
573
+ // Cap at 500 chars; on read failure, fall back to bare status.
574
+ if (typeof resp.text !== 'function') {
575
+ return {
576
+ ok: false,
577
+ status: resp.status,
578
+ error: new Error(`LRS responded ${resp.status}`),
579
+ };
580
+ }
581
+ return resp.text().then(
582
+ (respBody): SendOutcome => ({
583
+ ok: false,
584
+ status: resp.status,
585
+ error: new Error(
586
+ `LRS responded ${resp.status}${respBody ? `: ${respBody.slice(0, 500)}` : ''}`
587
+ ),
588
+ }),
589
+ (): SendOutcome => ({
590
+ ok: false,
591
+ status: resp.status,
592
+ error: new Error(`LRS responded ${resp.status}`),
593
+ })
594
+ );
576
595
  }
577
596
 
578
597
  #resolveAuth(forceRefresh: boolean): Promise<string> {