refactor(tools): challenge tests - checks for title and ids (#51432)

This commit is contained in:
Naomi Carrigan
2023-09-07 09:25:18 -07:00
committed by GitHub
parent 5cf0b93dfb
commit 3c0c14b427
3 changed files with 27 additions and 20 deletions
+11 -2
View File
@@ -314,9 +314,18 @@ function populateTestsForLang({ lang, challenges, meta }) {
throw new AssertionError(result.error);
}
const { id, title, block, dashedName } = challenge;
assert.exists(
dashedName,
`Missing dashedName for challenge ${id} in ${block}.`
);
const pathAndTitle = `${block}/${dashedName}`;
mongoIds.check(id, title);
challengeTitles.check(title, pathAndTitle);
const idVerificationMessage = mongoIds.check(id, title);
assert.isNull(idVerificationMessage, idVerificationMessage);
const dupeTitleCheck = challengeTitles.check(dashedName, block);
assert.isTrue(
dupeTitleCheck,
`All challenges within a block must have a unique dashed name. ${dashedName} (at ${pathAndTitle}) is already assigned`
);
});
const { challengeType } = challenge;
+13 -14
View File
@@ -1,8 +1,11 @@
class ChallengeTitles {
constructor() {
this.knownTitles = [];
/**
* Takes the shape of { [block]: title[]}
*/
this.knownTitles = {};
}
check(title, pathAndTitle) {
check(title, block) {
if (typeof title !== 'string') {
throw new Error(
`Expected a valid string for ${title}, but got a(n) ${typeof title}`
@@ -12,19 +15,15 @@ class ChallengeTitles {
if (titleToCheck.length === 0) {
throw new Error('Expected a title length greater than 0');
}
// reassign titleToCheck if challenge is part of the project
// based curriculum
const isProjectCurriculumChallenge = title.match(/^Step\s*\d+$/);
titleToCheck = isProjectCurriculumChallenge ? pathAndTitle : titleToCheck;
const isKnown = this.knownTitles.includes(titleToCheck);
// TODO: check for the exceptions or remove the warning.
if (isKnown) {
console.warn(`
All current curriculum challenges must have a unique title.
The title ${title} (at ${pathAndTitle}) is already assigned
`);
if (!this.knownTitles[block]) {
this.knownTitles[block] = [];
}
this.knownTitles = [...this.knownTitles, titleToCheck];
const isKnown = this.knownTitles[block].includes(title);
if (isKnown) {
return false;
}
this.knownTitles[block].push(title);
return true;
}
}
+3 -4
View File
@@ -24,16 +24,15 @@ class MongoIds {
try {
schema.validate(id);
} catch {
throw Error(`Expected a valid ObjectId for ${title}, but got ${id}`);
return `Expected a valid ObjectId for ${title}, but got ${id}`;
}
const idIndex = findIndex(this.knownIds, existing => id === existing);
if (idIndex !== -1 && !duplicatedProjectIds.includes(id)) {
throw Error(`The id for challenge ${title} appears more than once.
With the exception of duplicatedProjectIds this should not happen.
`);
return `The id for challenge ${title} appears more than once. With the exception of duplicatedProjectIds this should not happen.`;
}
this.knownIds = [...this.knownIds, id];
return null;
}
}