diff options
Diffstat (limited to 'packages/trpc')
| -rw-r--r-- | packages/trpc/routers/bookmarks.ts | 9 | ||||
| -rw-r--r-- | packages/trpc/routers/tags.test.ts | 34 |
2 files changed, 40 insertions, 3 deletions
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index 815cf90d..2a02a0cd 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -50,6 +50,7 @@ import { zSearchBookmarksRequestSchema, zUpdateBookmarksRequestSchema, } from "@karakeep/shared/types/bookmarks"; +import { normalizeTagName } from "@karakeep/shared/utils/tag"; import type { AuthedContext, Context } from "../index"; import { authedProcedure, router } from "../index"; @@ -867,9 +868,11 @@ export const bookmarksAppRouter = router({ }; } - const toAddTagNames = input.attach.flatMap((i) => - i.tagName ? [i.tagName] : [], - ); + const toAddTagNames = input.attach + .flatMap((i) => (i.tagName ? [i.tagName] : [])) + .map(normalizeTagName) // strip leading # + .filter((n) => n.length > 0); // drop empty results + const toAddTagIds = input.attach.flatMap((i) => i.tagId ? [i.tagId] : [], ); diff --git a/packages/trpc/routers/tags.test.ts b/packages/trpc/routers/tags.test.ts index c5f92cb2..1e7118d2 100644 --- a/packages/trpc/routers/tags.test.ts +++ b/packages/trpc/routers/tags.test.ts @@ -143,4 +143,38 @@ describe("Tags Routes", () => { const resUser2 = await apiUser2.list(); expect(resUser2.tags.some((tag) => tag.name === "user1Tag")).toBeFalsy(); // Should not see other user's tags }); + + test<CustomTestContext>("create strips extra leading hashes", async ({ + apiCallers, + db, + }) => { + const api = apiCallers[0].tags; + + const created = await api.create({ name: "##demo" }); + expect(created.name).toBe("demo"); + + // Confirm DB row too + const row = await db.query.bookmarkTags.findFirst({ + where: eq(bookmarkTags.id, created.id), + }); + expect(row?.name).toBe("demo"); + }); + + test<CustomTestContext>("update normalizes leading hashes", async ({ + apiCallers, + db, + }) => { + const api = apiCallers[0].tags; + + const created = await api.create({ name: "#foo" }); + const updated = await api.update({ tagId: created.id, name: "##bar" }); + + expect(updated.name).toBe("bar"); + + // Confirm DB row too + const row = await db.query.bookmarkTags.findFirst({ + where: eq(bookmarkTags.id, updated.id), + }); + expect(row?.name).toBe("bar"); + }); }); |
