Compare commits
No commits in common. "master" and "v2.0.3" have entirely different histories.
22
.drone.yml
Normal file
22
.drone.yml
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
kind: pipeline
|
||||||
|
name: integration
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: build
|
||||||
|
image: node
|
||||||
|
commands:
|
||||||
|
- yarn install --frozen-lockfile
|
||||||
|
- name: test
|
||||||
|
image: node
|
||||||
|
commands:
|
||||||
|
- yarn test
|
||||||
|
depends_on:
|
||||||
|
- build
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
branch:
|
||||||
|
- develop
|
||||||
|
- feature/*
|
||||||
|
- hotfix/*
|
||||||
|
event:
|
||||||
|
- push
|
49
.eslintrc
Normal file
49
.eslintrc
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
{
|
||||||
|
"parserOptions": {
|
||||||
|
"ecmaVersion": 6
|
||||||
|
},
|
||||||
|
"extends": [
|
||||||
|
"eslint:recommended"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
"camelcase": "error",
|
||||||
|
"brace-style": [
|
||||||
|
"error",
|
||||||
|
"1tbs"
|
||||||
|
],
|
||||||
|
"comma-dangle": [
|
||||||
|
"error",
|
||||||
|
"never"
|
||||||
|
],
|
||||||
|
"comma-spacing": [
|
||||||
|
"error",
|
||||||
|
{
|
||||||
|
"before": false,
|
||||||
|
"after": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"comma-style": [
|
||||||
|
"error",
|
||||||
|
"last"
|
||||||
|
],
|
||||||
|
"arrow-body-style": [
|
||||||
|
"error",
|
||||||
|
"as-needed"
|
||||||
|
],
|
||||||
|
"arrow-parens": [
|
||||||
|
"error",
|
||||||
|
"as-needed"
|
||||||
|
],
|
||||||
|
"arrow-spacing": "error",
|
||||||
|
"no-var": "error",
|
||||||
|
"prefer-template": "error",
|
||||||
|
"prefer-const": "error"
|
||||||
|
},
|
||||||
|
"globals": {
|
||||||
|
"exports": "writable",
|
||||||
|
"module": "writable",
|
||||||
|
"require": "writable",
|
||||||
|
"process": "writable",
|
||||||
|
"console": "writable"
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,38 +0,0 @@
|
||||||
name: Publish to NPM
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
tags:
|
|
||||||
- '*'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
|
|
||||||
runs-on: node
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Use Node.js
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 20.x
|
|
||||||
- run: yarn install --frozen-lockfile
|
|
||||||
- run: yarn build
|
|
||||||
- run: yarn test
|
|
||||||
- run: yarn lint
|
|
||||||
|
|
||||||
publish:
|
|
||||||
needs: build
|
|
||||||
runs-on: node
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 20.x
|
|
||||||
- run: yarn install --frozen-lockfile
|
|
||||||
- run: yarn build
|
|
||||||
- run: yarn package
|
|
||||||
- run: cp -r ./bin ${{ secrets.PROD_REPO_PATH }}
|
|
||||||
- uses: https://github.com/JS-DevTools/npm-publish@v3
|
|
||||||
with:
|
|
||||||
token: ${{ secrets.NPM_TOKEN }}
|
|
|
@ -1,35 +0,0 @@
|
||||||
name: Stage
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- develop
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
|
|
||||||
runs-on: node
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Use Node.js
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 20.x
|
|
||||||
- run: yarn install --frozen-lockfile
|
|
||||||
- run: yarn build
|
|
||||||
- run: yarn test
|
|
||||||
- run: yarn lint
|
|
||||||
|
|
||||||
rsync:
|
|
||||||
needs: build
|
|
||||||
runs-on: node
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 20.x
|
|
||||||
- run: yarn install --frozen-lockfile
|
|
||||||
- run: yarn build
|
|
||||||
- run: yarn package
|
|
||||||
- run: cp -r ./bin ${{ secrets.STAGE_REPO_PATH }}
|
|
|
@ -1,25 +0,0 @@
|
||||||
name: Test
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- feature/*
|
|
||||||
- hotfix/*
|
|
||||||
- renovate/*
|
|
||||||
- dependabot/*
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
|
|
||||||
runs-on: node
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Use Node.js
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 20.x
|
|
||||||
- run: yarn install --frozen-lockfile
|
|
||||||
- run: yarn build
|
|
||||||
- run: yarn test
|
|
||||||
- run: yarn lint
|
|
|
@ -1,18 +0,0 @@
|
||||||
Epic: \
|
|
||||||
Story Points:
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
*No description*
|
|
||||||
|
|
||||||
## Acceptance Criteria
|
|
||||||
|
|
||||||
*No acceptance criteria*
|
|
||||||
|
|
||||||
## Subtasks
|
|
||||||
|
|
||||||
*No subtasks*
|
|
||||||
|
|
||||||
## Notes
|
|
||||||
|
|
||||||
*No notes*
|
|
|
@ -1,29 +0,0 @@
|
||||||
# Description
|
|
||||||
|
|
||||||
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
|
|
||||||
|
|
||||||
Fixes # (issue)
|
|
||||||
|
|
||||||
## Type of change
|
|
||||||
|
|
||||||
Please delete options that are not relevant.
|
|
||||||
|
|
||||||
- [ ] Bug fix (non-breaking change which fixes an issue)
|
|
||||||
- [ ] New feature (non-breaking change which adds functionality)
|
|
||||||
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
|
||||||
- [ ] This change requires a documentation update
|
|
||||||
|
|
||||||
# How Has This Been Tested?
|
|
||||||
|
|
||||||
Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration.
|
|
||||||
|
|
||||||
# Checklist
|
|
||||||
|
|
||||||
- [ ] My code follows the style guidelines of this project
|
|
||||||
- [ ] I have performed a self-review of my own code
|
|
||||||
- [ ] I have commented my code, particularly in hard-to-understand areas
|
|
||||||
- [ ] I have made corresponding changes to the documentation
|
|
||||||
- [ ] My changes generate no new warnings
|
|
||||||
- [ ] I have added tests that provde my fix is effective or that my feature works
|
|
||||||
- [ ] New and existing unit tests pass locally with my changes
|
|
||||||
- [ ] Any dependent changes have been merged and published in downstream modules
|
|
7
.gitignore
vendored
7
.gitignore
vendored
|
@ -1,6 +1,3 @@
|
||||||
node_modules/
|
node_modules
|
||||||
dist/
|
dist
|
||||||
bin/
|
|
||||||
coverage/
|
|
||||||
yarn-error.log
|
yarn-error.log
|
||||||
.DS_Store
|
|
|
@ -1,5 +1 @@
|
||||||
src/
|
app.ts
|
||||||
bin/
|
|
||||||
tests/
|
|
||||||
jest.config.cjs
|
|
||||||
jest.setup.js
|
|
|
@ -196,10 +196,7 @@ function ban (member) {
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
// handle error here
|
// handle error here
|
||||||
});
|
});
|
||||||
}
|
}# Contributing to Random Bunny
|
||||||
```
|
|
||||||
|
|
||||||
# Contributing to Random Bunny
|
|
||||||
|
|
||||||
First off, thanks for taking the time to contribute!
|
First off, thanks for taking the time to contribute!
|
||||||
|
|
||||||
|
|
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2020-2023 Vylpes
|
Copyright (c) 2020 Vylpes
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
9
app.ts
Normal file
9
app.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import randomBunny from "./src";
|
||||||
|
|
||||||
|
async function app() {
|
||||||
|
const result = await randomBunny('rabbits', 'hot', 100);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
app();
|
123
docs/cli.md
123
docs/cli.md
|
@ -1,123 +0,0 @@
|
||||||
# CLI
|
|
||||||
|
|
||||||
Since Version 2.2, Random Bunny contains a command line interface (CLI).
|
|
||||||
|
|
||||||
## Downloads
|
|
||||||
|
|
||||||
The project can be downloaded as a binary for your system via the [GitHub Releases](https://github.com/Vylpes/random-bunny/releases) or [Forgejo Releases](https://git.vylpes.xyz/RabbitLabs/random-bunny/releases) page.
|
|
||||||
|
|
||||||
We currently support:
|
|
||||||
- Linux (x64)
|
|
||||||
- Windows (x64)
|
|
||||||
- macOS (x64, Arm64\*)
|
|
||||||
|
|
||||||
The git repository can also be cloned and ran via `yarn build` and `yarn start`.
|
|
||||||
|
|
||||||
You can produce the binary using the `yarn package` command. This creates the binaries in the `./bin` folder.
|
|
||||||
|
|
||||||
> **NOTE:** We are aware of a bug in the macOS Arm64 builds failing to execute. For now you're still able to use the x64 builds under Rosetta fine. This will hopefully be fixed in a future release.
|
|
||||||
|
|
||||||
## Default Output
|
|
||||||
|
|
||||||
By default, the command will fetch a random image from `r/rabbits` and return it in a human-readable output.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ random-bunny
|
|
||||||
|
|
||||||
Archived = false
|
|
||||||
Author = Rabbit_Owner
|
|
||||||
Downvotes = 0
|
|
||||||
Hidden = false
|
|
||||||
Permalink = /r/Rabbits/comments/1av1rg9/cute_baby_bun/
|
|
||||||
Subreddit = Rabbits
|
|
||||||
Subreddit Subscribers = 486084
|
|
||||||
Title = Cute baby bun
|
|
||||||
Upvotes = 211
|
|
||||||
Url = https://i.redd.it/sfz0srdrimjc1.png
|
|
||||||
```
|
|
||||||
|
|
||||||
## Help
|
|
||||||
|
|
||||||
The command also includes a help option in case you are stuck.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ random-bunny --help
|
|
||||||
|
|
||||||
# or
|
|
||||||
|
|
||||||
$ random-bunny -h
|
|
||||||
|
|
||||||
Usage: random-bunny [options]
|
|
||||||
|
|
||||||
Get a random image url from a subreddit of your choosing
|
|
||||||
|
|
||||||
Options:
|
|
||||||
-V, --version output the version number
|
|
||||||
-s, --subreddit <subreddit> The subreddit to search (default: "rabbits")
|
|
||||||
-j, --json Output as JSON
|
|
||||||
-q, --query-metadata Include query metadata in result
|
|
||||||
-o <file> Output to file
|
|
||||||
--sort <sort> Sort by (choices: "hot", "new", "top", default: "hot")
|
|
||||||
--limit <limit> The amount of posts to fetch from the reddit api (default: 100)
|
|
||||||
-h, --help display help for command
|
|
||||||
```
|
|
||||||
|
|
||||||
## JSON output
|
|
||||||
|
|
||||||
You can also convert the output into JSON, if you need to input it to another program.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ random-bunny --json
|
|
||||||
|
|
||||||
# or
|
|
||||||
|
|
||||||
$ randon-bunny -j
|
|
||||||
|
|
||||||
{"Archived":false,"Author":"Rabbit_Owner","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1av1rg9/cute_baby_bun/","Subreddit":"Rabbits","SubredditSubscribers":486085,"Title":"Cute baby bun","Ups":210,"Url":"https://i.redd.it/sfz0srdrimjc1.png"}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Sort
|
|
||||||
|
|
||||||
You can also choose the sorting option which reddit will use to return the available posts to randomise from.
|
|
||||||
|
|
||||||
This defaults to "hot". The valid options are "hot", "new", and "top".
|
|
||||||
|
|
||||||
```
|
|
||||||
$ random-bunny --sort hot
|
|
||||||
$ random-bunny --sort new
|
|
||||||
$ random-bunny --sort top
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
## Subreddit
|
|
||||||
|
|
||||||
You can change the subreddit which the command fetches from.
|
|
||||||
|
|
||||||
This defaults to "rabbits"
|
|
||||||
|
|
||||||
```
|
|
||||||
$ random-bunny --subreddit rabbits
|
|
||||||
$ random-bunny -s horses
|
|
||||||
```
|
|
||||||
|
|
||||||
## Output to file
|
|
||||||
|
|
||||||
If you'd rather send the output to a file, you can supply the `-o` flag.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ randombunny -o ~/Desktop/output.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
## Reddit API Return Limits
|
|
||||||
|
|
||||||
You can also limit the amount the posts the script requests from the Reddit API
|
|
||||||
using the `--limit` option.
|
|
||||||
|
|
||||||
This defaults to 100. This accepts any number between 1 and 100.
|
|
||||||
|
|
||||||
Please note limiting the calls to less than 100 will give a higher chance of
|
|
||||||
the script not finding any valid image post to return.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ random-bunny --limit 50
|
|
||||||
```
|
|
|
@ -1,54 +0,0 @@
|
||||||
import js from "@eslint/js";
|
|
||||||
import ts from "typescript-eslint";
|
|
||||||
|
|
||||||
export default [
|
|
||||||
{
|
|
||||||
ignores: [
|
|
||||||
"**/dist/",
|
|
||||||
"eslint.config.mjs",
|
|
||||||
"jest.config.cjs",
|
|
||||||
"jest.setup.js"
|
|
||||||
],
|
|
||||||
},
|
|
||||||
js.configs.recommended,
|
|
||||||
...ts.configs.recommended,
|
|
||||||
{
|
|
||||||
languageOptions: {
|
|
||||||
globals: {
|
|
||||||
exports: "writable",
|
|
||||||
module: "writable",
|
|
||||||
require: "writable",
|
|
||||||
process: "writable",
|
|
||||||
console: "writable",
|
|
||||||
jest: "writable",
|
|
||||||
},
|
|
||||||
|
|
||||||
ecmaVersion: 6,
|
|
||||||
sourceType: "script",
|
|
||||||
},
|
|
||||||
|
|
||||||
files: [
|
|
||||||
"./src",
|
|
||||||
"./tests"
|
|
||||||
],
|
|
||||||
|
|
||||||
rules: {
|
|
||||||
camelcase: "error",
|
|
||||||
"brace-style": ["error", "1tbs"],
|
|
||||||
"comma-dangle": ["error", "never"],
|
|
||||||
|
|
||||||
"comma-spacing": ["error", {
|
|
||||||
before: false,
|
|
||||||
after: true,
|
|
||||||
}],
|
|
||||||
|
|
||||||
"comma-style": ["error", "last"],
|
|
||||||
"arrow-body-style": ["error", "as-needed"],
|
|
||||||
"arrow-parens": ["error", "as-needed"],
|
|
||||||
"arrow-spacing": "error",
|
|
||||||
"no-var": "error",
|
|
||||||
"prefer-template": "error",
|
|
||||||
"prefer-const": "error",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
];
|
|
|
@ -1,4 +0,0 @@
|
||||||
module.exports = {
|
|
||||||
preset: "ts-jest",
|
|
||||||
setupFiles: [ "./jest.setup.js" ]
|
|
||||||
}
|
|
|
@ -1,3 +0,0 @@
|
||||||
jest.setTimeout(1 * 1000); // 1 second
|
|
||||||
jest.resetModules();
|
|
||||||
jest.resetAllMocks();
|
|
21
license
Normal file
21
license
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 Vylpes
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
58
package.json
58
package.json
|
@ -1,13 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "random-bunny",
|
"name": "random-bunny",
|
||||||
"version": "2.3.0",
|
"version": "2.0.3",
|
||||||
"description": "Get a random subreddit image url",
|
"description": "Get a random subreddit image url",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"author": "Vylpes",
|
"author": "Vylpes",
|
||||||
"exports": "./dist/index.js",
|
"main": "./dist/index",
|
||||||
"typings": "./dist/index.d.ts",
|
"typings": "./dist",
|
||||||
"bin": "./dist/cli.js",
|
|
||||||
"main": "./dist/index.js",
|
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"rabbit",
|
"rabbit",
|
||||||
"bunny",
|
"bunny",
|
||||||
|
@ -18,19 +16,15 @@
|
||||||
"placeholder"
|
"placeholder"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"commander": "^12.0.0",
|
|
||||||
"glob-parent": "^6.0.0",
|
"glob-parent": "^6.0.0",
|
||||||
"got-cjs": "^12.5.4",
|
"got": "^11.8.3",
|
||||||
"htmlparser2": "^9.1.0",
|
|
||||||
"linqts": "^1.14.4"
|
"linqts": "^1.14.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "node dist/cli.js",
|
"start": "ts-node app.ts",
|
||||||
"test": "jest",
|
"test": "echo none",
|
||||||
"lint": "eslint .",
|
"lint": "eslint ."
|
||||||
"release": "np --no-publish",
|
|
||||||
"package": "pkg . --no-bytecode"
|
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny/issues",
|
"url": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny/issues",
|
||||||
|
@ -39,39 +33,9 @@
|
||||||
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
|
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
|
||||||
"funding": "https://ko-fi.com/vylpes",
|
"funding": "https://ko-fi.com/vylpes",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/eslintrc": "^3.1.0",
|
"@types/node": "^16.11.11",
|
||||||
"@eslint/js": "^9.8.0",
|
"eslint": "^7.17.0",
|
||||||
"@types/eslint": "^9.6.0",
|
"ts-node": "^10.4.0",
|
||||||
"@types/jest": "^29.5.8",
|
"typescript": "^4.5.2"
|
||||||
"@types/node": "^20.0.0",
|
|
||||||
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
||||||
"@typescript-eslint/parser": "^7.18.0",
|
|
||||||
"@yao-pkg/pkg": "^5.12.0",
|
|
||||||
"eslint": "^9.8.0",
|
|
||||||
"jest": "^29.7.0",
|
|
||||||
"jest-mock-extended": "^3.0.3",
|
|
||||||
"np": "^10.0.0",
|
|
||||||
"ts-jest": "^29.1.1",
|
|
||||||
"ts-mockito": "^2.6.1",
|
|
||||||
"typescript": "^5.0.0",
|
|
||||||
"typescript-eslint": "^7.18.0"
|
|
||||||
},
|
|
||||||
"resolutions": {
|
|
||||||
"np/**/got": "^14.0.0",
|
|
||||||
"**/semver": "^7.5.2",
|
|
||||||
"@babel/traverse": "^7.23.2"
|
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"dist"
|
|
||||||
],
|
|
||||||
"pkg": {
|
|
||||||
"scripts": "dist/**/*.js",
|
|
||||||
"targets": [
|
|
||||||
"latest-linux-x64",
|
|
||||||
"latest-macos-x64",
|
|
||||||
"latest-macos-arm64",
|
|
||||||
"latest-win-x64"
|
|
||||||
],
|
|
||||||
"outputPath": "bin"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
15
readme.md
15
readme.md
|
@ -14,10 +14,6 @@ or if you're using yarn
|
||||||
yarn add random-bunny
|
yarn add random-bunny
|
||||||
```
|
```
|
||||||
|
|
||||||
### Binary
|
|
||||||
|
|
||||||
The project can be downloaded as a binary for your system via the [GitHub Releases](https://github.com/Vylpes/random-bunny/releases) or [Gitea Releases](https://gitea.vylpes.xyz/RabbitLabs/random-bunny/releases) page.
|
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -25,7 +21,7 @@ The project can be downloaded as a binary for your system via the [GitHub Releas
|
||||||
import randomBunny from "random-bunny";
|
import randomBunny from "random-bunny";
|
||||||
|
|
||||||
// ... In an async function
|
// ... In an async function
|
||||||
const result = await randomBunny('rabbits', 'hot');
|
const result = await randomBunny('rabbits', 'hot', 100);
|
||||||
console.log(result);
|
console.log(result);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -33,11 +29,10 @@ console.log(result);
|
||||||
|
|
||||||
### `randomBunny()`
|
### `randomBunny()`
|
||||||
|
|
||||||
Returns a `json string` for a random post. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), and `limit` (1-100, default 100)
|
Returns a `json string` for a random post. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `maxTries?` (default 100)
|
||||||
|
|
||||||
The json string which gets returned consists of:
|
The json string which gets returned consists of:
|
||||||
- archived
|
- archived
|
||||||
- author
|
|
||||||
- downs
|
- downs
|
||||||
- hidden
|
- hidden
|
||||||
- permalink
|
- permalink
|
||||||
|
@ -49,11 +44,7 @@ The json string which gets returned consists of:
|
||||||
|
|
||||||
`sortBy` will default to 'hot' if not given or invalid
|
`sortBy` will default to 'hot' if not given or invalid
|
||||||
|
|
||||||
## CLI
|
`maxTries` prevents the script from rerolling too many times. The script rerolls the randomiser if the post its given doesn't contain an image. Default 100.
|
||||||
|
|
||||||
Random bunny can also be used as a CLI. This is accessible via the executable (see git releases) or via `src/cli.ts`
|
|
||||||
|
|
||||||
For more details, see the [documentation](https://docs.vylpes.xyz/books/random-bunny).
|
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
{
|
|
||||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
|
||||||
"baseBranches": ["develop"],
|
|
||||||
"labels": ["type/dependencies"]
|
|
||||||
}
|
|
25
src/cli.ts
25
src/cli.ts
|
@ -1,25 +0,0 @@
|
||||||
import { Command, Option } from "commander";
|
|
||||||
import randomBunny from "./index";
|
|
||||||
import ICliOptions from "./contracts/ICliOptions";
|
|
||||||
import { exit } from "process";
|
|
||||||
import CliHelper from "./helpers/cliHelper";
|
|
||||||
|
|
||||||
const program = new Command();
|
|
||||||
|
|
||||||
program
|
|
||||||
.name('random-bunny')
|
|
||||||
.description('Get a random image url from a subreddit of your choosing')
|
|
||||||
.version('2.3')
|
|
||||||
.option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits')
|
|
||||||
.option('-j, --json', 'Output as JSON')
|
|
||||||
.option('-q, --query-metadata', 'Include query metadata in result')
|
|
||||||
.option('-o <file>', 'Output to file')
|
|
||||||
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']))
|
|
||||||
.addOption(new Option('--limit <limit>', 'The amount of posts to fetch from the reddit api').default(100));
|
|
||||||
|
|
||||||
program.parse();
|
|
||||||
|
|
||||||
const options: ICliOptions = program.opts();
|
|
||||||
|
|
||||||
randomBunny(options.subreddit, options.sort, options.limit)
|
|
||||||
.then((response) => exit(CliHelper.Endpoint(response, options)));
|
|
|
@ -1,7 +0,0 @@
|
||||||
export enum ErrorCode {
|
|
||||||
Unknown,
|
|
||||||
FailedToFetchReddit,
|
|
||||||
UnableToParseJSON,
|
|
||||||
NoImageResultsFound,
|
|
||||||
LimitOutOfRange,
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
export default class ErrorMessages {
|
|
||||||
public static readonly FailedToFetchReddit = "Failed to fetch result from Reddit";
|
|
||||||
public static readonly UnableToParseJSON = "Unable to parse the JSON result";
|
|
||||||
public static readonly NoImageResultsFound = "No image results found in response from Reddit";
|
|
||||||
public static readonly LimitOutOfRange = "Limit must be a number between 1 and 100";
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
export default interface ICliOptions {
|
|
||||||
subreddit: string,
|
|
||||||
json?: boolean,
|
|
||||||
sort: "new" | "hot" | "top",
|
|
||||||
o?: string,
|
|
||||||
limit: number,
|
|
||||||
queryMetadata?: boolean,
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
import { ErrorCode } from "../constants/ErrorCode";
|
|
||||||
|
|
||||||
export default interface IError {
|
|
||||||
Code: ErrorCode;
|
|
||||||
Message: string;
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
export default interface IFetchResult {
|
export default interface IFetchResult {
|
||||||
data: {
|
data: {
|
||||||
archived: boolean,
|
archived: boolean,
|
||||||
author: string,
|
|
||||||
downs: number,
|
downs: number,
|
||||||
hidden: boolean,
|
hidden: boolean,
|
||||||
permalink: string,
|
permalink: string,
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
export default interface IRedditResult {
|
export default interface IRedditResult {
|
||||||
Archived: boolean,
|
Archived: boolean,
|
||||||
Author: string,
|
|
||||||
Downs: number,
|
Downs: number,
|
||||||
Hidden: boolean,
|
Hidden: boolean,
|
||||||
Permalink: string,
|
Permalink: string,
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
import IError from "./IError.js";
|
import IRedditResult from "./IRedditResult";
|
||||||
import IRedditResult from "./IRedditResult.js";
|
|
||||||
import QueryResult from "./QueryResult.js";
|
|
||||||
|
|
||||||
export default interface IReturnResult {
|
export default interface IReturnResult {
|
||||||
IsSuccess: boolean;
|
IsSuccess: boolean;
|
||||||
Query: QueryResult;
|
|
||||||
Result?: IRedditResult;
|
Result?: IRedditResult;
|
||||||
Error?: IError;
|
|
||||||
}
|
}
|
|
@ -1,5 +0,0 @@
|
||||||
export default interface QueryResult {
|
|
||||||
subreddit: string,
|
|
||||||
sortBy: string,
|
|
||||||
limit: number,
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
import { writeFileSync } from "fs";
|
|
||||||
import ICliOptions from "../contracts/ICliOptions";
|
|
||||||
import IReturnResult from "../contracts/IReturnResult";
|
|
||||||
import OutputHelper from "./outputHelper";
|
|
||||||
|
|
||||||
export default class CliHelper {
|
|
||||||
public static Endpoint(response: IReturnResult, options: ICliOptions): number {
|
|
||||||
if (response.IsSuccess) {
|
|
||||||
const output = OutputHelper.GenerateOutput(response, options);
|
|
||||||
|
|
||||||
if (options.o) {
|
|
||||||
writeFileSync(options.o, `${output}\n`);
|
|
||||||
} else {
|
|
||||||
console.log(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
const error = response.Error!;
|
|
||||||
|
|
||||||
console.error(error.Message, error.Code);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
import fetch from "got-cjs";
|
|
||||||
import * as htmlparser from "htmlparser2";
|
|
||||||
|
|
||||||
export default class ImageHelper {
|
|
||||||
public static async FetchImageFromRedditGallery(url: string): Promise<string | undefined> {
|
|
||||||
const fetched = await fetch(url);
|
|
||||||
|
|
||||||
if (!fetched || fetched.errored || fetched.statusCode != 200) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const dom = htmlparser.parseDocument(fetched.body);
|
|
||||||
const img = htmlparser.DomUtils.findOne((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children, true);
|
|
||||||
|
|
||||||
const imgSrc = img?.attributes.find(x => x.name == "src")?.value;
|
|
||||||
|
|
||||||
return imgSrc;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
import ICliOptions from "../contracts/ICliOptions";
|
|
||||||
import IReturnResult from "../contracts/IReturnResult";
|
|
||||||
|
|
||||||
export default class OutputHelper {
|
|
||||||
public static GenerateOutput(response: IReturnResult, options: ICliOptions): string {
|
|
||||||
const result = response.Result!;
|
|
||||||
|
|
||||||
const outputLines: string[] = [];
|
|
||||||
|
|
||||||
if (options.json) {
|
|
||||||
return JSON.stringify(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
outputLines.push(`Archived = ${result.Archived}`);
|
|
||||||
outputLines.push(`Author = ${result.Author}`);
|
|
||||||
outputLines.push(`Downvotes = ${result.Downs}`);
|
|
||||||
outputLines.push(`Hidden = ${result.Hidden}`);
|
|
||||||
outputLines.push(`Permalink = ${result.Permalink}`);
|
|
||||||
outputLines.push(`Subreddit = ${result.Subreddit}`);
|
|
||||||
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
|
|
||||||
outputLines.push(`Title = ${result.Title}`);
|
|
||||||
outputLines.push(`Upvotes = ${result.Ups}`);
|
|
||||||
outputLines.push(`Url = ${result.Url}`);
|
|
||||||
|
|
||||||
if (options.queryMetadata != null) {
|
|
||||||
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
|
|
||||||
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
|
|
||||||
outputLines.push(`Query.Limit = ${response.Query.limit}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return outputLines.join("\n");
|
|
||||||
}
|
|
||||||
}
|
|
150
src/index.ts
150
src/index.ts
|
@ -1,48 +1,23 @@
|
||||||
import IReturnResult from "./contracts/IReturnResult";
|
import IReturnResult from "./contracts/IReturnResult";
|
||||||
import IRedditResult from "./contracts/IRedditResult";
|
import IRedditResult from "./contracts/IRedditResult";
|
||||||
import fetch from "got-cjs";
|
import fetch from "got";
|
||||||
import { List } from 'linqts';
|
import { List } from 'linqts';
|
||||||
import IFetchResult from "./contracts/IFetchResult";
|
import IFetchResult from "./contracts/IFetchResult";
|
||||||
import { ErrorCode } from "./constants/ErrorCode";
|
|
||||||
import ErrorMessages from "./constants/ErrorMessages";
|
|
||||||
import ImageHelper from "./helpers/imageHelper";
|
|
||||||
|
|
||||||
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot', limit: number = 100): Promise<IReturnResult> {
|
const sortable = [
|
||||||
if (limit < 1 || limit > 100) {
|
'new',
|
||||||
return {
|
'hot',
|
||||||
IsSuccess: false,
|
'top'
|
||||||
Query: {
|
];
|
||||||
subreddit: subreddit,
|
|
||||||
sortBy: sortBy,
|
|
||||||
limit: limit,
|
|
||||||
},
|
|
||||||
Error: {
|
|
||||||
Code: ErrorCode.LimitOutOfRange,
|
|
||||||
Message: ErrorMessages.LimitOutOfRange,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=${limit}`)
|
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IReturnResult> {
|
||||||
.then((res) => {
|
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
||||||
return res;
|
|
||||||
})
|
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
||||||
.catch(() => {
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return {
|
return {
|
||||||
IsSuccess: false,
|
IsSuccess: false
|
||||||
Query: {
|
|
||||||
subreddit: subreddit,
|
|
||||||
sortBy: sortBy,
|
|
||||||
limit: limit,
|
|
||||||
},
|
|
||||||
Error: {
|
|
||||||
Code: ErrorCode.FailedToFetchReddit,
|
|
||||||
Message: ErrorMessages.FailedToFetchReddit,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,93 +25,44 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
||||||
|
|
||||||
if (!json) {
|
if (!json) {
|
||||||
return {
|
return {
|
||||||
IsSuccess: false,
|
IsSuccess: false
|
||||||
Query: {
|
|
||||||
subreddit: subreddit,
|
|
||||||
sortBy: sortBy,
|
|
||||||
limit: limit,
|
|
||||||
},
|
|
||||||
Error: {
|
|
||||||
Code: ErrorCode.UnableToParseJSON,
|
|
||||||
Message: ErrorMessages.UnableToParseJSON,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const data: IFetchResult[] = json.data.children;
|
const data: IFetchResult[] = json.data.children;
|
||||||
|
|
||||||
const dataWithImages = new List<IFetchResult>(data)
|
const dataWithImages = new List<IFetchResult>(data)
|
||||||
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png') || x!.data.url.includes("/gallery/"))
|
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
let random = 0;
|
for (let i = 0; i < maxTries; i++) {
|
||||||
|
const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
|
||||||
|
|
||||||
if (dataWithImages.length == 0) {
|
const randomSelect = dataWithImages[random];
|
||||||
return {
|
|
||||||
IsSuccess: false,
|
if (!randomSelect) continue;
|
||||||
Query: {
|
|
||||||
subreddit: subreddit,
|
const randomData = randomSelect.data;
|
||||||
sortBy: sortBy,
|
|
||||||
limit: limit,
|
const redditResult: IRedditResult = {
|
||||||
},
|
Archived: randomData['archived'],
|
||||||
Error: {
|
Downs: randomData['downs'],
|
||||||
Code: ErrorCode.NoImageResultsFound,
|
Hidden: randomData['hidden'],
|
||||||
Message: ErrorMessages.NoImageResultsFound,
|
Permalink: randomData['permalink'],
|
||||||
},
|
Subreddit: randomData['subreddit'],
|
||||||
|
SubredditSubscribers: randomData['subreddit_subscribers'],
|
||||||
|
Title: randomData['title'],
|
||||||
|
Ups: randomData['ups'],
|
||||||
|
Url: randomData['url']
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
IsSuccess: true,
|
||||||
|
Result: redditResult
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
random = Math.floor((Math.random() * (dataWithImages.length - 1)) + 0); // Between 0 and (size - 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const randomSelect = dataWithImages[random];
|
|
||||||
|
|
||||||
const randomData = randomSelect.data;
|
|
||||||
|
|
||||||
let url: string;
|
|
||||||
|
|
||||||
if (randomData.url.includes("/gallery")) {
|
|
||||||
const galleryImage = await ImageHelper.FetchImageFromRedditGallery(randomData.url);
|
|
||||||
|
|
||||||
if (!galleryImage) {
|
|
||||||
return {
|
|
||||||
IsSuccess: false,
|
|
||||||
Query: {
|
|
||||||
subreddit: subreddit,
|
|
||||||
sortBy: sortBy,
|
|
||||||
limit: limit,
|
|
||||||
},
|
|
||||||
Error: {
|
|
||||||
Code: ErrorCode.NoImageResultsFound,
|
|
||||||
Message: ErrorMessages.NoImageResultsFound,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
url = galleryImage;
|
|
||||||
} else {
|
|
||||||
url = randomData.url;
|
|
||||||
}
|
|
||||||
|
|
||||||
const redditResult: IRedditResult = {
|
|
||||||
Author: randomData['author'],
|
|
||||||
Archived: randomData['archived'],
|
|
||||||
Downs: randomData['downs'],
|
|
||||||
Hidden: randomData['hidden'],
|
|
||||||
Permalink: randomData['permalink'],
|
|
||||||
Subreddit: randomData['subreddit'],
|
|
||||||
SubredditSubscribers: randomData['subreddit_subscribers'],
|
|
||||||
Title: randomData['title'],
|
|
||||||
Ups: randomData['ups'],
|
|
||||||
Url: url,
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
IsSuccess: true,
|
IsSuccess: false
|
||||||
Query: {
|
}
|
||||||
subreddit: subreddit,
|
|
||||||
sortBy: sortBy,
|
|
||||||
limit: limit,
|
|
||||||
},
|
|
||||||
Result: redditResult
|
|
||||||
};
|
|
||||||
}
|
}
|
|
@ -1,32 +0,0 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
||||||
|
|
||||||
exports[`GenerateOutput EXPECT standout output to be returned 1`] = `
|
|
||||||
"Archived = false
|
|
||||||
Author = author
|
|
||||||
Downvotes = 0
|
|
||||||
Hidden = false
|
|
||||||
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
|
|
||||||
Subreddit = Rabbits
|
|
||||||
Subreddit Subscribers = 654751
|
|
||||||
Title = This is my Ms Bear!
|
|
||||||
Upvotes = 17
|
|
||||||
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
|
|
||||||
`;
|
|
||||||
|
|
||||||
exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"}"`;
|
|
||||||
|
|
||||||
exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = `
|
|
||||||
"Archived = false
|
|
||||||
Author = author
|
|
||||||
Downvotes = 0
|
|
||||||
Hidden = false
|
|
||||||
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
|
|
||||||
Subreddit = Rabbits
|
|
||||||
Subreddit Subscribers = 654751
|
|
||||||
Title = This is my Ms Bear!
|
|
||||||
Upvotes = 17
|
|
||||||
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d
|
|
||||||
Query.Subreddit = rabbits
|
|
||||||
Query.Sort By = hot
|
|
||||||
Query.Limit = 100"
|
|
||||||
`;
|
|
|
@ -1,118 +0,0 @@
|
||||||
import fs from "fs";
|
|
||||||
import CliHelper from "../../src/helpers/cliHelper";
|
|
||||||
import ICliOptions from "../../src/contracts/ICliOptions";
|
|
||||||
import IReturnResult from "../../src/contracts/IReturnResult";
|
|
||||||
import OutputHelper from "../../src/helpers/outputHelper";
|
|
||||||
import { ErrorCode } from "../../src/constants/ErrorCode";
|
|
||||||
|
|
||||||
describe("Endpoint", () => {
|
|
||||||
describe("GIVEN response is successful", () => {
|
|
||||||
test("GIVEN options.o is defined, EXPECT output written to file", () => {
|
|
||||||
// Arrange
|
|
||||||
const response = {
|
|
||||||
IsSuccess: true,
|
|
||||||
} as IReturnResult;
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
o: "file.txt",
|
|
||||||
} as ICliOptions;
|
|
||||||
|
|
||||||
OutputHelper.GenerateOutput = jest.fn().mockReturnValue("test output");
|
|
||||||
|
|
||||||
fs.writeFileSync = jest.fn();
|
|
||||||
|
|
||||||
console.log = jest.fn();
|
|
||||||
|
|
||||||
console.error = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const result = CliHelper.Endpoint(response, options);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(result).toBe(0);
|
|
||||||
|
|
||||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledTimes(1);
|
|
||||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledWith(response, options);
|
|
||||||
|
|
||||||
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
|
|
||||||
expect(fs.writeFileSync).toHaveBeenCalledWith("file.txt", "test output\n");
|
|
||||||
|
|
||||||
expect(console.log).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(console.error).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN options.o is undefined, EXPECT output logged to console", () => {
|
|
||||||
// Arrange
|
|
||||||
const response = {
|
|
||||||
IsSuccess: true,
|
|
||||||
} as IReturnResult;
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
o: undefined,
|
|
||||||
} as ICliOptions;
|
|
||||||
|
|
||||||
OutputHelper.GenerateOutput = jest.fn().mockReturnValue("test output");
|
|
||||||
|
|
||||||
fs.writeFileSync = jest.fn();
|
|
||||||
|
|
||||||
console.log = jest.fn();
|
|
||||||
|
|
||||||
console.error = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const result = CliHelper.Endpoint(response, options);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(result).toBe(0);
|
|
||||||
|
|
||||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledTimes(1);
|
|
||||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledWith(response, options);
|
|
||||||
|
|
||||||
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(console.log).toHaveBeenCalledTimes(1);
|
|
||||||
expect(console.log).toHaveBeenCalledWith("test output");
|
|
||||||
|
|
||||||
expect(console.error).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN response is failure, EXPECT error logged to console", () => {
|
|
||||||
// Arrange
|
|
||||||
const response = {
|
|
||||||
IsSuccess: false,
|
|
||||||
Error: {
|
|
||||||
Message: "error message",
|
|
||||||
Code: ErrorCode.FailedToFetchReddit,
|
|
||||||
},
|
|
||||||
} as IReturnResult;
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
o: "file.txt",
|
|
||||||
} as ICliOptions;
|
|
||||||
|
|
||||||
OutputHelper.GenerateOutput = jest.fn().mockReturnValue("test output");
|
|
||||||
|
|
||||||
fs.writeFileSync = jest.fn();
|
|
||||||
|
|
||||||
console.log = jest.fn();
|
|
||||||
|
|
||||||
console.error = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const result = CliHelper.Endpoint(response, options);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(result).toBe(1);
|
|
||||||
|
|
||||||
expect(OutputHelper.GenerateOutput).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(console.log).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(console.error).toHaveBeenCalledTimes(1);
|
|
||||||
expect(console.error).toHaveBeenCalledWith("error message", ErrorCode.FailedToFetchReddit);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,90 +0,0 @@
|
||||||
import ImageHelper from "../../src/helpers/imageHelper";
|
|
||||||
import fetch from "got-cjs";
|
|
||||||
|
|
||||||
jest.mock('got-cjs');
|
|
||||||
const fetchMock = jest.mocked(fetch);
|
|
||||||
|
|
||||||
describe("FetchImageFromRedditGallery", () => {
|
|
||||||
test("EXPECT image url to be returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
|
||||||
errored: undefined,
|
|
||||||
statusCode: 200,
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith("https://redd.it/gallery/image");
|
|
||||||
|
|
||||||
expect(result).toBe("https://preview.redd.it/image.png");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN fetch is unable to return data, EXPECT undefined returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue(null);
|
|
||||||
|
|
||||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
||||||
|
|
||||||
expect(result).toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN fetch is an error, EXPECT undefined returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
|
||||||
errored: "Error",
|
|
||||||
statusCode: 200,
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
||||||
|
|
||||||
expect(result).toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN fetch is not status code of 200, EXPECT undefined returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
|
||||||
errored: undefined,
|
|
||||||
statusCode: 500,
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
||||||
|
|
||||||
expect(result).toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN image tag is not found, EXPECT undefined returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: "<html><body></body></html>",
|
|
||||||
errored: undefined,
|
|
||||||
statusCode: 200,
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
||||||
|
|
||||||
expect(result).toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN image source attribute is not found, EXPECT undefined returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: "<html><body><img /></body></html>",
|
|
||||||
errored: undefined,
|
|
||||||
statusCode: 200,
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
||||||
|
|
||||||
expect(result).toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN image source attribute is not found that is a preview.redd.it url, EXPECT undefined returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: "<html><body><img src='main.png' /></body></html>",
|
|
||||||
errored: undefined,
|
|
||||||
statusCode: 200,
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
||||||
|
|
||||||
expect(result).toBeUndefined();
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,105 +0,0 @@
|
||||||
import ICliOptions from "../../src/contracts/ICliOptions";
|
|
||||||
import IReturnResult from "../../src/contracts/IReturnResult";
|
|
||||||
import OutputHelper from "../../src/helpers/outputHelper";
|
|
||||||
|
|
||||||
describe("GenerateOutput", () => {
|
|
||||||
test("EXPECT standout output to be returned", () => {
|
|
||||||
// Arrange
|
|
||||||
const response = {
|
|
||||||
IsSuccess: true,
|
|
||||||
Query: {
|
|
||||||
subreddit: "rabbits",
|
|
||||||
sortBy: "hot",
|
|
||||||
limit: 100,
|
|
||||||
},
|
|
||||||
Result: {
|
|
||||||
Archived: false,
|
|
||||||
Author: 'author',
|
|
||||||
Downs: 0,
|
|
||||||
Hidden: false,
|
|
||||||
Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/",
|
|
||||||
Subreddit: "Rabbits",
|
|
||||||
SubredditSubscribers: 654751,
|
|
||||||
Title: "This is my Ms Bear!",
|
|
||||||
Ups: 17,
|
|
||||||
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
|
|
||||||
},
|
|
||||||
} as IReturnResult;
|
|
||||||
|
|
||||||
const options = {} as ICliOptions;
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const result = OutputHelper.GenerateOutput(response, options);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(result).toMatchSnapshot();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN options.json is true, EXPECT output to be returned as JSON", () => {
|
|
||||||
// Arrange
|
|
||||||
const response = {
|
|
||||||
IsSuccess: true,
|
|
||||||
Query: {
|
|
||||||
subreddit: "rabbits",
|
|
||||||
sortBy: "hot",
|
|
||||||
limit: 100,
|
|
||||||
},
|
|
||||||
Result: {
|
|
||||||
Archived: false,
|
|
||||||
Author: 'author',
|
|
||||||
Downs: 0,
|
|
||||||
Hidden: false,
|
|
||||||
Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/",
|
|
||||||
Subreddit: "Rabbits",
|
|
||||||
SubredditSubscribers: 654751,
|
|
||||||
Title: "This is my Ms Bear!",
|
|
||||||
Ups: 17,
|
|
||||||
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
|
|
||||||
},
|
|
||||||
} as IReturnResult;
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
json: true,
|
|
||||||
} as ICliOptions;
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const result = OutputHelper.GenerateOutput(response, options);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(result).toMatchSnapshot();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added", () => {
|
|
||||||
// Arrange
|
|
||||||
const response = {
|
|
||||||
IsSuccess: true,
|
|
||||||
Query: {
|
|
||||||
subreddit: "rabbits",
|
|
||||||
sortBy: "hot",
|
|
||||||
limit: 100,
|
|
||||||
},
|
|
||||||
Result: {
|
|
||||||
Archived: false,
|
|
||||||
Author: 'author',
|
|
||||||
Downs: 0,
|
|
||||||
Hidden: false,
|
|
||||||
Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/",
|
|
||||||
Subreddit: "Rabbits",
|
|
||||||
SubredditSubscribers: 654751,
|
|
||||||
Title: "This is my Ms Bear!",
|
|
||||||
Ups: 17,
|
|
||||||
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
|
|
||||||
},
|
|
||||||
} as IReturnResult;
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
queryMetadata: true,
|
|
||||||
} as ICliOptions;
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const result = OutputHelper.GenerateOutput(response, options);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(result).toMatchSnapshot();
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,348 +0,0 @@
|
||||||
import { ErrorCode } from "../src/constants/ErrorCode";
|
|
||||||
import ErrorMessages from "../src/constants/ErrorMessages";
|
|
||||||
import ImageHelper from "../src/helpers/imageHelper";
|
|
||||||
import randomBunny from "../src/index";
|
|
||||||
import fetch from "got-cjs";
|
|
||||||
|
|
||||||
jest.mock('got-cjs');
|
|
||||||
const fetchMock = jest.mocked(fetch);
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
fetchMock.mockReset();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('randomBunny', () => {
|
|
||||||
test('GIVEN subreddit AND sortBy is supplied, EXPECT successful result', async() => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
archived: false,
|
|
||||||
author: 'author',
|
|
||||||
downs: 0,
|
|
||||||
hidden: false,
|
|
||||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
|
||||||
subreddit: 'Rabbits',
|
|
||||||
subreddit_subscribers: 298713,
|
|
||||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
|
||||||
ups: 1208,
|
|
||||||
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new');
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeTruthy();
|
|
||||||
expect(result.Result).toBeDefined();
|
|
||||||
expect(result.Error).toBeUndefined();
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN sortBy is NOT supplied, expect it to default to hot', async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
archived: false,
|
|
||||||
author: 'author',
|
|
||||||
downs: 0,
|
|
||||||
hidden: false,
|
|
||||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
|
||||||
subreddit: 'Rabbits',
|
|
||||||
subreddit_subscribers: 298713,
|
|
||||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
|
||||||
ups: 1208,
|
|
||||||
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits');
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeTruthy();
|
|
||||||
expect(result.Result).toBeDefined();
|
|
||||||
expect(result.Error).toBeUndefined();
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN the fetch fails, EXPECT failure result', async () => {
|
|
||||||
fetchMock.mockRejectedValue('Test Reason')
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new');
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeFalsy();
|
|
||||||
expect(result.Result).toBeUndefined();
|
|
||||||
expect(result.Error).toBeDefined();
|
|
||||||
|
|
||||||
expect(result.Error!.Code).toBe(ErrorCode.FailedToFetchReddit);
|
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.FailedToFetchReddit);
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN the result is NOT valid JSON, EXPECT failure result', async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify(null),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new');
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeFalsy();
|
|
||||||
expect(result.Result).toBeUndefined();
|
|
||||||
expect(result.Error).toBeDefined();
|
|
||||||
|
|
||||||
expect(result.Error!.Code).toBe(ErrorCode.UnableToParseJSON);
|
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.UnableToParseJSON);
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN randomSelect does NOT find a response, EXPECT failure result', async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new');
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeFalsy();
|
|
||||||
expect(result.Result).toBeUndefined();
|
|
||||||
expect(result.Error).toBeDefined();
|
|
||||||
|
|
||||||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN randomSelect does NOT find a valid response, EXPECT failure result', async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
archived: false,
|
|
||||||
author: 'author',
|
|
||||||
downs: 0,
|
|
||||||
hidden: false,
|
|
||||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
|
||||||
subreddit: 'Rabbits',
|
|
||||||
subreddit_subscribers: 298713,
|
|
||||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
|
||||||
ups: 1208,
|
|
||||||
url: 'https://i.redd.it/cr8xudsnkgua1.webp',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new');
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeFalsy();
|
|
||||||
expect(result.Result).toBeUndefined();
|
|
||||||
expect(result.Error).toBeDefined();
|
|
||||||
|
|
||||||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN data fetched is a gallery AND an image is returned from the helper, EXPECT this to be used", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
archived: false,
|
|
||||||
author: 'author',
|
|
||||||
downs: 0,
|
|
||||||
hidden: false,
|
|
||||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
|
||||||
subreddit: 'Rabbits',
|
|
||||||
subreddit_subscribers: 298713,
|
|
||||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
|
||||||
ups: 1208,
|
|
||||||
url: 'https://i.redd.it/gallery/cr8xudsnkgua1',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
ImageHelper.FetchImageFromRedditGallery = jest.fn().mockResolvedValue("https://i.redd.it/cr8xudsnkgua1.jpg")
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new');
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeTruthy();
|
|
||||||
expect(result.Result).toBeDefined();
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
|
||||||
|
|
||||||
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledTimes(1);
|
|
||||||
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledWith("https://i.redd.it/gallery/cr8xudsnkgua1");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN data fetched is a gallery AND an image is not returned from the helper, EXPECT error", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
archived: false,
|
|
||||||
author: 'author',
|
|
||||||
downs: 0,
|
|
||||||
hidden: false,
|
|
||||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
|
||||||
subreddit: 'Rabbits',
|
|
||||||
subreddit_subscribers: 298713,
|
|
||||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
|
||||||
ups: 1208,
|
|
||||||
url: 'https://i.redd.it/gallery/cr8xudsnkgua1',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
ImageHelper.FetchImageFromRedditGallery = jest.fn().mockResolvedValue(undefined)
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new');
|
|
||||||
|
|
||||||
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBe(false);
|
|
||||||
expect(result.Error).toBeDefined();
|
|
||||||
expect(result.Error?.Code).toBe(ErrorCode.NoImageResultsFound);
|
|
||||||
expect(result.Error?.Message).toBe(ErrorMessages.NoImageResultsFound);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN limit is supplied, EXPECT limit sent to the API", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
archived: false,
|
|
||||||
author: 'author',
|
|
||||||
downs: 0,
|
|
||||||
hidden: false,
|
|
||||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
|
||||||
subreddit: 'Rabbits',
|
|
||||||
subreddit_subscribers: 298713,
|
|
||||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
|
||||||
ups: 1208,
|
|
||||||
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new', 50);
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeTruthy();
|
|
||||||
expect(result.Result).toBeDefined();
|
|
||||||
expect(result.Error).toBeUndefined();
|
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=50');
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN limit is less than 1, EXPECT error to be returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
archived: false,
|
|
||||||
author: 'author',
|
|
||||||
downs: 0,
|
|
||||||
hidden: false,
|
|
||||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
|
||||||
subreddit: 'Rabbits',
|
|
||||||
subreddit_subscribers: 298713,
|
|
||||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
|
||||||
ups: 1208,
|
|
||||||
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new', 0);
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeFalsy();
|
|
||||||
expect(result.Result).toBeUndefined();
|
|
||||||
expect(result.Error).toBeDefined();
|
|
||||||
|
|
||||||
expect(result.Error!.Code).toBe(ErrorCode.LimitOutOfRange);
|
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.LimitOutOfRange);
|
|
||||||
|
|
||||||
expect(fetchMock).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN limit is greater than 100, EXPECT error to be returned", async () => {
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
body: JSON.stringify({
|
|
||||||
data: {
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
archived: false,
|
|
||||||
author: 'author',
|
|
||||||
downs: 0,
|
|
||||||
hidden: false,
|
|
||||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
|
||||||
subreddit: 'Rabbits',
|
|
||||||
subreddit_subscribers: 298713,
|
|
||||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
|
||||||
ups: 1208,
|
|
||||||
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await randomBunny('rabbits', 'new', 101);
|
|
||||||
|
|
||||||
expect(result.IsSuccess).toBeFalsy();
|
|
||||||
expect(result.Result).toBeUndefined();
|
|
||||||
expect(result.Error).toBeDefined();
|
|
||||||
|
|
||||||
expect(result.Error!.Code).toBe(ErrorCode.LimitOutOfRange);
|
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.LimitOutOfRange);
|
|
||||||
|
|
||||||
expect(fetchMock).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -5,7 +5,7 @@
|
||||||
/* Basic Options */
|
/* Basic Options */
|
||||||
// "incremental": true, /* Enable incremental compilation */
|
// "incremental": true, /* Enable incremental compilation */
|
||||||
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
|
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
|
||||||
"module": "Node16", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
||||||
// "lib": [], /* Specify library files to be included in the compilation. */
|
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
// "checkJs": true, /* Report errors in .js files. */
|
// "checkJs": true, /* Report errors in .js files. */
|
||||||
|
@ -44,7 +44,7 @@
|
||||||
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
|
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
|
||||||
|
|
||||||
/* Module Resolution Options */
|
/* Module Resolution Options */
|
||||||
"moduleResolution": "node16", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||||
|
@ -73,11 +73,7 @@
|
||||||
"./src",
|
"./src",
|
||||||
],
|
],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"./tests",
|
"./tests"
|
||||||
"jest.config.cjs",
|
]
|
||||||
"jest.setup.js"
|
|
||||||
],
|
|
||||||
"ts-node": {
|
|
||||||
"esm": true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue