Compare commits
13 commits
e56faa2082
...
490b8e9546
Author | SHA1 | Date | |
---|---|---|---|
490b8e9546 | |||
7574139a07 | |||
2b3e65302b | |||
6f0109ae6e | |||
ed297b2643 | |||
56c886862c | |||
7c9411b4e2 | |||
c9d44683a0 | |||
35910fe6f9 | |||
bfc4879107 | |||
f93d10f8bb | |||
4658a7bc6f | |||
44ea80a42a |
22 changed files with 631 additions and 700 deletions
|
@ -1 +0,0 @@
|
||||||
dist/
|
|
50
.eslintrc
50
.eslintrc
|
@ -1,50 +0,0 @@
|
||||||
{
|
|
||||||
"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",
|
|
||||||
"jest": "writable"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -13,7 +13,7 @@ jobs:
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: yarn install --frozen-lockfile
|
||||||
|
@ -26,7 +26,7 @@ jobs:
|
||||||
runs-on: node
|
runs-on: node
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- uses: actions/setup-node@v3
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: yarn install --frozen-lockfile
|
||||||
|
|
|
@ -17,7 +17,7 @@ jobs:
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: yarn install --frozen-lockfile
|
||||||
|
|
39
docs/cli.md
39
docs/cli.md
|
@ -4,7 +4,7 @@ Since Version 2.2, Random Bunny contains a command line interface (CLI).
|
||||||
|
|
||||||
## Downloads
|
## Downloads
|
||||||
|
|
||||||
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.
|
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:
|
We currently support:
|
||||||
- Linux (x64)
|
- Linux (x64)
|
||||||
|
@ -13,6 +13,8 @@ We currently support:
|
||||||
|
|
||||||
The git repository can also be cloned and ran via `yarn build` and `yarn start`.
|
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.
|
> **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
|
## Default Output
|
||||||
|
@ -20,7 +22,7 @@ The git repository can also be cloned and ran via `yarn build` and `yarn start`.
|
||||||
By default, the command will fetch a random image from `r/rabbits` and return it in a human-readable output.
|
By default, the command will fetch a random image from `r/rabbits` and return it in a human-readable output.
|
||||||
|
|
||||||
```
|
```
|
||||||
$ randombunny
|
$ random-bunny
|
||||||
|
|
||||||
Archived = false
|
Archived = false
|
||||||
Downvotes = 0
|
Downvotes = 0
|
||||||
|
@ -38,11 +40,11 @@ Url = https://i.redd.it/sfz0srdrimjc1.png
|
||||||
The command also includes a help option in case you are stuck.
|
The command also includes a help option in case you are stuck.
|
||||||
|
|
||||||
```
|
```
|
||||||
$ randombunny --help
|
$ random-bunny --help
|
||||||
|
|
||||||
# or
|
# or
|
||||||
|
|
||||||
$ randombunny -h
|
$ random-bunny -h
|
||||||
|
|
||||||
Usage: random-bunny [options]
|
Usage: random-bunny [options]
|
||||||
|
|
||||||
|
@ -55,6 +57,7 @@ Options:
|
||||||
-q, --query-metadata Include query metadata in result
|
-q, --query-metadata Include query metadata in result
|
||||||
-o <file> Output to file
|
-o <file> Output to file
|
||||||
--sort <sort> Sort by (choices: "hot", "new", "top", default: "hot")
|
--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
|
-h, --help display help for command
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -63,11 +66,11 @@ Options:
|
||||||
You can also convert the output into JSON, if you need to input it to another program.
|
You can also convert the output into JSON, if you need to input it to another program.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ randombunny --json
|
$ random-bunny --json
|
||||||
|
|
||||||
# or
|
# or
|
||||||
|
|
||||||
$ randonbunny -j
|
$ randon-bunny -j
|
||||||
|
|
||||||
{"Archived":false,"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"}
|
{"Archived":false,"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"}
|
||||||
```
|
```
|
||||||
|
@ -79,9 +82,9 @@ You can also choose the sorting option which reddit will use to return the avail
|
||||||
This defaults to "hot". The valid options are "hot", "new", and "top".
|
This defaults to "hot". The valid options are "hot", "new", and "top".
|
||||||
|
|
||||||
```
|
```
|
||||||
$ randombunny --sort hot
|
$ random-bunny --sort hot
|
||||||
$ randombunny --sort new
|
$ random-bunny --sort new
|
||||||
$ randomBunny --sort top
|
$ random-bunny --sort top
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,8 +95,8 @@ You can change the subreddit which the command fetches from.
|
||||||
This defaults to "rabbits"
|
This defaults to "rabbits"
|
||||||
|
|
||||||
```
|
```
|
||||||
$ randombunny --subreddit rabbits
|
$ random-bunny --subreddit rabbits
|
||||||
$ randombunny -s horses
|
$ random-bunny -s horses
|
||||||
```
|
```
|
||||||
|
|
||||||
## Output to file
|
## Output to file
|
||||||
|
@ -103,3 +106,17 @@ If you'd rather send the output to a file, you can supply the `-o` flag.
|
||||||
```
|
```
|
||||||
$ randombunny -o ~/Desktop/output.txt
|
$ 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
|
||||||
|
```
|
||||||
|
|
54
eslint.config.mjs
Normal file
54
eslint.config.mjs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
];
|
11
package.json
11
package.json
|
@ -39,19 +39,22 @@
|
||||||
"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": {
|
||||||
"@types/eslint": "^8.21.1",
|
"@eslint/eslintrc": "^3.1.0",
|
||||||
|
"@eslint/js": "^9.7.0",
|
||||||
|
"@types/eslint": "^9.6.0",
|
||||||
"@types/jest": "^29.5.8",
|
"@types/jest": "^29.5.8",
|
||||||
"@types/node": "^20.0.0",
|
"@types/node": "^20.0.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
||||||
"@typescript-eslint/parser": "^7.0.0",
|
"@typescript-eslint/parser": "^7.0.0",
|
||||||
"eslint": "^8.49.0",
|
"@yao-pkg/pkg": "^5.12.0",
|
||||||
|
"eslint": "^9.7.0",
|
||||||
"jest": "^29.7.0",
|
"jest": "^29.7.0",
|
||||||
"jest-mock-extended": "^3.0.3",
|
"jest-mock-extended": "^3.0.3",
|
||||||
"np": "^10.0.0",
|
"np": "^10.0.0",
|
||||||
"pkg": "^5.8.1",
|
|
||||||
"ts-jest": "^29.1.1",
|
"ts-jest": "^29.1.1",
|
||||||
"ts-mockito": "^2.6.1",
|
"ts-mockito": "^2.6.1",
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0",
|
||||||
|
"typescript-eslint": "^7.17.0"
|
||||||
},
|
},
|
||||||
"resolutions": {
|
"resolutions": {
|
||||||
"np/**/got": "^14.0.0",
|
"np/**/got": "^14.0.0",
|
||||||
|
|
|
@ -33,7 +33,7 @@ console.log(result);
|
||||||
|
|
||||||
### `randomBunny()`
|
### `randomBunny()`
|
||||||
|
|
||||||
Returns a `json string` for a random post. Accepts 2 arguments: `subreddit`, and `sortby` ('new', 'hot', 'top')
|
Returns a `json string` for a random post. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), and `limit` (1-100, default 100)
|
||||||
|
|
||||||
The json string which gets returned consists of:
|
The json string which gets returned consists of:
|
||||||
- archived
|
- archived
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
"baseBranches": ["develop"]
|
"baseBranches": ["develop"],
|
||||||
|
"labels": ["type/dependencies"]
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,11 +14,12 @@ program
|
||||||
.option('-j, --json', 'Output as JSON')
|
.option('-j, --json', 'Output as JSON')
|
||||||
.option('-q, --query-metadata', 'Include query metadata in result')
|
.option('-q, --query-metadata', 'Include query metadata in result')
|
||||||
.option('-o <file>', 'Output to file')
|
.option('-o <file>', 'Output to file')
|
||||||
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']));
|
.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();
|
program.parse();
|
||||||
|
|
||||||
const options: ICliOptions = program.opts();
|
const options: ICliOptions = program.opts();
|
||||||
|
|
||||||
randomBunny(options.subreddit, options.sort)
|
randomBunny(options.subreddit, options.sort, options.limit)
|
||||||
.then((response) => exit(CliHelper.Endpoint(response, options)));
|
.then((response) => exit(CliHelper.Endpoint(response, options)));
|
|
@ -3,4 +3,5 @@ export enum ErrorCode {
|
||||||
FailedToFetchReddit,
|
FailedToFetchReddit,
|
||||||
UnableToParseJSON,
|
UnableToParseJSON,
|
||||||
NoImageResultsFound,
|
NoImageResultsFound,
|
||||||
|
LimitOutOfRange,
|
||||||
}
|
}
|
|
@ -2,4 +2,5 @@ export default class ErrorMessages {
|
||||||
public static readonly FailedToFetchReddit = "Failed to fetch result from Reddit";
|
public static readonly FailedToFetchReddit = "Failed to fetch result from Reddit";
|
||||||
public static readonly UnableToParseJSON = "Unable to parse the JSON result";
|
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 NoImageResultsFound = "No image results found in response from Reddit";
|
||||||
|
public static readonly LimitOutOfRange = "Limit must be a number between 1 and 100";
|
||||||
}
|
}
|
|
@ -3,5 +3,6 @@ export default interface ICliOptions {
|
||||||
json?: boolean,
|
json?: boolean,
|
||||||
sort: "new" | "hot" | "top",
|
sort: "new" | "hot" | "top",
|
||||||
o?: string,
|
o?: string,
|
||||||
|
limit: number,
|
||||||
queryMetadata?: boolean,
|
queryMetadata?: boolean,
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
export default interface QueryResult {
|
export default interface QueryResult {
|
||||||
subreddit: string,
|
subreddit: string,
|
||||||
sortBy: string,
|
sortBy: string,
|
||||||
|
limit: number,
|
||||||
}
|
}
|
|
@ -9,7 +9,7 @@ export default class CliHelper {
|
||||||
const output = OutputHelper.GenerateOutput(response, options);
|
const output = OutputHelper.GenerateOutput(response, options);
|
||||||
|
|
||||||
if (options.o) {
|
if (options.o) {
|
||||||
writeFileSync(options.o, output);
|
writeFileSync(options.o, `${output}\n`);
|
||||||
} else {
|
} else {
|
||||||
console.log(output);
|
console.log(output);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ export default class OutputHelper {
|
||||||
if (options.queryMetadata != null) {
|
if (options.queryMetadata != null) {
|
||||||
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
|
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
|
||||||
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
|
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
|
||||||
|
outputLines.push(`Query.Limit = ${response.Query.limit}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return outputLines.join("\n");
|
return outputLines.join("\n");
|
||||||
|
|
24
src/index.ts
24
src/index.ts
|
@ -7,8 +7,23 @@ import { ErrorCode } from "./constants/ErrorCode";
|
||||||
import ErrorMessages from "./constants/ErrorMessages";
|
import ErrorMessages from "./constants/ErrorMessages";
|
||||||
import ImageHelper from "./helpers/imageHelper";
|
import ImageHelper from "./helpers/imageHelper";
|
||||||
|
|
||||||
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise<IReturnResult> {
|
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot', limit: number = 100): Promise<IReturnResult> {
|
||||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`)
|
if (limit < 1 || limit > 100) {
|
||||||
|
return {
|
||||||
|
IsSuccess: false,
|
||||||
|
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}`)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
return res;
|
return res;
|
||||||
})
|
})
|
||||||
|
@ -22,6 +37,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
||||||
Query: {
|
Query: {
|
||||||
subreddit: subreddit,
|
subreddit: subreddit,
|
||||||
sortBy: sortBy,
|
sortBy: sortBy,
|
||||||
|
limit: limit,
|
||||||
},
|
},
|
||||||
Error: {
|
Error: {
|
||||||
Code: ErrorCode.FailedToFetchReddit,
|
Code: ErrorCode.FailedToFetchReddit,
|
||||||
|
@ -38,6 +54,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
||||||
Query: {
|
Query: {
|
||||||
subreddit: subreddit,
|
subreddit: subreddit,
|
||||||
sortBy: sortBy,
|
sortBy: sortBy,
|
||||||
|
limit: limit,
|
||||||
},
|
},
|
||||||
Error: {
|
Error: {
|
||||||
Code: ErrorCode.UnableToParseJSON,
|
Code: ErrorCode.UnableToParseJSON,
|
||||||
|
@ -60,6 +77,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
||||||
Query: {
|
Query: {
|
||||||
subreddit: subreddit,
|
subreddit: subreddit,
|
||||||
sortBy: sortBy,
|
sortBy: sortBy,
|
||||||
|
limit: limit,
|
||||||
},
|
},
|
||||||
Error: {
|
Error: {
|
||||||
Code: ErrorCode.NoImageResultsFound,
|
Code: ErrorCode.NoImageResultsFound,
|
||||||
|
@ -85,6 +103,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
||||||
Query: {
|
Query: {
|
||||||
subreddit: subreddit,
|
subreddit: subreddit,
|
||||||
sortBy: sortBy,
|
sortBy: sortBy,
|
||||||
|
limit: limit,
|
||||||
},
|
},
|
||||||
Error: {
|
Error: {
|
||||||
Code: ErrorCode.NoImageResultsFound,
|
Code: ErrorCode.NoImageResultsFound,
|
||||||
|
@ -115,6 +134,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
||||||
Query: {
|
Query: {
|
||||||
subreddit: subreddit,
|
subreddit: subreddit,
|
||||||
sortBy: sortBy,
|
sortBy: sortBy,
|
||||||
|
limit: limit,
|
||||||
},
|
},
|
||||||
Result: redditResult
|
Result: redditResult
|
||||||
};
|
};
|
||||||
|
|
|
@ -25,5 +25,6 @@ Title = This is my Ms Bear!
|
||||||
Upvotes = 17
|
Upvotes = 17
|
||||||
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d
|
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d
|
||||||
Query.Subreddit = rabbits
|
Query.Subreddit = rabbits
|
||||||
Query.Sort By = hot"
|
Query.Sort By = hot
|
||||||
|
Query.Limit = 100"
|
||||||
`;
|
`;
|
||||||
|
|
|
@ -35,7 +35,7 @@ describe("Endpoint", () => {
|
||||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledWith(response, options);
|
expect(OutputHelper.GenerateOutput).toHaveBeenCalledWith(response, options);
|
||||||
|
|
||||||
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
|
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
|
||||||
expect(fs.writeFileSync).toHaveBeenCalledWith("file.txt", "test output");
|
expect(fs.writeFileSync).toHaveBeenCalledWith("file.txt", "test output\n");
|
||||||
|
|
||||||
expect(console.log).not.toHaveBeenCalled();
|
expect(console.log).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ describe("GenerateOutput", () => {
|
||||||
Query: {
|
Query: {
|
||||||
subreddit: "rabbits",
|
subreddit: "rabbits",
|
||||||
sortBy: "hot",
|
sortBy: "hot",
|
||||||
|
limit: 100,
|
||||||
},
|
},
|
||||||
Result: {
|
Result: {
|
||||||
Archived: false,
|
Archived: false,
|
||||||
|
@ -40,6 +41,7 @@ describe("GenerateOutput", () => {
|
||||||
Query: {
|
Query: {
|
||||||
subreddit: "rabbits",
|
subreddit: "rabbits",
|
||||||
sortBy: "hot",
|
sortBy: "hot",
|
||||||
|
limit: 100,
|
||||||
},
|
},
|
||||||
Result: {
|
Result: {
|
||||||
Archived: false,
|
Archived: false,
|
||||||
|
@ -72,6 +74,7 @@ describe("GenerateOutput", () => {
|
||||||
Query: {
|
Query: {
|
||||||
subreddit: "rabbits",
|
subreddit: "rabbits",
|
||||||
sortBy: "hot",
|
sortBy: "hot",
|
||||||
|
limit: 100,
|
||||||
},
|
},
|
||||||
Result: {
|
Result: {
|
||||||
Archived: false,
|
Archived: false,
|
||||||
|
|
|
@ -7,6 +7,10 @@ import fetch from "got-cjs";
|
||||||
jest.mock('got-cjs');
|
jest.mock('got-cjs');
|
||||||
const fetchMock = jest.mocked(fetch);
|
const fetchMock = jest.mocked(fetch);
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fetchMock.mockReset();
|
||||||
|
});
|
||||||
|
|
||||||
describe('randomBunny', () => {
|
describe('randomBunny', () => {
|
||||||
test('GIVEN subreddit AND sortBy is supplied, EXPECT successful result', async() => {
|
test('GIVEN subreddit AND sortBy is supplied, EXPECT successful result', async() => {
|
||||||
fetchMock.mockResolvedValue({
|
fetchMock.mockResolvedValue({
|
||||||
|
@ -122,7 +126,7 @@ describe('randomBunny', () => {
|
||||||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||||
|
|
||||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
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 () => {
|
test('GIVEN randomSelect does NOT find a valid response, EXPECT failure result', async () => {
|
||||||
|
@ -231,4 +235,106 @@ describe('randomBunny', () => {
|
||||||
expect(result.Error?.Code).toBe(ErrorCode.NoImageResultsFound);
|
expect(result.Error?.Code).toBe(ErrorCode.NoImageResultsFound);
|
||||||
expect(result.Error?.Message).toBe(ErrorMessages.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,
|
||||||
|
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,
|
||||||
|
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,
|
||||||
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in a new issue