Skip to content

Commit

Permalink
Add achievement tracking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmineee-li committed May 6, 2024
1 parent ce8c44b commit 01495f3
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 13 deletions.
207 changes: 195 additions & 12 deletions server/src/achievement/achievement.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { AchievementDto, AchievementTrackerDto } from './achievement.dto';
import { AppAbility, CaslAbilityFactory } from '../casl/casl-ability.factory';
import { ChallengeDto, ChallengeLocationDto } from '../challenge/challenge.dto';
import { AchievementTypeDto } from './achievement.dto';
import { GroupGateway } from '../group/group.gateway';

describe('AchievementModule E2E', () => {
let app: INestApplication;
Expand All @@ -36,12 +37,17 @@ describe('AchievementModule E2E', () => {
let eventService: EventService;
let user: User;
let tracker: AchievementTracker;
let event: EventBase;
let exJourney: EventBase
let exJourney1: EventBase;
let exJourney2: EventBase;
let exChallenge1: EventBase;
let exChallenge2: EventBase;
let organizationService: OrganizationService;
let abilityFactory: CaslAbilityFactory;
let abilityFactory: CaslAbilityFactory;
let fullAbility: AppAbility;
let orgUsage: OrganizationSpecialUsage;
let challengeService: ChallengeService;
let groupGateway : GroupGateway;

/** beforeAll runs before anything else. It adds new users and prerequisites.
* afterAll runs after all the tests. It removes lingering values in the database.
Expand Down Expand Up @@ -80,6 +86,7 @@ describe('AchievementModule E2E', () => {
organizationService = module.get<OrganizationService>(OrganizationService);
abilityFactory = module.get<CaslAbilityFactory>(CaslAbilityFactory);
fullAbility = abilityFactory.createFull();
groupGateway = moduleRef.get<GroupGateway>(GroupGateway);

user = await userService.register(
'[email protected]',
Expand All @@ -99,7 +106,6 @@ describe('AchievementModule E2E', () => {
where: { id: user.id },
include: { memberOf: true },
});

// tracker = await achievementService.getAchievementsByIdsForAbility(fullAbility, )
console.log = log;
});
Expand Down Expand Up @@ -204,18 +210,195 @@ describe('AchievementModule E2E', () => {
});

describe('Testing achievement tracker', () => {
it('should update tracker progress when a challenge is completed', async () => {
// Assuming a challenge completion would update an existing tracker
const initialProgress = tracker.progress;
await challengeService.completeChallenge(user);
// it('should update tracker progress when a challenge is completed', async () => {
// // Assuming a challenge completion would update an existing tracker
// const initialProgress = tracker.progress;
// await challengeService.completeChallenge(user);

const updatedTracker = await prisma.achievementTracker.findUnique({
where: { id: tracker.id },
// const updatedTracker = await prisma.achievementTracker.findUnique({
// where: { id: tracker.id },
// });
// if (updatedTracker) {
// expect(updatedTracker.progress).toBeGreaterThan(initialProgress);
// }

// console.log("initial progress: " + initialProgress);
// console.log("updated progress: " + updatedTracker?.progress);
// });

// it('should mark tracker as complete when achievement criteria are met', async () => {
// // Complete a challenge that gives the final point needed
// const orgUsage = OrganizationSpecialUsage;
// const orgId = (
// await organizationService.getDefaultOrganization(orgUsage.DEVICE_LOGIN)
// ).id;

// exJourney1 = await organizationService.makeDefaultEvent(orgId);
// const achChalJourDto: AchievementDto = {
// id: "",
// eventId: exJourney1.id,
// name: 'achChalJourDto',
// description: 'ach dto',
// requiredPoints: 1,
// imageUrl: 'update test',
// locationType: ChallengeLocationDto.ARTS_QUAD,
// achievementType: AchievementTypeDto.TOTAL_CHALLENGES_OR_JOURNEYS,
// initialOrganizationId: orgId,
// };

// const achChalJour = await prisma.achievement.findFirstOrThrow({
// where: { name: 'achChalJourDto' },
// });

// const trackerChalJour = await achievementService.createAchievementTracker(
// user,
// achChalJour.id,
// );

// await groupGateway.setCurrentEvent(user, { eventId: tracker.id });
// await challengeService.completeChallenge(user);

// const completedTracker = await prisma.achievementTracker.findUnique({
// where: { id: tracker.id },
// });
// expect(completedTracker).not.toBeNull();
// expect(completedTracker!.dateComplete).not.toBeNull();
// });


it('should test ach tracker on dif achievement types; update tracker with progress; and mark tracker as complete when achievement criteria are met', async () => {
const orgUsage = OrganizationSpecialUsage;
const orgId = (
await organizationService.getDefaultOrganization(orgUsage.DEVICE_LOGIN)
).id;

exJourney1 = await organizationService.makeDefaultEvent(orgId);
exJourney2 = await organizationService.makeDefaultEvent(orgId);
exChallenge1 = await organizationService.makeDefaultEvent(orgId);
exChallenge2 = await organizationService.makeDefaultEvent(orgId);
const achChalJourDto: AchievementDto = {
id: "",
eventId: exJourney1.id,
name: 'achChalJourDto',
description: 'ach dto',
requiredPoints: 1,
imageUrl: 'update test',
locationType: ChallengeLocationDto.ARTS_QUAD,
achievementType: AchievementTypeDto.TOTAL_CHALLENGES_OR_JOURNEYS,
initialOrganizationId: orgId,
};
const achChalDto: AchievementDto = {
id: "",
eventId: exChallenge1.id,
name: 'achChalDto',
description: 'ach dto',
requiredPoints: 1,
imageUrl: 'update test',
locationType: ChallengeLocationDto.ARTS_QUAD,
achievementType: AchievementTypeDto.TOTAL_CHALLENGES,
initialOrganizationId: orgId,
};

const achPtsDto: AchievementDto = {
id: "",
eventId: exChallenge2.id,
name: 'achPtsDto',
description: 'ach dto',
requiredPoints: 10,
imageUrl: 'update test',
locationType: ChallengeLocationDto.ARTS_QUAD,
achievementType: AchievementTypeDto.TOTAL_POINTS,
initialOrganizationId: orgId,
};

const achJourDto: AchievementDto = {
id: "",
eventId: exJourney2.id,
name: 'achJourDto',
description: 'ach dto',
requiredPoints: 1,
imageUrl: 'update test',
locationType: ChallengeLocationDto.ARTS_QUAD,
achievementType: AchievementTypeDto.TOTAL_JOURNEYS,
initialOrganizationId: orgId,
};

await achievementService.upsertAchievementFromDto(fullAbility, achChalDto);
await achievementService.upsertAchievementFromDto(fullAbility, achChalJourDto);
await achievementService.upsertAchievementFromDto(fullAbility, achJourDto);
await achievementService.upsertAchievementFromDto(fullAbility, achPtsDto);

const achChal = await prisma.achievement.findFirstOrThrow({
where: { name: 'achChalDto' },
});
if (updatedTracker) {
expect(updatedTracker.progress).toBeGreaterThan(initialProgress);
}
const achChalJour = await prisma.achievement.findFirstOrThrow({
where: { name: 'achChalJourDto' },
});
const achJour = await prisma.achievement.findFirstOrThrow({
where: { name: 'achJourDto' },
});
const achPts = await prisma.achievement.findFirstOrThrow({
where: { name: 'achPtsDto' },
});

const trackerChal = await achievementService.createAchievementTracker(
user,
achChal.id,
);
const trackerChalJour = await achievementService.createAchievementTracker(
user,
achChalJour.id,
);
const trackerJour = await achievementService.createAchievementTracker(
user,
achJour.id,
);
const trackerPts = await achievementService.createAchievementTracker(
user,
achPts.id,
);


await groupGateway.setCurrentEvent(user, { eventId: exChallenge1.id });
await challengeService.completeChallenge(user);

const newTrackerChal = await prisma.achievementTracker.findFirstOrThrow({where: { id: trackerChal.id }});

expect (newTrackerChal.progress).toBeGreaterThan(0);
expect (newTrackerChal.dateComplete).not.toBeNull();

expect (trackerChalJour.progress).toBe(0);
expect (trackerJour.progress).toBe(0);
expect (trackerPts.progress).toBe(0);

await groupGateway.setCurrentEvent(user, { eventId: exJourney1.id });
await challengeService.completeChallenge(user);

const newTrackerChalJour = await prisma.achievementTracker.findFirstOrThrow({where: { id: trackerChalJour.id }});
expect (newTrackerChalJour.progress).toBeGreaterThan(0);
expect (newTrackerChalJour.dateComplete).not.toBeNull();

// const completedTracker = await prisma.achievementTracker.findUnique({
// where: { id: trackerChal.id },
// });
// const completedTrackerJour = await prisma.achievementTracker.findUnique({
// where: { id: trackerChalJour.id },
// });
// const completedTrackerJour2 = await prisma.achievementTracker.findUnique({
// where: { id: trackerJour.id },
// });
// const completedTrackerPts = await prisma.achievementTracker.findUnique({
// where: { id: trackerPts.id },
// });

// expect(completedTracker?.progress).toBeGreaterThan(0);
// expect(completedTrackerJour?.progress).toBeGreaterThan(0);
// expect(completedTrackerJour2?.progress).toBeGreaterThan(0);
// expect(completedTrackerPts?.progress).toBeGreaterThan(0);

// achievem
});

});
/*
describe('Achievement tracker functions', () => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/organization/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class OrganizationService {
}

return await this.prisma.challenge.create({
data: {
data: {
...defaultChallengeData,
linkedEventId: evId,
eventIndex: index,
Expand Down

0 comments on commit 01495f3

Please sign in to comment.