({ position, levels, testStatus }: Input)
| 22 | * - step.subtasks as { name: string, status: 'ACTIVE' | 'COMPLETE' | 'INCOMPLETE' }[] |
| 23 | */ |
| 24 | const formatLevels = ({ position, levels, testStatus }: Input): Output => { |
| 25 | // clone levels |
| 26 | |
| 27 | const levelIndex: number = levels.findIndex((l: TT.Level) => l.id === position.levelId) |
| 28 | |
| 29 | if (levelIndex === -1) { |
| 30 | throw new Error(`Level ${position.levelId} not found`) |
| 31 | } |
| 32 | |
| 33 | const currentLevel = levels[levelIndex] |
| 34 | |
| 35 | let stepIndex = currentLevel.steps.findIndex((s: TT.Step) => s.id === position.stepId) |
| 36 | if (stepIndex === -1) { |
| 37 | stepIndex = levels[levelIndex].steps.length |
| 38 | } |
| 39 | |
| 40 | if (position.complete) { |
| 41 | stepIndex += 1 |
| 42 | } |
| 43 | |
| 44 | // current level |
| 45 | const levelUI: T.LevelUI = { |
| 46 | ...currentLevel, |
| 47 | status: position.complete ? 'COMPLETE' : 'ACTIVE', |
| 48 | steps: currentLevel.steps.map((step: TT.Step, index) => { |
| 49 | // label step status for step component |
| 50 | let status: T.ProgressStatus = 'INCOMPLETE' |
| 51 | let subtasks |
| 52 | if (index < stepIndex) { |
| 53 | status = 'COMPLETE' |
| 54 | } else if (index === stepIndex) { |
| 55 | status = 'ACTIVE' |
| 56 | } else { |
| 57 | status = 'INCOMPLETE' |
| 58 | } |
| 59 | if (step.subtasks && step.subtasks) { |
| 60 | const testSummaries = Object.keys(testStatus?.summary || {}) |
| 61 | if (testSummaries.length && testSummaries.length !== step.subtasks.length) { |
| 62 | // test result count and subtask count don't match |
| 63 | // something is wrong with the tutorial |
| 64 | // NOTE: hacky temp solution as should be caught by tutorial creators / build tools |
| 65 | logger( |
| 66 | 'Error: subtasks and test results have a different number of results. This is likely an error with the tutorial or an edited test file.', |
| 67 | ) |
| 68 | } |
| 69 | subtasks = step.subtasks.map((subtask: string, subtaskIndex: number) => { |
| 70 | let subtaskStatus: T.ProgressStatus = 'INCOMPLETE' |
| 71 | // task is complete, subtasks must be complete |
| 72 | if (status === 'COMPLETE') { |
| 73 | subtaskStatus = 'COMPLETE' |
| 74 | // task is active, check which are complete from test results |
| 75 | } else if (status === 'ACTIVE') { |
| 76 | subtaskStatus = !!(testStatus?.summary && testStatus.summary[subtaskIndex]) ? 'COMPLETE' : 'ACTIVE' |
| 77 | } |
| 78 | return { |
| 79 | name: subtask, |
| 80 | status: subtaskStatus, |
| 81 | } |
no test coverage detected