-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
426 lines (297 loc) · 11.4 KB
/
index.test.ts
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
import { EasyFileAccess } from './index'
import { isEqual } from 'lodash'
var fs_access: EasyFileAccess
var exists, mkdir, write, cat, remove, abs, rename, stat, sep, list, test_rename, add, add_for
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const describe = async (description: string, func: any) => {
console.log(`Start: ${description}`)
await func()
console.log(`Success: ${description}`)
}
const it = async (description: string, func: any) => {
console.log(` Start: ${description}`)
await func()
console.log(` Success: ${description}`)
}
class expect_ {
input: any
constructor(input) {
this.input = input
}
toBe(params) {
if (params === this.input) {
return
} else {
throw (`Tobe Assert Failed, ${params}, ${this.input}`)
}
}
toContain(params) {
if (this.input.includes(params)) {
return
} else {
throw (`ToContain Assert Failed, ${params}, ${this.input}`)
}
}
toEqual(params) {
if (isEqual(params, this.input)) {
return
} else {
throw (`ToEqual Assert Failed, ${params}, ${this.input}`)
}
}
}
function expect(inp) {
return new expect_(inp)
}
const select_handle = async () => {
const handle = await window.showDirectoryPicker()
fs_access = new EasyFileAccess(handle);
({ exists, mkdir, write, cat, remove, abs, rename, stat, sep, list, add, add_for } = fs_access)
}
const start_test = async () => {
// @ts-ignore
window.fs = fs_access
await describe("abs", async () => {
await it("gets the defaults right", async () => {
expect(await abs()).toBe('/');
expect(await abs("/demo")).toBe("/demo");
});
})
await describe("cat", async () => {
await it("can read a markdown file", async () => {
expect(await cat("/demo/readme.md")).toContain("# Hello!");
});
await it("throw error when reading directory", async () => {
try {
expect(await cat("/demo"))
} catch (e) {
console.log(e)
}
});
await it("can json parse it", async () => {
expect(await cat("/demo/test.json").then(JSON.parse)).toEqual({
hello: "world",
});
});
})
await describe("list", async () => {
await it("can load the demo", async () => {
const files = await list("/demo");
//expect(files).not.toContain( sep + "files.js");
expect(files).toContain(sep + `demo${sep}a/`);
expect(files).toContain(sep + `demo${sep}readme.md`);
});
}),
await describe("mkdir", async () => {
await it("create a new directory", async () => {
expect(await exists("/demo/b/")).toBe(false);
const res = await mkdir("/demo/b/");
expect(await exists("/demo/b/")).toBe(true);
expect(res).toBe(await abs("/demo/b/"));
});
await it("does not throw if it already exists", async () => {
expect(await exists("/demo/a/")).toBe(true);
const res = await mkdir("/demo/a/");
expect(await exists("/demo/a/")).toBe(true);
expect(res).toBe(await abs("/demo/a/"));
});
await it("creates it even if the parent does not exist", async () => {
await remove("/demo/c/");
expect(await exists("/demo/c/")).toBe(false);
const res = await mkdir("/demo/c/d/e/");
expect(await exists("/demo/c/d/e/")).toBe(true);
expect(res).toBe(await abs("/demo/c/d/e/"));
await remove("/demo/c/");
});
})
await describe("remove", async () => {
await it("removes a file", async () => {
await write("/demo/remove.md", "Hello!");
console.log('write finished')
const result = await cat("/demo/remove.md")
console.log(result)
await remove("/demo/remove.md");
expect(await exists("/demo/remove.md")).toBe(false);
//expect(file).toBe(await abs("/demo/remove.md"));
});
await it("removes a directory", async () => {
await mkdir("/demo/b/");
expect(await exists("/demo/b/")).toBe(true);
const file = await remove("/demo/b/");
expect(await exists("/demo/b/")).toBe(false);
//expect(file).toBe(await abs("/demo/b"));
});
await it("removes a directory with files", async () => {
await mkdir("/demo/b/");
await write("/demo/b/remove.md", "Hello!");
expect(await exists("/demo/b/")).toBe(true);
expect(await cat("/demo/b/remove.md")).toBe("Hello!");
const file = await remove("/demo/b/");
expect(await exists("/demo/b/")).toBe(false);
expect(file).toBe(await abs("/demo/b/"));
});
await it("removes a directory with deeply nested files", async () => {
await mkdir("/demo/x/");
await write("/demo/x/remove.md", "Hello!");
await mkdir("/demo/x/c/");
await write("/demo/x/c/remove.md", "Hello!");
expect(await exists("/demo/x/")).toBe(true);
expect(await cat("/demo/x/remove.md")).toBe("Hello!");
expect(await exists("/demo/x/c/")).toBe(true);
expect(await cat("/demo/x/c/remove.md")).toBe("Hello!");
const file = await remove("/demo/x/");
expect(await exists("/demo/x/")).toBe(false);
expect(file).toBe(await abs("/demo/x/"));
});
await it("cannot remove the root", async () => {
//await expect(remove("/")).rejects.toThrow(/remove the root/);
});
await it("will ignore a non-existing file", async () => {
expect(await exists("/demo/d/")).toBe(false);
await expect(await remove("/demo/d/")).toEqual(await abs("/demo/d/"));
});
});
await describe("rename", async () => {
const src = "/demo/rename.txt";
console.log('outside start')
await write(src, 'rename.content')
console.log('break outside 1')
await it("can simply move a file", async () => {
console.log('Start execute')
const dst = "/demo/rename-zzz.txt";
expect(await exists(dst)).toBe(false);
console.log('break 1')
const res = await rename(src, dst);
console.log('break 2')
expect(await exists(src)).toBe(false);
console.log('break 3')
expect(await exists(dst)).toBe(true);
console.log('break 4')
expect(res).toBe(await abs(dst));
console.log('break 5')
await remove(dst);
});
await it("can work with nested folders", async () => {
await write(src, "hello")
const dst = "/demo/rename/zzz.txt";
expect(await exists(dst)).toBe(false);
const res = await rename(src, dst);
expect(await exists(src)).toBe(false);
expect(await exists(dst)).toBe(true);
expect(res).toBe(await abs(dst));
await remove("/demo/rename/");
await remove(src)
});
await it("works with folders", async () => {
const src = "/demo/rename/";
const dst = "/demo/renamed/";
await write("/demo/rename/test.txt", "hello");
expect(await exists(dst)).toBe(false);
const res = await rename(src, dst);
expect(await exists(src)).toBe(false);
expect(await exists(dst)).toBe(true);
expect(res).toBe(await abs(dst));
await remove(dst);
await remove(src)
});
});
await describe("stat", async () => {
await it("can analyze whether a path is a directory or not", async () => {
const result_demo = await stat("/demo/")
const result_readme = await stat("/demo/readme.md")
//sleep(1000)
expect(result_demo.isDirectory()).toBe(true);
expect(result_readme.isDirectory()).toBe(false);
});
await it("can read some dates", async () => {
const middle = await stat("/demo/readme.md");
const date = middle.atimeMs
console.log(middle)
//expect(date.constructor.name).toBe("Date");
//expect(date).toEqual(new Date(date));
});
});
await describe("write", async () => {
await it("creates a new file", async () => {
expect(await exists("/demo/deleteme.md")).toBe(false);
await write("/demo/deleteme.md", "Hello!");
expect(await cat("/demo/deleteme.md")).toBe("Hello!");
expect(await exists("/demo/deleteme.md")).toBe(true);
});
await remove('/demo/deleteme.md')
await it("creates a new empty file", async () => {
expect(await exists("/demo/deleteme.md")).toBe(false);
await write("/demo/deleteme.md");
expect(await exists("/demo/deleteme.md")).toBe(true);
});
});
await describe('other', async ()=>{
await it('list a dir', async ()=>{
const res = await list('/demo/a')
console.log(res)
})
await it('add a file', async ()=>{
await add('/demo/a/b/c/d.txt')
expect(await exists('/demo/a/b/c/d.txt')).toBe(true)
expect(await exists('/demo/a/b/c/d')).toBe(false)
})
await it('add a folder', async ()=>{
await add('/demo/a/b/c/e/')
expect(await exists('/demo/a/b/c/e')).toBe(false)
expect(await exists('/demo/a/b/c/e/')).toBe(true)
})
await describe("add_for", async () => {
await it("creates a new file within a folder", async () => {
// Create a new folder to use as the parent directory
await add_for("/demo/", "add_for/");
// Ensure that the file doesn't already exist
expect(await exists("/demo/add_for/test.txt")).toBe(false);
// Create a new file within the parent directory
await add_for("/demo/add_for/", "test.txt");
// Check that the file was created successfully
expect(await exists("/demo/add_for/test.txt")).toBe(true);
});
await it("creates a new file within a file's parent directory", async () => {
// Create a new file to use as the parent directory
await add_for("/demo/", "add_for/test2.txt");
// Ensure that the file doesn't already exist
expect(await exists("/demo/add_for/test2.txt")).toBe(true);
// Create a new file within the parent directory of the existing file
await add_for("/demo/add_for/test2.txt", "test3.txt");
// Check that the file was created successfully
expect(await exists("/demo/add_for/test3.txt")).toBe(true);
});
await it("creates a new folder within a folder", async () => {
// Create a new folder to use as the parent directory
await add_for("/demo/", "add_for2/");
// Ensure that the folder doesn't already exist
expect(await exists("/demo/add_for2/test/")).toBe(false);
// Create a new folder within the parent directory
await add_for("/demo/add_for2/", "test/");
// Check that the folder was created successfully
expect(await exists("/demo/add_for2/test/")).toBe(true);
});
await it("creates a new folder within a file's parent directory", async () => {
// Create a new file to use as the parent directory
await add_for("/demo/", "add_for3/test.txt");
// Ensure that the folder doesn't already exist
expect(await exists("/demo/add_for3/test/")).toBe(false);
// Create a new folder within the parent directory of the existing file
await add_for("/demo/add_for3/test.txt", "test/");
// Check that the folder was created successfully
expect(await exists("/demo/add_for3/test/")).toBe(true);
});
});
})
console.log('ALL TEST SUCCESS !!!')
}
//@ts-ignore
window.fs = fs_access
document.getElementById("select")?.addEventListener('click', select_handle)
document.getElementById("start")?.addEventListener('click', async () => {
//test_rename()
//console.log('out side assertion')
start_test()
})