Skip to content

Commit

Permalink
added stub use cases for loading avatar config
Browse files Browse the repository at this point in the history
  • Loading branch information
DerKatsche committed Dec 10, 2024
1 parent b3aa976 commit bf847dd
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export default class AvatarPort
return "AVATAR-PORT";
}

onAvatarConfigLoaded(avatarConfig: AvatarConfigTO): void {
this.mappedAdapters.forEach((adapters) => {
adapters.forEach((adapter) => {
if (adapter.onAvatarConfigLoaded)
adapter.onAvatarConfigLoaded(avatarConfig);
});
});
}

onAvatarConfigChanged(
newAvatarConfig: AvatarConfigTO,
avatarConfigDiff: Partial<AvatarConfigTO>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AvatarConfigTO from "../../DataTransferObjects/AvatarConfigTO";

export default interface IAvatarAdapter {
onAvatarConfigLoaded?(avatarConfig: AvatarConfigTO): void;
onAvatarConfigChanged?(
newAvatarConfig: AvatarConfigTO,
avatarConfigDiff: Partial<AvatarConfigTO>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import IAvatarAdapter from "../AvatarPort/IAvatarAdapter";
import { IAbstractPort } from "./IAbstractPort";

export default interface IAvatarPort extends IAbstractPort<IAvatarAdapter> {
onAvatarConfigLoaded(avatarConfig: AvatarConfigTO): void;
onAvatarConfigChanged(
newAvatarConfig: AvatarConfigTO,
avatarConfigDiff: Partial<AvatarConfigTO>,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { IAsyncUsecase } from "../../Abstract/IAsyncUsecase";
export default interface ILoadAvatarConfigUseCase
extends IAsyncUsecase<void, void> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { inject, injectable } from "inversify";
import PORT_TYPES from "../../../DependencyInjection/Ports/PORT_TYPES";
import type IAvatarPort from "../../Ports/Interfaces/IAvatarPort";
import ILoadAvatarConfigUseCase from "./ILoadAvatarConfigUseCase";
import CORE_TYPES from "~DependencyInjection/CoreTypes";
import type ILoggerPort from "../../Ports/Interfaces/ILoggerPort";
import { LogLevelTypes } from "../../../Domain/Types/LogLevelTypes";
import type IEntityContainer from "../../../Domain/EntityContainer/IEntityContainer";
import UserDataEntity from "../../../Domain/Entities/UserDataEntity";
import type IBackendPort from "../../Ports/Interfaces/IBackendPort";
import AvatarEntity from "src/Components/Core/Domain/Entities/AvatarEntity";

@injectable()
export default class LoadAvatarConfigUseCase
implements ILoadAvatarConfigUseCase
{
constructor(
@inject(CORE_TYPES.ILogger) private logger: ILoggerPort,
@inject(PORT_TYPES.IAvatarPort) private avatarPort: IAvatarPort,
@inject(CORE_TYPES.IEntityContainer)
private entityContainer: IEntityContainer,
@inject(CORE_TYPES.IBackendAdapter)
private backend: IBackendPort,
) {}

async executeAsync(): Promise<void> {
let userDataEntities =
this.entityContainer.getEntitiesOfType(UserDataEntity);
if (userDataEntities.length === 0) {
this.logger.log(LogLevelTypes.ERROR, "No user data entity found");
return;
} else if (userDataEntities.length > 1) {
this.logger.log(LogLevelTypes.ERROR, "Multiple user data entities found");
return;
}

// TODO: actually load avatar config from backend
if (!userDataEntities[0].avatar) {
userDataEntities[0].avatar = new AvatarEntity();
// let avatarConfig = await this.backend.loadAvatarConfigAsync();
}

this.avatarPort.onAvatarConfigLoaded(userDataEntities[0].avatar);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import CalculateLearningSpaceScoreUseCase from "../../Application/UseCases/Calcu
import ICalculateLearningSpaceScoreUseCase from "../../Application/UseCases/CalculateLearningSpaceScore/ICalculateLearningSpaceScoreUseCase";
import ILoadLearningElementUseCase from "../../Application/UseCases/LoadLearningElement/ILoadLearningElementUseCase";
import LoadLearningElementUseCase from "../../Application/UseCases/LoadLearningElement/LoadLearningElementUseCase";
import ILoadAvatarUseCase from "../../Application/UseCases/LoadAvatar/ILoadAvatarUseCase";
import LoadAvatarUseCase from "../../Application/UseCases/LoadAvatar/LoadAvatarUseCase";
import ILoadAvatarConfigUseCase from "../../Application/UseCases/LoadAvatarConfig/ILoadAvatarConfigUseCase";
import LoadAvatarConfigUseCase from "../../Application/UseCases/LoadAvatarConfig/LoadAvatarConfigUseCase";
import ILoadLearningSpaceUseCase from "../../Application/UseCases/LoadLearningSpace/ILoadLearningSpaceUseCase";
import LoadLearningSpaceUseCase from "../../Application/UseCases/LoadLearningSpace/LoadLearningSpaceUseCase";
import ILoadLearningWorldUseCase from "../../Application/UseCases/LoadLearningWorld/ILoadLearningWorldUseCase";
Expand Down Expand Up @@ -94,8 +94,8 @@ const UseCaseDIContainer = new ContainerModule((bind) => {
.to(SetWorldCompletionModalToShownUseCase)
.inSingletonScope();

bind<ILoadAvatarUseCase>(USECASE_TYPES.ILoadAvatarUseCase)
.to(LoadAvatarUseCase)
bind<ILoadAvatarConfigUseCase>(USECASE_TYPES.ILoadAvatarUseCase)
.to(LoadAvatarConfigUseCase)
.inSingletonScope();

bind<IScoreLearningElementUseCase>(USECASE_TYPES.IScoreLearningElementUseCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import BUILDER_TYPES from "~DependencyInjection/Builders/BUILDER_TYPES";
import bind from "bind-decorator";
import USECASE_TYPES from "~DependencyInjection/UseCases/USECASE_TYPES";
import type ILoadLearningSpaceUseCase from "src/Components/Core/Application/UseCases/LoadLearningSpace/ILoadLearningSpaceUseCase";
import type ILoadAvatarUseCase from "src/Components/Core/Application/UseCases/LoadAvatar/ILoadAvatarUseCase";
import type IPresentationDirector from "../../../PresentationBuilder/IPresentationDirector";
import type IPresentationBuilder from "../../../PresentationBuilder/IPresentationBuilder";
import type INavigation from "../../Navigation/INavigation";
Expand Down Expand Up @@ -47,22 +46,20 @@ export default class LearningSpaceSceneDefinition
private navigation: INavigation,
@inject(USECASE_TYPES.ILoadLearningSpaceUseCase)
private loadSpaceUseCase: ILoadLearningSpaceUseCase,
@inject(USECASE_TYPES.ILoadAvatarUseCase)
private loadAvatarUseCase: ILoadAvatarUseCase,
@inject(BUILDER_TYPES.IAvatarCameraBuilder)
private avatarCameraBuilder: IPresentationBuilder,
@inject(USECASE_TYPES.IGetUserLocationUseCase)
private getUserLocationUseCase: IGetUserLocationUseCase,
@inject(BUILDER_TYPES.IAmbienceBuilder)
private ambienceBuilder: IAmbienceBuilder,
@inject(PORT_TYPES.ILearningWorldPort)
private learningWorldPort: ILearningWorldPort
private learningWorldPort: ILearningWorldPort,
) {
super();
this.learningWorldPort.registerAdapter(this, LocationScope._global);
}

protected override preTasks = [this.loadAvatarPreTask, this.loadSpacePreTask];
protected override preTasks = [this.loadSpacePreTask];

protected override async initializeScene(): Promise<void> {
this.scene.clearColor = new Color4(0.66, 0.83, 0.98, 1);
Expand Down Expand Up @@ -106,11 +103,6 @@ export default class LearningSpaceSceneDefinition
this.navigation.reset();
}

@bind
private async loadAvatarPreTask(): Promise<void> {
await this.loadAvatarUseCase.executeAsync();
}

@bind
private async loadSpacePreTask(): Promise<void> {
const userLocation = this.getUserLocationUseCase.execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NullEngine, Scene } from "@babylonjs/core";
import { mock } from "jest-mock-extended";
import ILoadAvatarUseCase from "../../../../../Core/Application/UseCases/LoadAvatar/ILoadAvatarUseCase";
import ILoadLearningSpaceUseCase from "../../../../../Core/Application/UseCases/LoadLearningSpace/ILoadLearningSpaceUseCase";
import BUILDER_TYPES from "../../../../../Core/DependencyInjection/Builders/BUILDER_TYPES";
import CoreDIContainer from "../../../../../Core/DependencyInjection/CoreDIContainer";
Expand All @@ -20,7 +19,6 @@ const presentationDirectorMock = mock<IPresentationDirector>();
const presentationBuilderMock = mock<IPresentationBuilder>();
const navigationMock = mock<INavigation>();
const loadSpaceUseCaseMock = mock<ILoadLearningSpaceUseCase>();
const loadAvatarUseCaseMock = mock<ILoadAvatarUseCase>();
const getUserLocationUseCaseMock = mock<IGetUserLocationUseCase>();

const getUserLocationUseCaseReturnValue = {
Expand All @@ -35,28 +33,25 @@ describe("LearningSpaceScene", () => {
CoreDIContainer.snapshot();

CoreDIContainer.rebind(BUILDER_TYPES.IPresentationDirector).toConstantValue(
presentationDirectorMock
presentationDirectorMock,
);
CoreDIContainer.rebind(BUILDER_TYPES.ILearningSpaceBuilder).toConstantValue(
presentationBuilderMock
presentationBuilderMock,
);
CoreDIContainer.rebind(BUILDER_TYPES.IAvatarBuilder).toConstantValue(
presentationBuilderMock
presentationBuilderMock,
);
CoreDIContainer.rebind(BUILDER_TYPES.IAvatarCameraBuilder).toConstantValue(
presentationBuilderMock
presentationBuilderMock,
);
CoreDIContainer.rebind(CORE_TYPES.INavigation).toConstantValue(
navigationMock
navigationMock,
);
CoreDIContainer.rebind(
USECASE_TYPES.ILoadLearningSpaceUseCase
USECASE_TYPES.ILoadLearningSpaceUseCase,
).toConstantValue(loadSpaceUseCaseMock);
CoreDIContainer.rebind(USECASE_TYPES.ILoadAvatarUseCase).toConstantValue(
loadAvatarUseCaseMock
);
CoreDIContainer.rebind(
USECASE_TYPES.IGetUserLocationUseCase
USECASE_TYPES.IGetUserLocationUseCase,
).toConstantValue(getUserLocationUseCaseMock);
});

Expand All @@ -73,23 +68,23 @@ describe("LearningSpaceScene", () => {
test("initializeScene doesn't throw", () => {
systemUnderTest["scene"] = new Scene(new NullEngine());
presentationBuilderMock.getPresenter.mockReturnValue(
mock<ILearningSpacePresenter>()
mock<ILearningSpacePresenter>(),
);
presentationBuilderMock.getViewModel.mockReturnValue(
new AvatarCameraViewModel()
new AvatarCameraViewModel(),
);
getUserLocationUseCaseMock.execute.mockReturnValue(
getUserLocationUseCaseReturnValue
getUserLocationUseCaseReturnValue,
);

expect(
async () => await systemUnderTest["initializeScene"]()
async () => await systemUnderTest["initializeScene"](),
).not.toThrow();
});

test("preTasks calls the LoadSpace use case", async () => {
getUserLocationUseCaseMock.execute.mockReturnValue(
getUserLocationUseCaseReturnValue
getUserLocationUseCaseReturnValue,
);

for (const task of systemUnderTest["preTasks"]) {
Expand All @@ -99,18 +94,6 @@ describe("LearningSpaceScene", () => {
expect(loadSpaceUseCaseMock.executeAsync).toHaveBeenCalledTimes(1);
});

test("preTasks contains call to loadAvatarUseCase", async () => {
getUserLocationUseCaseMock.execute.mockReturnValue(
getUserLocationUseCaseReturnValue
);

for (const task of systemUnderTest["preTasks"]) {
await task();
}

expect(loadAvatarUseCaseMock.executeAsync).toHaveBeenCalledTimes(1);
});

test("onLearningSpaceLoaded sets the private member spaceData", () => {
const spaceData = new LearningSpaceTO();
spaceData.template = LearningSpaceTemplateType.L;
Expand Down

0 comments on commit bf847dd

Please sign in to comment.