Skip to content

Commit

Permalink
Merge pull request #201 from neoPix/test/stamps
Browse files Browse the repository at this point in the history
[test] changing the tests for the new stamp generation system
  • Loading branch information
Hacksore authored Feb 7, 2022
2 parents e19f322 + 3df0b93 commit 06b94eb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion __tests__/controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('EuropeanController', () => {
controller.session.accessToken = 'MockToken';

(got as any).mockReturnValueOnce({
body: ['stamp1', 'stamp2'],
body: {stamps: ['stamp1', 'stamp2'], generated: new Date().toISOString(), steps: 20000},
statusCode: 200,
});

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bluelinky",
"version": "7.6.2",
"version": "7.6.3",
"description": "An unofficial nodejs API wrapper for Hyundai bluelink",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
19 changes: 13 additions & 6 deletions src/constants/europe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export type EULanguages = 'cs'|'da'|'nl'|'en'|'fi'|'fr'|'de'|'it'|'pl'|'hu'|'no'
export const EU_LANGUAGES: EULanguages[] = ['cs', 'da', 'nl', 'en', 'fi', 'fr', 'de', 'it', 'pl', 'hu', 'no', 'sk', 'es', 'sv'];
export const DEFAULT_LANGUAGE: EULanguages = 'en';

interface StampCollection {
stamps: string[];
generated: string;
frequency: number;
}

export interface EuropeanBrandEnvironment {
brand: Brand;
host: string;
Expand Down Expand Up @@ -40,21 +46,22 @@ const cacheResult = <T>(fn: (...options: any[]) => Promise<T>, durationInMS = 60
};
};

const ONE_DAY_IN_MS = 60000 * 60 * 24;
const L15_MINUTES_IN_MS = 60000 * 15;

const getStampList = async (file: string, stampsFile = `https://raw.githubusercontent.com/neoPix/bluelinky-stamps/master/${file}.json`): Promise<string[]> => {
const getStampList = async (file: string, stampsFile = `https://raw.githubusercontent.com/neoPix/bluelinky-stamps/master/${file}.v2.json`): Promise<StampCollection> => {
if (stampsFile.startsWith(('file://'))) {
const [,path] = stampsFile.split('file://');
const content = await promisify(readFile)(path);
return JSON.parse(content.toString('utf-8'));
}
const { body } = await got(stampsFile, { json: true });
return body;
return body as StampCollection;
};

const getStamp = (brand: string, stampsTimeout: number = ONE_DAY_IN_MS) => cacheResult(async (stampsFile?: string) => {
const list = await getStampList(brand, stampsFile);
return list[Math.floor(Math.random() * list.length)];
const getStamp = (brand: string, stampsTimeout: number = L15_MINUTES_IN_MS) => cacheResult(async (stampsFile?: string) => {
const { stamps, generated, frequency } = await getStampList(brand, stampsFile);
const position = ((Date.now() - new Date(generated).getTime()) / frequency) | 0;
return stamps[Math.min(position + Math.floor(Math.random() * 5), stamps.length - 1)];
}, stampsTimeout);


Expand Down

0 comments on commit 06b94eb

Please sign in to comment.