1
1
import { ExportDataService , ListExport } from './ExportDataService' ;
2
- import { conn as db } from '../database/client' ;
2
+ import { client , conn as db } from '../database/client' ;
3
3
import config from '../config' ;
4
4
import { S3Bucket , cleanupS3Bucket } from '@pocket-tools/aws-utils' ;
5
5
import { GetObjectCommand , GetObjectCommandOutput } from '@aws-sdk/client-s3' ;
6
6
import { eventBridgeClient } from '../aws/eventBridgeClient' ;
7
7
import path from 'path' ;
8
+ import {
9
+ createShareableListHelper ,
10
+ clearDb ,
11
+ createShareableListItemHelper ,
12
+ } from '../test/helpers' ;
8
13
9
- describe ( 'AnnotationsExportService ' , ( ) => {
10
- const userId = 12345 ;
14
+ describe ( 'ExportDataService ' , ( ) => {
15
+ const userId = 3483487 ;
11
16
const encodedId = '4a5b3c' ;
12
17
const bucket = new S3Bucket ( config . export . bucket . name , {
13
18
region : config . aws . region ,
14
19
endpoint : config . aws . endpoint ,
15
20
} ) ;
16
21
const conn = db ( ) ;
22
+ const prisma = client ( ) ;
17
23
beforeAll ( async ( ) => {
18
24
await cleanupS3Bucket ( bucket ) ;
25
+ await createShareableListHelper ( prisma , {
26
+ userId,
27
+ title : 'Empty list' ,
28
+ } ) ;
29
+ const someList = await createShareableListHelper ( prisma , {
30
+ userId,
31
+ title : 'List of Stuff!' ,
32
+ } ) ;
33
+ await Promise . all (
34
+ [ ...Array ( 40 ) . keys ( ) ] . map ( ( ix ) =>
35
+ createShareableListItemHelper ( prisma , {
36
+ list : someList ,
37
+ sortOrder : ix ,
38
+ } ) ,
39
+ ) ,
40
+ ) ;
19
41
} ) ;
20
42
afterAll ( async ( ) => {
21
43
await cleanupS3Bucket ( bucket ) ;
44
+ await clearDb ( prisma ) ;
22
45
await conn . destroy ( ) ;
23
46
} ) ;
24
47
it ( 'passes a smoke test' , async ( ) => {
@@ -33,25 +56,45 @@ describe('AnnotationsExportService', () => {
33
56
const exportedFiles = await bucket . listAllObjects (
34
57
path . join ( config . export . bucket . partsPrefix , encodedId , 'collections' ) ,
35
58
) ;
59
+ expect ( exportedFiles ) . toIncludeSameMembers ( [
60
+ 'parts/4a5b3c/collections/empty-list.json' ,
61
+ 'parts/4a5b3c/collections/list-of-stuff.json' ,
62
+ ] ) ;
36
63
// More than 1 file written to proper path
37
64
expect ( exportedFiles . length ) . toBeGreaterThan ( 1 ) ;
38
- const getObject = new GetObjectCommand ( {
39
- Key : exportedFiles [ 0 ] ,
40
- Bucket : config . export . bucket . name ,
65
+ const getList = async ( key : string ) => {
66
+ const getObject = new GetObjectCommand ( {
67
+ Key : key ,
68
+ Bucket : config . export . bucket . name ,
69
+ } ) ;
70
+ const result : GetObjectCommandOutput = await bucket . s3 . send ( getObject ) ;
71
+ const body : ListExport = JSON . parse (
72
+ ( await result . Body ?. transformToString ( ) ) ?? '{}' ,
73
+ ) ;
74
+ return body ;
75
+ } ;
76
+ const emptyList = await getList ( 'parts/4a5b3c/collections/empty-list.json' ) ;
77
+ expect ( emptyList ) . toMatchObject ( {
78
+ createdAt : expect . toBeDateString ( ) ,
79
+ description : expect . toBeString ( ) ,
80
+ title : expect . toBeString ( ) ,
81
+ slug : expect . toBeString ( ) ,
82
+
83
+ items : expect . toBeArrayOfSize ( 0 ) ,
41
84
} ) ;
42
- const result : GetObjectCommandOutput = await bucket . s3 . send ( getObject ) ;
43
- const body : ListExport = JSON . parse (
44
- ( await result . Body ?. transformToString ( ) ) ?? '{}' ,
85
+ const listOfStuff = await getList (
86
+ 'parts/4a5b3c/collections/list-of-stuff.json' ,
45
87
) ;
46
- // Has the slug in the filename
47
- expect ( exportedFiles [ 0 ] ) . toMatch ( body . slug ) ;
48
88
// Has expected data shape
49
- expect ( body ) . toMatchObject ( {
89
+ expect ( listOfStuff ) . toMatchObject ( {
50
90
createdAt : expect . toBeDateString ( ) ,
51
91
description : expect . toBeString ( ) ,
52
92
title : expect . toBeString ( ) ,
53
93
slug : expect . toBeString ( ) ,
54
94
// At least one
95
+ items : expect . toBeArrayOfSize ( 40 ) ,
96
+ } ) ;
97
+ expect ( listOfStuff ) . toMatchObject ( {
55
98
items : expect . arrayContaining ( [
56
99
{
57
100
excerpt : expect . toBeString ( ) ,
0 commit comments