aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/lists.test.ts
blob: 8797b35e525fcd7d99d100b6999c969a46e5c871 (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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import { beforeEach, describe, expect, test } from "vitest";
import { z } from "zod";

import {
  BookmarkTypes,
  zNewBookmarkRequestSchema,
} from "@karakeep/shared/types/bookmarks";
import { zNewBookmarkListSchema } from "@karakeep/shared/types/lists";

import type { APICallerType, CustomTestContext } from "../testUtils";
import { defaultBeforeEach } from "../testUtils";

async function createTestBookmark(api: APICallerType) {
  const newBookmarkInput: z.infer<typeof zNewBookmarkRequestSchema> = {
    type: BookmarkTypes.TEXT,
    text: "Test bookmark text",
  };
  const createdBookmark = await api.bookmarks.createBookmark(newBookmarkInput);
  return createdBookmark.id;
}

beforeEach<CustomTestContext>(defaultBeforeEach(true));

describe("Lists Routes", () => {
  test<CustomTestContext>("create list", async ({ apiCallers }) => {
    const api = apiCallers[0].lists;
    const newListInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Test List",
      description: "A test list",
      icon: "📋",
      type: "manual",
    };

    const createdList = await api.create(newListInput);

    expect(createdList).toMatchObject({
      name: newListInput.name,
      description: newListInput.description,
      icon: newListInput.icon,
      type: newListInput.type,
    });

    const lists = await api.list();
    const listFromList = lists.lists.find((l) => l.id === createdList.id);
    expect(listFromList).toBeDefined();
    expect(listFromList?.name).toEqual(newListInput.name);
  });

  test<CustomTestContext>("edit list", async ({ apiCallers }) => {
    const api = apiCallers[0].lists;

    // First, create a list
    const createdListInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Original List",
      description: "Original description",
      icon: "📋",
      type: "manual",
    };
    const createdList = await api.create(createdListInput);

    // Update it
    const updatedListInput = {
      listId: createdList.id,
      name: "Updated List",
      description: "Updated description",
      icon: "⭐️",
    };
    const updatedList = await api.edit(updatedListInput);

    expect(updatedList.name).toEqual(updatedListInput.name);
    expect(updatedList.description).toEqual(updatedListInput.description);
    expect(updatedList.icon).toEqual(updatedListInput.icon);

    // Verify the update
    const lists = await api.list();
    const listFromList = lists.lists.find((l) => l.id === createdList.id);
    expect(listFromList).toBeDefined();
    expect(listFromList?.name).toEqual(updatedListInput.name);

    // Test editing a non-existent list
    await expect(() =>
      api.edit({ listId: "non-existent-id", name: "Fail" }),
    ).rejects.toThrow(/List not found/);
  });

  test<CustomTestContext>("merge lists", async ({ apiCallers }) => {
    const api = apiCallers[0].lists;

    // First, create a real bookmark
    const bookmarkId = await createTestBookmark(apiCallers[0]);

    // Create two lists
    const sourceListInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Source List",
      type: "manual",
      icon: "📚",
    };
    const targetListInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Target List",
      type: "manual",
      icon: "📖",
    };
    const sourceList = await api.create(sourceListInput);
    const targetList = await api.create(targetListInput);

    // Add the real bookmark to source list
    await api.addToList({ listId: sourceList.id, bookmarkId });

    // Merge
    await api.merge({
      sourceId: sourceList.id,
      targetId: targetList.id,
      deleteSourceAfterMerge: true,
    });

    // Verify source list is deleted and bookmark is in target
    const lists = await api.list();
    expect(lists.lists.find((l) => l.id === sourceList.id)).toBeUndefined();
    const targetListsOfBookmark = await api.getListsOfBookmark({
      bookmarkId,
    });
    expect(
      targetListsOfBookmark.lists.find((l) => l.id === targetList.id),
    ).toBeDefined();

    // Test merging invalid lists
    await expect(() =>
      api.merge({
        sourceId: sourceList.id,
        targetId: "non-existent-id",
        deleteSourceAfterMerge: true,
      }),
    ).rejects.toThrow(/List not found/);
  });

  test<CustomTestContext>("delete list", async ({ apiCallers }) => {
    const api = apiCallers[0].lists;

    // Create a list
    const createdListInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "List to Delete",
      type: "manual",
      icon: "📚",
    };
    const createdList = await api.create(createdListInput);

    // Delete it
    await api.delete({ listId: createdList.id });

    // Verify it's deleted
    const lists = await api.list();
    expect(lists.lists.find((l) => l.id === createdList.id)).toBeUndefined();

    // Test deleting a non-existent list
    await expect(() =>
      api.delete({ listId: "non-existent-id" }),
    ).rejects.toThrow(/List not found/);
  });

  test<CustomTestContext>("add and remove from list", async ({
    apiCallers,
  }) => {
    const api = apiCallers[0].lists;

    // First, create a real bookmark
    const bookmarkId = await createTestBookmark(apiCallers[0]);

    // Create a manual list
    const listInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Manual List",
      type: "manual",
      icon: "📚",
    };
    const createdList = await api.create(listInput);

    // Add to list
    await api.addToList({ listId: createdList.id, bookmarkId });

    // Verify addition
    const listsOfBookmark = await api.getListsOfBookmark({
      bookmarkId,
    });
    expect(
      listsOfBookmark.lists.find((l) => l.id === createdList.id),
    ).toBeDefined();

    // Remove from list
    await api.removeFromList({ listId: createdList.id, bookmarkId });

    // Verify removal
    const updatedListsOfBookmark = await api.getListsOfBookmark({
      bookmarkId,
    });
    expect(
      updatedListsOfBookmark.lists.find((l) => l.id === createdList.id),
    ).toBeUndefined();

    // Test on smart list (should fail)
    const smartListInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Smart List",
      type: "smart",
      query: "#example",
      icon: "📚",
    };
    const smartList = await api.create(smartListInput);
    await expect(() =>
      api.addToList({ listId: smartList.id, bookmarkId }),
    ).rejects.toThrow(/Smart lists cannot be added to/);
  });

  test<CustomTestContext>("get and list lists", async ({ apiCallers }) => {
    const api = apiCallers[0].lists;

    const newListInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Get Test List",
      type: "manual",
      icon: "📚",
    };
    const createdList = await api.create(newListInput);

    const getList = await api.get({ listId: createdList.id });
    expect(getList.name).toEqual(newListInput.name);

    const lists = await api.list();
    expect(lists.lists.length).toBeGreaterThan(0);
    expect(lists.lists.find((l) => l.id === createdList.id)).toBeDefined();
  });

  test<CustomTestContext>("get lists of bookmark and stats", async ({
    apiCallers,
  }) => {
    const api = apiCallers[0].lists;

    // First, create a real bookmark
    const bookmarkId = await createTestBookmark(apiCallers[0]);

    // Create a list and add the bookmark
    const listInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Stats Test List",
      type: "manual",
      icon: "📚",
    };
    const createdList = await api.create(listInput);
    await api.addToList({ listId: createdList.id, bookmarkId });

    const listsOfBookmark = await api.getListsOfBookmark({
      bookmarkId,
    });
    expect(listsOfBookmark.lists.length).toBeGreaterThan(0);

    const stats = await api.stats();
    expect(stats.stats.get(createdList.id)).toBeGreaterThan(0);
  });
});

