hub-client-import-zip.test.mjs
73 lines 4.0 KB
Raw
1 import assert from 'assert';
2 import { test } from 'node:test';
3 import JSZip from 'jszip';
4 import {
5 getHubImportFileMode,
6 buildImportZipBlobWithJsZip,
7 DEFAULT_HUB_IMPORT_ZIP_LIMITS,
8 } from '../web/hub/hub-client-import-zip.mjs';
9
10 test('getHubImportFileMode: direct single, sequential multi pdf, client_zip markdown multi', () => {
11 const a = new File([new Uint8Array([1])], 'a.pdf', { type: 'application/pdf' });
12 const b = new File([new Uint8Array([1])], 'b.pdf', { type: 'application/pdf' });
13 const c1 = new File([new Uint8Array([1])], 'a.csv', { type: 'text/csv' });
14 const c2 = new File([new Uint8Array([1])], 'b.csv', { type: 'text/csv' });
15 const z = new File([new Uint8Array([1])], 'e.zip', { type: 'application/zip' });
16 assert.equal(getHubImportFileMode('pdf', [a]), 'direct');
17 assert.equal(getHubImportFileMode('pdf', [a, b]), 'sequential');
18 assert.equal(getHubImportFileMode('generic-csv', [c1, c2]), 'sequential');
19 const x1 = new File([new Uint8Array([1])], 'a.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
20 const x2 = new File([new Uint8Array([1])], 'b.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
21 const v1 = new File([new Uint8Array([1])], 'a.vcf', { type: 'text/vcard' });
22 const v2 = new File([new Uint8Array([1])], 'b.vcf', { type: 'text/vcard' });
23 assert.equal(getHubImportFileMode('excel-xlsx', [x1, x2]), 'sequential');
24 assert.equal(getHubImportFileMode('vcf', [v1, v2]), 'sequential');
25 const m1 = new File([new Uint8Array([1])], 'x.md', { type: 'text/markdown' });
26 const m2 = new File([new Uint8Array([1])], 'y.md', { type: 'text/markdown' });
27 assert.equal(getHubImportFileMode('markdown', [m1, m2]), 'client_zip');
28 assert.equal(getHubImportFileMode('markdown', [z]), 'direct');
29 });
30
31 test('getHubImportFileMode: chatgpt always client_zip unless single server zip', () => {
32 const c = new File([new Uint8Array([1])], 'conversations.json', { type: 'application/json' });
33 const z = new File([new Uint8Array([1])], 'e.zip', { type: 'application/zip' });
34 assert.equal(getHubImportFileMode('chatgpt-export', [c]), 'client_zip');
35 assert.equal(getHubImportFileMode('chatgpt-export', [z]), 'direct');
36 });
37
38 test('getHubImportFileMode: claude: multi md = zip, multi json = sequential', () => {
39 const j1 = new File([new Uint8Array([1])], 'a.json', { type: 'application/json' });
40 const j2 = new File([new Uint8Array([1])], 'b.json', { type: 'application/json' });
41 const m1 = new File([new Uint8Array([1])], 'a.md', { type: 'text/markdown' });
42 const m2 = new File([new Uint8Array([1])], 'b.md', { type: 'text/markdown' });
43 assert.equal(getHubImportFileMode('claude-export', [j1, j2]), 'sequential');
44 assert.equal(getHubImportFileMode('claude-export', [m1, m2]), 'client_zip');
45 });
46
47 test('buildImportZipBlobWithJsZip: preserves paths, duplicate rename', async () => {
48 const w = { warn: () => {} };
49 const a = new File([new TextEncoder().encode('# a')], 'a.md', { type: 'text/plain' });
50 const b = new File([new TextEncoder().encode('# b')], 'a.md', { type: 'text/plain' });
51 const blob = await buildImportZipBlobWithJsZip(JSZip, [a, b], DEFAULT_HUB_IMPORT_ZIP_LIMITS, w);
52 const buf = await blob.arrayBuffer();
53 const jz = new JSZip();
54 const u = await jz.loadAsync(buf);
55 const names = Object.keys(u.files).filter((k) => !u.files[k].dir);
56 assert.equal(names.length, 2);
57 assert(names.some((n) => n === 'a.md' || n === 'a(1).md'), 'expected deduped name');
58 });
59
60 test('buildImportZipBlobWithJsZip: rejects too many small files (limit)', async () => {
61 const limits = {
62 maxZipBytes: 10 * 1024 * 1024,
63 maxUncompressedBytes: 10 * 1024 * 1024,
64 maxFiles: 2,
65 };
66 const a = new File([new Uint8Array([0])], '1.md', { type: 'text/plain' });
67 const b = new File([new Uint8Array([0])], '2.md', { type: 'text/plain' });
68 const c = new File([new Uint8Array([0])], '3.md', { type: 'text/plain' });
69 await assert.rejects(
70 () => buildImportZipBlobWithJsZip(JSZip, [a, b, c], limits, {}),
71 /Too many files/,
72 );
73 });
File History 1 commit