Add sort option #123

Merged
VylpesTester merged 2 commits from feature/74-cli-sort into develop 2023-12-08 17:10:01 +00:00
6 changed files with 88 additions and 1 deletions
Showing only changes of commit 9bca084bc4 - Show all commits

View file

@ -10,6 +10,7 @@ program
.description('Get a random image url from a subreddit of your choosing') .description('Get a random image url from a subreddit of your choosing')
.version('2.2') .version('2.2')
.option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits') .option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits')
.option('-q, --query-metadata', 'Include query metadata in result')
.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']));
program.parse(); program.parse();
@ -32,6 +33,11 @@ randomBunny(options.subreddit, options.sort)
outputLines.push(`Upvotes = ${result.Ups}`); outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`); outputLines.push(`Url = ${result.Url}`);
if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
}
console.log(outputLines.join("\n")); console.log(outputLines.join("\n"));
exit(0); exit(0);
} else { } else {

View file

@ -1,4 +1,5 @@
export default interface ICliOptions { export default interface ICliOptions {
subreddit: string, subreddit: string,
sort: string, sort: string,
queryMetadata?: boolean,
} }

View file

@ -1,8 +1,10 @@
import IError from "./IError.js"; import IError from "./IError.js";
import IRedditResult from "./IRedditResult.js"; 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; Error?: IError;
} }

View file

@ -0,0 +1,4 @@
export default interface QueryResult {
subreddit: string,
sortBy: string,
}

View file

@ -26,6 +26,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
if (!result) { if (!result) {
return { return {
IsSuccess: false, IsSuccess: false,
Query: {
subreddit: subreddit,
sortBy: sortBy,
},
Error: { Error: {
Code: ErrorCode.FailedToFetchReddit, Code: ErrorCode.FailedToFetchReddit,
Message: ErrorMessages.FailedToFetchReddit, Message: ErrorMessages.FailedToFetchReddit,
@ -38,6 +42,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
if (!json) { if (!json) {
return { return {
IsSuccess: false, IsSuccess: false,
Query: {
subreddit: subreddit,
sortBy: sortBy,
},
Error: { Error: {
Code: ErrorCode.UnableToParseJSON, Code: ErrorCode.UnableToParseJSON,
Message: ErrorMessages.UnableToParseJSON, Message: ErrorMessages.UnableToParseJSON,
@ -56,6 +64,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
if (dataWithImages.length == 0) { if (dataWithImages.length == 0) {
return { return {
IsSuccess: false, IsSuccess: false,
Query: {
subreddit: subreddit,
sortBy: sortBy,
},
Error: { Error: {
Code: ErrorCode.NoImageResultsFound, Code: ErrorCode.NoImageResultsFound,
Message: ErrorMessages.NoImageResultsFound, Message: ErrorMessages.NoImageResultsFound,
@ -83,6 +95,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
return { return {
IsSuccess: true, IsSuccess: true,
Query: {
subreddit: subreddit,
sortBy: sortBy,
},
Result: redditResult Result: redditResult
}; };
} }

View file

@ -88,7 +88,65 @@ describe('subreddit', () => {
expect(subreddit).toBe('Horses'); expect(subreddit).toBe('Horses');
}, 5000); }, 5000);
}) });
describe('sort', () => {
test('GIVEN --sort is not supplird, EXPECT sort to be defaulted', async () => {
const result = await cli(['-q'], '.');
const sortBy = result.stdout.split('\n')
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Query.Sort By')!
.split(' = ')[1];
expect(sortBy).toBe('hot');
}, 5000);
test('GIVEN --sort is supplied WITH a valid input, EXPECT sort to be used', async () => {
const result = await cli(['-q', '--sort', 'new'], '.');
const sortBy = result.stdout.split('\n')
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Query.Sort By')!
.split(' = ')[1];
expect(sortBy).toBe('new');
}, 5000);
test('GIVEN --sort is supplied WITH an invalid input, EXPECT error', async () => {
const result = await cli(['-q', '--sort', 'invalid'], '.');
expect(result.code).toBe(1);
expect(result.stderr).toBe("error: option '--sort <sort>' argument 'invalid' is invalid. Allowed choices are hot, new, top.\n");
});
});
describe('query-metadata', () => {
test('GIVEN --query-metadata is not supplied, EXPECT no query metadata returned', async () => {
const result = await cli([], '.');
const query = result.stdout.split('\n')
.find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query'));
expect(query).toBeUndefined();
}, 5000);
test('GIVEN --query-metadata is not supplied, EXPECT no query metadata returned', async () => {
const result = await cli(['--query-metadata'], '.');
const query = result.stdout.split('\n')
.find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query'));
expect(query).toBeDefined();
}, 5000);
test('GIVEN -q is not supplied, EXPECT no query metadata returned', async () => {
const result = await cli(['-q'], '.');
const query = result.stdout.split('\n')
.find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query'));
expect(query).toBeDefined();
}, 5000);
});
function cli(args: string[], cwd: string): Promise<cliResult> { function cli(args: string[], cwd: string): Promise<cliResult> {
return new Promise(resolve => { return new Promise(resolve => {