"use strict";

const chunker = require('..');
const assert = require('assert');
const fs = require('fs');
const co = require('co');
const os = require('os');
const crypto = require('crypto');

// TODO: test 0-byte input source
// TODO: test non-0-byte but 1-chunk input source
// TODO: test with 1MB chunk size over 10.5MB file

describe('chunker', function() {
	it('chunks a stream into smaller streams', co.wrap(function*() {
		const input = crypto.pseudoRandomBytes(1024*1024);
		const chunkSize = 17*1024;

		const tempfname = `${os.tmpdir()}/chunker1mb`;
		const f = fs.openSync(tempfname, 'w');
		fs.writeSync(f, input, 0, input.length);
		fs.closeSync(f);

		const inputStream = fs.createReadStream(tempfname);
		inputStream.setMaxListeners(4096);

		let count = 0;
		for(let chunkStream of chunker.chunk(inputStream, chunkSize)) {
			//console.log({count, chunkStream});
			let writeBuf = new Buffer(0);
			const doneReading = new Promise(function(resolve, reject) {
				chunkStream.on('data', function(data) {
					writeBuf = Buffer.concat([writeBuf, data]);
				});
				chunkStream.once('end', resolve);
			});
			yield doneReading;

			if(count == Math.floor(input.length / chunkSize)) {
				assert.deepEqual(input.slice(chunkSize * count), writeBuf);
			} else {
				assert.equal(writeBuf.length, chunkSize);
				assert.deepEqual(input.slice(chunkSize * count, chunkSize * count + chunkSize), writeBuf);
			}
			count += 1;
		}
	}));
});

@@ -421,16 +421,185 @@
 on*() %7B%0A
+%09%09const params = %5B%0A%09%09%09 %7BinputSize: 1024*1024, chunkSize: 17*1024%7D%0A%09%09%09,%7BinputSize: 1024*1024*4.5, chunkSize: 1024*1024%7D%0A%09%09%5D;%0A%09%09for(let p of params) %7B%0A%09%09%09console.log(p);%0A%09
 %09%09const 
@@ -635,20 +635,23 @@
 tes(
-1024*1024
+p.inputSize
 );%0A
+%09
 %09%09co
@@ -670,18 +670,23 @@
 e = 
-17*1024
+p.chunkSize
 ;%0A%0A
+%09
 %09%09co
@@ -728,14 +728,16 @@
 nker
-1mb
+test
 %60;%0A
+%09
 %09%09co
@@ -773,16 +773,17 @@
 , 'w');%0A
+%09
 %09%09fs.wri
@@ -819,16 +819,17 @@
 gth);%0A%09%09
+%09
 fs.close
@@ -838,16 +838,17 @@
 nc(f);%0A%0A
+%09
 %09%09const 
@@ -895,16 +895,17 @@
 ame);%0A%09%09
+%09
 inputStr
@@ -932,16 +932,17 @@
 4096);%0A%0A
+%09
 %09%09let co
@@ -948,24 +948,25 @@
 ount = 0;%0A%09%09
+%09
 for(let chun
@@ -1017,16 +1017,17 @@
 ize)) %7B%0A
+%09
 %09%09%09//con
@@ -1057,24 +1057,25 @@
 tream%7D);%0A%09%09%09
+%09
 let writeBuf
@@ -1092,16 +1092,17 @@
 fer(0);%0A
+%09
 %09%09%09const
@@ -1156,24 +1156,25 @@
 ject) %7B%0A%09%09%09%09
+%09
 chunkStream.
@@ -1197,24 +1197,25 @@
 ion(data) %7B%0A
+%09
 %09%09%09%09%09writeBu
@@ -1251,16 +1251,17 @@
 data%5D);%0A
+%09
 %09%09%09%09%7D);%0A
@@ -1260,24 +1260,25 @@
 %09%09%09%09%7D);%0A%09%09%09%09
+%09
 chunkStream.
@@ -1302,20 +1302,22 @@
 ve);%0A%09%09%09
+%09
 %7D);%0A
+%09
 %09%09%09yield
@@ -1331,16 +1331,17 @@
 ading;%0A%0A
+%09
 %09%09%09if(co
@@ -1387,24 +1387,25 @@
 ize)) %7B%0A%09%09%09%09
+%09
 assert.deepE
@@ -1455,16 +1455,17 @@
 uf);%0A%09%09%09
+%09
 %7D else %7B
@@ -1465,16 +1465,17 @@
  else %7B%0A
+%09
 %09%09%09%09asse
@@ -1508,24 +1508,25 @@
 chunkSize);%0A
+%09
 %09%09%09%09assert.d
@@ -1611,18 +1611,20 @@
 uf);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09count
@@ -1630,16 +1630,21 @@
 t += 1;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D))

Add a failing test