fs-object-storage 1.0.0

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.
@@ -0,0 +1,267 @@
1
+ /**
2
+ * Unit tests for StreamConverter
3
+ */
4
+ import StreamConverter from '../../src/lib/StreamConverter.js';
5
+ import { strict as assert } from 'assert';
6
+ import { Readable, Writable } from 'stream';
7
+
8
+ describe('StreamConverter', () => {
9
+ describe('bufferToString', () => {
10
+ it('should convert buffer to string with default encoding', () => {
11
+ const buffer = Buffer.from('Hello, World!');
12
+ const result = StreamConverter.bufferToString(buffer);
13
+
14
+ assert.strictEqual(result, 'Hello, World!');
15
+ });
16
+
17
+ it('should convert buffer to string with specified encoding', () => {
18
+ const buffer = Buffer.from('Hello, World!');
19
+ const result = StreamConverter.bufferToString(buffer, 'base64');
20
+
21
+ assert.strictEqual(result, buffer.toString('base64'));
22
+ });
23
+
24
+ it('should handle empty buffer', () => {
25
+ const buffer = Buffer.alloc(0);
26
+ const result = StreamConverter.bufferToString(buffer);
27
+
28
+ assert.strictEqual(result, '');
29
+ });
30
+
31
+ it('should handle UTF-8 characters', () => {
32
+ const buffer = Buffer.from('こんにちは', 'utf8');
33
+ const result = StreamConverter.bufferToString(buffer);
34
+
35
+ assert.strictEqual(result, 'こんにちは');
36
+ });
37
+ });
38
+
39
+ describe('stringToBuffer', () => {
40
+ it('should convert string to buffer with default encoding', () => {
41
+ const str = 'Hello, World!';
42
+ const result = StreamConverter.stringToBuffer(str);
43
+
44
+ assert.ok(Buffer.isBuffer(result));
45
+ assert.strictEqual(result.toString(), str);
46
+ });
47
+
48
+ it('should convert string to buffer with specified encoding', () => {
49
+ const str = 'Hello, World!';
50
+ const result = StreamConverter.stringToBuffer(str, 'base64');
51
+
52
+ assert.ok(Buffer.isBuffer(result));
53
+ assert.strictEqual(result.toString('base64'), str);
54
+ });
55
+
56
+ it('should handle empty string', () => {
57
+ const str = '';
58
+ const result = StreamConverter.stringToBuffer(str);
59
+
60
+ assert.ok(Buffer.isBuffer(result));
61
+ assert.strictEqual(result.length, 0);
62
+ });
63
+
64
+ it('should handle UTF-8 characters', () => {
65
+ const str = 'こんにちは';
66
+ const result = StreamConverter.stringToBuffer(str);
67
+
68
+ assert.strictEqual(result.toString('utf8'), str);
69
+ });
70
+ });
71
+
72
+ describe('streamToBuffer', () => {
73
+ it('should convert readable stream to buffer', async () => {
74
+ const testData = 'Hello, World!';
75
+ const stream = new Readable({
76
+ read() {
77
+ this.push(testData);
78
+ this.push(null);
79
+ }
80
+ });
81
+
82
+ const result = await StreamConverter.streamToBuffer(stream);
83
+
84
+ assert.ok(Buffer.isBuffer(result));
85
+ assert.strictEqual(result.toString(), testData);
86
+ });
87
+
88
+ it('should handle empty stream', async () => {
89
+ const stream = new Readable({
90
+ read() {
91
+ this.push(null);
92
+ }
93
+ });
94
+
95
+ const result = await StreamConverter.streamToBuffer(stream);
96
+
97
+ assert.ok(Buffer.isBuffer(result));
98
+ assert.strictEqual(result.length, 0);
99
+ });
100
+
101
+ it('should handle chunked data', async () => {
102
+ const chunks = ['Hello, ', 'World!'];
103
+ let chunkIndex = 0;
104
+
105
+ const stream = new Readable({
106
+ read() {
107
+ if (chunkIndex < chunks.length) {
108
+ this.push(chunks[chunkIndex++]);
109
+ } else {
110
+ this.push(null);
111
+ }
112
+ }
113
+ });
114
+
115
+ const result = await StreamConverter.streamToBuffer(stream);
116
+
117
+ assert.strictEqual(result.toString(), 'Hello, World!');
118
+ });
119
+
120
+ it('should handle stream errors', async () => {
121
+ const stream = new Readable({
122
+ read() {
123
+ this.emit('error', new Error('Stream error'));
124
+ }
125
+ });
126
+
127
+ try {
128
+ await StreamConverter.streamToBuffer(stream);
129
+ assert.fail('Should have thrown an error');
130
+ } catch (error) {
131
+ assert.strictEqual(error.message, 'Stream error');
132
+ }
133
+ });
134
+ });
135
+
136
+ describe('bufferToStream', () => {
137
+ it('should convert buffer to readable stream', async () => {
138
+ const testData = 'Hello, World!';
139
+ const buffer = Buffer.from(testData);
140
+ const stream = StreamConverter.bufferToStream(buffer);
141
+
142
+ assert.ok(stream instanceof Readable);
143
+
144
+ const result = await StreamConverter.streamToBuffer(stream);
145
+ assert.strictEqual(result.toString(), testData);
146
+ });
147
+
148
+ it('should handle empty buffer', async () => {
149
+ const buffer = Buffer.alloc(0);
150
+ const stream = StreamConverter.bufferToStream(buffer);
151
+
152
+ const result = await StreamConverter.streamToBuffer(stream);
153
+ assert.strictEqual(result.length, 0);
154
+ });
155
+
156
+ it('should create stream that emits data and end events', (done) => {
157
+ const testData = 'Test data';
158
+ const buffer = Buffer.from(testData);
159
+ const stream = StreamConverter.bufferToStream(buffer);
160
+
161
+ let receivedData = '';
162
+
163
+ stream.on('data', (chunk) => {
164
+ receivedData += chunk.toString();
165
+ });
166
+
167
+ stream.on('end', () => {
168
+ assert.strictEqual(receivedData, testData);
169
+ done();
170
+ });
171
+
172
+ stream.on('error', done);
173
+ });
174
+ });
175
+
176
+ describe('createPassThroughStream', () => {
177
+ it('should create a writable stream that collects data', async () => {
178
+ const testData = 'Hello, World!';
179
+ const { stream, promise } = StreamConverter.createPassThroughStream();
180
+
181
+ assert.ok(stream instanceof Writable);
182
+
183
+ stream.write(testData);
184
+ stream.end();
185
+
186
+ const result = await promise;
187
+ assert.ok(Buffer.isBuffer(result));
188
+ assert.strictEqual(result.toString(), testData);
189
+ });
190
+
191
+ it('should handle multiple writes', async () => {
192
+ const chunks = ['Hello, ', 'World!'];
193
+ const { stream, promise } = StreamConverter.createPassThroughStream();
194
+
195
+ chunks.forEach(chunk => stream.write(chunk));
196
+ stream.end();
197
+
198
+ const result = await promise;
199
+ assert.strictEqual(result.toString(), 'Hello, World!');
200
+ });
201
+
202
+ it('should handle empty stream', async () => {
203
+ const { stream, promise } = StreamConverter.createPassThroughStream();
204
+
205
+ stream.end();
206
+
207
+ const result = await promise;
208
+ assert.strictEqual(result.length, 0);
209
+ });
210
+
211
+ it('should handle stream errors', async () => {
212
+ const { stream, promise } = StreamConverter.createPassThroughStream();
213
+
214
+ stream.emit('error', new Error('Write error'));
215
+
216
+ try {
217
+ await promise;
218
+ assert.fail('Should have thrown an error');
219
+ } catch (error) {
220
+ assert.strictEqual(error.message, 'Write error');
221
+ }
222
+ });
223
+ });
224
+
225
+ describe('normalizeData', () => {
226
+ it('should normalize string data', () => {
227
+ const str = 'Hello, World!';
228
+ const result = StreamConverter.normalizeData(str);
229
+
230
+ assert.ok(Buffer.isBuffer(result));
231
+ assert.strictEqual(result.toString(), str);
232
+ });
233
+
234
+ it('should normalize buffer data', () => {
235
+ const buffer = Buffer.from('Hello, World!');
236
+ const result = StreamConverter.normalizeData(buffer);
237
+
238
+ assert.ok(Buffer.isBuffer(result));
239
+ assert.strictEqual(result, buffer);
240
+ });
241
+
242
+ it('should normalize Uint8Array data', () => {
243
+ const uint8Array = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
244
+ const result = StreamConverter.normalizeData(uint8Array);
245
+
246
+ assert.ok(Buffer.isBuffer(result));
247
+ assert.strictEqual(result.toString(), 'Hello');
248
+ });
249
+
250
+ it('should handle empty string', () => {
251
+ const result = StreamConverter.normalizeData('');
252
+
253
+ assert.ok(Buffer.isBuffer(result));
254
+ assert.strictEqual(result.length, 0);
255
+ });
256
+
257
+ it('should handle undefined/null', () => {
258
+ const resultUndefined = StreamConverter.normalizeData(undefined);
259
+ const resultNull = StreamConverter.normalizeData(null);
260
+
261
+ assert.ok(Buffer.isBuffer(resultUndefined));
262
+ assert.ok(Buffer.isBuffer(resultNull));
263
+ assert.strictEqual(resultUndefined.length, 0);
264
+ assert.strictEqual(resultNull.length, 0);
265
+ });
266
+ });
267
+ });
package/unit-tests.js ADDED
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Simple unit tests for all components
3
+ */
4
+ import ErrorHandler from './src/lib/ErrorHandler.js';
5
+ import PathConverter from './src/lib/PathConverter.js';
6
+ import StreamConverter from './src/lib/StreamConverter.js';
7
+ import { strict as assert } from 'assert';
8
+ import { test } from 'node:test';
9
+
10
+ // ErrorHandler tests
11
+ test('ErrorHandler - convert NoSuchKey to ENOENT', () => {
12
+ const minioError = new Error('NoSuchKey: The specified key does not exist.');
13
+ minioError.code = 'NoSuchKey';
14
+ const fsError = ErrorHandler.convertError(minioError, '/test/file.txt');
15
+ assert.strictEqual(fsError.code, 'ENOENT');
16
+ assert.strictEqual(fsError.errno, -2);
17
+ assert.strictEqual(fsError.path, '/test/file.txt');
18
+ });
19
+
20
+ test('ErrorHandler - convert AccessDenied to EACCES', () => {
21
+ const minioError = new Error('AccessDenied: Access Denied.');
22
+ minioError.code = 'AccessDenied';
23
+ const fsError = ErrorHandler.convertError(minioError, '/test/file.txt');
24
+ assert.strictEqual(fsError.code, 'EACCES');
25
+ assert.strictEqual(fsError.errno, -13);
26
+ });
27
+
28
+ test('ErrorHandler - create filesystem error', () => {
29
+ const error = ErrorHandler.createError('ENOENT', '/test/file.txt', 'open');
30
+ assert.strictEqual(error.code, 'ENOENT');
31
+ assert.strictEqual(error.errno, -2);
32
+ assert.strictEqual(error.path, '/test/file.txt');
33
+ assert.strictEqual(error.syscall, 'open');
34
+ });
35
+
36
+ // PathConverter tests
37
+ // PathConverterはインスタンス生成が必要
38
+ const pc = new PathConverter({ bucket: 'bucket' });
39
+ test('PathConverter - split path correctly', () => {
40
+ const result = pc.pathToMinIO('/bucket/path/to/file.txt');
41
+ assert.strictEqual(result.bucket, 'bucket');
42
+ assert.strictEqual(result.key, 'bucket/path/to/file.txt');
43
+ });
44
+
45
+ test('PathConverter - join path correctly', () => {
46
+ const result = pc.joinPath('/bucket', 'path/to/file.txt');
47
+ assert.strictEqual(result, '/bucket/path/to/file.txt');
48
+ });
49
+
50
+ test('PathConverter - normalize path', () => {
51
+ const result = pc.joinPath('/bucket//path///to', 'file.txt');
52
+ assert.strictEqual(result, '/bucket/path/to/file.txt');
53
+ });
54
+
55
+ test('PathConverter - get parent path', () => {
56
+ const result = pc.getParentPath('/bucket/path/to/file.txt');
57
+ assert.strictEqual(result, '/bucket/path/to');
58
+ });
59
+
60
+ test('PathConverter - get basename', () => {
61
+ const result = pc.getFileName('/bucket/path/to/file.txt');
62
+ assert.strictEqual(result, 'file.txt');
63
+ });
64
+
65
+ // StreamConverter tests
66
+ // bufferToString → Buffer.toString
67
+ // stringToBuffer → Buffer.from
68
+ // normalizeData → Buffer.from or identity
69
+ function normalizeData(data) {
70
+ if (Buffer.isBuffer(data)) return data;
71
+ if (typeof data === 'string') return Buffer.from(data);
72
+ throw new Error('Unsupported type');
73
+ }
74
+ test('StreamConverter - buffer to string', () => {
75
+ const buffer = Buffer.from('Hello, World!');
76
+ const result = buffer.toString();
77
+ assert.strictEqual(result, 'Hello, World!');
78
+ });
79
+
80
+ test('StreamConverter - string to buffer', () => {
81
+ const str = 'Hello, World!';
82
+ const result = Buffer.from(str);
83
+ assert.ok(Buffer.isBuffer(result));
84
+ assert.strictEqual(result.toString(), str);
85
+ });
86
+
87
+ test('StreamConverter - normalize data (string)', () => {
88
+ const str = 'Hello, World!';
89
+ const result = normalizeData(str);
90
+ assert.ok(Buffer.isBuffer(result));
91
+ assert.strictEqual(result.toString(), str);
92
+ });
93
+
94
+ test('StreamConverter - normalize data (buffer)', () => {
95
+ const buffer = Buffer.from('Hello, World!');
96
+ const result = normalizeData(buffer);
97
+ assert.ok(Buffer.isBuffer(result));
98
+ assert.strictEqual(result, buffer);
99
+ });
100
+
101
+ console.log('✅ All unit tests completed successfully!');