<commit_before>serverRequire = require 'mundle'
phantom = require 'phantom'
http = require 'http'
https = require 'https'
fs = require 'fs'
path = require 'path'
{exec} = require 'child_process'
{loadFile} = require 'mocks'
exposedServerRequire = loadFile "#{__dirname}/node_modules/mundle/lib//exposed/server.js"
connect = require 'connect'
request = require 'superagent'

# 
# 
# 
# SERVER TESTS
# 
# 
#

serverTypes =
	HTTPServer : (callback)->
		serverRequire.listen 3000, callback
	# HTTPSServer : (callback)->
	#		serverRequire.listen 3001, key : 1111, callback
	externalHTTPServer : (callback)->
		server = (http.createServer()).listen 3002, callback
		serverRequire.listen server
	# externalHTTPSServer : (callback)->
	#		server = https.createServer key : 1111
	#		server.listen 3003, callback
	#		serverRequire.listen server
	connectServer : (callback)->
		app = connect()
		(app.use serverRequire.connect())
			.listen 3004, callback

createTestFile = (filePath, text)->
	makePath = (dirPath)->
		unless fs.existsSync (parent = path.dirname dirPath)
			makePath parent
		fs.mkdirSync dirPath unless fs.existsSync dirPath
	
	makePath path.dirname filePath
	fs.writeFileSync filePath, text

exports.testListen = 
	clientServed : (test)->
		serversTested = 0
		test.expect (Object.keys serverTypes).length
		
		phantom.create (ph)->
			for serverType, serverInit of serverTypes
				do (serverType)->
					server = serverInit ->
						{address, port} = server.address()
						ph.createPage (page)->
							page.set 'onError', (msg, trace)->
								console.log msg
								trace.forEach (item)->
									console.log "	 #{item.file}:#{item.line}"
							page.includeJs "http://#{address}:#{port}/mundlejs/require.js", ->
								# test.equal status, 'success', 'serving client code at #{address}:#{port} over #{serverType}'
								page.evaluate ->
									window.require?
								, (exists)->
									test.ok exists, 'require function is available'
									test.done() if ++serversTested is (Object.keys serverTypes).length
									server.close()
									ph.exit()
			console.warn "Test fails because phantomjs doesn't have function.prototype.bind"
			console.warn "http://code.google.com/p/phantomjs/issues/detail?id=522"

exports.testConnect = (test)->
	test.expect 3
	app = connect()
		.use(serverRequire.connect())
		.use (req, res)->
			test.ok 'One test gets through past the mundle middleware'
			res.writeHead 200, 'Content-Type' : 'text/javascript'
			res.end 'Mundle didn\'t touch it'
	.listen 3005, ->
		relTestPath = path.relative process.cwd(), "#{__dirname}/foo/bar/testConnectFile.js"
		createTestFile relTestPath, 'Hello, testConnect!!'
		request.get 'http://0.0.0.0:3005/mundlejs/b/foo/bar/testConnectFile.js', (res)->
			{results} = (JSON.parse res.text)
			test.deepEqual results, {'/b/foo/bar/testConnectFile.js' : 'Hello, testConnect!!'}, 'Connect respond with the mundle'

			request.get 'http://0.0.0.0:3005/foo/bar/testConnectFile.js', (res)->
				test.deepEqual res.text, 'Mundle didn\'t touch it', 'Connect bypass mundle middleware'
				test.done()

exports.testSetBasePath =
	relativePath : (test)->
		relTestPath = path.relative process.cwd(), "#{__dirname}/foo/bar/testFile.js"
		createTestFile relTestPath, 'Hello, mundlejs!!'

		test.expect 2
		serverRequire.setBasePath path.dirname relTestPath
		serverRequire '/b/testFile.js', {}, (errors, results)->
			test.ifError errors
			test.deepEqual results, {'/b/testFile.js' : 'Hello, mundlejs!!'}
			test.done()

	absolutePath : (test)->
		absTestPath = "#{__dirname}/foo1/bar1/testFile1.js"
		createTestFile absTestPath, 'Hello, mundlejs!!'

		test.expect 2
		serverRequire.setBasePath path.dirname absTestPath
		serverRequire '/b/testFile1.js', {}, (errors, results)->
			test.ifError errors
			test.deepEqual results, {'/b/testFile1.js' : 'Hello, mundlejs!!'}, errors
			test.done()

