|
1 | 1 | import JSZip from "jszip"; |
2 | 2 | import BackupExport from "./export"; |
3 | | -import BackupImport from "./import"; |
| 3 | +import { parseBackupZipFile } from "./utils"; |
4 | 4 | import type { BackupData } from "./struct"; |
5 | 5 | import { describe, expect, it } from "vitest"; |
6 | 6 | import ZipFileSystem from "@Packages/filesystem/zip/zip"; |
7 | 7 |
|
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); |
12 | 12 | await new BackupExport(fs).export({ |
13 | 13 | script: [], |
14 | 14 | subscribe: [], |
15 | 15 | }); |
16 | | - const resp = await new BackupImport(fs).parse(); |
| 16 | + const resp = await parseBackupZipFile(zipFile); |
17 | 17 | expect(resp).toEqual({ |
18 | 18 | script: [], |
19 | 19 | subscribe: [], |
20 | 20 | }); |
21 | 21 | }); |
22 | 22 |
|
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); |
24 | 26 | const data: BackupData = { |
25 | 27 | script: [ |
26 | 28 | { |
@@ -102,10 +104,268 @@ describe("backup", () => { |
102 | 104 | expect(data.script[0].storage.data.num).toEqual("n1"); |
103 | 105 | expect(data.script[0].storage.data.str).toEqual("sdata"); |
104 | 106 | expect(data.script[0].storage.data.bool).toEqual("bfalse"); |
105 | | - const resp = await new BackupImport(fs).parse(); |
| 107 | + const resp = await parseBackupZipFile(zipFile); |
106 | 108 | data.script[0].storage.data.num = 1; |
107 | 109 | data.script[0].storage.data.str = "data"; |
108 | 110 | data.script[0].storage.data.bool = false; |
109 | 111 | expect(resp).toEqual(data); |
110 | 112 | }); |
| 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 | + }); |
111 | 371 | }); |
0 commit comments