fix: handle video challenges

This commit is contained in:
Oliver Eyton-Williams
2020-08-13 12:25:53 +02:00
committed by Mrugesh Mohapatra
parent a66455b983
commit 1ec6cf1efd
4 changed files with 96 additions and 32 deletions

View File

@ -93,14 +93,17 @@ async function buildCurriculum(file, curriculum) {
async function parseTranslation(engPath, transPath, dict) { async function parseTranslation(engPath, transPath, dict) {
const engChal = await parseMarkdown(engPath); const engChal = await parseMarkdown(engPath);
const translatedChal = await parseMarkdown(transPath); const translatedChal = await parseMarkdown(transPath);
const codeLang = engChal.files[0] ? engChal.files[0].ext : null; const codeLang =
engChal.files && engChal.files[0] ? engChal.files[0].ext : null;
const engWithTranslatedComments = translateCommentsInChallenge( const engWithTranslatedComments = codeLang
engChal, ? translateCommentsInChallenge(
getChallengeLang(transPath), engChal,
dict, getChallengeLang(transPath),
codeLang dict,
); codeLang
)
: engChal;
return mergeChallenges(engWithTranslatedComments, translatedChal); return mergeChallenges(engWithTranslatedComments, translatedChal);
} }

View File

@ -109,6 +109,17 @@ const ENGLISH_CHALLENGE_NO_FILES = {
files: [] files: []
}; };
const ENGLISH_VIDEO_CHALLENGE = {
id: 'id',
title: 'Title',
challengeType: 0,
videoId: 'abc123',
forumTopicId: 12345,
question: 'english question',
description: 'description html string',
instructions: 'instructions html string'
};
const TRANSLATED_CERTIFICATE = { const TRANSLATED_CERTIFICATE = {
id: '561add10cb82ac38a17513bc', id: '561add10cb82ac38a17513bc',
title: 'Responsive Web Design Certificate', title: 'Responsive Web Design Certificate',
@ -170,6 +181,17 @@ const TRANSLATED_CHALLENGE = {
] ]
}; };
const TRANSLATED_VIDEO_CHALLENGE = {
id: 'id',
title: 'Title',
challengeType: 0,
videoId: 'abc123',
forumTopicId: 12345,
question: 'translated question',
description: 'translated description html string',
instructions: 'translated instructions html string'
};
const WRONG_NUM_TESTS_CHALLENGE = { const WRONG_NUM_TESTS_CHALLENGE = {
id: 'id', id: 'id',
title: 'Title', title: 'Title',
@ -202,6 +224,8 @@ exports.ENGLISH_CERTIFICATE = ENGLISH_CERTIFICATE;
exports.ENGLISH_CHALLENGE = ENGLISH_CHALLENGE; exports.ENGLISH_CHALLENGE = ENGLISH_CHALLENGE;
exports.ENGLISH_CHALLENGE_TWO_SOLUTIONS = ENGLISH_CHALLENGE_TWO_SOLUTIONS; exports.ENGLISH_CHALLENGE_TWO_SOLUTIONS = ENGLISH_CHALLENGE_TWO_SOLUTIONS;
exports.ENGLISH_CHALLENGE_NO_FILES = ENGLISH_CHALLENGE_NO_FILES; exports.ENGLISH_CHALLENGE_NO_FILES = ENGLISH_CHALLENGE_NO_FILES;
exports.ENGLISH_VIDEO_CHALLENGE = ENGLISH_VIDEO_CHALLENGE;
exports.TRANSLATED_CERTIFICATE = TRANSLATED_CERTIFICATE; exports.TRANSLATED_CERTIFICATE = TRANSLATED_CERTIFICATE;
exports.TRANSLATED_CHALLENGE = TRANSLATED_CHALLENGE; exports.TRANSLATED_CHALLENGE = TRANSLATED_CHALLENGE;
exports.TRANSLATED_VIDEO_CHALLENGE = TRANSLATED_VIDEO_CHALLENGE;
exports.WRONG_NUM_TESTS_CHALLENGE = WRONG_NUM_TESTS_CHALLENGE; exports.WRONG_NUM_TESTS_CHALLENGE = WRONG_NUM_TESTS_CHALLENGE;

View File

@ -31,31 +31,55 @@ exports.translateCommentsInChallenge = (challenge, lang, dict, codeLang) => {
}; };
exports.mergeChallenges = (engChal, transChal) => { exports.mergeChallenges = (engChal, transChal) => {
if (!transChal.tests || transChal.tests.length !== engChal.tests.length) const hasTests =
throw Error( (engChal.tests && transChal.tests) ||
`Challenges in both languages must have the same number of tests. (engChal.question && transChal.question);
title: ${engChal.title}
localeTitle: ${transChal.localeTitle}`
);
const translatedTests =
engChal.challengeType === 7
? transChal.tests.map(({ title }, i) => ({
title,
id: engChal.tests[i].id
}))
: transChal.tests.map(({ text }, i) => ({
text,
testString: engChal.tests[i].testString
}));
const challenge = { const challenge = {
...engChal, ...engChal,
description: transChal.description, description: transChal.description,
instructions: transChal.instructions, instructions: transChal.instructions,
localeTitle: transChal.localeTitle, localeTitle: transChal.localeTitle,
forumTopicId: transChal.forumTopicId, forumTopicId: transChal.forumTopicId
tests: translatedTests
}; };
if (!hasTests)
throw Error(
`Both challenges must have tests or questions.
title: ${engChal.title}
localeTitle: ${transChal.localeTitle}`
);
// TODO: this should break the build when we go to production, but
// not for testing.
if (transChal.tests && transChal.tests.length !== engChal.tests.length) {
console.error(
`Challenges in both languages must have the same number of tests.
title: ${engChal.title}
localeTitle: ${transChal.localeTitle}`
);
return challenge;
}
// throw Error(
// `Challenges in both languages must have the same number of tests.
// title: ${engChal.title}
// localeTitle: ${transChal.localeTitle}`
// );
if (transChal.tests) {
const translatedTests =
engChal.challengeType === 7
? transChal.tests.map(({ title }, i) => ({
title,
id: engChal.tests[i].id
}))
: transChal.tests.map(({ text }, i) => ({
text,
testString: engChal.tests[i].testString
}));
challenge.tests = translatedTests;
} else {
challenge.question = transChal.question;
}
// certificates do not have forumTopicIds // certificates do not have forumTopicIds
if (challenge.challengeType === 7) delete challenge.forumTopicId; if (challenge.challengeType === 7) delete challenge.forumTopicId;
return challenge; return challenge;

View File

@ -9,9 +9,11 @@ const {
ENGLISH_CHALLENGE, ENGLISH_CHALLENGE,
ENGLISH_CHALLENGE_NO_FILES, ENGLISH_CHALLENGE_NO_FILES,
ENGLISH_CHALLENGE_TWO_SOLUTIONS, ENGLISH_CHALLENGE_TWO_SOLUTIONS,
ENGLISH_VIDEO_CHALLENGE,
TRANSLATED_CERTIFICATE, TRANSLATED_CERTIFICATE,
TRANSLATED_CHALLENGE, TRANSLATED_CHALLENGE,
WRONG_NUM_TESTS_CHALLENGE TRANSLATED_VIDEO_CHALLENGE
// WRONG_NUM_TESTS_CHALLENGE
} = require('./__fixtures__/challenge-objects'); } = require('./__fixtures__/challenge-objects');
const { SIMPLE_TRANSLATION } = require('./__mocks__/mock-comments'); const { SIMPLE_TRANSLATION } = require('./__mocks__/mock-comments');
@ -30,6 +32,11 @@ const COMBINED_CERTIFICATE = mergeChallenges(
TRANSLATED_CERTIFICATE TRANSLATED_CERTIFICATE
); );
const COMBINED_VIDEO_CHALLENGE = mergeChallenges(
ENGLISH_VIDEO_CHALLENGE,
TRANSLATED_VIDEO_CHALLENGE
);
let logSpy; let logSpy;
describe('translation parser', () => { describe('translation parser', () => {
@ -98,11 +105,12 @@ describe('translation parser', () => {
TRANSLATED_CHALLENGE.localeTitle TRANSLATED_CHALLENGE.localeTitle
); );
}); });
it('throws an error if the numbers of tests do not match', () => { // TODO: reinstate this after alpha testing.
expect(() => // it('throws an error if the numbers of tests do not match', () => {
mergeChallenges(ENGLISH_CHALLENGE, WRONG_NUM_TESTS_CHALLENGE) // expect(() =>
).toThrow(); // mergeChallenges(ENGLISH_CHALLENGE, WRONG_NUM_TESTS_CHALLENGE)
}); // ).toThrow();
// });
it('takes the forum id from the second challenge', () => { it('takes the forum id from the second challenge', () => {
expect(COMBINED_CHALLENGE.forumTopicId).toBe( expect(COMBINED_CHALLENGE.forumTopicId).toBe(
TRANSLATED_CHALLENGE.forumTopicId TRANSLATED_CHALLENGE.forumTopicId
@ -130,6 +138,11 @@ describe('translation parser', () => {
false false
); );
}); });
it('takes the question from the second challenge', () => {
expect(COMBINED_VIDEO_CHALLENGE.question).toBe(
TRANSLATED_VIDEO_CHALLENGE.question
);
});
}); });
describe('translateCommentsInChallenge', () => { describe('translateCommentsInChallenge', () => {