Skip to content

Commit

Permalink
chore: add unit test on otter training helpers (#2672)
Browse files Browse the repository at this point in the history
## Proposed change

<!--
Please include a summary of the changes and the related issue.
Please also include relevant motivation and context.
-->

## Related issues

<!--
Please make sure to follow the [contribution
guidelines](https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md)
-->

*- No issue associated -*

<!-- * 🐛 Fix #issue -->
<!-- * 🐛 Fix resolves #issue -->
<!-- * 🚀 Feature #issue -->
<!-- * 🚀 Feature resolves #issue -->
<!-- * :octocat: Pull Request #issue -->
  • Loading branch information
cpaulve-1A authored Jan 8, 2025
2 parents 994eea7 + 4565659 commit 6905db3
Show file tree
Hide file tree
Showing 12 changed files with 475 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import {
} from 'rxjs';
import {
checkIfPathInMonacoTree,
} from '../../../helpers/monaco-tree.helper';
} from '../../../helpers/monaco-tree/monaco-tree.helper';
import {
flattenTree,
WebContainerService,
Expand Down
33 changes: 4 additions & 29 deletions apps/showcase/src/components/training/training.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ import {
import {
LoggerService,
} from '@o3r/logger';
import type {
DirectoryNode,
FileSystemTree,
} from '@webcontainer/api';
import {
firstValueFrom,
} from 'rxjs';
import {
overrideFileSystemTree,
} from '../../services';
getFilesContent,
isTrainingResource,
} from '../../helpers/file-system/index';
import {
EditorMode,
TrainingProject,
Expand Down Expand Up @@ -95,31 +92,9 @@ type TrainingStep = {
};
};

/** Resources to get file content */
type Resource = {
/** Resource path */
path: string;
/** Resource content */
content: string;
};

/** RegExp of current step index at the end of the location URL (example: http://url/#/fragment#3) */
const currentStepLocationRegExp = new RegExp(/#([0-9]+)$/);

/**
* Generate a file system tree composed of the deep merge of all the resources passed in parameters
* @param resources Sorted list of path and content to load. If a file is defined several time, the last occurrence
* overrides the others
*/
function getFilesContent(resources: Resource[]) {
return (resources.reduce((fileSystemTree: FileSystemTree, resource) => {
const sanitizedPath = `./${resource.path.replace(new RegExp('^[.]/?'), '')}`;
const parsedPath = sanitizedPath.split('/').filter((pathEl) => !!pathEl);
overrideFileSystemTree(fileSystemTree, (JSON.parse(resource.content) as { fileSystemTree: FileSystemTree }).fileSystemTree, parsedPath);
return fileSystemTree;
}, {} as FileSystemTree)['.'] as DirectoryNode).directory;
}

@Component({
selector: 'o3r-training',
standalone: true,
Expand Down Expand Up @@ -235,7 +210,7 @@ export class TrainingComponent implements OnInit {
path,
content: await this.loadResource(contentUrl)
})
))).filter((resource): resource is Resource => !!resource.content);
))).filter((resource) => isTrainingResource(resource));
const filesContent = getFilesContent(resources);

Check warning on line 214 in apps/showcase/src/components/training/training.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/showcase/src/components/training/training.component.ts#L213-L214

Added lines #L213 - L214 were not covered by tests
step.dynamicContent[solutionProject ? 'solutionProject' : 'project'].set({
startingFile: step.description.filesConfiguration!.startingFile || '',
Expand Down
Loading

0 comments on commit 6905db3

Please sign in to comment.