describe("recursive delete", () => {
  test<CustomTestContext>("non-recursive delete (deleteChildren=false)", async ({
    apiCallers,
  }) => {
    const api = apiCallers[0].lists;

    // Create parent list
    const parentInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Parent List",
      type: "manual",
      icon: "📂",
    };
    const parentList = await api.create(parentInput);

    // Create child list
    const childInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Child List",
      parentId: parentList.id,
      type: "manual",
      icon: "📄",
    };
    const childList = await api.create(childInput);

    // Test both default behavior and explicit false
    // Default (should be false)
    await api.delete({ listId: parentList.id });

    let lists = await api.list();
    expect(lists.lists.find((l) => l.id === parentList.id)).toBeUndefined();
    let remainingChild = lists.lists.find((l) => l.id === childList.id);
    expect(remainingChild).toBeDefined();
    expect(remainingChild?.parentId).toBeNull();

    // Create another parent-child pair to test explicit false
    const parent2 = await api.create({
      name: "Parent List 2",
      type: "manual",
      icon: "📂",
    });
    const child2 = await api.create({
      name: "Child List 2",
      parentId: parent2.id,
      type: "manual",
      icon: "📄",
    });

    // Explicit deleteChildren=false
    await api.delete({ listId: parent2.id, deleteChildren: false });

    lists = await api.list();
    expect(lists.lists.find((l) => l.id === parent2.id)).toBeUndefined();
    remainingChild = lists.lists.find((l) => l.id === child2.id);
    expect(remainingChild).toBeDefined();
    expect(remainingChild?.parentId).toBeNull();
  });

  test<CustomTestContext>("recursive delete with multiple children", async ({
    apiCallers,
  }) => {
    const api = apiCallers[0].lists;

    // Create parent list
    const parentInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Parent List",
      type: "manual",
      icon: "📂",
    };
    const parentList = await api.create(parentInput);

    // Create multiple child lists
    const child1Input: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Child List 1",
      parentId: parentList.id,
      type: "manual",
      icon: "📄",
    };
    const child1 = await api.create(child1Input);

    const child2Input: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Child List 2",
      parentId: parentList.id,
      type: "manual",
      icon: "📄",
    };
    const child2 = await api.create(child2Input);

    const child3Input: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Child List 3",
      parentId: parentList.id,
      type: "smart",
      query: "is:fav",
      icon: "",
    };
    const child3 = await api.create(child3Input);

    // Delete parent with deleteChildren=true
    await api.delete({ listId: parentList.id, deleteChildren: true });

    // Verify all lists are deleted
    const lists = await api.list();
    expect(lists.lists.find((l) => l.id === parentList.id)).toBeUndefined();
    expect(lists.lists.find((l) => l.id === child1.id)).toBeUndefined();
    expect(lists.lists.find((l) => l.id === child2.id)).toBeUndefined();
    expect(lists.lists.find((l) => l.id === child3.id)).toBeUndefined();
  });

  test<CustomTestContext>("recursive delete preserves bookmarks in deleted lists", async ({
    apiCallers,
  }) => {
    const api = apiCallers[0].lists;

    // Create a bookmark first
    const bookmarkId = await createTestBookmark(apiCallers[0]);

    // Create parent list
    const parentInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Parent List",
      type: "manual",
      icon: "📂",
    };
    const parentList = await api.create(parentInput);

    // Create child list with bookmark
    const childInput: z.infer<typeof zNewBookmarkListSchema> = {
      name: "Child List",
      parentId: parentList.id,
      type: "manual",
      icon: "📄",
    };
    const childList = await api.create(childInput);

    // Add bookmark to child list
    await api.addToList({ listId: childList.id, bookmarkId });

    // Verify bookmark is in the list
    const listsBeforeDelete = await api.getListsOfBookmark({ bookmarkId });
    expect(
      listsBeforeDelete.lists.find((l) => l.id === childList.id),
    ).toBeDefined();

    // Delete parent with deleteChildren=true
    await api.delete({ listId: parentList.id, deleteChildren: true });

    // Verify lists are deleted
    const allLists = await api.list();
    expect(allLists.lists.find((l) => l.id === parentList.id)).toBeUndefined();
    expect(allLists.lists.find((l) => l.id === childList.id)).toBeUndefined();

    // Verify bookmark still exists but is not in any list
    const listsAfterDelete = await api.getListsOfBookmark({ bookmarkId });
    expect(listsAfterDelete.lists).toHaveLength(0);

    // Verify the bookmark itself still exists by trying to access it
    const bookmark = await apiCallers[0].bookmarks.getBookmark({
      bookmarkId,
    });
    expect(bookmark).toBeDefined();
    expect(bookmark.id).toBe(bookmarkId);
  });

  test<CustomTestContext>("recursive delete with complex hierarchy", async ({
    apiCallers,
  }) => {
    const api = apiCallers[0].lists;

    // Create a complex tree structure:
    //     root
    //    /  |  \
    //   A   B   C
    //  /|   |   |\
    // D E   F   G H
    //       |
    //       I

    const root = await api.create({
      name: "Root",
      type: "manual",
      icon: "🌳",
    });

    const listA = await api.create({
      name: "List A",
      parentId: root.id,
      type: "manual",
      icon: "📂",
    });

    const listB = await api.create({
      name: "List B",
      parentId: root.id,
      type: "smart",
      query: "is:fav",
      icon: "📂",
    });

    const listC = await api.create({
      name: "List C",
      parentId: root.id,
      type: "manual",
      icon: "📂",
    });

    const listD = await api.create({
      name: "List D",
      parentId: listA.id,
      type: "manual",
      icon: "📄",
    });

    const listE = await api.create({
      name: "List E",
      parentId: listA.id,
      type: "smart",
      query: "is:archived",
      icon: "📄",
    });

    const listF = await api.create({
      name: "List F",
      parentId: listB.id,
      type: "manual",
      icon: "📄",
    });

    const listG = await api.create({
      name: "List G",
      parentId: listC.id,
      type: "manual",
      icon: "📄",
    });

    const listH = await api.create({
      name: "List H",
      parentId: listC.id,
      type: "smart",
      query: "is:fav",
      icon: "📄",
    });

    const listI = await api.create({
      name: "List I",
      parentId: listF.id,
      type: "manual",
      icon: "📄",
    });

    const allCreatedIds = [
      root.id,
      listA.id,
      listB.id,
      listC.id,
      listD.id,
      listE.id,
      listF.id,
      listG.id,
      listH.id,
      listI.id,
    ];

    // Delete root with deleteChildren=true
    await api.delete({ listId: root.id, deleteChildren: true });

    // Verify entire tree is deleted
    const remainingLists = await api.list();
    allCreatedIds.forEach((id) => {
      expect(remainingLists.lists.find((l) => l.id === id)).toBeUndefined();
    });
  });

  test<CustomTestContext>("recursive delete edge cases", async ({
    apiCallers,
  }) => {
    const api = apiCallers[0].lists;

    // Test 1: Delete list with no children (should work fine)
    const standaloneList = await api.create({
      name: "Standalone List",
      type: "manual",
      icon: "📄",
    });

    await api.delete({ listId: standaloneList.id, deleteChildren: true });
    let lists = await api.list();
    expect(lists.lists.find((l) => l.id === standaloneList.id)).toBeUndefined();

    // Test 2: Delete child directly (no recursion needed)
    const parent = await api.create({
      name: "Parent",
      type: "manual",
      icon: "📂",
    });

    const child = await api.create({
      name: "Child",
      parentId: parent.id,
      type: "manual",
      icon: "📄",
    });

    await api.delete({ listId: child.id, deleteChildren: true });
    lists = await api.list();
    expect(lists.lists.find((l) => l.id === parent.id)).toBeDefined();
    expect(lists.lists.find((l) => l.id === child.id)).toBeUndefined();
  });

  test<CustomTestContext>("partial recursive delete on middle node", async ({
    apiCallers,
  }) => {
    const api = apiCallers[0].lists;

    // Create hierarchy: grandparent -> parent -> child
    const grandparent = await api.create({
      name: "Grandparent",
      type: "manual",
      icon: "📂",
    });

    const parent = await api.create({
      name: "Parent",
      parentId: grandparent.id,
      type: "manual",
      icon: "📂",
    });

    const child = await api.create({
      name: "Child",
      parentId: parent.id,
      type: "manual",
      icon: "📄",
    });

    // Delete middle node (parent) with deleteChildren=true
    await api.delete({ listId: parent.id, deleteChildren: true });

    // Verify parent and child are deleted, but grandparent remains
    const lists = await api.list();
    expect(lists.lists.find((l) => l.id === grandparent.id)).toBeDefined();
    expect(lists.lists.find((l) => l.id === parent.id)).toBeUndefined();
    expect(lists.lists.find((l) => l.id === child.id)).toBeUndefined();
  });
});