diff options
| author | Mohamed Bassem <me@mbassem.com> | 2025-02-23 22:50:12 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2025-02-23 23:16:52 +0000 |
| commit | 14e4fed321634dc014ad2f15cafef3ed0123855e (patch) | |
| tree | 0b7b1f1157bacea13b93161c07ab48561544fd28 /packages/trpc/routers/assets.test.ts | |
| parent | e5cb9aa848009ea22c1385e4d33b7edf372979fb (diff) | |
| download | karakeep-14e4fed321634dc014ad2f15cafef3ed0123855e.tar.zst | |
feat: Add a setting page to manage assets. Fixes #730
Diffstat (limited to 'packages/trpc/routers/assets.test.ts')
| -rw-r--r-- | packages/trpc/routers/assets.test.ts | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/packages/trpc/routers/assets.test.ts b/packages/trpc/routers/assets.test.ts new file mode 100644 index 00000000..d7db35be --- /dev/null +++ b/packages/trpc/routers/assets.test.ts @@ -0,0 +1,128 @@ +import { beforeEach, describe, expect, test } from "vitest"; + +import { assets, AssetTypes } from "@hoarder/db/schema"; +import { BookmarkTypes, ZAssetType } from "@hoarder/shared/types/bookmarks"; + +import type { CustomTestContext } from "../testUtils"; +import { defaultBeforeEach } from "../testUtils"; + +beforeEach<CustomTestContext>(defaultBeforeEach(true)); + +describe("Asset Routes", () => { + test<CustomTestContext>("mutate assets", async ({ apiCallers, db }) => { + const api = apiCallers[0].assets; + const userId = await apiCallers[0].users.whoami().then((u) => u.id); + + const bookmark = await apiCallers[0].bookmarks.createBookmark({ + url: "https://google.com", + type: BookmarkTypes.LINK, + }); + await Promise.all([ + db.insert(assets).values({ + id: "asset1", + assetType: AssetTypes.LINK_SCREENSHOT, + bookmarkId: bookmark.id, + userId, + }), + db.insert(assets).values({ + id: "asset2", + assetType: AssetTypes.LINK_BANNER_IMAGE, + bookmarkId: bookmark.id, + userId, + }), + db.insert(assets).values({ + id: "asset3", + assetType: AssetTypes.LINK_FULL_PAGE_ARCHIVE, + bookmarkId: bookmark.id, + userId, + }), + db.insert(assets).values({ + id: "asset4", + assetType: AssetTypes.UNKNOWN, + bookmarkId: null, + userId, + }), + db.insert(assets).values({ + id: "asset5", + assetType: AssetTypes.UNKNOWN, + bookmarkId: null, + userId, + }), + db.insert(assets).values({ + id: "asset6", + assetType: AssetTypes.UNKNOWN, + bookmarkId: null, + userId, + }), + ]); + + const validateAssets = async ( + expected: { id: string; assetType: ZAssetType }[], + ) => { + const b = await apiCallers[0].bookmarks.getBookmark({ + bookmarkId: bookmark.id, + }); + b.assets.sort((a, b) => a.id.localeCompare(b.id)); + expect(b.assets).toEqual(expected); + }; + + await api.attachAsset({ + bookmarkId: bookmark.id, + asset: { + id: "asset4", + assetType: "screenshot", + }, + }); + + await validateAssets([ + { id: "asset1", assetType: "screenshot" }, + { id: "asset2", assetType: "bannerImage" }, + { id: "asset3", assetType: "fullPageArchive" }, + { id: "asset4", assetType: "screenshot" }, + ]); + + await api.replaceAsset({ + bookmarkId: bookmark.id, + oldAssetId: "asset1", + newAssetId: "asset5", + }); + + await validateAssets([ + { id: "asset2", assetType: "bannerImage" }, + { id: "asset3", assetType: "fullPageArchive" }, + { id: "asset4", assetType: "screenshot" }, + { id: "asset5", assetType: "screenshot" }, + ]); + + await api.detachAsset({ + bookmarkId: bookmark.id, + assetId: "asset4", + }); + + await validateAssets([ + { id: "asset2", assetType: "bannerImage" }, + { id: "asset3", assetType: "fullPageArchive" }, + { id: "asset5", assetType: "screenshot" }, + ]); + + // You're not allowed to attach/replace a fullPageArchive + await expect( + async () => + await api.replaceAsset({ + bookmarkId: bookmark.id, + oldAssetId: "asset3", + newAssetId: "asset6", + }), + ).rejects.toThrow(/You can't attach this type of asset/); + await expect( + async () => + await api.attachAsset({ + bookmarkId: bookmark.id, + asset: { + id: "asset6", + assetType: "fullPageArchive", + }, + }), + ).rejects.toThrow(/You can't attach this type of asset/); + }); +}); |
