-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathwrite.test.ts
More file actions
366 lines (296 loc) · 10.2 KB
/
Copy pathwrite.test.ts
File metadata and controls
366 lines (296 loc) · 10.2 KB
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
import path from 'path'
import { assert } from 'vitest'
import { WriteEntry } from '../../../src/sandbox/filesystem'
import { isDebug, sandboxTest } from '../../setup.js'
sandboxTest('write file', async ({ sandbox }) => {
const filename = 'test_write.txt'
const content = 'This is a test file.'
// Attempt to write with undefined path and content
await sandbox.files
// @ts-ignore
.write(undefined, content)
.then((e) => {
assert.isUndefined(e)
})
.catch((err) => {
assert.instanceOf(err, Error)
assert.include(err.message, 'Path or files are required')
})
const info = await sandbox.files.write(filename, content)
assert.isFalse(Array.isArray(info))
assert.equal(info.name, filename)
assert.equal(info.type, 'file')
assert.equal(info.path, `/home/user/${filename}`)
const exists = await sandbox.files.exists(filename)
assert.isTrue(exists)
const readContent = await sandbox.files.read(filename)
assert.equal(readContent, content)
if (isDebug) {
await sandbox.files.remove(filename)
}
})
sandboxTest('write multiple files', async ({ sandbox }) => {
const numTestFiles = 10
// Attempt to write with empty files array
const emptyInfo = await sandbox.files.write([])
assert.isTrue(Array.isArray(emptyInfo))
assert.equal(emptyInfo.length, 0)
// Attempt to write with undefined path and file array
await sandbox.files
// @ts-ignore
.write(undefined, [
{ path: 'one_test_file.txt', data: 'This is a test file.' },
])
.then((e) => {
assert.isUndefined(e)
})
.catch((err) => {
assert.instanceOf(err, Error)
assert.include(err.message, 'Path or files are required')
})
// Attempt to write with path and file array
await sandbox.files
// @ts-ignore
.write('/path/to/file', [
{ path: 'one_test_file.txt', data: 'This is a test file.' },
])
.then((e) => {
assert.isUndefined(e)
})
.catch((err) => {
assert.instanceOf(err, Error)
assert.include(
err.message,
'Cannot specify both path and array of files. You have to specify either path and data for a single file or an array for multiple files.'
)
})
// Attempt to write with one file in array
const info = await sandbox.files.write([
{ path: 'one_test_file.txt', data: 'This is a test file.' },
])
assert.isTrue(Array.isArray(info))
assert.equal(info[0].name, 'one_test_file.txt')
assert.equal(info[0].type, 'file')
assert.equal(info[0].path, '/home/user/one_test_file.txt')
// Attempt to write with multiple files in array
const files: WriteEntry[] = []
for (let i = 0; i < numTestFiles; i++) {
let path = ''
if (i % 2 == 0) {
path = `/${i}/multi_test_file${i}.txt`
} else {
path = `/home/user/multi_test_file${i}.txt`
}
files.push({
path: path,
data: `This is a test file ${i}.`,
})
}
const infos = await sandbox.files.write(files)
assert.isTrue(Array.isArray(infos))
assert.equal(infos.length, files.length)
// Attempt to write with multiple files in array
for (let i = 0; i < files.length; i++) {
const file = files[i]
const info = infos[i]
assert.equal(info.name, path.basename(file.path))
assert.equal(info.path, file.path)
assert.equal(info.type, 'file')
const exists = await sandbox.files.exists(file.path)
assert.isTrue(exists)
const readContent = await sandbox.files.read(file.path)
assert.equal(readContent, file.data)
}
if (isDebug) {
for (const file of files) {
await sandbox.files.remove(file.path)
}
}
})
sandboxTest('write file', async ({ sandbox }) => {
const filename = 'test_write.txt'
const content = 'This is a test file.'
const info = await sandbox.files.write(filename, content)
assert.isFalse(Array.isArray(info))
assert.equal(info.name, filename)
assert.equal(info.type, 'file')
assert.equal(info.path, `/home/user/${filename}`)
const exists = await sandbox.files.exists(filename)
assert.isTrue(exists)
const readContent = await sandbox.files.read(filename)
assert.equal(readContent, content)
if (isDebug) {
await sandbox.files.remove(filename)
}
})
sandboxTest('overwrite file', async ({ sandbox }) => {
const filename = 'test_overwrite.txt'
const initialContent = 'Initial content.'
const newContent = 'New content.'
await sandbox.files.write(filename, initialContent)
await sandbox.files.write(filename, newContent)
const readContent = await sandbox.files.read(filename)
assert.equal(readContent, newContent)
if (isDebug) {
await sandbox.files.remove(filename)
}
})
sandboxTest('write to non-existing directory', async ({ sandbox }) => {
const filename = 'non_existing_dir/test_write.txt'
const content = 'This should succeed too.'
await sandbox.files.write(filename, content)
const exists = await sandbox.files.exists(filename)
assert.isTrue(exists)
const readContent = await sandbox.files.read(filename)
assert.equal(readContent, content)
if (isDebug) {
await sandbox.files.remove(filename)
}
})
sandboxTest('writeFiles with empty array', async ({ sandbox }) => {
const emptyInfo = await sandbox.files.writeFiles([])
assert.isTrue(Array.isArray(emptyInfo))
assert.equal(emptyInfo.length, 0)
})
sandboxTest('writeFiles with multiple files', async ({ sandbox }) => {
const numTestFiles = 10
const files: WriteEntry[] = []
for (let i = 0; i < numTestFiles; i++) {
const filePath = `writefiles_test_${i}.txt`
files.push({
path: filePath,
data: `This is a test file ${i}.`,
})
}
const infos = await sandbox.files.writeFiles(files)
assert.isTrue(Array.isArray(infos))
assert.equal(infos.length, files.length)
for (let i = 0; i < files.length; i++) {
const file = files[i]
const info = infos[i]
assert.equal(info.name, path.basename(file.path))
assert.equal(info.path, `/home/user/${file.path}`)
assert.equal(info.type, 'file')
const exists = await sandbox.files.exists(info.path)
assert.isTrue(exists)
const readContent = await sandbox.files.read(info.path)
assert.equal(readContent, file.data)
}
if (isDebug) {
for (const file of files) {
await sandbox.files.remove(file.path)
}
}
})
sandboxTest('writeFiles with different data types', async ({ sandbox }) => {
const textData = 'Text string data'
const arrayBufferData = new TextEncoder().encode('ArrayBuffer data').buffer
const blobData = new Blob(['Blob data'], { type: 'text/plain' })
const streamContent = 'ReadableStream data'
const encoder = new TextEncoder()
const streamData = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode(streamContent))
controller.close()
},
})
const files: WriteEntry[] = [
{ path: 'writefiles_text.txt', data: textData },
{ path: 'writefiles_arraybuffer.txt', data: arrayBufferData },
{ path: 'writefiles_blob.txt', data: blobData },
{ path: 'writefiles_stream.txt', data: streamData },
]
const infos = await sandbox.files.writeFiles(files)
assert.equal(infos.length, 4)
// Verify text file
const textContent = await sandbox.files.read('writefiles_text.txt')
assert.equal(textContent, textData)
// Verify ArrayBuffer file
const arrayBufferContent = await sandbox.files.read(
'writefiles_arraybuffer.txt'
)
assert.equal(arrayBufferContent, 'ArrayBuffer data')
// Verify Blob file
const blobContent = await sandbox.files.read('writefiles_blob.txt')
assert.equal(blobContent, 'Blob data')
// Verify ReadableStream file
const streamFileContent = await sandbox.files.read('writefiles_stream.txt')
assert.equal(streamFileContent, streamContent)
if (isDebug) {
for (const file of files) {
await sandbox.files.remove(file.path)
}
}
})
sandboxTest('writeFiles creates parent directories', async ({ sandbox }) => {
const files: WriteEntry[] = [
{
path: 'writefiles_nested_dir/nested/file1.txt',
data: 'Content in nested directory',
},
]
const infos = await sandbox.files.writeFiles(files)
assert.equal(infos.length, 1)
assert.equal(
infos[0].path,
'/home/user/writefiles_nested_dir/nested/file1.txt'
)
const exists = await sandbox.files.exists(infos[0].path)
assert.isTrue(exists)
const content = await sandbox.files.read(infos[0].path)
assert.equal(content, 'Content in nested directory')
if (isDebug) {
await sandbox.files.remove(files[0].path)
}
})
sandboxTest('writeFiles overwrites existing files', async ({ sandbox }) => {
const filename = 'writefiles_overwrite.txt'
const initialContent = 'Initial content'
const newContent = 'New content'
// Write initial file
await sandbox.files.writeFiles([{ path: filename, data: initialContent }])
let readContent = await sandbox.files.read(filename)
assert.equal(readContent, initialContent)
// Overwrite with new content
await sandbox.files.writeFiles([{ path: filename, data: newContent }])
readContent = await sandbox.files.read(filename)
assert.equal(readContent, newContent)
if (isDebug) {
await sandbox.files.remove(filename)
}
})
sandboxTest(
'write ReadableStream with octet stream upload',
async ({ sandbox }) => {
const filename = 'test_write_octet_stream.bin'
const content = 'Streamed octet-stream upload. '.repeat(10_000)
const stream = new Blob([content]).stream()
const info = await sandbox.files.write(filename, stream, {
useOctetStream: true,
})
assert.equal(info.path, `/home/user/${filename}`)
const readContent = await sandbox.files.read(filename)
assert.equal(readContent, content)
if (isDebug) {
await sandbox.files.remove(filename)
}
}
)
sandboxTest(
'write ReadableStream with octet stream upload and gzip',
async ({ sandbox }) => {
const filename = 'test_write_octet_stream_gzip.bin'
const content = 'Streamed gzipped octet-stream upload. '.repeat(10_000)
const stream = new Blob([content]).stream()
const info = await sandbox.files.write(filename, stream, {
useOctetStream: true,
gzip: true,
})
assert.equal(info.path, `/home/user/${filename}`)
const readContent = await sandbox.files.read(filename)
assert.equal(readContent, content)
if (isDebug) {
await sandbox.files.remove(filename)
}
}
)