|
| 1 | +import { test, expect } from 'vitest'; |
| 2 | +import { ListObjectsV2Command } from '@aws-sdk/client-s3'; |
| 3 | +import { listR2Directory } from './listR2Directory.mjs'; |
| 4 | +import { R2_RETRY_LIMIT } from './limits.mjs'; |
| 5 | + |
| 6 | +test('adds subdirectories and files properly', async () => { |
| 7 | + const now = new Date(); |
| 8 | + |
| 9 | + // Add a second so we can check the directory's last modified is determined properly |
| 10 | + const directoryLastModified = new Date(now.getTime() + 1000); |
| 11 | + |
| 12 | + const client = { |
| 13 | + async send(cmd: ListObjectsV2Command) { |
| 14 | + expect(cmd.input.Bucket).toStrictEqual('dist-prod'); |
| 15 | + expect(cmd.input.Prefix).toStrictEqual('some/directory/'); |
| 16 | + |
| 17 | + return { |
| 18 | + IsTruncated: false, |
| 19 | + NextContinuationToken: undefined, |
| 20 | + CommonPrefixes: [ |
| 21 | + { Prefix: 'some/directory/subdirectory1/' }, |
| 22 | + { Prefix: 'some/directory/subdirectory2/' }, |
| 23 | + { Prefix: 'some/directory/subdirectory3/' }, |
| 24 | + ], |
| 25 | + Contents: [ |
| 26 | + { |
| 27 | + Key: 'some/directory/file.txt', |
| 28 | + LastModified: now, |
| 29 | + Size: 1, |
| 30 | + }, |
| 31 | + { |
| 32 | + Key: 'some/directory/file2.txt', |
| 33 | + LastModified: directoryLastModified, |
| 34 | + Size: 2, |
| 35 | + }, |
| 36 | + { |
| 37 | + Key: 'some/directory/file3.txt', |
| 38 | + LastModified: now, |
| 39 | + Size: 3, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }; |
| 43 | + }, |
| 44 | + }; |
| 45 | + |
| 46 | + // @ts-expect-error don't need full client |
| 47 | + const result = await listR2Directory(client, 'dist-prod', 'some/directory/'); |
| 48 | + |
| 49 | + expect(result).toStrictEqual({ |
| 50 | + subdirectories: ['subdirectory1/', 'subdirectory2/', 'subdirectory3/'], |
| 51 | + hasIndexHtmlFile: false, |
| 52 | + files: [ |
| 53 | + { |
| 54 | + name: 'file.txt', |
| 55 | + lastModified: now, |
| 56 | + size: 1, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: 'file2.txt', |
| 60 | + lastModified: directoryLastModified, |
| 61 | + size: 2, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: 'file3.txt', |
| 65 | + lastModified: now, |
| 66 | + size: 3, |
| 67 | + }, |
| 68 | + ], |
| 69 | + lastModified: directoryLastModified, |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +test('handles truncation properly', async () => { |
| 74 | + const now = new Date(); |
| 75 | + |
| 76 | + const client = { |
| 77 | + async send(cmd: ListObjectsV2Command) { |
| 78 | + expect(cmd.input.Bucket).toStrictEqual('dist-prod'); |
| 79 | + expect(cmd.input.Prefix).toStrictEqual('some/directory/'); |
| 80 | + |
| 81 | + switch (cmd.input.ContinuationToken) { |
| 82 | + case undefined: { |
| 83 | + return { |
| 84 | + IsTruncated: true, |
| 85 | + NextContinuationToken: '1', |
| 86 | + CommonPrefixes: [{ Prefix: 'some/directory/subdirectory1/' }], |
| 87 | + Contents: [ |
| 88 | + { |
| 89 | + Key: 'some/directory/file.txt', |
| 90 | + LastModified: now, |
| 91 | + Size: 1, |
| 92 | + }, |
| 93 | + ], |
| 94 | + }; |
| 95 | + } |
| 96 | + case '1': { |
| 97 | + return { |
| 98 | + IsTruncated: true, |
| 99 | + NextContinuationToken: '2', |
| 100 | + CommonPrefixes: [{ Prefix: 'some/directory/subdirectory2/' }], |
| 101 | + Contents: [ |
| 102 | + { |
| 103 | + Key: 'some/directory/file2.txt', |
| 104 | + LastModified: now, |
| 105 | + Size: 2, |
| 106 | + }, |
| 107 | + ], |
| 108 | + }; |
| 109 | + } |
| 110 | + case '2': { |
| 111 | + return { |
| 112 | + IsTruncated: false, |
| 113 | + NextContinuationToken: undefined, |
| 114 | + CommonPrefixes: [{ Prefix: 'some/directory/subdirectory3/' }], |
| 115 | + Contents: [ |
| 116 | + { |
| 117 | + Key: 'some/directory/file3.txt', |
| 118 | + LastModified: now, |
| 119 | + Size: 3, |
| 120 | + }, |
| 121 | + ], |
| 122 | + }; |
| 123 | + } |
| 124 | + } |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + // @ts-expect-error don't need full client |
| 129 | + const result = await listR2Directory(client, 'dist-prod', 'some/directory/'); |
| 130 | + |
| 131 | + expect(result).toStrictEqual({ |
| 132 | + subdirectories: ['subdirectory1/', 'subdirectory2/', 'subdirectory3/'], |
| 133 | + hasIndexHtmlFile: false, |
| 134 | + files: [ |
| 135 | + { |
| 136 | + name: 'file.txt', |
| 137 | + lastModified: now, |
| 138 | + size: 1, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: 'file2.txt', |
| 142 | + lastModified: now, |
| 143 | + size: 2, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: 'file3.txt', |
| 147 | + lastModified: now, |
| 148 | + size: 3, |
| 149 | + }, |
| 150 | + ], |
| 151 | + lastModified: now, |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +test('retries properly', async () => { |
| 156 | + let retries = R2_RETRY_LIMIT; |
| 157 | + |
| 158 | + let requestsSent = 0; |
| 159 | + const client = { |
| 160 | + async send() { |
| 161 | + requestsSent++; |
| 162 | + |
| 163 | + throw new TypeError('dummy'); |
| 164 | + }, |
| 165 | + }; |
| 166 | + |
| 167 | + const result = listR2Directory( |
| 168 | + // @ts-expect-error don't need full client |
| 169 | + client, |
| 170 | + 'dist-prod', |
| 171 | + 'some/directory/', |
| 172 | + retries |
| 173 | + ); |
| 174 | + |
| 175 | + await expect(result).rejects.toThrow('exhausted R2 retries'); |
| 176 | + expect(requestsSent).toBe(retries); |
| 177 | +}); |
0 commit comments