exports.testDependencyResolution = {}

### 
	The Mundle in "testMundle" actually refers to the packages installed by mundle install.
	Currently we're not supporting versioning. But some of the client-side work for it was done already, so the tests exercise those cases.
###
# 'nested' refers to parsed synchronous require calls in a file vs. top level, i.e. an asynchronous require
exports.testMundle =
	standardPackage : (test)->
		test.expect 2
		contents = fs.readFileSync "#{__dirname}/mundles/testMundle1/underscore.js", 'utf8'
		serverRequire '/m/testMundle1/1.4.2', {}, (errors, results)->
			test.ifError errors
			test.equal results['/m/testMundle1/1.4.2'], contents, 'Should get file contents of main js file'
			test.done()
	
	standardPackageDefaultVersion : (test)->
		test.expect 2
		contents = fs.readFileSync "#{__dirname}/mundles/testMundle1/underscore.js", 'utf8'
		serverRequire '/m/testMundle1/0.0.0', {}, (errors, results)->
			test.ifError errors
			test.equal results['/m/testMundle1/0.0.0'], contents, 'Should get file contents of main js file'
			test.done()
	
	standardPackageSubFile : (test)->
		test.expect 2
		contents = fs.readFileSync "#{__dirname}/mundles/testMundle1/underscore-min.js", 'utf8'
		serverRequire '/m/testMundle1/1.4.2/underscore-min.js', {}, (errors, results)->
			test.ifError errors
			test.equal results['/m/testMundle1/1.4.2/underscore-min.js'], contents, 'Should get file contents of main js file'
			test.done()
	
	standardPackageDefaultVersionSubFile : (test)->
		test.expect 2
		contents = fs.readFileSync "#{__dirname}/mundles/testMundle1/underscore-min.js", 'utf8'
		serverRequire '/m/testMundle1/0.0.0/underscore-min.js', {}, (errors, results)->
			test.ifError errors
			test.equal results['/m/testMundle1/0.0.0/underscore-min.js'], contents, 'Should get file contents of main js file'
			test.done()
	
	mundleNestedDependencyPathResolution : (test)->
		test.expect 6
		serverRequire '/m/testMundleNestedDependencies/0.0.0', {}, (errors, results)->
			test.ok ((Object.keys results).indexOf '/m/testMundleNestedDependencies/0.0.0/foo.js') isnt -1, 'Relative path as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/b/bar/foo.js') isnt -1, 'Absolute path as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/m/foo/0.0.0') isnt -1, 'Mundle as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/m/foo/1.1.1') isnt -1, 'Mundle with version as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/m/foo/0.0.0/bar.js') isnt -1, 'Mundle and a relative path as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/m/foo/1.1.1/bar.js') isnt -1, 'Mundle with version and a relative path as a mundle nested dependency'
			test.done()
	
	noPackageJsonWithIndexJs : (test)->
		test.expect 1
		test.done()
	
	noPackageJsonWithMainJs : (test)->
		test.expect 1
		test.done()

# 'nested' refers to parsed synchronous require calls in a file vs. a top level error in an asynchronous require
exports.testErrorFormatting =
	setUp : (callback)->
		serverRequire.setBasePath __dirname
		callback()

	noErrors : (test)-><commit_msg>Just need to support index.js as default js file if none defined in package.json<commit_after>serverRequire = require 'mundle'
