MCPcopy Create free account
hub / github.com/coderoad/coderoad-vscode / formatLevels

Function formatLevels

web-app/src/containers/Tutorial/formatLevels.ts:24–113  ·  view source on GitHub ↗
({ position, levels, testStatus }: Input)

Source from the content-addressed store, hash-verified

22 * - step.subtasks as { name: string, status: 'ACTIVE' | 'COMPLETE' | 'INCOMPLETE' }[]
23 */
24const 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 }

Callers 1

TutorialPageFunction · 0.85

Calls 1

loggerFunction · 0.50

Tested by

no test coverage detected