More tests and fixes

This commit is contained in:
Jonathan Putney
2019-11-14 16:07:42 -05:00
parent caa5977a4b
commit 929176fb3e
14 changed files with 385 additions and 238 deletions

View File

@@ -17,8 +17,8 @@ export default class AICC extends Scorm12API {
constructor() {
super();
this.cmi = new CMI(this);
this.nav = new NAV(this);
this.cmi = new CMI();
this.nav = new NAV();
}
/**

View File

@@ -609,7 +609,6 @@ export default class BaseAPI {
this.throwSCORMError(203);
}
}
return;
} else {
return refObject;
}
@@ -776,6 +775,14 @@ export default class BaseAPI {
return JSON.parse(JSON.stringify(cmi));
}
/**
* Gets the current total time as total_time + session_time
* APIs that inherit BaseAPI should override this function
*/
getCurrentTotalTime() {
this.cmi.getCurrentTotalTime();
}
/**
* Throws a SCORM error
*

View File

@@ -24,7 +24,7 @@ export default class Scorm12API extends BaseAPI {
constructor() {
super(scorm12_error_codes);
this.cmi = new CMI(this);
this.cmi = new CMI();
// Rename functions to match 1.2 Spec and expose to modules
this.LMSInitialize = this.lmsInitialize;
this.LMSFinish = this.lmsFinish;
@@ -192,20 +192,6 @@ export default class Scorm12API extends BaseAPI {
return detail ? detailMessage : basicMessage;
}
/**
* Adds the current session time to the existing total time.
*
* @return {string}
*/
getCurrentTotalTime() {
const timeRegex = new RegExp(scorm12_regex.CMITime);
const totalTime = this.cmi.core.total_time;
const sessionTime = this.cmi.core.session_time;
return Utilities.addHHMMSSTimeStrings(totalTime, sessionTime, timeRegex);
}
/**
* Replace the whole API with another
*

View File

@@ -30,8 +30,8 @@ export default class Scorm2004API extends BaseAPI {
constructor() {
super(scorm2004_error_codes);
this.cmi = new CMI(this);
this.adl = new ADL(this);
this.cmi = new CMI();
this.adl = new ADL();
// Rename functions to match 2004 Spec and expose to modules
this.Initialize = this.lmsInitialize;
@@ -420,17 +420,4 @@ export default class Scorm2004API extends BaseAPI {
this.cmi = newAPI.cmi;
this.adl = newAPI.adl;
}
/**
* Adds the current session time to the existing total time.
*
* @return {string} ISO8601 Duration
*/
getCurrentTotalTime() {
const totalTime = this.cmi.total_time;
const sessionTime = this.cmi.session_time;
return Util.addTwoDurations(totalTime, sessionTime,
scorm2004_regex.CMITimespan);
}
}

View File

@@ -10,6 +10,7 @@ import {scorm12_constants} from '../constants/api_constants';
import {scorm12_error_codes} from '../constants/error_codes';
import {scorm12_regex} from '../constants/regex';
import {ValidationError} from '../exceptions';
import * as Utilities from '../utilities';
const constants = scorm12_constants;
const regex = scorm12_regex;
@@ -244,6 +245,15 @@ export class CMI extends BaseCMI {
this.#comments_from_lms = comments_from_lms :
throwReadOnlyError();
}
/**
* Adds the current session time to the existing total time.
*
* @return {string}
*/
getCurrentTotalTime() {
return this.core.getCurrentTotalTime();
}
}
/**
@@ -475,6 +485,19 @@ class CMICore extends BaseCMI {
}
}
/**
* Adds the current session time to the existing total time.
*
* @return {string}
*/
getCurrentTotalTime() {
return Utilities.addHHMMSSTimeStrings(
this.#total_time,
this.#session_time,
new RegExp(scorm12_regex.CMITimespan)
);
}
/**
* toJSON for cmi.core
*

View File

@@ -11,6 +11,7 @@ import {scorm2004_regex} from '../constants/regex';
import {scorm2004_error_codes} from '../constants/error_codes';
import {learner_responses} from '../constants/response_constants';
import {ValidationError} from '../exceptions';
import * as Util from '../utilities';
const constants = scorm2004_constants;
const regex = scorm2004_regex;
@@ -466,6 +467,19 @@ export class CMI extends BaseCMI {
!this.initialized ? this.#total_time = total_time : throwReadOnlyError();
}
/**
* Adds the current session time to the existing total time.
*
* @return {string} ISO8601 Duration
*/
getCurrentTotalTime() {
return Util.addTwoDurations(
this.#total_time,
this.#session_time,
scorm2004_regex.CMITimespan,
);
}
/**
* toJSON for cmi
*

View File

@@ -44,7 +44,7 @@ export function getSecondsAsHHMMSS(totalSeconds: Number) {
export function getSecondsAsISODuration(seconds: Number) {
// SCORM spec does not deal with negative durations, give zero back
if (!seconds || seconds <= 0) {
return 'P0S';
return 'PT0S';
}
let duration = 'P';
@@ -61,6 +61,11 @@ export function getSecondsAsISODuration(seconds: Number) {
}
if (value) {
if ((duration.indexOf('D') > 0 ||
sign === 'H' || sign === 'M' || sign === 'S') &&
duration.indexOf('T') === -1) {
duration += 'T';
}
duration += `${value}${sign}`;
}
});