phantom = require 'phantom'
http = require 'http'
https = require 'https'
fs = require 'fs'
path = require 'path'
{exec} = require 'child_process'
{loadFile} = require 'mocks'
exposedServerRequire = loadFile "#{__dirname}/node_modules/mundle/lib//exposed/server.js"
connect = require 'connect'
request = require 'superagent'

# 
# 
# 
# SERVER TESTS
# 
# 
#

serverTypes =
	HTTPServer : (callback)->
		serverRequire.listen 3000, callback
	# HTTPSServer : (callback)->
	#		serverRequire.listen 3001, key : 1111, callback
	externalHTTPServer : (callback)->
		server = (http.createServer()).listen 3002, callback
		serverRequire.listen server
	# externalHTTPSServer : (callback)->
	#		server = https.createServer key : 1111
	#		server.listen 3003, callback
	#		serverRequire.listen server
	connectServer : (callback)->
		app = connect()
		(app.use serverRequire.connect())
			.listen 3004, callback

createTestFile = (filePath, text)->
	makePath = (dirPath)->
		unless fs.existsSync (parent = path.dirname dirPath)
			makePath parent
		fs.mkdirSync dirPath unless fs.existsSync dirPath
	
	makePath path.dirname filePath
	fs.writeFileSync filePath, text

exports.testListen = 
	clientServed : (test)->
		serversTested = 0
		test.expect (Object.keys serverTypes).length
		
		phantom.create (ph)->
			for serverType, serverInit of serverTypes
				do (serverType)->
					server = serverInit ->
						{address, port} = server.address()
						ph.createPage (page)->
							page.set 'onError', (msg, trace)->
								console.log msg
								trace.forEach (item)->
									console.log "	 #{item.file}:#{item.line}"
							page.includeJs "http://#{address}:#{port}/mundlejs/require.js", ->
								# test.equal status, 'success', 'serving client code at #{address}:#{port} over #{serverType}'
								page.evaluate ->
									window.require?
								, (exists)->
									test.ok exists, 'require function is available'
									test.done() if ++serversTested is (Object.keys serverTypes).length
									server.close()
									ph.exit()
			console.warn "Test fails because phantomjs doesn't have function.prototype.bind"
			console.warn "http://code.google.com/p/phantomjs/issues/detail?id=522"

exports.testConnect = (test)->
	test.expect 3
	app = connect()
		.use(serverRequire.connect())
		.use (req, res)->
			test.ok 'One test gets through past the mundle middleware'
			res.writeHead 200, 'Content-Type' : 'text/javascript'
			res.end 'Mundle didn\'t touch it'
	.listen 3005, ->
		relTestPath = path.relative process.cwd(), "#{__dirname}/foo/bar/testConnectFile.js"
		createTestFile relTestPath, 'Hello, testConnect!!'
		request.get 'http://0.0.0.0:3005/mundlejs/b/foo/bar/testConnectFile.js', (res)->
			{results} = (JSON.parse res.text)
			test.deepEqual results, {'/b/foo/bar/testConnectFile.js' : 'Hello, testConnect!!'}, 'Connect respond with the mundle'

			request.get 'http://0.0.0.0:3005/foo/bar/testConnectFile.js', (res)->
				test.deepEqual res.text, 'Mundle didn\'t touch it', 'Connect bypass mundle middleware'
				test.done()

exports.testSetBasePath =
	relativePath : (test)->
		relTestPath = path.relative process.cwd(), "#{__dirname}/foo/bar/testFile.js"
		createTestFile relTestPath, 'Hello, mundlejs!!'

		test.expect 2
		serverRequire.setBasePath path.dirname relTestPath
		serverRequire '/b/testFile.js', {}, (errors, results)->
			test.ifError errors
			test.deepEqual results, {'/b/testFile.js' : 'Hello, mundlejs!!'}
			test.done()

	absolutePath : (test)->
		absTestPath = "#{__dirname}/foo1/bar1/testFile1.js"
		createTestFile absTestPath, 'Hello, mundlejs!!'

		test.expect 2
		serverRequire.setBasePath path.dirname absTestPath
		serverRequire '/b/testFile1.js', {}, (errors, results)->
			test.ifError errors
			test.deepEqual results, {'/b/testFile1.js' : 'Hello, mundlejs!!'}, errors
			test.done()

