var async = require('async');

module.exports = function(ctx) {
	var Lab = require('lab');

	// Test shortcuts
	var expect = Lab.expect;
	var before = Lab.before;
	var after = Lab.after;
	var describe = Lab.experiment;
	var it = Lab.test;

	describe('verify set deep population', function() {

		it('should delete any existing documents with the test name', function(done) {
			ctx.server.inject({
				method: 'delete',
				url: ctx.baseSetUrl
			}, function(res) {
				expect(res.statusCode == 200 || res.statusCode == 404).to.equal(true);
				done();
			});
		});

		it('should start with an empty pattern list on set endpoint', function(done) {
			ctx.server.inject({method: 'get', url: ctx.baseSetUrl}, function(res) {
				expect(res.statusCode).to.equal(200);
				expect(res.result.patterns.length).to.equal(0);
				done();
			});
		});

		var baseSetPatternUrl = '/api/set/' + ctx.setName + '/pattern';
		var patternNames = ['test-set-patt-1', 'test-set-patt-2', 'test-set-patt-3'];
		var patternIds = [];
		it('should allow me to create several new patterns', function(done) {
			var tasks = [];
			patternNames.forEach(function(patternName) {
				tasks.push(function(cb) {
					var patternData = {
						name: patternName,
						length: 8,
						locked: false
					};
					ctx.server.inject({method: 'post', url: baseSetPatternUrl, payload: JSON.stringify(patternData)}, function(res) {
						expect(res.statusCode).to.equal(200);
						expect(res.result.length).to.equal(8);
						patternIds.push(res.result.id);
						cb();
					});
				});
			});
			// note: if I ran this in parallel, the following test fails, as if there is some race condition here? scary
			// TODO: figure out if this is a problem
			async.series(tasks, function(err, result) {
				expect(!!err).to.equal(false);
				done();
			});
		});

		var resSet;
		it('should have added the new patterns to the set for me', function(done) {
			ctx.server.inject({method: 'get', url: ctx.baseSetUrl}, function(res) {
				expect(res.statusCode).to.equal(200);
				resSet = res.result;
				expect(resSet.patterns.length).to.equal(patternNames.length);
				done();
			});
		});

		it('should have populated result set', function(done) {
			resSet.patterns.forEach(function(pattern) {
				expect(patternNames.some(function(name) {
					return name === pattern.name;
				})).to.equal(true);
				expect(pattern.length).to.equal(8);
			});
			done();
		});

	});
};

@@ -1260,16 +1260,345 @@
 d: false
+,%0A%09%09%09%09%09%09rows: %5B%0A%09%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09%09poolEntry: 'some-pool-entry-1',%0A%09%09%09%09%09%09%09%09steps: %5B1, 0, 0, 0, 1, 0, 0, 0.5%5D%0A%09%09%09%09%09%09%09%7D,%0A%09%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09%09poolEntry: 'some-pool-entry-2',%0A%09%09%09%09%09%09%09%09steps: %5B1, 0, 0, 0, 1, 0, 0, 0.75%5D%0A%09%09%09%09%09%09%09%7D,%0A%09%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09%09poolEntry: 'some-pool-entry-3',%0A%09%09%09%09%09%09%09%09steps: %5B1, 0, 0, 0, 1, 0, 0, 0.875%5D%0A%09%09%09%09%09%09%09%7D,%0A%09%09%09%09%09%09%5D
 %0A%09%09%09%09%09%7D;
@@ -2528,16 +2528,69 @@
 done) %7B%0A
+%09%09%09console.log('resSet: ' + JSON.stringify(resSet));%0A
 %09%09%09resSe
@@ -2780,16 +2780,102 @@
 %0A%09%09%09%7D);%0A
+%09%09%09expect(resSet.patterns%5B1%5D.patternRows%5B2%5D.poolEntry).to.equal('some-pool-entry-3');%0A
 %09%09%09done(

Make deep-populate test better.