@fgv/ts-web-extras 5.0.1-8 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.rush/temp/chunked-rush-logs/ts-web-extras.build.chunks.jsonl +9 -9
- package/.rush/temp/operation/build/all.log +9 -9
- package/.rush/temp/operation/build/log-chunks.jsonl +9 -9
- package/.rush/temp/operation/build/state.json +1 -1
- package/.rush/temp/shrinkwrap-deps.json +40 -38
- package/config/jest.config.json +1 -1
- package/config/rig.json +1 -1
- package/dist/index.browser.js +24 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +1 -0
- package/dist/packlets/crypto/browserHashProvider.js +66 -0
- package/dist/packlets/crypto/browserHashProvider.js.map +1 -0
- package/dist/packlets/crypto/index.js +27 -0
- package/dist/packlets/crypto/index.js.map +1 -0
- package/dist/packlets/file-api-types/index.js +156 -0
- package/dist/packlets/file-api-types/index.js.map +1 -0
- package/dist/packlets/file-tree/fileApiTreeAccessors.js +330 -0
- package/dist/packlets/file-tree/fileApiTreeAccessors.js.map +1 -0
- package/dist/packlets/file-tree/index.js +27 -0
- package/dist/packlets/file-tree/index.js.map +1 -0
- package/dist/packlets/helpers/fileTreeHelpers.js +94 -0
- package/dist/packlets/helpers/fileTreeHelpers.js.map +1 -0
- package/dist/packlets/helpers/index.js +27 -0
- package/dist/packlets/helpers/index.js.map +1 -0
- package/dist/packlets/url-utils/index.js +27 -0
- package/dist/packlets/url-utils/index.js.map +1 -0
- package/dist/packlets/url-utils/urlParams.js +157 -0
- package/dist/packlets/url-utils/urlParams.js.map +1 -0
- package/dist/test/setupTests.js +74 -0
- package/dist/test/setupTests.js.map +1 -0
- package/dist/test/unit/browserHashProvider.test.js +140 -0
- package/dist/test/unit/browserHashProvider.test.js.map +1 -0
- package/dist/test/unit/fileApiTreeAccessors.test.js +1137 -0
- package/dist/test/unit/fileApiTreeAccessors.test.js.map +1 -0
- package/dist/test/unit/fileApiTypes.test.js +442 -0
- package/dist/test/unit/fileApiTypes.test.js.map +1 -0
- package/dist/test/unit/fileTreeHelpers.test.js +590 -0
- package/dist/test/unit/fileTreeHelpers.test.js.map +1 -0
- package/dist/test/unit/urlParams.test.js +393 -0
- package/dist/test/unit/urlParams.test.js.map +1 -0
- package/dist/test/utils/testHelpers.js +124 -0
- package/dist/test/utils/testHelpers.js.map +1 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.browser.d.ts +2 -0
- package/lib/index.browser.d.ts.map +1 -0
- package/lib/index.browser.js +40 -0
- package/lib/index.browser.js.map +1 -0
- package/package.json +20 -6
- package/rush-logs/ts-web-extras.build.cache.log +1 -3
- package/rush-logs/ts-web-extras.build.log +9 -9
- package/src/index.browser.ts +24 -0
- package/temp/build/typescript/ts_vnCx6LlY.json +1 -0
- package/temp/coverage/crypto/browserHashProvider.ts.html +1 -1
- package/temp/coverage/crypto/index.html +1 -1
- package/temp/coverage/file-tree/fileApiTreeAccessors.ts.html +1 -1
- package/temp/coverage/file-tree/index.html +1 -1
- package/temp/coverage/helpers/fileTreeHelpers.ts.html +1 -1
- package/temp/coverage/helpers/index.html +1 -1
- package/temp/coverage/index.html +1 -1
- package/temp/coverage/lcov-report/crypto/browserHashProvider.ts.html +1 -1
- package/temp/coverage/lcov-report/crypto/index.html +1 -1
- package/temp/coverage/lcov-report/file-tree/fileApiTreeAccessors.ts.html +1 -1
- package/temp/coverage/lcov-report/file-tree/index.html +1 -1
- package/temp/coverage/lcov-report/helpers/fileTreeHelpers.ts.html +1 -1
- package/temp/coverage/lcov-report/helpers/index.html +1 -1
- package/temp/coverage/lcov-report/index.html +1 -1
- package/temp/coverage/lcov-report/url-utils/index.html +1 -1
- package/temp/coverage/lcov-report/url-utils/urlParams.ts.html +1 -1
- package/temp/coverage/url-utils/index.html +1 -1
- package/temp/coverage/url-utils/urlParams.ts.html +1 -1
- package/temp/test/jest/haste-map-7492f1b44480e0cdd1f220078fb3afd8-c8dd6c3430605adeb2f1cadf4f75e791-8c9336785555d572065b28c111982ba4 +0 -0
- package/temp/test/jest/perf-cache-7492f1b44480e0cdd1f220078fb3afd8-da39a3ee5e6b4b0d3255bfef95601890 +1 -1
- package/temp/ts-web-extras.api.json +1 -1
- package/.rush/temp/3b013d9faf7f9da4019da446f1d05aef5eb5f80d.tar.log +0 -121
- package/temp/build/typescript/ts_l9Fw4VUO.json +0 -1
- /package/temp/test/jest/jest-transform-cache-7492f1b44480e0cdd1f220078fb3afd8-79ef2876fae7ca75eedb2aa53dc48338/{3f/package_3fe59327dabcb949f1b2ad415f67debd → 0e/package_0eb6535f5987849d93ea51ef33a14cf6} +0 -0
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
import '@fgv/ts-utils-jest';
|
|
23
|
+
import { FileTreeHelpers } from '../../packlets/helpers';
|
|
24
|
+
import { createMockFile, createMockFileList, createMockDirectoryFileList } from '../utils/testHelpers';
|
|
25
|
+
describe('FileTreeHelpers', () => {
|
|
26
|
+
describe('fromFileList', () => {
|
|
27
|
+
test('creates FileTree from simple FileList', async () => {
|
|
28
|
+
const fileList = createMockFileList([
|
|
29
|
+
{ name: 'file1.txt', content: 'content1' },
|
|
30
|
+
{ name: 'file2.txt', content: 'content2' }
|
|
31
|
+
]);
|
|
32
|
+
const result = await FileTreeHelpers.fromFileList(fileList);
|
|
33
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
34
|
+
// Verify files are accessible with proper paths
|
|
35
|
+
expect(fileTree.getFile('/file1.txt')).toSucceed();
|
|
36
|
+
expect(fileTree.getFile('/file2.txt')).toSucceed();
|
|
37
|
+
// Verify content can be retrieved
|
|
38
|
+
const content1 = fileTree.hal.getFileContents('/file1.txt');
|
|
39
|
+
expect(content1).toSucceedWith('content1');
|
|
40
|
+
const content2 = fileTree.hal.getFileContents('/file2.txt');
|
|
41
|
+
expect(content2).toSucceedWith('content2');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
test('creates FileTree from FileList with prefix', async () => {
|
|
45
|
+
const fileList = createMockFileList([{ name: 'test.txt', content: 'test content' }]);
|
|
46
|
+
const result = await FileTreeHelpers.fromFileList(fileList, { prefix: '/upload' });
|
|
47
|
+
expect(result).toSucceed();
|
|
48
|
+
});
|
|
49
|
+
test('handles empty FileList', async () => {
|
|
50
|
+
const fileList = createMockFileList([]);
|
|
51
|
+
const result = await FileTreeHelpers.fromFileList(fileList);
|
|
52
|
+
expect(result).toSucceed();
|
|
53
|
+
});
|
|
54
|
+
test('handles various file types', async () => {
|
|
55
|
+
const fileList = createMockFileList([
|
|
56
|
+
{ name: 'data.json', content: '{"key": "value"}', type: 'application/json' },
|
|
57
|
+
{ name: 'script.js', content: 'console.log("hello");', type: 'application/javascript' },
|
|
58
|
+
{ name: 'style.css', content: 'body { margin: 0; }', type: 'text/css' },
|
|
59
|
+
{ name: 'readme.txt', content: 'This is a readme', type: 'text/plain' }
|
|
60
|
+
]);
|
|
61
|
+
const result = await FileTreeHelpers.fromFileList(fileList);
|
|
62
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
63
|
+
expect(fileTree.getFile('/data.json')).toSucceed();
|
|
64
|
+
expect(fileTree.getFile('/script.js')).toSucceed();
|
|
65
|
+
expect(fileTree.getFile('/style.css')).toSucceed();
|
|
66
|
+
expect(fileTree.getFile('/readme.txt')).toSucceed();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
test('automatically applies MIME types as contentType', async () => {
|
|
70
|
+
const fileList = createMockFileList([
|
|
71
|
+
{ name: 'data.json', content: '{"key": "value"}', type: 'application/json' },
|
|
72
|
+
{ name: 'script.js', content: 'console.log("hello");', type: 'application/javascript' },
|
|
73
|
+
{ name: 'style.css', content: 'body { margin: 0; }', type: 'text/css' },
|
|
74
|
+
{ name: 'readme.txt', content: 'This is a readme', type: 'text/plain' },
|
|
75
|
+
{ name: 'image.png', content: 'PNG data', type: 'image/png' },
|
|
76
|
+
{ name: 'document.pdf', content: 'PDF content', type: 'application/pdf' },
|
|
77
|
+
{ name: 'archive.zip', content: 'ZIP data', type: 'application/zip' }
|
|
78
|
+
]);
|
|
79
|
+
const result = await FileTreeHelpers.fromFileList(fileList);
|
|
80
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
81
|
+
// Verify MIME types are automatically applied as contentType
|
|
82
|
+
expect(fileTree.getFile('/data.json')).toSucceedAndSatisfy((file) => {
|
|
83
|
+
expect(file.contentType).toBe('application/json');
|
|
84
|
+
});
|
|
85
|
+
expect(fileTree.getFile('/script.js')).toSucceedAndSatisfy((file) => {
|
|
86
|
+
expect(file.contentType).toBe('application/javascript');
|
|
87
|
+
});
|
|
88
|
+
expect(fileTree.getFile('/style.css')).toSucceedAndSatisfy((file) => {
|
|
89
|
+
expect(file.contentType).toBe('text/css');
|
|
90
|
+
});
|
|
91
|
+
expect(fileTree.getFile('/readme.txt')).toSucceedAndSatisfy((file) => {
|
|
92
|
+
expect(file.contentType).toBe('text/plain');
|
|
93
|
+
});
|
|
94
|
+
expect(fileTree.getFile('/image.png')).toSucceedAndSatisfy((file) => {
|
|
95
|
+
expect(file.contentType).toBe('image/png');
|
|
96
|
+
});
|
|
97
|
+
expect(fileTree.getFile('/document.pdf')).toSucceedAndSatisfy((file) => {
|
|
98
|
+
expect(file.contentType).toBe('application/pdf');
|
|
99
|
+
});
|
|
100
|
+
expect(fileTree.getFile('/archive.zip')).toSucceedAndSatisfy((file) => {
|
|
101
|
+
expect(file.contentType).toBe('application/zip');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
test('handles files without MIME types', async () => {
|
|
106
|
+
const fileList = createMockFileList([
|
|
107
|
+
{ name: 'no-type.txt', content: 'content without type' },
|
|
108
|
+
{ name: 'empty-type.dat', content: 'empty type', type: '' },
|
|
109
|
+
{ name: 'unknown.ext', content: 'unknown extension' }
|
|
110
|
+
]);
|
|
111
|
+
const result = await FileTreeHelpers.fromFileList(fileList);
|
|
112
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
113
|
+
// Files without MIME types should have undefined contentType
|
|
114
|
+
expect(fileTree.getFile('/no-type.txt')).toSucceedAndSatisfy((file) => {
|
|
115
|
+
expect(file.contentType).toBe('text/plain'); // Default from createMockFile
|
|
116
|
+
});
|
|
117
|
+
expect(fileTree.getFile('/empty-type.dat')).toSucceedAndSatisfy((file) => {
|
|
118
|
+
expect(file.contentType).toBe('text/plain'); // Browser defaults empty type to text/plain
|
|
119
|
+
});
|
|
120
|
+
expect(fileTree.getFile('/unknown.ext')).toSucceedAndSatisfy((file) => {
|
|
121
|
+
expect(file.contentType).toBe('text/plain'); // Default from createMockFile
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
test('forces content type to string (not templated)', async () => {
|
|
126
|
+
const fileList = createMockFileList([
|
|
127
|
+
{ name: 'test.json', content: '{"test": true}', type: 'application/json' }
|
|
128
|
+
]);
|
|
129
|
+
const result = await FileTreeHelpers.fromFileList(fileList);
|
|
130
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
131
|
+
// Verify the return type is FileTree<string> not FileTree<T>
|
|
132
|
+
expect(fileTree.getFile('/test.json')).toSucceedAndSatisfy((file) => {
|
|
133
|
+
expect(typeof file.contentType).toBe('string');
|
|
134
|
+
expect(file.contentType).toBe('application/json');
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
test('propagates file reading errors', async () => {
|
|
139
|
+
const badFile = {
|
|
140
|
+
name: 'bad.txt',
|
|
141
|
+
size: 10,
|
|
142
|
+
type: 'text/plain',
|
|
143
|
+
lastModified: Date.now(),
|
|
144
|
+
text: () => Promise.reject(new Error('Read failed'))
|
|
145
|
+
};
|
|
146
|
+
const fileList = createMockFileList([{ name: 'good.txt', content: 'good content' }]);
|
|
147
|
+
// Create a FileList with just the bad file
|
|
148
|
+
const dt = new DataTransfer();
|
|
149
|
+
dt.items.add(badFile);
|
|
150
|
+
const badFileList = dt.files;
|
|
151
|
+
const result = await FileTreeHelpers.fromFileList(badFileList);
|
|
152
|
+
expect(result).toFailWith(/Failed to read file/);
|
|
153
|
+
});
|
|
154
|
+
test('maintains file accessibility through FileTree API', async () => {
|
|
155
|
+
const fileList = createMockFileList([{ name: 'test.json', content: '{"name": "test", "value": 42}' }]);
|
|
156
|
+
const result = await FileTreeHelpers.fromFileList(fileList);
|
|
157
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
158
|
+
const file = fileTree.getFile('/test.json');
|
|
159
|
+
expect(file).toSucceedAndSatisfy((fileItem) => {
|
|
160
|
+
// Test JSON parsing through FileTree
|
|
161
|
+
const jsonResult = fileItem.getContents();
|
|
162
|
+
expect(jsonResult).toSucceedAndSatisfy((parsed) => {
|
|
163
|
+
expect(parsed).toEqual({ name: 'test', value: 42 });
|
|
164
|
+
});
|
|
165
|
+
// Test raw content access
|
|
166
|
+
const rawResult = fileItem.getRawContents();
|
|
167
|
+
expect(rawResult).toSucceedWith('{"name": "test", "value": 42}');
|
|
168
|
+
// Test file properties
|
|
169
|
+
expect(fileItem.name).toBe('test.json');
|
|
170
|
+
expect(fileItem.extension).toBe('.json');
|
|
171
|
+
expect(fileItem.baseName).toBe('test');
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
describe('fromDirectoryUpload', () => {
|
|
177
|
+
test('creates FileTree from directory structure', async () => {
|
|
178
|
+
const fileList = createMockDirectoryFileList([
|
|
179
|
+
{ path: 'project/src/index.js', content: 'console.log("main");' },
|
|
180
|
+
{ path: 'project/src/utils.js', content: 'export const helper = () => {};' },
|
|
181
|
+
{ path: 'project/package.json', content: '{"name": "test-project"}' },
|
|
182
|
+
{ path: 'project/README.md', content: '# Test Project' }
|
|
183
|
+
]);
|
|
184
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
185
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
186
|
+
// Verify all files are accessible
|
|
187
|
+
expect(fileTree.getFile('/project/src/index.js')).toSucceed();
|
|
188
|
+
expect(fileTree.getFile('/project/src/utils.js')).toSucceed();
|
|
189
|
+
expect(fileTree.getFile('/project/package.json')).toSucceed();
|
|
190
|
+
expect(fileTree.getFile('/project/README.md')).toSucceed();
|
|
191
|
+
// Verify directory structure
|
|
192
|
+
expect(fileTree.getDirectory('/project')).toSucceed();
|
|
193
|
+
expect(fileTree.getDirectory('/project/src')).toSucceed();
|
|
194
|
+
// Verify content retrieval
|
|
195
|
+
const packageContent = fileTree.hal.getFileContents('/project/package.json');
|
|
196
|
+
expect(packageContent).toSucceedWith('{"name": "test-project"}');
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
test('creates FileTree from directory with prefix', async () => {
|
|
200
|
+
const fileList = createMockDirectoryFileList([
|
|
201
|
+
{ path: 'app/config.json', content: '{"setting": "value"}' },
|
|
202
|
+
{ path: 'app/main.js', content: 'console.log("app");' }
|
|
203
|
+
]);
|
|
204
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList, { prefix: '/upload' });
|
|
205
|
+
expect(result).toSucceed();
|
|
206
|
+
});
|
|
207
|
+
test('preserves directory hierarchy', async () => {
|
|
208
|
+
const fileList = createMockDirectoryFileList([
|
|
209
|
+
{ path: 'deep/nested/structure/file1.txt', content: 'content1' },
|
|
210
|
+
{ path: 'deep/nested/structure/file2.txt', content: 'content2' },
|
|
211
|
+
{ path: 'deep/other/branch/file3.txt', content: 'content3' },
|
|
212
|
+
{ path: 'deep/file4.txt', content: 'content4' }
|
|
213
|
+
]);
|
|
214
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
215
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
216
|
+
// Test all directory levels
|
|
217
|
+
expect(fileTree.getDirectory('/deep')).toSucceed();
|
|
218
|
+
expect(fileTree.getDirectory('/deep/nested')).toSucceed();
|
|
219
|
+
expect(fileTree.getDirectory('/deep/nested/structure')).toSucceed();
|
|
220
|
+
expect(fileTree.getDirectory('/deep/other')).toSucceed();
|
|
221
|
+
expect(fileTree.getDirectory('/deep/other/branch')).toSucceed();
|
|
222
|
+
// Test directory traversal
|
|
223
|
+
const deepDir = fileTree.getDirectory('/deep');
|
|
224
|
+
expect(deepDir).toSucceedAndSatisfy((dir) => {
|
|
225
|
+
const children = dir.getChildren();
|
|
226
|
+
expect(children).toSucceedAndSatisfy((items) => {
|
|
227
|
+
const names = items.map((item) => item.name).sort();
|
|
228
|
+
expect(names).toContain('nested');
|
|
229
|
+
expect(names).toContain('other');
|
|
230
|
+
expect(names).toContain('file4.txt');
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
test('handles webkitRelativePath correctly', async () => {
|
|
236
|
+
// Create files with explicit webkitRelativePath
|
|
237
|
+
const files = [
|
|
238
|
+
createMockFile({
|
|
239
|
+
name: 'index.html',
|
|
240
|
+
content: '<html></html>',
|
|
241
|
+
webkitRelativePath: 'website/index.html'
|
|
242
|
+
}),
|
|
243
|
+
createMockFile({
|
|
244
|
+
name: 'style.css',
|
|
245
|
+
content: 'body {}',
|
|
246
|
+
webkitRelativePath: 'website/css/style.css'
|
|
247
|
+
})
|
|
248
|
+
];
|
|
249
|
+
// Create FileList manually to preserve webkitRelativePath
|
|
250
|
+
const dt = new DataTransfer();
|
|
251
|
+
files.forEach((file) => dt.items.add(file));
|
|
252
|
+
const fileList = dt.files;
|
|
253
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
254
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
255
|
+
expect(fileTree.getFile('/website/index.html')).toSucceed();
|
|
256
|
+
expect(fileTree.getFile('/website/css/style.css')).toSucceed();
|
|
257
|
+
expect(fileTree.getDirectory('/website')).toSucceed();
|
|
258
|
+
expect(fileTree.getDirectory('/website/css')).toSucceed();
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
test('handles single file in directory', async () => {
|
|
262
|
+
const fileList = createMockDirectoryFileList([{ path: 'single/file.txt', content: 'lone file' }]);
|
|
263
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
264
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
265
|
+
expect(fileTree.getFile('/single/file.txt')).toSucceed();
|
|
266
|
+
expect(fileTree.getDirectory('/single')).toSucceed();
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
test('handles empty directory upload', async () => {
|
|
270
|
+
const fileList = createMockDirectoryFileList([]);
|
|
271
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
272
|
+
expect(result).toSucceed();
|
|
273
|
+
});
|
|
274
|
+
test('automatically applies MIME types as contentType in directory upload', async () => {
|
|
275
|
+
const fileList = createMockDirectoryFileList([
|
|
276
|
+
{ path: 'project/package.json', content: '{"name": "test"}', type: 'application/json' },
|
|
277
|
+
{ path: 'project/src/index.js', content: 'console.log("main");', type: 'application/javascript' },
|
|
278
|
+
{ path: 'project/src/styles.css', content: 'body { color: red; }', type: 'text/css' },
|
|
279
|
+
{ path: 'project/README.md', content: '# Project', type: 'text/markdown' },
|
|
280
|
+
{ path: 'project/data.xml', content: '<root></root>', type: 'application/xml' },
|
|
281
|
+
{ path: 'project/assets/logo.svg', content: '<svg></svg>', type: 'image/svg+xml' },
|
|
282
|
+
{ path: 'project/docs/manual.pdf', content: 'PDF content', type: 'application/pdf' }
|
|
283
|
+
]);
|
|
284
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
285
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
286
|
+
// Verify MIME types are automatically applied as contentType
|
|
287
|
+
expect(fileTree.getFile('/project/package.json')).toSucceedAndSatisfy((file) => {
|
|
288
|
+
expect(file.contentType).toBe('application/json');
|
|
289
|
+
});
|
|
290
|
+
expect(fileTree.getFile('/project/src/index.js')).toSucceedAndSatisfy((file) => {
|
|
291
|
+
expect(file.contentType).toBe('application/javascript');
|
|
292
|
+
});
|
|
293
|
+
expect(fileTree.getFile('/project/src/styles.css')).toSucceedAndSatisfy((file) => {
|
|
294
|
+
expect(file.contentType).toBe('text/css');
|
|
295
|
+
});
|
|
296
|
+
expect(fileTree.getFile('/project/README.md')).toSucceedAndSatisfy((file) => {
|
|
297
|
+
expect(file.contentType).toBe('text/markdown');
|
|
298
|
+
});
|
|
299
|
+
expect(fileTree.getFile('/project/data.xml')).toSucceedAndSatisfy((file) => {
|
|
300
|
+
expect(file.contentType).toBe('application/xml');
|
|
301
|
+
});
|
|
302
|
+
expect(fileTree.getFile('/project/assets/logo.svg')).toSucceedAndSatisfy((file) => {
|
|
303
|
+
expect(file.contentType).toBe('image/svg+xml');
|
|
304
|
+
});
|
|
305
|
+
expect(fileTree.getFile('/project/docs/manual.pdf')).toSucceedAndSatisfy((file) => {
|
|
306
|
+
expect(file.contentType).toBe('application/pdf');
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
test('handles mixed MIME types and missing types in directory upload', async () => {
|
|
311
|
+
const fileList = createMockDirectoryFileList([
|
|
312
|
+
{ path: 'mixed/typed.json', content: '{}', type: 'application/json' },
|
|
313
|
+
{ path: 'mixed/untyped.txt', content: 'plain text' }, // No type specified
|
|
314
|
+
{ path: 'mixed/empty-type.dat', content: 'data', type: '' }, // Empty type
|
|
315
|
+
{ path: 'mixed/binary.bin', content: 'binary data', type: 'application/octet-stream' }
|
|
316
|
+
]);
|
|
317
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
318
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
319
|
+
expect(fileTree.getFile('/mixed/typed.json')).toSucceedAndSatisfy((file) => {
|
|
320
|
+
expect(file.contentType).toBe('application/json');
|
|
321
|
+
});
|
|
322
|
+
expect(fileTree.getFile('/mixed/untyped.txt')).toSucceedAndSatisfy((file) => {
|
|
323
|
+
expect(file.contentType).toBe('text/plain'); // Default from createMockFile
|
|
324
|
+
});
|
|
325
|
+
expect(fileTree.getFile('/mixed/empty-type.dat')).toSucceedAndSatisfy((file) => {
|
|
326
|
+
expect(file.contentType).toBe('text/plain'); // Browser defaults empty type to text/plain
|
|
327
|
+
});
|
|
328
|
+
expect(fileTree.getFile('/mixed/binary.bin')).toSucceedAndSatisfy((file) => {
|
|
329
|
+
expect(file.contentType).toBe('application/octet-stream');
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
test('forces content type to string for directory upload (not templated)', async () => {
|
|
334
|
+
const fileList = createMockDirectoryFileList([
|
|
335
|
+
{ path: 'app/config.json', content: '{"app": "test"}', type: 'application/json' }
|
|
336
|
+
]);
|
|
337
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
338
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
339
|
+
// Verify the return type is FileTree<string> not FileTree<T>
|
|
340
|
+
expect(fileTree.getFile('/app/config.json')).toSucceedAndSatisfy((file) => {
|
|
341
|
+
expect(typeof file.contentType).toBe('string');
|
|
342
|
+
expect(file.contentType).toBe('application/json');
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
// Note: fromFileApiFiles method was deprecated and removed.
|
|
348
|
+
// The functionality has been replaced by the create() method in FileApiTreeAccessors
|
|
349
|
+
// which uses TreeInitializer[] instead of IFileApiFile[].
|
|
350
|
+
describe('getOriginalFile', () => {
|
|
351
|
+
test('retrieves original File object by path', () => {
|
|
352
|
+
const fileList = createMockFileList([
|
|
353
|
+
{ name: 'target.txt', content: 'target content', type: 'text/plain' },
|
|
354
|
+
{ name: 'other.txt', content: 'other content', type: 'text/plain' }
|
|
355
|
+
]);
|
|
356
|
+
const result = FileTreeHelpers.getOriginalFile(fileList, 'target.txt');
|
|
357
|
+
expect(result).toSucceedAndSatisfy((file) => {
|
|
358
|
+
expect(file.name).toBe('target.txt');
|
|
359
|
+
expect(file.type).toBe('text/plain');
|
|
360
|
+
expect(file.size).toBeGreaterThan(0);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
test('handles webkitRelativePath matching', () => {
|
|
364
|
+
const fileList = createMockDirectoryFileList([
|
|
365
|
+
{ path: 'folder/subfolder/file.txt', content: 'content', type: 'text/plain' }
|
|
366
|
+
]);
|
|
367
|
+
const result = FileTreeHelpers.getOriginalFile(fileList, 'folder/subfolder/file.txt');
|
|
368
|
+
expect(result).toSucceedAndSatisfy((file) => {
|
|
369
|
+
expect(file.name).toBe('file.txt');
|
|
370
|
+
expect(file.webkitRelativePath).toBe('folder/subfolder/file.txt');
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
test('fails gracefully for non-existent files', () => {
|
|
374
|
+
const fileList = createMockFileList([{ name: 'exists.txt', content: 'content' }]);
|
|
375
|
+
const result = FileTreeHelpers.getOriginalFile(fileList, 'missing.txt');
|
|
376
|
+
expect(result).toFailWith(/File not found: missing\.txt/);
|
|
377
|
+
});
|
|
378
|
+
test('handles empty FileList', () => {
|
|
379
|
+
const fileList = createMockFileList([]);
|
|
380
|
+
const result = FileTreeHelpers.getOriginalFile(fileList, 'any.txt');
|
|
381
|
+
expect(result).toFailWith(/File not found/);
|
|
382
|
+
});
|
|
383
|
+
test('prioritizes exact name match over webkitRelativePath', () => {
|
|
384
|
+
// Create files with both regular names and webkitRelativePath
|
|
385
|
+
const files = [
|
|
386
|
+
createMockFile({ name: 'file.txt', content: 'regular file' }),
|
|
387
|
+
createMockFile({
|
|
388
|
+
name: 'other.txt',
|
|
389
|
+
content: 'directory file',
|
|
390
|
+
webkitRelativePath: 'file.txt' // Same as first file's name
|
|
391
|
+
})
|
|
392
|
+
];
|
|
393
|
+
const dt = new DataTransfer();
|
|
394
|
+
files.forEach((file) => dt.items.add(file));
|
|
395
|
+
const fileList = dt.files;
|
|
396
|
+
const result = FileTreeHelpers.getOriginalFile(fileList, 'file.txt');
|
|
397
|
+
expect(result).toSucceedAndSatisfy((file) => {
|
|
398
|
+
expect(file.name).toBe('file.txt');
|
|
399
|
+
expect(file.size).toBeGreaterThan(0);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
describe('extractFileListMetadata', () => {
|
|
404
|
+
test('extracts complete metadata from FileList', () => {
|
|
405
|
+
const testTime = Date.now();
|
|
406
|
+
const fileList = createMockFileList([
|
|
407
|
+
{
|
|
408
|
+
name: 'document.pdf',
|
|
409
|
+
content: 'PDF content here',
|
|
410
|
+
type: 'application/pdf',
|
|
411
|
+
lastModified: testTime
|
|
412
|
+
},
|
|
413
|
+
{
|
|
414
|
+
name: 'image.png',
|
|
415
|
+
content: 'PNG data',
|
|
416
|
+
type: 'image/png',
|
|
417
|
+
lastModified: testTime + 1000
|
|
418
|
+
}
|
|
419
|
+
]);
|
|
420
|
+
const metadata = FileTreeHelpers.extractFileListMetadata(fileList);
|
|
421
|
+
expect(metadata).toHaveLength(2);
|
|
422
|
+
const pdfMeta = metadata.find((m) => m.name === 'document.pdf');
|
|
423
|
+
expect(pdfMeta).toEqual({
|
|
424
|
+
path: 'document.pdf',
|
|
425
|
+
name: 'document.pdf',
|
|
426
|
+
size: expect.any(Number),
|
|
427
|
+
type: 'application/pdf',
|
|
428
|
+
lastModified: testTime
|
|
429
|
+
});
|
|
430
|
+
const pngMeta = metadata.find((m) => m.name === 'image.png');
|
|
431
|
+
expect(pngMeta).toEqual({
|
|
432
|
+
path: 'image.png',
|
|
433
|
+
name: 'image.png',
|
|
434
|
+
size: expect.any(Number),
|
|
435
|
+
type: 'image/png',
|
|
436
|
+
lastModified: testTime + 1000
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
test('includes webkitRelativePath in path field', () => {
|
|
440
|
+
var _a, _b;
|
|
441
|
+
const fileList = createMockDirectoryFileList([
|
|
442
|
+
{ path: 'project/src/main.js', content: 'main code', type: 'application/javascript' },
|
|
443
|
+
{ path: 'project/assets/logo.png', content: 'logo data', type: 'image/png' }
|
|
444
|
+
]);
|
|
445
|
+
const metadata = FileTreeHelpers.extractFileListMetadata(fileList);
|
|
446
|
+
expect(metadata).toHaveLength(2);
|
|
447
|
+
expect((_a = metadata.find((m) => m.name === 'main.js')) === null || _a === void 0 ? void 0 : _a.path).toBe('project/src/main.js');
|
|
448
|
+
expect((_b = metadata.find((m) => m.name === 'logo.png')) === null || _b === void 0 ? void 0 : _b.path).toBe('project/assets/logo.png');
|
|
449
|
+
});
|
|
450
|
+
test('handles various file sizes correctly', () => {
|
|
451
|
+
const fileList = createMockFileList([
|
|
452
|
+
{ name: 'tiny.txt', content: '' },
|
|
453
|
+
{ name: 'small.txt', content: 'small' },
|
|
454
|
+
{ name: 'large.txt', content: 'x'.repeat(10000) }
|
|
455
|
+
]);
|
|
456
|
+
const metadata = FileTreeHelpers.extractFileListMetadata(fileList);
|
|
457
|
+
const sizes = metadata.map((m) => m.size);
|
|
458
|
+
expect(sizes[0]).toBe(0); // empty file
|
|
459
|
+
expect(sizes[1]).toBeGreaterThan(0); // small file
|
|
460
|
+
expect(sizes[2]).toBeGreaterThan(sizes[1]); // large file
|
|
461
|
+
});
|
|
462
|
+
test('returns empty array for empty FileList', () => {
|
|
463
|
+
const fileList = createMockFileList([]);
|
|
464
|
+
const metadata = FileTreeHelpers.extractFileListMetadata(fileList);
|
|
465
|
+
expect(metadata).toEqual([]);
|
|
466
|
+
});
|
|
467
|
+
test('preserves file order', () => {
|
|
468
|
+
const expectedOrder = ['first.txt', 'second.txt', 'third.txt'];
|
|
469
|
+
const fileList = createMockFileList(expectedOrder.map((name) => ({ name, content: 'content' })));
|
|
470
|
+
const metadata = FileTreeHelpers.extractFileListMetadata(fileList);
|
|
471
|
+
const actualOrder = metadata.map((m) => m.name);
|
|
472
|
+
expect(actualOrder).toEqual(expectedOrder);
|
|
473
|
+
});
|
|
474
|
+
});
|
|
475
|
+
describe('extractFileMetadata', () => {
|
|
476
|
+
test('extracts metadata from single file', () => {
|
|
477
|
+
const testTime = Date.now();
|
|
478
|
+
const file = createMockFile({
|
|
479
|
+
name: 'single.txt',
|
|
480
|
+
content: 'single file content',
|
|
481
|
+
type: 'text/plain',
|
|
482
|
+
lastModified: testTime
|
|
483
|
+
});
|
|
484
|
+
const metadata = FileTreeHelpers.extractFileMetadata(file);
|
|
485
|
+
expect(metadata).toEqual({
|
|
486
|
+
path: 'single.txt',
|
|
487
|
+
name: 'single.txt',
|
|
488
|
+
size: expect.any(Number),
|
|
489
|
+
type: 'text/plain',
|
|
490
|
+
lastModified: testTime
|
|
491
|
+
});
|
|
492
|
+
});
|
|
493
|
+
test('handles webkitRelativePath for single file', () => {
|
|
494
|
+
const file = createMockFile({
|
|
495
|
+
name: 'nested.txt',
|
|
496
|
+
content: 'nested content',
|
|
497
|
+
type: 'text/plain',
|
|
498
|
+
webkitRelativePath: 'folder/nested.txt'
|
|
499
|
+
});
|
|
500
|
+
const metadata = FileTreeHelpers.extractFileMetadata(file);
|
|
501
|
+
expect(metadata.path).toBe('folder/nested.txt');
|
|
502
|
+
expect(metadata.name).toBe('nested.txt');
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
describe('Error propagation', () => {
|
|
506
|
+
test('propagates errors from FileApiTreeAccessors.fromFileList', async () => {
|
|
507
|
+
const badFile = {
|
|
508
|
+
name: 'bad.txt',
|
|
509
|
+
size: 10,
|
|
510
|
+
type: 'text/plain',
|
|
511
|
+
lastModified: Date.now(),
|
|
512
|
+
text: () => Promise.reject(new Error('File system error'))
|
|
513
|
+
};
|
|
514
|
+
// Create a FileList with just the bad file
|
|
515
|
+
const dt = new DataTransfer();
|
|
516
|
+
dt.items.add(badFile);
|
|
517
|
+
const badFileList = dt.files;
|
|
518
|
+
const result = await FileTreeHelpers.fromFileList(badFileList);
|
|
519
|
+
expect(result).toFailWith(/Failed to read file/);
|
|
520
|
+
});
|
|
521
|
+
test('propagates errors from FileApiTreeAccessors.fromDirectoryUpload', async () => {
|
|
522
|
+
const badFile = {
|
|
523
|
+
name: 'bad.txt',
|
|
524
|
+
size: 10,
|
|
525
|
+
type: 'text/plain',
|
|
526
|
+
lastModified: Date.now(),
|
|
527
|
+
webkitRelativePath: 'folder/bad.txt',
|
|
528
|
+
text: () => Promise.reject(new Error('Directory read error'))
|
|
529
|
+
};
|
|
530
|
+
const dt = new DataTransfer();
|
|
531
|
+
dt.items.add(badFile);
|
|
532
|
+
const fileList = dt.files;
|
|
533
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
534
|
+
expect(result).toFailWith(/Failed to read file/);
|
|
535
|
+
});
|
|
536
|
+
test('propagates errors from FileApiTreeAccessors.create via fromFileList', async () => {
|
|
537
|
+
const badFile = {
|
|
538
|
+
name: 'bad.txt',
|
|
539
|
+
size: 10,
|
|
540
|
+
type: 'text/plain',
|
|
541
|
+
lastModified: Date.now(),
|
|
542
|
+
text: () => Promise.reject(new Error('API error'))
|
|
543
|
+
};
|
|
544
|
+
// Create FileList with the bad file
|
|
545
|
+
const dt = new DataTransfer();
|
|
546
|
+
dt.items.add(badFile);
|
|
547
|
+
const fileList = dt.files;
|
|
548
|
+
const result = await FileTreeHelpers.fromFileList(fileList);
|
|
549
|
+
expect(result).toFailWith(/Failed to read file/);
|
|
550
|
+
});
|
|
551
|
+
});
|
|
552
|
+
describe('Integration scenarios', () => {
|
|
553
|
+
test('handles realistic file upload scenario', async () => {
|
|
554
|
+
const fileList = createMockFileList([
|
|
555
|
+
{ name: 'photo1.jpg', content: 'JPEG data 1', type: 'image/jpeg' },
|
|
556
|
+
{ name: 'photo2.jpg', content: 'JPEG data 2', type: 'image/jpeg' },
|
|
557
|
+
{ name: 'metadata.txt', content: 'Photo metadata', type: 'text/plain' }
|
|
558
|
+
]);
|
|
559
|
+
const result = await FileTreeHelpers.fromFileList(fileList, { prefix: '/uploads' });
|
|
560
|
+
expect(result).toSucceed();
|
|
561
|
+
});
|
|
562
|
+
test('handles realistic project upload scenario', async () => {
|
|
563
|
+
const fileList = createMockDirectoryFileList([
|
|
564
|
+
{ path: 'my-app/package.json', content: '{"name": "my-app"}' },
|
|
565
|
+
{ path: 'my-app/src/index.js', content: 'console.log("Hello");' },
|
|
566
|
+
{ path: 'my-app/src/components/App.jsx', content: 'export default App;' },
|
|
567
|
+
{ path: 'my-app/public/index.html', content: '<html></html>' },
|
|
568
|
+
{ path: 'my-app/README.md', content: '# My App' }
|
|
569
|
+
]);
|
|
570
|
+
const result = await FileTreeHelpers.fromDirectoryUpload(fileList);
|
|
571
|
+
expect(result).toSucceedAndSatisfy((fileTree) => {
|
|
572
|
+
// Verify complete project structure
|
|
573
|
+
expect(fileTree.getDirectory('/my-app')).toSucceed();
|
|
574
|
+
expect(fileTree.getDirectory('/my-app/src')).toSucceed();
|
|
575
|
+
expect(fileTree.getDirectory('/my-app/src/components')).toSucceed();
|
|
576
|
+
expect(fileTree.getDirectory('/my-app/public')).toSucceed();
|
|
577
|
+
// Verify all files
|
|
578
|
+
expect(fileTree.getFile('/my-app/package.json')).toSucceed();
|
|
579
|
+
expect(fileTree.getFile('/my-app/src/index.js')).toSucceed();
|
|
580
|
+
expect(fileTree.getFile('/my-app/src/components/App.jsx')).toSucceed();
|
|
581
|
+
expect(fileTree.getFile('/my-app/public/index.html')).toSucceed();
|
|
582
|
+
expect(fileTree.getFile('/my-app/README.md')).toSucceed();
|
|
583
|
+
// Verify content access
|
|
584
|
+
const packageContent = fileTree.hal.getFileContents('/my-app/package.json');
|
|
585
|
+
expect(packageContent).toSucceedWith('{"name": "my-app"}');
|
|
586
|
+
});
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
});
|
|
590
|
+
//# sourceMappingURL=fileTreeHelpers.test.js.map
|