exports.testDependencyResolution = {}

### 
	The Mundle in "testMundle" actually refers to the packages installed by mundle install.
	Currently we're not supporting versioning. But some of the client-side work for it was done already, so the tests exercise those cases.
###
# 'nested' refers to parsed synchronous require calls in a file vs. top level, i.e. an asynchronous require
exports.testMundle =
	standardPackage : (test)->
		test.expect 2
		contents = fs.readFileSync "#{__dirname}/mundles/testMundle1/underscore.js", 'utf8'
		serverRequire '/m/testMundle1/1.4.2', {}, (errors, results)->
			test.ifError errors
			test.equal results['/m/testMundle1/1.4.2'], contents, 'Should get file contents of main js file'
			test.done()
	
	standardPackageDefaultVersion : (test)->
		test.expect 2
		contents = fs.readFileSync "#{__dirname}/mundles/testMundle1/underscore.js", 'utf8'
		serverRequire '/m/testMundle1/0.0.0', {}, (errors, results)->
			test.ifError errors
			test.equal results['/m/testMundle1/0.0.0'], contents, 'Should get file contents of main js file'
			test.done()
	
	standardPackageSubFile : (test)->
		test.expect 2
		contents = fs.readFileSync "#{__dirname}/mundles/testMundle1/underscore-min.js", 'utf8'
		serverRequire '/m/testMundle1/1.4.2/underscore-min.js', {}, (errors, results)->
			test.ifError errors
			test.equal results['/m/testMundle1/1.4.2/underscore-min.js'], contents, 'Should get file contents of main js file'
			test.done()
	
	standardPackageDefaultVersionSubFile : (test)->
		test.expect 2
		contents = fs.readFileSync "#{__dirname}/mundles/testMundle1/underscore-min.js", 'utf8'
		serverRequire '/m/testMundle1/0.0.0/underscore-min.js', {}, (errors, results)->
			test.ifError errors
			test.equal results['/m/testMundle1/0.0.0/underscore-min.js'], contents, 'Should get file contents of main js file'
			test.done()
	
	mundleNestedDependencyPathResolution : (test)->
		test.expect 6
		serverRequire '/m/testMundleNestedDependencies/0.0.0', {}, (errors, results)->
			test.ok ((Object.keys results).indexOf '/m/testMundleNestedDependencies/0.0.0/foo.js') isnt -1, 'Relative path as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/b/bar/foo.js') isnt -1, 'Absolute path as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/m/foo/0.0.0') isnt -1, 'Mundle as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/m/foo/1.1.1') isnt -1, 'Mundle with version as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/m/foo/0.0.0/bar.js') isnt -1, 'Mundle and a relative path as a mundle nested dependency'
			test.ok ((Object.keys results).indexOf '/m/foo/1.1.1/bar.js') isnt -1, 'Mundle with version and a relative path as a mundle nested dependency'
			test.done()
	
	noPackageJsonWithIndexJs : (test)->
		test.expect 1
		test.done()

# 'nested' refers to parsed synchronous require calls in a file vs. a top level error in an asynchronous require
exports.testErrorFormatting =
	setUp : (callback)->
		serverRequire.setBasePath __dirname
		callback()

	noErrors : (test)->
{'fraction_non_alphanumeric': 0.1400808567983545, 'fraction_numerical': 0.01893751329881552, 'mean_word_length': 3.8822714681440442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '3991209', 'n_tokens_mistral': 5183, 'n_tokens_neox': 4527, 'n_words': 1175}