aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/assets.test.ts
blob: 822e48b1edaeda2f2f9294079dc2ce642d77d794 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { beforeEach, describe, expect, test } from "vitest";

import { assets, AssetTypes } from "@karakeep/db/schema";
import { BookmarkTypes, ZAssetType } from "@karakeep/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/);
  });
});