aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/bookmarks.test.ts
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-09-22 14:56:19 +0000
committerMohamedBassem <me@mbassem.com>2024-09-22 15:04:20 +0000
commita770e55520245b7afc2b7a30aa6127eebcb6ea0d (patch)
tree7a2e4041e2d16413ee0e8dd060be41b58253c995 /packages/trpc/routers/bookmarks.test.ts
parent55f5c7f40d6569d0769a3b7a9060db5ec1d3b93b (diff)
downloadkarakeep-a770e55520245b7afc2b7a30aa6127eebcb6ea0d.tar.zst
feature(web): Show attachments and allow users to manipulate them.
Diffstat (limited to 'packages/trpc/routers/bookmarks.test.ts')
-rw-r--r--packages/trpc/routers/bookmarks.test.ts97
1 files changed, 95 insertions, 2 deletions
diff --git a/packages/trpc/routers/bookmarks.test.ts b/packages/trpc/routers/bookmarks.test.ts
index 802bd992..5219e522 100644
--- a/packages/trpc/routers/bookmarks.test.ts
+++ b/packages/trpc/routers/bookmarks.test.ts
@@ -1,7 +1,7 @@
import { assert, beforeEach, describe, expect, test } from "vitest";
-import { bookmarks } from "@hoarder/db/schema";
-import { BookmarkTypes } from "@hoarder/shared/types/bookmarks";
+import { assets, AssetTypes, bookmarks } from "@hoarder/db/schema";
+import { BookmarkTypes, ZAssetType } from "@hoarder/shared/types/bookmarks";
import type { CustomTestContext } from "../testUtils";
import { defaultBeforeEach } from "../testUtils";
@@ -326,4 +326,97 @@ describe("Bookmark Routes", () => {
await validateWithLimit(10);
await validateWithLimit(100);
});
+
+ test<CustomTestContext>("mutate assets", async ({ apiCallers, db }) => {
+ const api = apiCallers[0].bookmarks;
+
+ const bookmark = await api.createBookmark({
+ url: "https://google.com",
+ type: BookmarkTypes.LINK,
+ });
+ await Promise.all([
+ db.insert(assets).values({
+ id: "asset1",
+ assetType: AssetTypes.LINK_SCREENSHOT,
+ bookmarkId: bookmark.id,
+ }),
+ db.insert(assets).values({
+ id: "asset2",
+ assetType: AssetTypes.LINK_BANNER_IMAGE,
+ bookmarkId: bookmark.id,
+ }),
+ db.insert(assets).values({
+ id: "asset3",
+ assetType: AssetTypes.LINK_FULL_PAGE_ARCHIVE,
+ bookmarkId: bookmark.id,
+ }),
+ ]);
+
+ const validateAssets = async (
+ expected: { id: string; assetType: ZAssetType }[],
+ ) => {
+ const b = await api.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: "asset4",
+ }),
+ ).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/);
+ });
});