Skip to content

Commit a123e26

Browse files
cyfung1031CodFrm
andcommitted
✅ 修订单元测试 backup.test.ts (#914)
* 修订单元测试 backup.test.ts * 添加超时时间 * 添加超时 * 修订 --------- Co-authored-by: 王一之 <[email protected]>
1 parent 0c7ddd1 commit a123e26

File tree

2 files changed

+278
-17
lines changed

2 files changed

+278
-17
lines changed

packages/filesystem/zip/zip.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import JSZip from "jszip";
1+
import type JSZip from "jszip";
22
import type { File, FileReader, FileWriter } from "@Packages/filesystem/filesystem";
33
import type FileSystem from "@Packages/filesystem/filesystem";
44
import { ZipFileReader, ZipFileWriter } from "./rw";
@@ -9,8 +9,8 @@ export default class ZipFileSystem implements FileSystem {
99
basePath: string;
1010

1111
// zip为空时,创建一个空的zip
12-
constructor(zip?: JSZip, basePath?: string) {
13-
this.zip = zip || new JSZip();
12+
constructor(zip: JSZip, basePath?: string) {
13+
this.zip = zip;
1414
this.basePath = basePath || "";
1515
}
1616

@@ -45,16 +45,17 @@ export default class ZipFileSystem implements FileSystem {
4545

4646
async list(): Promise<File[]> {
4747
const files: File[] = [];
48-
Object.keys(this.zip.files).forEach((key) => {
48+
for (const [filename, details] of Object.entries(this.zip.files)) {
49+
const time = details.date.getTime();
4950
files.push({
50-
name: key,
51-
path: key,
51+
name: filename,
52+
path: filename,
5253
size: 0,
5354
digest: "",
54-
createtime: this.zip.files[key].date.getTime(),
55-
updatetime: this.zip.files[key].date.getTime(),
55+
createtime: time,
56+
updatetime: time,
5657
});
57-
});
58+
}
5859
return files;
5960
}
6061

src/pkg/backup/backup.test.ts

Lines changed: 268 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
import JSZip from "jszip";
22
import BackupExport from "./export";
3-
import BackupImport from "./import";
3+
import { parseBackupZipFile } from "./utils";
44
import type { BackupData } from "./struct";
55
import { describe, expect, it } from "vitest";
66
import ZipFileSystem from "@Packages/filesystem/zip/zip";
77

8-
describe("backup", () => {
9-
const zipFile = new JSZip();
10-
const fs = new ZipFileSystem(zipFile);
11-
it("empty", async () => {
8+
describe.concurrent("backup", () => {
9+
it.concurrent("empty", async () => {
10+
const zipFile = new JSZip();
11+
const fs = new ZipFileSystem(zipFile);
1212
await new BackupExport(fs).export({
1313
script: [],
1414
subscribe: [],
1515
});
16-
const resp = await new BackupImport(fs).parse();
16+
const resp = await parseBackupZipFile(zipFile);
1717
expect(resp).toEqual({
1818
script: [],
1919
subscribe: [],
2020
});
2121
});
2222

23-
it("export and import script", async () => {
23+
it.concurrent("export and import script - basic", async () => {
24+
const zipFile = new JSZip();
25+
const fs = new ZipFileSystem(zipFile);
2426
const data: BackupData = {
2527
script: [
2628
{
@@ -102,10 +104,268 @@ describe("backup", () => {
102104
expect(data.script[0].storage.data.num).toEqual("n1");
103105
expect(data.script[0].storage.data.str).toEqual("sdata");
104106
expect(data.script[0].storage.data.bool).toEqual("bfalse");
105-
const resp = await new BackupImport(fs).parse();
107+
const resp = await parseBackupZipFile(zipFile);
106108
data.script[0].storage.data.num = 1;
107109
data.script[0].storage.data.str = "data";
108110
data.script[0].storage.data.bool = false;
109111
expect(resp).toEqual(data);
110112
});
113+
114+
it.concurrent("export and import script - name and version only", async () => {
115+
const zipFile = new JSZip();
116+
const fs = new ZipFileSystem(zipFile);
117+
const data: BackupData = {
118+
script: [
119+
{
120+
code: `// ==UserScript==
121+
// @name New Userscript
122+
// @version 1
123+
// ==/UserScript==
124+
125+
console.log('hello world')`,
126+
options: {
127+
options: {},
128+
meta: {
129+
name: "test",
130+
modified: 1,
131+
file_url: "",
132+
},
133+
settings: {
134+
enabled: true,
135+
position: 1,
136+
},
137+
},
138+
resources: [
139+
{
140+
meta: { name: "test1", mimetype: "text/plain" },
141+
base64: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
142+
source: "hello world",
143+
},
144+
],
145+
requires: [
146+
{
147+
meta: { name: "test2", mimetype: "text/plain" },
148+
base64: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
149+
source: "hello world",
150+
},
151+
],
152+
requiresCss: [
153+
{
154+
meta: { name: "test3", mimetype: "application/javascript" },
155+
base64: "data:application/javascript;base64,aGVsbG8gd29ybGQ=",
156+
source: "hello world",
157+
},
158+
],
159+
storage: {
160+
ts: 1,
161+
data: {
162+
num: 1,
163+
str: "data",
164+
bool: false,
165+
},
166+
},
167+
},
168+
],
169+
subscribe: [],
170+
} as unknown as BackupData;
171+
await new BackupExport(fs).export(data);
172+
expect(data.script[0].storage.data.num).toEqual("n1");
173+
expect(data.script[0].storage.data.str).toEqual("sdata");
174+
expect(data.script[0].storage.data.bool).toEqual("bfalse");
175+
const resp = await parseBackupZipFile(zipFile);
176+
data.script[0].storage.data.num = 1;
177+
data.script[0].storage.data.str = "data";
178+
data.script[0].storage.data.bool = false;
179+
expect(resp).toEqual(data);
180+
});
181+
182+
it.concurrent("export and import script - 2 scripts", async () => {
183+
const zipFile = new JSZip();
184+
const fs = new ZipFileSystem(zipFile);
185+
const data: BackupData = {
186+
script: [
187+
{
188+
code: `// ==UserScript==
189+
// @name New Userscript 1
190+
// @version 1
191+
// ==/UserScript==
192+
193+
console.log('hello world')`,
194+
options: {
195+
options: {},
196+
meta: {
197+
name: "test01", // 不能重复
198+
modified: 1,
199+
file_url: "",
200+
},
201+
settings: {
202+
enabled: true,
203+
position: 1,
204+
},
205+
},
206+
resources: [
207+
{
208+
meta: { name: "test1", mimetype: "text/plain" },
209+
base64: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
210+
source: "hello world",
211+
},
212+
],
213+
requires: [
214+
{
215+
meta: { name: "test2", mimetype: "text/plain" },
216+
base64: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
217+
source: "hello world",
218+
},
219+
],
220+
requiresCss: [
221+
{
222+
meta: { name: "test3", mimetype: "application/javascript" },
223+
base64: "data:application/javascript;base64,aGVsbG8gd29ybGQ=",
224+
source: "hello world",
225+
},
226+
],
227+
storage: {
228+
ts: 1,
229+
data: {
230+
num: 1,
231+
str: "data",
232+
bool: false,
233+
},
234+
},
235+
},
236+
{
237+
code: `// ==UserScript==
238+
// @name New Userscript 2
239+
// @version 1
240+
// ==/UserScript==
241+
242+
console.log('hello world')`,
243+
options: {
244+
options: {},
245+
meta: {
246+
name: "test02", // 不能重复
247+
modified: 1,
248+
file_url: "",
249+
},
250+
settings: {
251+
enabled: true,
252+
position: 1,
253+
},
254+
},
255+
resources: [
256+
{
257+
meta: { name: "test1", mimetype: "text/plain" },
258+
base64: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
259+
source: "hello world",
260+
},
261+
],
262+
requires: [
263+
{
264+
meta: { name: "test2", mimetype: "text/plain" },
265+
base64: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
266+
source: "hello world",
267+
},
268+
],
269+
requiresCss: [
270+
{
271+
meta: { name: "test3", mimetype: "application/javascript" },
272+
base64: "data:application/javascript;base64,aGVsbG8gd29ybGQ=",
273+
source: "hello world",
274+
},
275+
],
276+
storage: {
277+
ts: 1,
278+
data: {},
279+
},
280+
},
281+
],
282+
subscribe: [],
283+
} as unknown as BackupData;
284+
await new BackupExport(fs).export(data);
285+
expect(data.script[0].storage.data.num).toEqual("n1");
286+
expect(data.script[0].storage.data.str).toEqual("sdata");
287+
expect(data.script[0].storage.data.bool).toEqual("bfalse");
288+
const resp = await parseBackupZipFile(zipFile);
289+
data.script[0].storage.data.num = 1;
290+
data.script[0].storage.data.str = "data";
291+
data.script[0].storage.data.bool = false;
292+
expect(resp).toEqual(data);
293+
});
294+
295+
it.concurrent("export and import script - 30 scripts + 20 subscribes", async () => {
296+
const zipFile = new JSZip();
297+
const fs = new ZipFileSystem(zipFile);
298+
const data: BackupData = {
299+
script: Array.from({ length: 30 }, (v, i) => {
300+
return {
301+
code: `// ==UserScript==
302+
// @name New Userscript ${i}
303+
// @version 1
304+
// ==/UserScript==
305+
306+
console.log('hello world')`,
307+
options: {
308+
options: {},
309+
meta: {
310+
name: `test_${i}`, // 不能重复
311+
modified: 1,
312+
file_url: "",
313+
},
314+
settings: {
315+
enabled: true,
316+
position: 1,
317+
},
318+
},
319+
resources: [
320+
{
321+
meta: { name: "test1", mimetype: "text/plain" },
322+
base64: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
323+
source: "hello world",
324+
},
325+
],
326+
requires: [
327+
{
328+
meta: { name: "test2", mimetype: "text/plain" },
329+
base64: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
330+
source: "hello world",
331+
},
332+
],
333+
requiresCss: [
334+
{
335+
meta: { name: "test3", mimetype: "application/javascript" },
336+
base64: "data:application/javascript;base64,aGVsbG8gd29ybGQ=",
337+
source: "hello world",
338+
},
339+
],
340+
storage: {
341+
ts: 1,
342+
data: {},
343+
},
344+
};
345+
}),
346+
subscribe: Array.from({ length: 20 }, (v, i) => {
347+
return {
348+
source: `// ==UserSubscribe==
349+
// @name New Usersubscribe
350+
// @namespace https://bbs.tampermonkey.net.cn/
351+
// @version 0.1.0
352+
// @description try to take over the world!
353+
// @author You
354+
// ==/UserSubscribe==
355+
356+
console.log('hello world')`,
357+
options: {
358+
meta: {
359+
name: `test_${i}`, // 不能重复
360+
modified: 1,
361+
url: "",
362+
},
363+
},
364+
};
365+
}),
366+
} as unknown as BackupData;
367+
await new BackupExport(fs).export(data);
368+
const resp = await parseBackupZipFile(zipFile);
369+
expect(resp).toEqual(data);
370+
});
111371
});

0 commit comments

Comments
 (0)