feat: enforce active quest limit in assignQuest
Adds a limit check to assignQuest that reads maxActiveQuests from game settings and throws a UserError when the user has reached their active quest limit. Completed quests are excluded from the count. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -75,6 +75,12 @@ describe("questService", () => {
|
||||
|
||||
describe("assignQuest", () => {
|
||||
it("should assign quest", async () => {
|
||||
const { gameSettingsService } = await import("@shared/modules/game-settings/game-settings.service");
|
||||
const mockGetSettings = spyOn(gameSettingsService, 'getSettings').mockResolvedValue({
|
||||
quest: { maxActiveQuests: 3 },
|
||||
} as any);
|
||||
|
||||
mockFindMany.mockResolvedValue([]); // no active quests
|
||||
mockReturning.mockResolvedValue([{ userId: 1n, questId: 101 }]);
|
||||
|
||||
const result = await questService.assignQuest("1", 101);
|
||||
@@ -86,6 +92,47 @@ describe("questService", () => {
|
||||
questId: 101,
|
||||
progress: 0
|
||||
});
|
||||
|
||||
mockGetSettings.mockRestore();
|
||||
});
|
||||
|
||||
it("should throw when user has reached active quest limit", async () => {
|
||||
// Mock gameSettingsService to return maxActiveQuests: 2
|
||||
const { gameSettingsService } = await import("@shared/modules/game-settings/game-settings.service");
|
||||
const mockGetSettings = spyOn(gameSettingsService, 'getSettings').mockResolvedValue({
|
||||
quest: { maxActiveQuests: 2 },
|
||||
} as any);
|
||||
|
||||
// User has 2 incomplete quests
|
||||
mockFindMany.mockResolvedValue([
|
||||
{ userId: 1n, questId: 1, completedAt: null },
|
||||
{ userId: 1n, questId: 2, completedAt: null },
|
||||
]);
|
||||
|
||||
expect(questService.assignQuest("1", 3)).rejects.toThrow(
|
||||
"You can only have 2 active quests at a time. Complete a quest before accepting a new one."
|
||||
);
|
||||
|
||||
mockGetSettings.mockRestore();
|
||||
});
|
||||
|
||||
it("should allow assignment when completed quests don't count toward limit", async () => {
|
||||
const { gameSettingsService } = await import("@shared/modules/game-settings/game-settings.service");
|
||||
const mockGetSettings = spyOn(gameSettingsService, 'getSettings').mockResolvedValue({
|
||||
quest: { maxActiveQuests: 2 },
|
||||
} as any);
|
||||
|
||||
// User has 2 quests but 1 is completed
|
||||
mockFindMany.mockResolvedValue([
|
||||
{ userId: 1n, questId: 1, completedAt: null },
|
||||
{ userId: 1n, questId: 2, completedAt: new Date() },
|
||||
]);
|
||||
mockReturning.mockResolvedValue([{ userId: 1n, questId: 3 }]);
|
||||
|
||||
const result = await questService.assignQuest("1", 3);
|
||||
expect(result).toEqual([{ userId: 1n, questId: 3 }]);
|
||||
|
||||
mockGetSettings.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user