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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
import { assert, beforeEach, describe, expect, test } from "vitest";
import { bookmarks } from "@hoarder/db/schema";
import { BookmarkTypes } from "@hoarder/shared/types/bookmarks";
import type { CustomTestContext } from "../testUtils";
import { defaultBeforeEach } from "../testUtils";
beforeEach<CustomTestContext>(defaultBeforeEach(true));
describe("Bookmark Routes", () => {
test<CustomTestContext>("create bookmark", async ({ apiCallers }) => {
const api = apiCallers[0].bookmarks;
const bookmark = await api.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
const res = await api.getBookmark({ bookmarkId: bookmark.id });
assert(res.content.type == BookmarkTypes.LINK);
expect(res.content.url).toEqual("https://google.com");
expect(res.favourited).toEqual(false);
expect(res.archived).toEqual(false);
expect(res.content.type).toEqual(BookmarkTypes.LINK);
});
test<CustomTestContext>("delete bookmark", async ({ apiCallers }) => {
const api = apiCallers[0].bookmarks;
// Create the bookmark
const bookmark = await api.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
// It should exist
await api.getBookmark({ bookmarkId: bookmark.id });
// Delete it
await api.deleteBookmark({ bookmarkId: bookmark.id });
// It shouldn't be there anymore
await expect(() =>
api.getBookmark({ bookmarkId: bookmark.id }),
).rejects.toThrow(/Bookmark not found/);
});
test<CustomTestContext>("update bookmark", async ({ apiCallers }) => {
const api = apiCallers[0].bookmarks;
// Create the bookmark
const bookmark = await api.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
await api.updateBookmark({
bookmarkId: bookmark.id,
archived: true,
favourited: true,
});
const res = await api.getBookmark({ bookmarkId: bookmark.id });
expect(res.archived).toBeTruthy();
expect(res.favourited).toBeTruthy();
});
test<CustomTestContext>("list bookmarks", async ({ apiCallers }) => {
const api = apiCallers[0].bookmarks;
const emptyBookmarks = await api.getBookmarks({});
expect(emptyBookmarks.bookmarks.length).toEqual(0);
const bookmark1 = await api.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
const bookmark2 = await api.createBookmark({
url: "https://google2.com",
type: BookmarkTypes.LINK,
});
{
const bookmarks = await api.getBookmarks({});
expect(bookmarks.bookmarks.length).toEqual(2);
}
// Archive and favourite bookmark1
await api.updateBookmark({
bookmarkId: bookmark1.id,
archived: true,
favourited: true,
});
{
const bookmarks = await api.getBookmarks({ archived: false });
expect(bookmarks.bookmarks.length).toEqual(1);
expect(bookmarks.bookmarks[0].id).toEqual(bookmark2.id);
}
{
const bookmarks = await api.getBookmarks({ favourited: true });
expect(bookmarks.bookmarks.length).toEqual(1);
expect(bookmarks.bookmarks[0].id).toEqual(bookmark1.id);
}
{
const bookmarks = await api.getBookmarks({ archived: true });
expect(bookmarks.bookmarks.length).toEqual(1);
expect(bookmarks.bookmarks[0].id).toEqual(bookmark1.id);
}
{
const bookmarks = await api.getBookmarks({ ids: [bookmark1.id] });
expect(bookmarks.bookmarks.length).toEqual(1);
expect(bookmarks.bookmarks[0].id).toEqual(bookmark1.id);
}
});
test<CustomTestContext>("update tags", async ({ apiCallers }) => {
const api = apiCallers[0].bookmarks;
const createdBookmark = await api.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
await api.updateTags({
bookmarkId: createdBookmark.id,
attach: [
{ tagName: "tag1" },
{ tagName: "tag2" },
{ tagName: "tag3" },
{ tagName: "tag4" },
],
detach: [],
});
let bookmark = await api.getBookmark({ bookmarkId: createdBookmark.id });
expect(bookmark.tags.map((t) => t.name).sort()).toEqual([
"tag1",
"tag2",
"tag3",
"tag4",
]);
const tag1Id = bookmark.tags.filter((t) => t.name == "tag1")[0].id;
await api.updateTags({
bookmarkId: bookmark.id,
attach: [{ tagName: "tag5" }],
detach: [{ tagId: tag1Id }, { tagName: "tag4" }],
});
bookmark = await api.getBookmark({ bookmarkId: bookmark.id });
expect(bookmark.tags.map((t) => t.name).sort()).toEqual([
"tag2",
"tag3",
"tag5",
]);
await api.updateTags({
bookmarkId: bookmark.id,
attach: [{ tagId: tag1Id }, { tagName: "tag4" }],
detach: [],
});
bookmark = await api.getBookmark({ bookmarkId: bookmark.id });
expect(bookmark.tags.map((t) => t.name).sort()).toEqual([
"tag1",
"tag2",
"tag3",
"tag4",
"tag5",
]);
await expect(() =>
api.updateTags({ bookmarkId: bookmark.id, attach: [{}], detach: [] }),
).rejects.toThrow(/You must provide either a tagId or a tagName/);
await expect(() =>
api.updateTags({ bookmarkId: bookmark.id, attach: [], detach: [{}] }),
).rejects.toThrow(/You must provide either a tagId or a tagName/);
await expect(() =>
api.updateTags({
bookmarkId: bookmark.id,
attach: [{ tagName: "" }],
detach: [{}],
}),
).rejects.toThrow(/You must provide either a tagId or a tagName/);
});
test<CustomTestContext>("update bookmark text", async ({ apiCallers }) => {
const api = apiCallers[0].bookmarks;
const createdBookmark = await api.createBookmark({
text: "HELLO WORLD",
type: BookmarkTypes.TEXT,
});
await api.updateBookmarkText({
bookmarkId: createdBookmark.id,
text: "WORLD HELLO",
});
const bookmark = await api.getBookmark({ bookmarkId: createdBookmark.id });
assert(bookmark.content.type == BookmarkTypes.TEXT);
expect(bookmark.content.text).toEqual("WORLD HELLO");
});
test<CustomTestContext>("privacy", async ({ apiCallers }) => {
const user1Bookmark = await apiCallers[0].bookmarks.createBookmark({
type: BookmarkTypes.LINK,
url: "https://google.com",
});
const user2Bookmark = await apiCallers[1].bookmarks.createBookmark({
type: BookmarkTypes.LINK,
url: "https://google.com",
});
// All interactions with the wrong user should fail
await expect(() =>
apiCallers[0].bookmarks.deleteBookmark({ bookmarkId: user2Bookmark.id }),
).rejects.toThrow(/User is not allowed to access resource/);
await expect(() =>
apiCallers[0].bookmarks.getBookmark({ bookmarkId: user2Bookmark.id }),
).rejects.toThrow(/User is not allowed to access resource/);
await expect(() =>
apiCallers[0].bookmarks.updateBookmark({ bookmarkId: user2Bookmark.id }),
).rejects.toThrow(/User is not allowed to access resource/);
await expect(() =>
apiCallers[0].bookmarks.updateTags({
bookmarkId: user2Bookmark.id,
attach: [],
detach: [],
}),
).rejects.toThrow(/User is not allowed to access resource/);
// Get bookmarks should only show the correct one
expect(
(await apiCallers[0].bookmarks.getBookmarks({})).bookmarks.map(
(b) => b.id,
),
).toEqual([user1Bookmark.id]);
expect(
(await apiCallers[1].bookmarks.getBookmarks({})).bookmarks.map(
(b) => b.id,
),
).toEqual([user2Bookmark.id]);
});
test<CustomTestContext>("bookmark links dedup", async ({ apiCallers }) => {
// Two users with google in their bookmarks
const bookmark1User1 = await apiCallers[0].bookmarks.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
expect(bookmark1User1.alreadyExists).toEqual(false);
const bookmark1User2 = await apiCallers[1].bookmarks.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
expect(bookmark1User2.alreadyExists).toEqual(false);
// User1 attempting to re-add google. Should return the existing bookmark
const bookmark2User1 = await apiCallers[0].bookmarks.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
expect(bookmark2User1.alreadyExists).toEqual(true);
expect(bookmark2User1.id).toEqual(bookmark1User1.id);
// User2 attempting to re-add google. Should return the existing bookmark
const bookmark2User2 = await apiCallers[1].bookmarks.createBookmark({
url: "https://google.com",
type: BookmarkTypes.LINK,
});
expect(bookmark2User2.alreadyExists).toEqual(true);
expect(bookmark2User2.id).toEqual(bookmark1User2.id);
// User1 adding google2. Should not return an existing bookmark
const bookmark3User1 = await apiCallers[0].bookmarks.createBookmark({
url: "https://google2.com",
type: BookmarkTypes.LINK,
});
expect(bookmark3User1.alreadyExists).toEqual(false);
});
// Ensure that the pagination returns all the results
test<CustomTestContext>("pagination", async ({ apiCallers, db }) => {
const user = await apiCallers[0].users.whoami();
let now = 100_000;
const bookmarkWithDate = (date_ms: number) => ({
userId: user.id,
createdAt: new Date(date_ms),
type: BookmarkTypes.TEXT as const,
});
// One normal bookmark
const values = [bookmarkWithDate(now)];
// 10 with a second in between
for (let i = 0; i < 10; i++) {
now -= 1000;
values.push(bookmarkWithDate(now));
}
// Another ten but at the same second
for (let i = 0; i < 10; i++) {
values.push(bookmarkWithDate(now));
}
// And then another one with a second afterwards
for (let i = 0; i < 10; i++) {
now -= 1000;
values.push(bookmarkWithDate(now));
}
// In total, we should have 31 bookmarks
const inserted = await db.insert(bookmarks).values(values).returning();
const validateWithLimit = async (limit: number) => {
const results: string[] = [];
let cursor = undefined;
// To avoid running the test forever
let i = 0;
do {
const res = await apiCallers[0].bookmarks.getBookmarks({
limit,
cursor,
useCursorV2: true,
});
results.push(...res.bookmarks.map((b) => b.id));
cursor = res.nextCursor;
i++;
} while (cursor && i < 100);
expect(results.sort()).toEqual(inserted.map((b) => b.id).sort());
};
await validateWithLimit(1);
await validateWithLimit(2);
await validateWithLimit(3);
await validateWithLimit(10);
await validateWithLimit(100);
});
});
|