Skip to content

Commit 4bb28b9

Browse files
committed
passing step tests
Signed-off-by: shmck <[email protected]>
1 parent 8af3bf6 commit 4bb28b9

File tree

4 files changed

+124
-95
lines changed

4 files changed

+124
-95
lines changed

src/utils/parse.ts

+99-70
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,13 @@ export function parseMdContent(md: string): TutorialFrame | never {
7272
};
7373
} else {
7474
// match step
75-
const stepRegex = /^(#{3}\s\(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*)/;
75+
const stepRegex = /^(#{3}\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/;
7676
const stepMatch: RegExpMatchArray | null = section.match(stepRegex);
7777
if (stepMatch && stepMatch.groups) {
7878
const { stepId, stepContent } = stepMatch.groups;
7979
mdContent.levels[current.level].steps[current.step] = {
80-
id: stepId,
80+
id: `${current.level + 1}.${current.step + 1}`,
8181
content: stepContent.trim(),
82-
setup: {},
83-
solution: {},
8482
};
8583
current = { ...current, step: current.step + 1 };
8684
} else {
@@ -131,76 +129,107 @@ export function parse(params: ParseParams): any {
131129

132130
// merge content levels and tutorial
133131

134-
parsed.levels = mdContent.levels.map((level: T.Level, levelIndex: number) => {
135-
// add level setup commits
136-
const levelId = level.id;
137-
if (params.commits[levelId]) {
138-
if (!level.setup) {
139-
level.setup = {};
132+
parsed.levels = mdContent.levels.map(
133+
(mdLevel: T.Level, mdLevelIndex: number) => {
134+
// add level setup commits
135+
let level: T.Level = { ...mdLevel };
136+
137+
const configLevel = params.skeleton.levels[mdLevelIndex];
138+
139+
if (configLevel) {
140+
// add level step commits
141+
const { steps, ...configLevelProps } = configLevel;
142+
level = { ...configLevelProps, ...level };
143+
if (steps) {
144+
steps.forEach((step: T.Step, index: number) => {
145+
console.log("step", step);
146+
const mdStep = level.steps[index];
147+
console.log("mdStep", mdStep);
148+
step = {
149+
...step,
150+
...mdStep,
151+
};
152+
153+
const stepKey = step.id;
154+
console.log("stepKey", stepKey);
155+
const stepSetupKey = `${stepKey}Q`;
156+
if (params.commits[stepSetupKey]) {
157+
if (!step.setup) {
158+
step.setup = {
159+
commits: [],
160+
};
161+
}
162+
step.setup.commits = params.commits[stepSetupKey];
163+
}
164+
165+
const stepSolutionKey = `${stepKey}A`;
166+
if (params.commits[stepSolutionKey]) {
167+
if (!step.solution) {
168+
step.solution = {
169+
commits: [],
170+
};
171+
}
172+
step.solution.commits = params.commits[stepSolutionKey];
173+
}
174+
// update level step
175+
level.steps[index] = step;
176+
});
177+
}
140178
}
141-
level.setup.commits = params.commits[levelId];
142-
}
143179

144-
// get yaml for level
145-
const configLevel = params.skeleton.levels.find(
146-
(l: Partial<T.Level>) => l.id === levelId
147-
);
148-
149-
let configSteps = {};
150-
if (configLevel) {
151-
const { steps, ...configLevelProps } = configLevel;
152-
level = { ...configLevelProps, ...level };
153-
if (steps) {
154-
steps.forEach((s: T.Step) => {
155-
configSteps[s.id] = s;
156-
});
180+
// try {
181+
// level.steps = (level.steps || []).map(
182+
// (step: T.Step, stepIndex: number) => {
183+
// const stepKey = `${levelId}S${stepIndex + 1}`;
184+
// const stepSetupKey = `${stepKey}Q`;
185+
// if (params.commits[stepSetupKey]) {
186+
// if (!step.setup) {
187+
// step.setup = {
188+
// commits: [],
189+
// };
190+
// }
191+
// step.setup.commits = params.commits[stepSetupKey];
192+
// }
193+
194+
// const stepSolutionKey = `${stepKey}A`;
195+
// if (params.commits[stepSolutionKey]) {
196+
// if (!step.solution) {
197+
// step.solution = {
198+
// commits: [],
199+
// };
200+
// }
201+
// step.solution.commits = params.commits[stepSolutionKey];
202+
// }
203+
204+
// // add markdown
205+
// const stepMarkdown: Partial<T.Step> =
206+
// mdContent.levels[level.id].steps[step.id];
207+
// if (stepMarkdown) {
208+
// step = { ...step, ...stepMarkdown };
209+
// }
210+
211+
// step.id = `${stepKey}`;
212+
// return step;
213+
// }
214+
// );
215+
// } catch (error) {
216+
// console.log(JSON.stringify(level.steps));
217+
// console.error("Error parsing level steps");
218+
// console.error(error.message);
219+
// }
220+
221+
console.log(params.commits);
222+
223+
if (params.commits[level.id]) {
224+
if (!level.setup) {
225+
level.setup = {};
226+
}
227+
level.setup.commits = params.commits[level.id];
157228
}
158-
}
159229

160-
// add level step commits
161-
// try {
162-
// level.steps = (level.steps || []).map(
163-
// (step: T.Step, stepIndex: number) => {
164-
// const stepKey = `${levelId}S${stepIndex + 1}`;
165-
// const stepSetupKey = `${stepKey}Q`;
166-
// if (params.commits[stepSetupKey]) {
167-
// if (!step.setup) {
168-
// step.setup = {
169-
// commits: [],
170-
// };
171-
// }
172-
// step.setup.commits = params.commits[stepSetupKey];
173-
// }
174-
175-
// const stepSolutionKey = `${stepKey}A`;
176-
// if (params.commits[stepSolutionKey]) {
177-
// if (!step.solution) {
178-
// step.solution = {
179-
// commits: [],
180-
// };
181-
// }
182-
// step.solution.commits = params.commits[stepSolutionKey];
183-
// }
184-
185-
// // add markdown
186-
// const stepMarkdown: Partial<T.Step> =
187-
// mdContent.levels[level.id].steps[step.id];
188-
// if (stepMarkdown) {
189-
// step = { ...step, ...stepMarkdown };
190-
// }
191-
192-
// step.id = `${stepKey}`;
193-
// return step;
194-
// }
195-
// );
196-
// } catch (error) {
197-
// console.log(JSON.stringify(level.steps));
198-
// console.error("Error parsing level steps");
199-
// console.error(error.message);
200-
// }
201-
202-
return level;
203-
});
230+
return level;
231+
}
232+
);
204233

205234
return parsed;
206235
}

tests/parse.test.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Some text
4040
`;
4141

4242
const skeleton = {
43-
levels: [{ id: "L1" }],
43+
levels: [{ id: "1" }],
4444
};
4545

4646
const result = parse({
@@ -299,7 +299,7 @@ The first step
299299
text: md,
300300
skeleton,
301301
commits: {
302-
L1S1Q: ["abcdefg1"],
302+
"1.1Q": ["abcdefg1"],
303303
},
304304
});
305305
const expected = {
@@ -355,7 +355,7 @@ The first step
355355
text: md,
356356
skeleton,
357357
commits: {
358-
L1S1Q: ["abcdefg1", "123456789"],
358+
"1.1Q": ["abcdefg1", "123456789"],
359359
},
360360
});
361361
const expected = {
@@ -406,7 +406,7 @@ The first step
406406
text: md,
407407
skeleton,
408408
commits: {
409-
L1: ["abcdefg1"],
409+
"1": ["abcdefg1"],
410410
},
411411
});
412412
const expected = {
@@ -464,8 +464,8 @@ Another line
464464
text: md,
465465
skeleton,
466466
commits: {
467-
L1: ["abcdefg1"],
468-
L1S1Q: ["12345678"],
467+
"1": ["abcdefg1"],
468+
"1.1Q": ["12345678"],
469469
},
470470
});
471471
const expected = {
@@ -519,8 +519,8 @@ The first step
519519
text: md,
520520
skeleton,
521521
commits: {
522-
L1S1Q: ["abcdefg1", "123456789"],
523-
L1S1A: ["1gfedcba", "987654321"],
522+
"1.1Q": ["abcdefg1", "123456789"],
523+
"1.1A": ["1gfedcba", "987654321"],
524524
},
525525
});
526526
const expected = {
@@ -644,12 +644,12 @@ The third step
644644
text: md,
645645
skeleton,
646646
commits: {
647-
L1S1Q: ["abcdef1", "123456789"],
648-
L1S1A: ["1fedcba", "987654321"],
649-
L1S2Q: ["2abcdef"],
650-
L1S2A: ["3abcdef"],
651-
L2S1Q: ["4abcdef"],
652-
L2S1A: ["5abcdef"],
647+
"1.1Q": ["abcdef1", "123456789"],
648+
"1.1A": ["1fedcba", "987654321"],
649+
"1.2Q": ["2abcdef"],
650+
"1.2A": ["3abcdef"],
651+
"2.1Q": ["4abcdef"],
652+
"2.1A": ["5abcdef"],
653653
},
654654
});
655655
const expected = {
@@ -664,7 +664,7 @@ The third step
664664
content: "First level content.",
665665
steps: [
666666
{
667-
id: "L11.1S1",
667+
id: "1.1",
668668
content: "The first step",
669669
setup: {
670670
commits: ["abcdef1", "123456789"],
@@ -759,7 +759,7 @@ The first step
759759
text: md,
760760
skeleton,
761761
commits: {
762-
L1S1Q: ["abcdef1", "123456789"],
762+
"1.1Q": ["abcdef1", "123456789"],
763763
},
764764
});
765765
const expected = {
@@ -935,7 +935,7 @@ Description.
935935
});
936936
});
937937

938-
xdescribe("hints", () => {
938+
describe("hints", () => {
939939
it("should parse hints for a step", () => {
940940
const md = `# Title
941941
@@ -971,7 +971,7 @@ The first step
971971
text: md,
972972
skeleton,
973973
commits: {
974-
L1S1Q: ["abcdef1", "123456789"],
974+
"1.1Q": ["abcdef1", "123456789"],
975975
},
976976
});
977977
const expected = {
@@ -1040,7 +1040,7 @@ And spans multiple lines.
10401040
text: md,
10411041
skeleton,
10421042
commits: {
1043-
L1S1Q: ["abcdef1", "123456789"],
1043+
"1.1Q": ["abcdef1", "123456789"],
10441044
},
10451045
});
10461046
const expected = {
@@ -1119,9 +1119,9 @@ The second uninterrupted step
11191119
text: md,
11201120
skeleton,
11211121
commits: {
1122-
L1S1Q: ["abcdef1"],
1123-
L1S1A: ["123456789"],
1124-
L1S2Q: ["fedcba1"],
1122+
"1.1Q": ["abcdef1"],
1123+
"1.1A": ["123456789"],
1124+
"1.2Q": ["fedcba1"],
11251125
},
11261126
});
11271127
const expected = {

tests/skeleton.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe("validate skeleton", () => {
186186
const valid = validateSkeleton(json);
187187
expect(valid).toBe(false);
188188
});
189-
it("should fial if level is missing id", () => {
189+
it("should fail if level is missing id", () => {
190190
const level1 = { ...validJson.levels[0], id: undefined };
191191
const json = {
192192
...validJson,

typings/tutorial.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export type Level = {
2525
export type Step = {
2626
id: string;
2727
content: string;
28-
setup: StepActions;
29-
solution: Maybe<StepActions>;
28+
setup?: StepActions;
29+
solution?: Maybe<StepActions>;
3030
subtasks?: { [testName: string]: boolean };
3131
hints?: string[];
3232
};

0 commit comments

Comments
 (0)