diff options
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/trpc/lib/__tests__/ruleEngine.test.ts | 21 | ||||
| -rw-r--r-- | packages/trpc/lib/ruleEngine.ts | 7 |
2 files changed, 15 insertions, 13 deletions
diff --git a/packages/trpc/lib/__tests__/ruleEngine.test.ts b/packages/trpc/lib/__tests__/ruleEngine.test.ts index d7f216e5..b737e3a5 100644 --- a/packages/trpc/lib/__tests__/ruleEngine.test.ts +++ b/packages/trpc/lib/__tests__/ruleEngine.test.ts @@ -172,10 +172,9 @@ describe("RuleEngine", () => { expect(engine).toBeInstanceOf(RuleEngine); }); - it("should throw an error if bookmark is not found", async () => { - await expect( - RuleEngine.forBookmark(ctx, "nonexistent-bookmark"), - ).rejects.toThrow("Bookmark nonexistent-bookmark not found"); + it("should return null if bookmark is not found", async () => { + const engine = await RuleEngine.forBookmark(ctx, "nonexistent-bookmark"); + expect(engine).toBeNull(); }); it("should load rules associated with the bookmark's user", async () => { @@ -189,7 +188,7 @@ describe("RuleEngine", () => { actions: [{ type: "addTag", tagId: tagId2 }], }); - const engine = await RuleEngine.forBookmark(ctx, bookmarkId); + const engine = (await RuleEngine.forBookmark(ctx, bookmarkId))!; // @ts-expect-error Accessing private property for test verification expect(engine.rules).toHaveLength(1); // @ts-expect-error Accessing private property for test verification @@ -201,7 +200,7 @@ describe("RuleEngine", () => { let engine: RuleEngine; beforeEach(async () => { - engine = await RuleEngine.forBookmark(ctx, bookmarkId); + engine = (await RuleEngine.forBookmark(ctx, bookmarkId))!; }); it("should return true for urlContains condition", () => { @@ -320,7 +319,7 @@ describe("RuleEngine", () => { .update(bookmarks) .set({ favourited: true }) .where(eq(bookmarks.id, bookmarkId)); - const updatedEngine = await RuleEngine.forBookmark(ctx, bookmarkId); + const updatedEngine = (await RuleEngine.forBookmark(ctx, bookmarkId))!; const condition: RuleEngineCondition = { type: "isFavourited" }; expect(updatedEngine.doesBookmarkMatchConditions(condition)).toBe(true); }); @@ -335,7 +334,7 @@ describe("RuleEngine", () => { .update(bookmarks) .set({ archived: true }) .where(eq(bookmarks.id, bookmarkId)); - const updatedEngine = await RuleEngine.forBookmark(ctx, bookmarkId); + const updatedEngine = (await RuleEngine.forBookmark(ctx, bookmarkId))!; const condition: RuleEngineCondition = { type: "isArchived" }; expect(updatedEngine.doesBookmarkMatchConditions(condition)).toBe(true); }); @@ -403,7 +402,7 @@ describe("RuleEngine", () => { } as Omit<RuleEngineRule, "id"> & { userId: string }; ruleId = await seedRule(tmp); testRule = { ...tmp, id: ruleId }; - engine = await RuleEngine.forBookmark(ctx, bookmarkId); + engine = (await RuleEngine.forBookmark(ctx, bookmarkId))!; }); it("should evaluate rule successfully when event and conditions match", async () => { @@ -492,7 +491,7 @@ describe("RuleEngine", () => { let engine: RuleEngine; beforeEach(async () => { - engine = await RuleEngine.forBookmark(ctx, bookmarkId); + engine = (await RuleEngine.forBookmark(ctx, bookmarkId))!; }); it("should execute addTag action", async () => { @@ -674,7 +673,7 @@ describe("RuleEngine", () => { actions: [{ type: "addToList", listId: listId1 }], }); - engine = await RuleEngine.forBookmark(ctx, bookmarkId); + engine = (await RuleEngine.forBookmark(ctx, bookmarkId))!; }); it("should process event and return only results for matching, enabled rules", async () => { diff --git a/packages/trpc/lib/ruleEngine.ts b/packages/trpc/lib/ruleEngine.ts index 233a6acf..acfd747e 100644 --- a/packages/trpc/lib/ruleEngine.ts +++ b/packages/trpc/lib/ruleEngine.ts @@ -72,13 +72,16 @@ export class RuleEngine { ); } - static async forBookmark(ctx: AuthedContext, bookmarkId: string) { + static async forBookmark( + ctx: AuthedContext, + bookmarkId: string, + ): Promise<RuleEngine | null> { const [bookmark, rules] = await Promise.all([ fetchBookmark(ctx.db, bookmarkId), RuleEngineRuleModel.getAll(ctx), ]); if (!bookmark) { - throw new Error(`Bookmark ${bookmarkId} not found`); + return null; } return new RuleEngine( ctx, |
