TemplateTest.coffee 4.21 KB
Newer Older
1
# TODO: UPDATE!
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#
# Quick custom unit tests for the Template class.
#
# Hacked this together without a testing harness but could use chai-as-promised, sinonChai, and mocha in the future if necessary.
#

Promise = require 'bluebird'
Template = require './Template'
assert = require 'assert'

#
# Site
#
@siteTest = =>
	data =
		domain: 'site.domain'
		trackersList: "    * google.com\n    * facebook.com"
		uniqueTrackersBlocked: 2
		deltaSize: 'Some ×'
		deltaTime: 'A %' 
		sizeBefore: '3.31MB'
		sizeAfter: '1.71MB'
		timeBefore: '5.43m'
		timeAfter: '21.42s'
		requestsBefore: '242'
		requestsAfter: '87'
		lastUpdate: '21 April 2016 at 11:39'

	(Template.renderSiteTemplateWithData data).then (template) =>
		# console.log template.markdown
		assert.equal template.markdown, @siteTemplateExpectation


#
# Tracker
#

@trackerTest = =>
	data =
		domain: 'tracker.domain'
		foundOn: "  * somedoma.in\n  * someotherdoma.in"

	(Template.renderTrackerTemplateWithData data).then (template) =>
		# console.log template.markdown
		assert.equal template.markdown, @trackerTemplateExpectation

#
# Expectations
#

@siteTemplateExpectation =
"""
# site.domain

> Brief description of the site – [Source](link)

## Ethical design violations

<!-- Automatic: the trackers list is generated and updated by Better Inspector. Please do not edit manually. -->
  * Trackers
<!-- trackersList -->
    * google.com
    * facebook.com
<!-- /trackersList -->

<!-- Manual: You may manually add other ethical design violation badges here. Valid ones are:
  * Aggressive
  * Doorslam
  * Clickbait
  * Fingerprint
  * Web bug
-->

<!-- Automatic: the After Better section is generated and updated by Better Inspector. Please do not edit manually. -->
## After Better

  * Safer: <!-- uniqueTrackersBlocked -->2<!-- /uniqueTrackersBlocked -->
  * Lighter: <!-- deltaSize -->Some ×<!-- /deltaSize -->
  * Faster: <!-- deltaTime -->A %<!-- /deltaTime -->
  * Weight: <!-- sizeBefore -->3.31MB<!-- /sizeBefore --> to <!-- sizeAfter -->1.71MB<!-- /sizeAfter -->
  * Speed: <!-- timeBefore -->5.43m<!-- /timeBefore --> to <!-- timeAfter -->21.42s<!-- /timeAfter -->
  * Requests: <!-- requestsBefore -->242<!-- /requestsBefore --> to <!-- requestsAfter -->87<!-- /requestsAfter -->

Statistics automatically updated by [Better Inspector](https://source.ind.ie/better/inspector) on <!-- lastUpdate -->21 April 2016 at 11:39<!-- /lastUpdate -->. ([Source](https://archive.better.fyi/http-archive/app/blob/master/site.domain.har))

<!-- Manual: Add block rules (site fixes, first-party blocking, etc.) for this site (if any), here.

## Block rules

### A block rule

Some explanation of the block rule.

```mson
- trigger:
  - url-filter: test.test
  - load-type: third-party
- action:
  - type: block
```
-->
"""

@trackerTemplateExpectation =
"""
# tracker.domain

> Brief description of the tracker – [Source](link)

## Ethical design violations

  * Tracker

<!-- Other ethical design violation badges go here. Valid ones are:
  * Aggressive
  * Doorslam
  * Clickbait
  * Fingerprint
  * Web bug
-->

## Block rule

<!--
	The original block rule is automatically generated by Better Inspector.
	If you want to edit it, you may. The Inspector will not touch your edits
	in future passes.
-->

```mson
- trigger:
	- url-filter: tracker.domain
	- load-type: third-party
- action:
	- type: block
```

## Found on
<!-- foundOn -->

  * somedoma.in
  * someotherdoma.in

<!-- /foundOn -->

<!-- Optional:
## Notes
  * Any special notes you have may go here.
-->

## Notes
  * None yet.
"""

#
# Run tests on promises.
#
@total = 0
@fail = 0

console.log ''

@failures = []
@tests = []
for test of @
	if test.indexOf('Test') isnt -1 then @tests.push @[test]

Promise.map(@tests, ((test) => return test().reflect()), {concurrency: 1})
	.each (test) =>
		@total++
		if test.isFulfilled()
			process.stdout.write '✓'
		else
			process.stdout.write '✗'
			@failures.push {testNumber: @total, reason: test.reason()} # save the failure reason
			@fail++
	.then =>
		console.log "\n\n#{(if @fail == 0 then "All #{@total} tests passed." else "#{@fail} tests failed (out of #{@total})")}\n"
		if @fail != 0
			console.log "Errors: \n"
			for failure in @failures
				console.log " (#{failure.testNumber}) #{failure.reason}\n"
			console.log ''