<commit_before>'use strict';

var frauPublisher = require('../../src/publisher'),
	request = require('request'),
	eventStream = require('event-stream'),
	gulpUtil = require('gulp-util');

function makeFile(filename, content) {
	return new gulpUtil.File({
		cwd: '/',
		base: '/',
		path: '/' + filename,
		contents: new Buffer(content),
		stat: {
			size: Buffer.byteLength(content)
		}
	});
}

describe('publisher', function() {
	it('should publish new file', function(cb) {
		var publisher = createPublisher( Math.random().toString(16).slice(2) );

		var html = makeFile('test.html', '<body></body>');
		var svg = makeFile('test.svg', '<g></g>');
		var woff = makeFile('test.woff', new Buffer(10).fill(0));

		eventStream
			.readArray([html, svg, woff])
			.pipe(publisher.getStream())
			.on('end', function() {
				// gulp-s3 sends the end event before it's actually done, so we need to introduce an artificial wait.
				setTimeout(function() {
					Promise.all([
						new Promise(function(resolve, reject) {
							request.get(publisher.getLocation() + 'test.html', { gzip: true }, function(err, res, body) {
								if (err) return reject(err);
								if (res.statusCode !== 200) return reject(new Error(res.statusCode));
								if (body !== '<body></body>') return reject(new Error(body));

								if (res.headers['content-encoding'] !== 'gzip') return reject(new Error(res.headers['content-encoding']));
								if (res.headers['content-type'] !== 'text/html; charset=utf-8') return reject(new Error(res.headers['content-type']));

								resolve();
							});
						}),
						new Promise(function(resolve, reject) {
							request.get(publisher.getLocation() + 'test.svg', { gzip: true }, function(err, res, body) {
								if (err) return reject(err);
								if (res.statusCode !== 200) return reject(new Error(res.statusCode));
								if (body !== '<g></g>') return reject(new Error(body));

								if (res.headers['content-encoding'] !== 'gzip') return reject(new Error(res.headers['content-encoding']));
								if (res.headers['content-type'] !== 'image/svg+xml') return reject(new Error(res.headers['content-type']));

								resolve();
							});
						}),
						new Promise(function(resolve, reject) {
							request.get(publisher.getLocation() + 'test.woff', { gzip: true }, function(err, res, body) {
								if (err) return reject(err);
								if (res.statusCode !== 200) return reject(new Error(res.statusCode));
								if (body !== woff.contents.toString()) return reject(new Error(body));

								if (res.headers['content-encoding']) return reject(new Error(res.headers['content-encoding']));
								if (res.headers['content-type'] !== 'application/font-woff') return reject(new Error(res.headers['content-type']));
								resolve();
							});
						})
					])
					.then(function() { cb(); }, cb);
				}, 500);
			});
	});

	it('should not overwrite a file', function(cb) {
		// This test relies on files having already been published with this
		//  devTag.  We could remove this dependency by publishing a file and
		//  then trying again.  We should do this if necessary, but it didn't
		//  start that way because it seems unnecessarily wasteful.
		var publisher = createPublisher( 'overwrite-test' );

		eventStream.readArray([makeFile('test.txt', 'some data')])
			.pipe( publisher.getStream() )
			.on('error', function() {
				cb();
			}).on('end', function() {
				cb('should not have published');
			});
	});
});

function createPublisher(devTag) {
	return frauPublisher.app({
		id: 'frau-publisher-test',
		creds: {
			key: process.env.CREDS_KEY,
			secret: process.env.CREDS_SECRET
		},
		devTag: devTag
	});
}<commit_msg>test: dont believe this timeout is necessary anymore<commit_after>'use strict';

var frauPublisher = require('../../src/publisher'),
	request = require('request'),
	eventStream = require('event-stream'),
	gulpUtil = require('gulp-util');

function makeFile(filename, content) {
	return new gulpUtil.File({
		cwd: '/',
		base: '/',
		path: '/' + filename,
		contents: new Buffer(content),
		stat: {
			size: Buffer.byteLength(content)
		}
	});
}

describe('publisher', function() {
	it('should publish new file', function(cb) {
		var publisher = createPublisher( Math.random().toString(16).slice(2) );

		var html = makeFile('test.html', '<body></body>');
		var svg = makeFile('test.svg', '<g></g>');
		var woff = makeFile('test.woff', new Buffer(10).fill(0));

		eventStream
			.readArray([html, svg, woff])
			.pipe(publisher.getStream())
			.on('end', function() {
				Promise.all([
					new Promise(function(resolve, reject) {
						request.get(publisher.getLocation() + 'test.html', { gzip: true }, function(err, res, body) {
							if (err) return reject(err);
							if (res.statusCode !== 200) return reject(new Error(res.statusCode));
							if (body !== '<body></body>') return reject(new Error(body));

							if (res.headers['content-encoding'] !== 'gzip') return reject(new Error(res.headers['content-encoding']));
							if (res.headers['content-type'] !== 'text/html; charset=utf-8') return reject(new Error(res.headers['content-type']));

							resolve();
						});
					}),
					new Promise(function(resolve, reject) {
						request.get(publisher.getLocation() + 'test.svg', { gzip: true }, function(err, res, body) {
							if (err) return reject(err);
							if (res.statusCode !== 200) return reject(new Error(res.statusCode));
							if (body !== '<g></g>') return reject(new Error(body));

							if (res.headers['content-encoding'] !== 'gzip') return reject(new Error(res.headers['content-encoding']));
							if (res.headers['content-type'] !== 'image/svg+xml') return reject(new Error(res.headers['content-type']));

							resolve();
						});
					}),
					new Promise(function(resolve, reject) {
						request.get(publisher.getLocation() + 'test.woff', { gzip: true }, function(err, res, body) {
							if (err) return reject(err);
							if (res.statusCode !== 200) return reject(new Error(res.statusCode));
							if (body !== woff.contents.toString()) return reject(new Error(body));

							if (res.headers['content-encoding']) return reject(new Error(res.headers['content-encoding']));
							if (res.headers['content-type'] !== 'application/font-woff') return reject(new Error(res.headers['content-type']));
							resolve();
						});
					})
				])
				.then(function() { cb(); }, cb);
			});
	});

	it('should not overwrite a file', function(cb) {
		// This test relies on files having already been published with this
		//  devTag.  We could remove this dependency by publishing a file and
		//  then trying again.  We should do this if necessary, but it didn't
		//  start that way because it seems unnecessarily wasteful.
		var publisher = createPublisher( 'overwrite-test' );

		eventStream.readArray([makeFile('test.txt', 'some data')])
			.pipe( publisher.getStream() )
			.on('error', function() {
				cb();
			}).on('end', function() {
				cb('should not have published');
			});
	});
});

function createPublisher(devTag) {
	return frauPublisher.app({
		id: 'frau-publisher-test',
		creds: {
			key: process.env.CREDS_KEY,
			secret: process.env.CREDS_SECRET
		},
		devTag: devTag
	});
}
{'fraction_non_alphanumeric': 0.18079644789787708, 'fraction_numerical': 0.004995143610378798, 'mean_word_length': 3.5591397849462365, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '639185', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2223, 'n_words': 542}