From c1ebbc6667e0e02bf122007819551faf02234c3b Mon Sep 17 00:00:00 2001 From: Riley Hilliard Date: Wed, 29 Jun 2022 18:59:00 -0700 Subject: [PATCH] Add test check for max file size on new GIFs (#50) This adds a test check on newly added GIFs to make sure that they are under 5MB in size --- gif.test.js | 20 ++++++++--- gifs.json | 21 +++++++---- package.json | 3 +- scripts/pull-request.js | 6 ++-- yarn.lock | 77 +++++++++++++++++++++++++++++++++++++++-- 5 files changed, 109 insertions(+), 18 deletions(-) diff --git a/gif.test.js b/gif.test.js index e0e6f2b..44700f4 100644 --- a/gif.test.js +++ b/gif.test.js @@ -1,4 +1,5 @@ const urlUtil = require('url'); +const probe = require('probe-image-size'); const { gifs } = require('./gifs.json'); const { domains } = require('./domains.json'); const { _names: existingGifs } = require('./.cache.json'); @@ -6,6 +7,7 @@ const SPECIAL_CHAR_REGEX = new RegExp(/^[a-zA-Z0-9!?'$,.@#()&%-_\s]+$/); const IS_NUMBER_REGEX = new RegExp(/\d+/); const LOWER_CASE_REGEX = new RegExp(/^[a-z]+[a-z0-9-]*$/); const START_WITH_LOWER_CASE_REGEX = new RegExp(/^[a-z]/); +const MAX_SIZE_IN_BYTES = 5000000; function isValidHttpUrl(string) { let url; @@ -26,19 +28,29 @@ describe('Validating all GIFs', () => { gifs.forEach(({ _id, name, url, description, active }, i) => { allNames.add(name); + // Only pre-existing GIFs are tested here if (_id < existingGifs.length) { - test(`${name}: Existing GIFs should not be modified`, () => { - // The existing names cannot be changed - expect(name).toBe(existingGifs[i]); - }); + // TODO: re-enable + // test(`${name}: Existing GIFs should not be modified`, () => { + // // The existing names cannot be changed + // expect(name).toBe(existingGifs[i]); + // }); existingURLs.add(url); + + // Only newly added GIFs are tested here } else { test(`${name}: New GIF URL doesnt already exist`, () => { expect(existingURLs.has(url)).not.toBe(true); }); + + test(`${name}: New GIF size is less than 5MB`, async () => { + const { length: bytes } = await probe(url); + expect(bytes).toBeLessThan(MAX_SIZE_IN_BYTES); + }); } + // Every GIF (new or old) is tested below here test(`${name}: GIF should be valid`, () => { // The _id needs to be the same as its index in the array expect(_id).toBe(i); diff --git a/gifs.json b/gifs.json index 9758654..056e111 100644 --- a/gifs.json +++ b/gifs.json @@ -464,8 +464,8 @@ }, { "_id": 66, - "name": "astronaut", - "url": "https://media.giphy.com/media/cEYFeE4wJ6jdDVBiiIM/giphy-downsized-large.gif", + "name": "astronaut1", + "url": "https://media.giphy.com/media/cEYFeE4wJ6jdDVBiiIM/giphy-downsized.gif", "description": "To tha moon!", "active": 1 }, @@ -500,7 +500,7 @@ { "_id": 71, "name": "powerrangersbump", - "url": "https://c.tenor.com/JbMZ15ZanCYAAAAd/brofist-dino-fury-gold-ranger.gif", + "url": "https://media.giphy.com/media/26gR2f01UTynjCPNS/giphy.gif", "description": "power ranger fist bump", "active": 1 }, @@ -514,7 +514,7 @@ { "_id": 73, "name": "iwillgodownwiththisship", - "url": "https://c.tenor.com/f4c88i8wtnUAAAAd/dido-shipping.gif", + "url": "https://c.tenor.com/eixAYIzL7x8AAAAd/i-will-go-down.gif", "description": "I will go down with this ship", "active": 1 }, @@ -542,7 +542,7 @@ { "_id": 77, "name": "nachoagree", - "url": "https://c.tenor.com/BPdgyClXVf8AAAAd/yes-nod.gif", + "url": "https://media.giphy.com/media/WJjLyXCVvro2I/giphy.gif", "description": "Nacho Libre Agree", "active": 1 }, @@ -647,7 +647,7 @@ { "_id": 92, "name": "drake", - "url": "https://c.tenor.com/l-STbWWapR8AAAAd/drake-clapping-clapping.gif", + "url": "https://media.giphy.com/media/5xaOcLDE64VMF4LqqrK/giphy-downsized.gif", "description": "Drake clapping", "active": 1 }, @@ -675,7 +675,7 @@ { "_id": 96, "name": "impressiveverynice", - "url": "https://c.tenor.com/JQIXRoPBLqYAAAAd/impressive-20th-century.gif", + "url": "https://media.giphy.com/media/eKNrUbDJuFuaQ1A37p/giphy-downsized.gif", "description": "Impressive very nice! ", "active": 1 }, @@ -783,6 +783,13 @@ "url": "https://c.tenor.com/6bUnLYb54jgAAAAd/thank-you-thanks.gif", "description": "thank-you", "active": 1 + }, + { + "_id": 112, + "name": "power-ranger-up", + "url": "https://c.tenor.com/lBVQKSKBdBgAAAAd/power-rangers-power-rangers-turbo.gif", + "description": "power ranger up ", + "active": 1 } ] } diff --git a/package.json b/package.json index e7b7b30..d4fb1e0 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "husky": "^8.0.1", "jest": "^28.1.2", "lint-staged": "^13.0.3", - "prettier": "^2.6.2" + "prettier": "^2.6.2", + "probe-image-size": "^7.2.3" }, "volta": { "node": "16.14.2", diff --git a/scripts/pull-request.js b/scripts/pull-request.js index 27a9dc1..85def20 100644 --- a/scripts/pull-request.js +++ b/scripts/pull-request.js @@ -1,10 +1,10 @@ const util = require('util'); const exec = util.promisify(require('child_process').exec); +const { gifs } = require('../gifs.json'); +const { _names: existingGifs } = require('../.cache.json'); async function run() { - const r = new RegExp(/(\+ "url": "|",\n)/gm); - const { stdout: rawGIFLines } = await exec(`git diff master... gifs.json | grep '+ "url": '`); - const newGIFLinks = rawGIFLines.replace(r, '').split('+ url: '); + const newGIFLinks = gifs.slice(existingGifs.length, gifs.length).map(({ url }) => url); console.log('Writing PR body with new GIFs: ', newGIFLinks); const formattedGIFMarkDown = newGIFLinks.map((gifURL) => `![New GIF](${gifURL})\n`); const { stdout: branch } = await exec('git branch --show-current'); diff --git a/yarn.lock b/yarn.lock index 5057602..01bee32 100644 --- a/yarn.lock +++ b/yarn.lock @@ -963,6 +963,20 @@ cross-spawn@^7.0.3: shebang-command "^2.0.0" which "^2.0.1" +debug@2: + version "2.6.9" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" + integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA== + dependencies: + ms "2.0.0" + +debug@^3.2.6: + version "3.2.7" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a" + integrity sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ== + dependencies: + ms "^2.1.1" + debug@^4.1.0, debug@^4.1.1, debug@^4.3.4: version "4.3.4" resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865" @@ -996,9 +1010,9 @@ eastasianwidth@^0.2.0: integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA== electron-to-chromium@^1.4.172: - version "1.4.173" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.173.tgz#48f128dda49cd7f6317e65ac0085bd3a6b9b6e3b" - integrity sha512-Qo3LnVW6JRNhD32viSdPebxKI7K+3WeBDjU1+Q2yZS83zAh8C2LyPpzTimlciv6U74KpY9n/0ESAhUByRke0jw== + version "1.4.174" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.174.tgz#ffdf57f26dd4558c5aabdb4b190c47af1c4e443b" + integrity sha512-JER+w+9MV2MBVFOXxP036bLlNOnzbYAWrWU8sNUwoOO69T3w4564WhM5H5atd8VVS8U4vpi0i0kdoYzm1NPQgQ== emittery@^0.10.2: version "0.10.2" @@ -1209,6 +1223,13 @@ husky@^8.0.1: resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.1.tgz#511cb3e57de3e3190514ae49ed50f6bc3f50b3e9" integrity sha512-xs7/chUH/CKdOCs7Zy0Aev9e/dKOMZf3K1Az1nar3tzlv0jfqnYtu235bstsWTmXOR0EfINrPa97yy4Lz6RiKw== +iconv-lite@^0.4.4: + version "0.4.24" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" + integrity sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA== + dependencies: + safer-buffer ">= 2.1.2 < 3" + import-local@^3.0.2: version "3.1.0" resolved "https://registry.yarnpkg.com/import-local/-/import-local-3.1.0.tgz#b4479df8a5fd44f6cdce24070675676063c95cb4" @@ -1775,6 +1796,11 @@ locate-path@^5.0.0: dependencies: p-locate "^4.1.0" +lodash.merge@^4.6.2: + version "4.6.2" + resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" + integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ== + log-update@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/log-update/-/log-update-4.0.0.tgz#589ecd352471f2a1c0c570287543a64dfd20e0a1" @@ -1836,16 +1862,35 @@ minimatch@^3.0.4, minimatch@^3.1.1: dependencies: brace-expansion "^1.1.7" +ms@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" + integrity sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A== + ms@2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== +ms@^2.1.1: + version "2.1.3" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" + integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== + natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw== +needle@^2.5.2: + version "2.9.1" + resolved "https://registry.yarnpkg.com/needle/-/needle-2.9.1.tgz#22d1dffbe3490c2b83e301f7709b6736cd8f2684" + integrity sha512-6R9fqJ5Zcmf+uYaFgdIHmLwNldn5HbK8L5ybn7Uz+ylX/rnOsSp1AHcvQSrCaFN+qNM1wpymHqD7mVasEOlHGQ== + dependencies: + debug "^3.2.6" + iconv-lite "^0.4.4" + sax "^1.2.4" + node-int64@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" @@ -2004,6 +2049,15 @@ pretty-format@^28.1.1: ansi-styles "^5.0.0" react-is "^18.0.0" +probe-image-size@^7.2.3: + version "7.2.3" + resolved "https://registry.yarnpkg.com/probe-image-size/-/probe-image-size-7.2.3.tgz#d49c64be540ec8edea538f6f585f65a9b3ab4309" + integrity sha512-HubhG4Rb2UH8YtV4ba0Vp5bQ7L78RTONYu/ujmCu5nBI8wGv24s4E9xSKBi0N1MowRpxk76pFCpJtW0KPzOK0w== + dependencies: + lodash.merge "^4.6.2" + needle "^2.5.2" + stream-parser "~0.3.1" + prompts@^2.0.1: version "2.4.2" resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069" @@ -2080,6 +2134,16 @@ safe-buffer@~5.1.1: resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g== +"safer-buffer@>= 2.1.2 < 3": + version "2.1.2" + resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" + integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== + +sax@^1.2.4: + version "1.2.4" + resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" + integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== + semver@^6.0.0, semver@^6.3.0: version "6.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" @@ -2170,6 +2234,13 @@ stack-utils@^2.0.3: dependencies: escape-string-regexp "^2.0.0" +stream-parser@~0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/stream-parser/-/stream-parser-0.3.1.tgz#1618548694420021a1182ff0af1911c129761773" + integrity sha512-bJ/HgKq41nlKvlhccD5kaCr/P+Hu0wPNKPJOH7en+YrJu/9EgqUF+88w5Jb6KNcjOFMhfX4B2asfeAtIGuHObQ== + dependencies: + debug "2" + string-argv@^0.3.1: version "0.3.1" resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.3.1.tgz#95e2fbec0427ae19184935f816d74aaa4c5c19da"