Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 234 additions & 1 deletion __tests__/questProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@ import {
User,
} from '../src/entity';
import { UserQuest, UserQuestStatus } from '../src/entity/user';
import { checkQuestProgress } from '../src/common/quest';
import {
checkQuestProgress,
syncMilestoneQuestProgress,
} from '../src/common/quest';
import { createMockLogger, saveFixtures } from './helpers';

const userId = '11111111-1111-4111-8111-111111111111';
const questIds = [
'22222222-2222-4222-8222-222222222222',
'33333333-3333-4333-8333-333333333333',
'44444444-4444-4444-8444-444444444444',
'88888888-8888-4888-8888-888888888881',
'88888888-8888-4888-8888-888888888882',
'88888888-8888-4888-8888-888888888883',
];
const rotationIds = [
'55555555-5555-4555-8555-555555555555',
'66666666-6666-4666-8666-666666666666',
'77777777-7777-4777-8777-777777777777',
'99999999-9999-4999-8999-999999999991',
'99999999-9999-4999-8999-999999999992',
'99999999-9999-4999-8999-999999999993',
];

let con: DataSource;
Expand Down Expand Up @@ -196,6 +205,230 @@ describe('checkQuestProgress', () => {
expect(userQuest.status).toBe(UserQuestStatus.InProgress);
});

it('should not advance quest completion milestone on completion alone, only on claim', async () => {
const now = new Date();
const logger = createMockLogger();
const periodStart = new Date(now.getTime() - 60 * 60 * 1000);
const periodEnd = new Date(now.getTime() + 60 * 60 * 1000);
const milestonePeriodStart = new Date('2026-03-25T00:00:00.000Z');
const milestonePeriodEnd = new Date('9999-12-31T23:59:59.000Z');

await saveFixtures(con, Quest, [
{
id: questIds[3],
name: 'Up and comer',
description: 'Claim 2 quests',
type: QuestType.Milestone,
eventType: QuestEventType.QuestComplete,
criteria: { targetCount: 2 },
active: true,
},
{
id: questIds[4],
name: 'Daily upvotes 1',
description: 'Upvote 1 post',
type: QuestType.Daily,
eventType: QuestEventType.PostUpvote,
criteria: { targetCount: 1 },
active: true,
},
{
id: questIds[5],
name: 'Daily upvotes 2',
description: 'Upvote 1 more post',
type: QuestType.Daily,
eventType: QuestEventType.PostUpvote,
criteria: { targetCount: 1 },
active: true,
},
]);

await saveFixtures(con, QuestRotation, [
{
id: rotationIds[3],
questId: questIds[3],
type: QuestType.Milestone,
plusOnly: false,
slot: 1,
periodStart: milestonePeriodStart,
periodEnd: milestonePeriodEnd,
},
{
id: rotationIds[4],
questId: questIds[4],
type: QuestType.Daily,
plusOnly: false,
slot: 1,
periodStart,
periodEnd,
},
{
id: rotationIds[5],
questId: questIds[5],
type: QuestType.Daily,
plusOnly: false,
slot: 2,
periodStart,
periodEnd,
},
]);

const didUpdate = await checkQuestProgress({
con,
logger,
userId,
eventType: QuestEventType.PostUpvote,
incrementBy: 1,
now,
});

expect(didUpdate).toBe(true);

const userQuests = await con.getRepository(UserQuest).find({
where: { userId },
order: { rotationId: 'ASC' },
});

expect(userQuests).toHaveLength(3);
expect(
userQuests
.filter(({ rotationId }) =>
[rotationIds[4], rotationIds[5]].includes(rotationId),
)
.map(({ progress, status }) => ({ progress, status })),
).toEqual([
{
progress: 1,
status: UserQuestStatus.Completed,
},
{
progress: 1,
status: UserQuestStatus.Completed,
},
]);

const milestoneQuest = userQuests.find(
({ rotationId }) => rotationId === rotationIds[3],
);
expect(milestoneQuest).toMatchObject({
progress: 0,
status: UserQuestStatus.InProgress,
});
});

it('should advance quest completion milestone when quests are claimed', async () => {
const now = new Date();
const logger = createMockLogger();
const periodStart = new Date(now.getTime() - 60 * 60 * 1000);
const periodEnd = new Date(now.getTime() + 60 * 60 * 1000);
const milestonePeriodStart = new Date('2026-03-25T00:00:00.000Z');
const milestonePeriodEnd = new Date('9999-12-31T23:59:59.000Z');

await saveFixtures(con, Quest, [
{
id: questIds[3],
name: 'Up and comer',
description: 'Claim 2 quests',
type: QuestType.Milestone,
eventType: QuestEventType.QuestComplete,
criteria: { targetCount: 2 },
active: true,
},
{
id: questIds[4],
name: 'Daily upvotes 1',
description: 'Upvote 1 post',
type: QuestType.Daily,
eventType: QuestEventType.PostUpvote,
criteria: { targetCount: 1 },
active: true,
},
{
id: questIds[5],
name: 'Daily upvotes 2',
description: 'Upvote 1 more post',
type: QuestType.Daily,
eventType: QuestEventType.PostUpvote,
criteria: { targetCount: 1 },
active: true,
},
]);

await saveFixtures(con, QuestRotation, [
{
id: rotationIds[3],
questId: questIds[3],
type: QuestType.Milestone,
plusOnly: false,
slot: 1,
periodStart: milestonePeriodStart,
periodEnd: milestonePeriodEnd,
},
{
id: rotationIds[4],
questId: questIds[4],
type: QuestType.Daily,
plusOnly: false,
slot: 1,
periodStart,
periodEnd,
},
{
id: rotationIds[5],
questId: questIds[5],
type: QuestType.Daily,
plusOnly: false,
slot: 2,
periodStart,
periodEnd,
},
]);

// Complete the daily quests
await checkQuestProgress({
con,
logger,
userId,
eventType: QuestEventType.PostUpvote,
incrementBy: 1,
now,
});

// Simulate claiming both daily quests
const dailyUserQuests = await con.getRepository(UserQuest).find({
where: {
userId,
rotationId: In([rotationIds[4], rotationIds[5]]),
},
});

for (const uq of dailyUserQuests) {
await con
.getRepository(UserQuest)
.update(
{ id: uq.id },
{ status: UserQuestStatus.Claimed, claimedAt: now },
);
}

// Sync milestones after claims
await syncMilestoneQuestProgress({
con,
userId,
eventType: QuestEventType.QuestComplete,
now,
});

const milestoneQuest = await con.getRepository(UserQuest).findOneOrFail({
where: { userId, rotationId: rotationIds[3] },
});

expect(milestoneQuest).toMatchObject({
progress: 2,
status: UserQuestStatus.Completed,
});
});

it('should not update claimed quests', async () => {
const now = new Date();
const logger = createMockLogger();
Expand Down
Loading
Loading