Turn audio into a shareable video. forked from nypublicradio/audiogram

settings-test.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. var tape = require("tape"),
  2. path = require("path"),
  3. validateSettings = require("../lib/settings/validate-settings.js"),
  4. validateThemes = require("../lib/settings/validate-themes.js"),
  5. load = require("../lib/settings/load.js");
  6. tape("Load test", function(test) {
  7. test.throws(function(){
  8. load("settings/blah.js");
  9. }, /No .+ file found/);
  10. test.throws(function(){
  11. load("README.md");
  12. }, /SyntaxError/);
  13. test.doesNotThrow(function(){
  14. load("settings/themes.json");
  15. });
  16. test.doesNotThrow(function(){
  17. load("settings/index.js");
  18. });
  19. test.end();
  20. });
  21. tape("Required fields", function(test) {
  22. test.throws(function(){
  23. validateSettings({});
  24. }, /settings.workingDirectory is required/);
  25. test.throws(function(){
  26. validateSettings({ workingDirectory: 1 });
  27. }, /settings.workingDirectory is required/);
  28. test.throws(function(){
  29. validateSettings({ workingDirectory: "" });
  30. }, /settings.storagePath/);
  31. test.throws(function(){
  32. validateSettings({ workingDirectory: "", storagePath: 1 });
  33. }, /settings.storagePath/);
  34. test.doesNotThrow(function(){
  35. validateSettings({ workingDirectory: path.join(__dirname), storagePath: path.join(__dirname) });
  36. }, /settings.storagePath/);
  37. test.doesNotThrow(function(){
  38. validateSettings({ workingDirectory: path.join(__dirname), s3Bucket: "bucket" });
  39. }, /settings.storagePath/);
  40. test.end();
  41. });
  42. tape("Max upload size", function(test) {
  43. test.throws(function(){
  44. validateSettings({ workingDirectory: path.join(__dirname), storagePath: path.join(__dirname), maxUploadSize: "a lot" });
  45. }, /settings.maxUploadSize/);
  46. test.end();
  47. });
  48. tape("Normalizing paths", function(test) {
  49. var relative = validateSettings({
  50. storagePath: "test/",
  51. workingDirectory: "test/"
  52. });
  53. var absolute = validateSettings({
  54. storagePath: path.join(__dirname),
  55. workingDirectory: path.join(__dirname)
  56. });
  57. var relative2 = validateSettings({
  58. storagePath: "test",
  59. workingDirectory: "test"
  60. });
  61. test.equal(path.relative(relative.storagePath, absolute.storagePath), "");
  62. test.equal(path.relative(relative.workingDirectory, absolute.workingDirectory), "");
  63. test.equal(path.relative(relative2.storagePath, absolute.storagePath), "");
  64. test.equal(path.relative(relative2.workingDirectory, absolute.workingDirectory), "");
  65. test.end();
  66. });
  67. tape("Normalize S3 bucket", function(test) {
  68. var settings = validateSettings({
  69. s3Bucket: "bucket",
  70. workingDirectory: "test/"
  71. });
  72. test.equal(settings.s3Bucket, "bucket");
  73. test.equal(settings.storagePath, "");
  74. var settings = validateSettings({
  75. s3Bucket: "s3://bucket",
  76. workingDirectory: "test/"
  77. });
  78. test.equal(settings.s3Bucket, "bucket");
  79. test.equal(settings.storagePath, "");
  80. settings = validateSettings({
  81. s3Bucket: "s3://bucket/",
  82. workingDirectory: "test/"
  83. });
  84. test.equal(settings.s3Bucket, "bucket");
  85. test.equal(settings.storagePath, "");
  86. settings = validateSettings({
  87. s3Bucket: "s3://bucket/",
  88. storagePath: "/",
  89. workingDirectory: "test/"
  90. });
  91. test.equal(settings.s3Bucket, "bucket");
  92. test.equal(settings.storagePath, "");
  93. settings = validateSettings({
  94. s3Bucket: "s3://bucket/",
  95. storagePath: "dir",
  96. workingDirectory: "test/"
  97. });
  98. test.equal(settings.s3Bucket, "bucket");
  99. test.equal(settings.storagePath, "dir/");
  100. settings = validateSettings({
  101. s3Bucket: "s3://bucket/",
  102. storagePath: "dir/",
  103. workingDirectory: "test/"
  104. });
  105. test.equal(settings.s3Bucket, "bucket");
  106. test.equal(settings.storagePath, "dir/");
  107. settings = validateSettings({
  108. s3Bucket: "s3://bucket/",
  109. storagePath: "/dir/",
  110. workingDirectory: "test/"
  111. });
  112. test.equal(settings.s3Bucket, "bucket");
  113. test.equal(settings.storagePath, "dir/");
  114. test.end();
  115. });
  116. tape("Fonts", function(test) {
  117. test.throws(function(){
  118. validateSettings({
  119. s3Bucket: "bucket",
  120. workingDirectory: "test/",
  121. fonts: 1
  122. });
  123. }, /settings.fonts/);
  124. test.throws(function(){
  125. validateSettings({
  126. s3Bucket: "bucket",
  127. workingDirectory: "test/",
  128. fonts: {}
  129. });
  130. }, /settings.fonts/);
  131. doesWarn(test, function(){
  132. validateSettings({
  133. s3Bucket: "bucket",
  134. workingDirectory: "test/",
  135. fonts: [
  136. {}
  137. ]
  138. });
  139. }, /settings.fonts.+missing/);
  140. doesWarn(test, function(){
  141. validateSettings({
  142. s3Bucket: "bucket",
  143. workingDirectory: "test/",
  144. fonts: [
  145. { family: "" }
  146. ]
  147. });
  148. }, /settings.fonts.+missing/);
  149. doesWarn(test, function(){
  150. validateSettings({
  151. s3Bucket: "bucket",
  152. workingDirectory: "test/",
  153. fonts: [
  154. { file: "" }
  155. ]
  156. });
  157. }, /settings.fonts.+missing/);
  158. doesWarn(test, function(){
  159. validateSettings({
  160. s3Bucket: "bucket",
  161. workingDirectory: "test/",
  162. fonts: [
  163. { file: "notarealfont.ttf", family: "fake" }
  164. ]
  165. });
  166. }, /Font file.+does not exist/);
  167. doesNotWarn(test, function(){
  168. validateSettings({
  169. s3Bucket: "bucket",
  170. workingDirectory: "test/",
  171. fonts: [
  172. { file: "settings/fonts/SourceSansPro-Light.ttf", family: "Source Sans Pro" },
  173. { file: path.join(__dirname, "..", "settings/fonts/SourceSansPro-Bold.ttf"), family: "Source Sans Pro", weight: "bold" }
  174. ]
  175. });
  176. });
  177. test.end();
  178. });
  179. tape("Themes", function(test) {
  180. doesWarn(test, function(){
  181. validateThemes({});
  182. }, /No themes/);
  183. doesWarn(test, function(){
  184. validateThemes([]);
  185. }, /No themes/);
  186. doesWarn(test, function(){
  187. validateThemes({ "default": {} });
  188. }, /No themes/);
  189. doesNotWarn(test, function(){
  190. validateThemes({ "default": { width: 0, height: 0, framesPerSecond: 0, samplesPerFrame: 0 }, "theme": {} });
  191. });
  192. doesNotWarn(test, function(){
  193. validateThemes({ "default": { framesPerSecond: 0, samplesPerFrame: 0 }, "theme": { width: 0, height: 0 } });
  194. });
  195. doesWarn(test, function(){
  196. validateThemes({ "default": { height: 0, framesPerSecond: 0, samplesPerFrame: 0 }, "theme": {} });
  197. }, /required property 'width'/);
  198. doesWarn(test, function(){
  199. validateThemes({ "default": { width: 0, framesPerSecond: 0, samplesPerFrame: 0 }, "theme": {} });
  200. }, /required property 'height'/);
  201. doesWarn(test, function(){
  202. validateThemes({ "default": { width: 0, height: 0, samplesPerFrame: 0 }, "theme": {} });
  203. }, /required property 'framesPerSecond'/);
  204. doesWarn(test, function(){
  205. validateThemes({ "default": { width: 0, height: 0, framesPerSecond: 0 }, "theme": {} });
  206. }, /required property 'samplesPerFrame'/);
  207. doesWarn(test, function(){
  208. validateThemes({ "default": { width: 0, height: 0, framesPerSecond: 0, samplesPerFrame: 0 }, "theme": { backgroundImage: "doesnotexist.jpg" } });
  209. }, /Background image.+does not exist/);
  210. doesNotWarn(test, function(){
  211. validateThemes({ "default": { width: 0, height: 0, framesPerSecond: 0, samplesPerFrame: 0 }, "theme": { backgroundImage: "subway.jpg" } });
  212. });
  213. doesNotWarn(test, function(){
  214. validateThemes({ "default": { width: 0, height: 0, framesPerSecond: 0, samplesPerFrame: 0 }, "theme": { backgroundImage: path.join(__dirname, "..", "settings/backgrounds/subway.jpg") } });
  215. });
  216. test.end();
  217. });
  218. function doesNotWarn(test, fn) {
  219. doesWarn(test, fn, "");
  220. }
  221. function doesWarn(test, fn, regexp) {
  222. var output = capture();
  223. fn();
  224. if (arguments.length > 2) {
  225. if (typeof regexp === "string") {
  226. test.equal(output(), regexp);
  227. } else {
  228. test.assert(regexp.test(output()));
  229. }
  230. } else {
  231. test.assert(output().length);
  232. }
  233. }
  234. function capture(){
  235. var write = process.stderr.write,
  236. out = "";
  237. process.stderr.write = function(str) {
  238. out += str;
  239. };
  240. return function(){
  241. process.stderr.write = write;
  242. return out;
  243. }
  244. }