From 0fc3ddcaad58953a2967f93e49ea54e2ef4e68bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=A8=20Audrey=20=E2=9C=A8?= Date: Thu, 20 Mar 2025 14:58:51 -0400 Subject: [PATCH] additional unit tests --- .../achievements/achievement-hub.spec.ts | 61 +++++++++++++++++-- .../src/tools/achievements/achievement-hub.ts | 5 -- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/libs/common/src/tools/achievements/achievement-hub.spec.ts b/libs/common/src/tools/achievements/achievement-hub.spec.ts index 3615a7eda9d..cb9a4e5e77e 100644 --- a/libs/common/src/tools/achievements/achievement-hub.spec.ts +++ b/libs/common/src/tools/achievements/achievement-hub.spec.ts @@ -4,8 +4,9 @@ import { ConsoleLogService } from "../../platform/services/console-log.service"; import { consoleSemanticLoggerProvider } from "../log"; import { AchievementHub } from "./achievement-hub"; -import { ItemCreatedEarnedEvent } from "./examples/achievement-events"; +import { ItemCreatedEarnedEvent, ItemCreatedProgressEvent } from "./examples/achievement-events"; import { + ItemCreatedTracker, TotallyAttachedAchievement, TotallyAttachedValidator, } from "./examples/example-validators"; @@ -81,7 +82,7 @@ describe("AchievementHub", () => { const validators$ = new Subject(); const events$ = new Subject(); const achievements$ = new Subject(); - const hub = new AchievementHub(validators$, events$, achievements$); + const hub = new AchievementHub(validators$, events$, achievements$, 10, testLog); const results$ = new ReplaySubject(3); achievements$.next(ItemCreatedEarnedEvent); achievements$.complete(); @@ -98,24 +99,72 @@ describe("AchievementHub", () => { }); describe("earned$", () => { - it("", async () => { + it("includes earned achievements from constructor", async () => { const validators$ = new Subject(); const events$ = new Subject(); const achievements$ = new Subject(); - const hub = new AchievementHub(validators$, events$, achievements$); + const hub = new AchievementHub(validators$, events$, achievements$, 10, testLog); const results$ = new ReplaySubject>(1); hub.earned$().subscribe(results$); + + achievements$.next(ItemCreatedEarnedEvent); + achievements$.complete(); + + const result = await firstValueFrom(results$); + expect(result.get(ItemCreatedEarnedEvent.achievement.name)).toEqual(ItemCreatedEarnedEvent); + }); + + it("includes earned achievements from validators", async () => { + const validators$ = new BehaviorSubject([TotallyAttachedValidator]); + const events$ = new Subject(); + const achievements$ = new Subject(); + const hub = new AchievementHub(validators$, events$, achievements$, 10, testLog); + const results$ = new ReplaySubject>(1); + hub.earned$().subscribe(results$); + + achievements$.complete(); + events$.next(ItemAddedEvent); + + const result = await firstValueFrom(results$); + expect(result.get(TotallyAttachedValidator.achievement)).toMatchObject({ + achievement: { type: "earned", name: TotallyAttachedValidator.achievement }, + }); }); }); describe("metrics$", () => { - it("", async () => { + it("includes progress events from constructor", async () => { const validators$ = new Subject(); const events$ = new Subject(); const achievements$ = new Subject(); - const hub = new AchievementHub(validators$, events$, achievements$); + const hub = new AchievementHub(validators$, events$, achievements$, 10, testLog); const results$ = new ReplaySubject>(1); hub.metrics$().subscribe(results$); + + achievements$.next(ItemCreatedProgressEvent); + achievements$.complete(); + + const result = await firstValueFrom(results$); + expect(result.get(ItemCreatedProgressEvent.achievement.name)).toEqual( + ItemCreatedProgressEvent, + ); + }); + + it("includes progress events from constructor", async () => { + const validators$ = new BehaviorSubject([ItemCreatedTracker]); + const events$ = new Subject(); + const achievements$ = new Subject(); + const hub = new AchievementHub(validators$, events$, achievements$, 10, testLog); + const results$ = new ReplaySubject>(1); + hub.metrics$().subscribe(results$); + + achievements$.complete(); + events$.next(ItemAddedEvent); + + const result = await firstValueFrom(results$); + expect(result.get(ItemCreatedTracker.active.metric)).toMatchObject({ + achievement: { type: "progress", name: ItemCreatedTracker.active.metric, value: 1 }, + }); }); }); }); diff --git a/libs/common/src/tools/achievements/achievement-hub.ts b/libs/common/src/tools/achievements/achievement-hub.ts index ec9366c7aac..b24f9802351 100644 --- a/libs/common/src/tools/achievements/achievement-hub.ts +++ b/libs/common/src/tools/achievements/achievement-hub.ts @@ -3,7 +3,6 @@ import { ReplaySubject, Subject, concat, - debounceTime, filter, map, shareReplay, @@ -27,8 +26,6 @@ import { UserActionEvent, } from "./types"; -const ACHIEVEMENT_INITIAL_DEBOUNCE_MS = 100; - export class AchievementHub { /** Instantiates the achievement hub. A new achievement hub should be created * per-user, and streams should be partitioned by user. @@ -88,7 +85,6 @@ export class AchievementHub { filter((e) => isEarnedEvent(e)), map((e) => e as AchievementEarnedEvent), latestEarnedMetrics(), - debounceTime(ACHIEVEMENT_INITIAL_DEBOUNCE_MS), tap((m) => this.log.debug(m, "earned achievements update")), startWith(new Map()), ); @@ -99,7 +95,6 @@ export class AchievementHub { filter((e) => isProgressEvent(e)), map((e) => e as AchievementProgressEvent), latestProgressMetrics(), - debounceTime(ACHIEVEMENT_INITIAL_DEBOUNCE_MS), tap((m) => this.log.debug(m, "achievement metrics update")), startWith(new Map()), );