forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.tests.js
More file actions
205 lines (180 loc) · 5.63 KB
/
Copy pathfs.tests.js
File metadata and controls
205 lines (180 loc) · 5.63 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
import fsOperation from "fileSystem";
import Url from "utils/Url";
import { TestRunner } from "./tester";
export async function runFsTests(writeOutput) {
const runner = new TestRunner("Filesystem API Tests");
const testDir = window.CACHE_STORAGE || "file:///sdcard/AcodeCache";
runner.test("CACHE_STORAGE is defined", (test) => {
test.assert(
typeof window.CACHE_STORAGE === "string",
"CACHE_STORAGE should be a string path",
);
});
runner.test("fsOperation returns a FileSystem object", (test) => {
const fs = fsOperation(testDir);
test.assert(fs !== null, "fsOperation should return filesystem handler");
test.assert(
typeof fs.createFile === "function",
"createFile should be a function",
);
test.assert(typeof fs.exists === "function", "exists should be a function");
});
runner.test(
"createFile, exists, writeFile, readFile, delete",
async (test) => {
const fs = fsOperation(testDir);
const filename = `__fs_test_${Date.now()}__.txt`;
const fileUrl = Url.join(testDir, filename);
try {
// 1. Create the file
const createdUrl = await fs.createFile(filename, "initial content");
test.assertEqual(
createdUrl,
fileUrl,
"Created file URL should match expected path",
);
// 2. Check existence
const fileFs = fsOperation(createdUrl);
const exists = await fileFs.exists();
test.assertEqual(exists, true, "Created file should exist");
// 3. Read content
const content = await fileFs.readFile("utf-8");
test.assertEqual(
content,
"initial content",
"Read content should match initial content",
);
// 4. Write new content
await fileFs.writeFile("updated content");
const updatedContent = await fileFs.readFile("utf-8");
test.assertEqual(
updatedContent,
"updated content",
"Read content should match updated content",
);
// 5. Stat check
const stat = await fileFs.stat();
test.assert(stat !== null, "Stat should not be null");
test.assertEqual(stat.isFile, true, "Stat should show isFile true");
test.assertEqual(
stat.isDirectory,
false,
"Stat should show isDirectory false",
);
// 6. Delete file
await fileFs.delete();
const existsAfterDelete = await fileFs.exists();
test.assertEqual(
existsAfterDelete,
false,
"File should not exist after deletion",
);
} catch (error) {
// Cleanup if anything fails
try {
const fileFs = fsOperation(fileUrl);
if (await fileFs.exists()) {
await fileFs.delete();
}
} catch (_) {}
throw error;
}
},
);
runner.test("createDirectory, lsDir, delete directory", async (test) => {
const fs = fsOperation(testDir);
const dirname = `__fs_dir_test_${Date.now()}__`;
const dirUrl = Url.join(testDir, dirname);
try {
// 1. Create directory
const createdDirUrl = await fs.createDirectory(dirname);
test.assertEqual(
createdDirUrl,
dirUrl,
"Created directory URL should match expected path",
);
const dirFs = fsOperation(createdDirUrl);
const exists = await dirFs.exists();
test.assertEqual(exists, true, "Created directory should exist");
// 2. Stat check
const stat = await dirFs.stat();
test.assertEqual(
stat.isDirectory,
true,
"Stat should show isDirectory true",
);
test.assertEqual(stat.isFile, false, "Stat should show isFile false");
// 3. Create a file inside directory
const fileUrl = await dirFs.createFile("child.txt", "child content");
// 4. List directory contents
const list = await dirFs.lsDir();
const child = list.find((item) => item.name === "child.txt");
test.assert(
child !== undefined,
"lsDir should list the created child file",
);
test.assertEqual(child.isFile, true, "child item should be a file");
// 5. Delete child file and directory recursively
const childFs = fsOperation(fileUrl);
await childFs.delete();
await dirFs.delete();
const dirExistsAfterDelete = await dirFs.exists();
test.assertEqual(
dirExistsAfterDelete,
false,
"Directory should not exist after deletion",
);
} catch (error) {
// Cleanup if anything fails
try {
const dirFs = fsOperation(dirUrl);
if (await dirFs.exists()) {
await dirFs.delete();
}
} catch (_) {}
throw error;
}
});
runner.test("read/write with explicit encodings", async (test) => {
const fs = fsOperation(testDir);
const utf8Filename = `__fs_utf8_test_${Date.now()}__.txt`;
const gbkFilename = `__fs_gbk_test_${Date.now()}__.txt`;
const utf8FileUrl = Url.join(testDir, utf8Filename);
const gbkFileUrl = Url.join(testDir, gbkFilename);
try {
// Write and read with UTF-8
const utf8Url = await fs.createFile(utf8Filename, "");
const utf8Fs = fsOperation(utf8Url);
await utf8Fs.writeFile("Hello 世界 (UTF-8)", "utf-8");
const utf8Content = await utf8Fs.readFile("utf-8");
test.assertEqual(
utf8Content,
"Hello 世界 (UTF-8)",
"UTF-8 read/write should match",
);
// Write and read with GBK (simplified Chinese characters like 世界)
const gbkUrl = await fs.createFile(gbkFilename, "");
const gbkFs = fsOperation(gbkUrl);
await gbkFs.writeFile("Hello 世界 (GBK)", "gbk");
const gbkContent = await gbkFs.readFile("gbk");
test.assertEqual(
gbkContent,
"Hello 世界 (GBK)",
"GBK read/write should match",
);
// Cleanup
await utf8Fs.delete();
await gbkFs.delete();
} catch (error) {
// Cleanup on failure
try {
await fsOperation(utf8FileUrl).delete();
} catch (_) {}
try {
await fsOperation(gbkFileUrl).delete();
} catch (_) {}
throw error;
}
});
return await runner.run(writeOutput);
}