Fix RSN format errors, add mode.

This commit is contained in:
maxswa
2025-02-24 19:41:27 -05:00
parent fd24890f67
commit 7d7c7f9b08
2 changed files with 26 additions and 6 deletions

View File

@@ -15,7 +15,10 @@ import {
InvalidFormatError, InvalidFormatError,
BH_MODES, BH_MODES,
parseJsonStats, parseJsonStats,
HiscoresResponse HiscoresResponse,
InvalidRSNError,
PlayerNotFoundError,
HiScoresError
} from '../src/index'; } from '../src/index';
const B0ATY_NAME = 'B0ATY'; const B0ATY_NAME = 'B0ATY';
@@ -24,6 +27,8 @@ const LYNX_TITAN_SPACE_NAME = 'lYnX tiTaN';
const LYNX_TITAN_UNDERSCORE_NAME = 'lYnX_tiTaN'; const LYNX_TITAN_UNDERSCORE_NAME = 'lYnX_tiTaN';
const LYNX_TITAN_HYPHEN_NAME = 'lYnX-tiTaN'; const LYNX_TITAN_HYPHEN_NAME = 'lYnX-tiTaN';
const LYNX_TITAN_FORMATTED_NAME = 'Lynx Titan'; const LYNX_TITAN_FORMATTED_NAME = 'Lynx Titan';
const NON_EXISTENT_NAME = 'nonExistent';
const ERROR_NAME = 'errorName';
const attackTopPage = readFileSync(`${__dirname}/attackTopPage.html`, 'utf8'); const attackTopPage = readFileSync(`${__dirname}/attackTopPage.html`, 'utf8');
const b0atyNamePage = readFileSync(`${__dirname}/b0atyNamePage.html`, 'utf8'); const b0atyNamePage = readFileSync(`${__dirname}/b0atyNamePage.html`, 'utf8');
@@ -57,6 +62,12 @@ jest.spyOn(axios, 'get').mockImplementation((url) => {
if (getStatsURL('main', LYNX_TITAN_FORMATTED_NAME, true) === url) { if (getStatsURL('main', LYNX_TITAN_FORMATTED_NAME, true) === url) {
return Promise.resolve({ status: 200, data: lynxTitanStats }); return Promise.resolve({ status: 200, data: lynxTitanStats });
} }
if (getPlayerTableURL('main', NON_EXISTENT_NAME) === url) {
return Promise.resolve({ data: '<html></html>' });
}
if (getPlayerTableURL('main', ERROR_NAME)) {
return Promise.reject();
}
throw new Error(`No mock response for URL: ${url}`); throw new Error(`No mock response for URL: ${url}`);
}); });
@@ -322,7 +333,15 @@ describe('Get name format', () => {
expect(data).toBe(B0ATY_FORMATTED_NAME); expect(data).toBe(B0ATY_FORMATTED_NAME);
}); });
it('throws an error for a name with invalid characters', async () => { it('throws an error for a name with invalid characters', async () => {
await expect(getRSNFormat('b&aty')).rejects.toBeTruthy(); await expect(getRSNFormat('b&aty')).rejects.toThrow(InvalidRSNError);
});
it('throws an error for a non-existent player', async () => {
await expect(getRSNFormat(NON_EXISTENT_NAME)).rejects.toThrow(
PlayerNotFoundError
);
});
it('throws an error for a hiscores issue', async () => {
await expect(getRSNFormat(ERROR_NAME)).rejects.toThrow(HiScoresError);
}); });
}); });
@@ -509,7 +528,7 @@ test('Get attack top page', async () => {
test('Get non-existent player', async () => { test('Get non-existent player', async () => {
getStats('fishy').catch((err) => { getStats('fishy').catch((err) => {
if (err.response) { if (err?.response) {
expect(err.response.status).toBe(404); expect(err.response.status).toBe(404);
} }
}); });

View File

@@ -86,11 +86,12 @@ export async function getOfficialStats(
*/ */
export async function getRSNFormat( export async function getRSNFormat(
rsn: string, rsn: string,
config?: AxiosRequestConfig config?: AxiosRequestConfig,
mode: Gamemode = 'main'
): Promise<string> { ): Promise<string> {
validateRSN(rsn); validateRSN(rsn);
const url = getPlayerTableURL('main', rsn); const url = getPlayerTableURL(mode, rsn);
try { try {
const response = await httpGet<string | Buffer | BinaryData | undefined>( const response = await httpGet<string | Buffer | BinaryData | undefined>(
url, url,
@@ -103,10 +104,10 @@ export async function getRSNFormat(
if (anchor) { if (anchor) {
return rsnFromElement(anchor); return rsnFromElement(anchor);
} }
throw new PlayerNotFoundError();
} catch { } catch {
throw new HiScoresError(); throw new HiScoresError();
} }
throw new PlayerNotFoundError();
} }
/** /**