EDSM unit tests.
This commit is contained in:
parent
1d0f22d1f4
commit
edd9ef88a6
5 changed files with 191 additions and 103 deletions
7
.idea/dictionaries/marley.xml
Normal file
7
.idea/dictionaries/marley.xml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<component name="ProjectDictionaryState">
|
||||||
|
<dictionary name="marley">
|
||||||
|
<words>
|
||||||
|
<w>edsm</w>
|
||||||
|
</words>
|
||||||
|
</dictionary>
|
||||||
|
</component>
|
|
@ -1,6 +1,12 @@
|
||||||
<component name="InspectionProjectProfileManager">
|
<component name="InspectionProjectProfileManager">
|
||||||
<profile version="1.0">
|
<profile version="1.0">
|
||||||
<option name="myName" value="Project Default" />
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="ExceptionCaughtLocallyJS" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||||
<inspection_tool class="JSIgnoredPromiseFromCall" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
<inspection_tool class="JSIgnoredPromiseFromCall" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
||||||
|
<inspection_tool class="SpellCheckingInspection" enabled="true" level="TYPO" enabled_by_default="true">
|
||||||
|
<option name="processCode" value="false" />
|
||||||
|
<option name="processLiterals" value="true" />
|
||||||
|
<option name="processComments" value="true" />
|
||||||
|
</inspection_tool>
|
||||||
</profile>
|
</profile>
|
||||||
</component>
|
</component>
|
|
@ -1,51 +1,56 @@
|
||||||
import type { systemEstimatedValue } from '../@types/edsmResponses';
|
import type {systemEstimatedValue} from '../@types/edsmResponses';
|
||||||
|
|
||||||
const EventEmitter = require('node:events');
|
const EventEmitter = require('node:events');
|
||||||
|
|
||||||
import { Log } from './Log';
|
import {Log} from './Log';
|
||||||
import { System } from './System';
|
import {System} from './System';
|
||||||
|
|
||||||
export class EDSM extends EventEmitter {
|
export class EDSM extends EventEmitter {
|
||||||
static #instance: EDSM;
|
static #instance: EDSM;
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
super();
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
static connect(): EDSM {
|
||||||
|
if (!EDSM.#instance) {
|
||||||
|
EDSM.#instance = new EDSM();
|
||||||
}
|
}
|
||||||
|
|
||||||
static connect(): EDSM {
|
return EDSM.#instance;
|
||||||
if (!EDSM.#instance) {
|
}
|
||||||
EDSM.#instance = new EDSM();
|
|
||||||
}
|
|
||||||
|
|
||||||
return EDSM.#instance;
|
/* ---------------------------------------------------------------------------- #request ---- */
|
||||||
|
|
||||||
|
// Submit a request to EDSM and return the response as an object
|
||||||
|
static async #request(
|
||||||
|
url: string,
|
||||||
|
options: { [x: string]: string },
|
||||||
|
): Promise<object | undefined> {
|
||||||
|
let data: object | undefined = undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url + '?' + new URLSearchParams(options));
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Network error - ${response}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
data = await response.json();
|
||||||
|
} catch (err) {
|
||||||
|
Log.write(`ERROR - EDSM.request(): ${err}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------- #request ---- */
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
// Submit a request to EDSM and return the response as an object
|
/* ---------------------------------------------------------------------- getSystemValue ---- */
|
||||||
static async #request(url: string, options: {[x: string]: string}): Promise<object|undefined> {
|
|
||||||
let data: object|undefined = undefined;
|
|
||||||
|
|
||||||
try {
|
static async getSystemValue(system: System): Promise<systemEstimatedValue | undefined> {
|
||||||
const response = await fetch(url + '?' + new URLSearchParams(options));
|
const url = 'https://www.edsm.net/api-system-v1/estimated-value';
|
||||||
|
const response = await EDSM.#request(url, {systemName: system.name});
|
||||||
if (!response.ok) {
|
return (
|
||||||
throw new Error(`Network error - ${response}`);
|
response as systemEstimatedValue
|
||||||
}
|
);
|
||||||
|
}
|
||||||
data = await response.json();
|
}
|
||||||
} catch (err) {
|
|
||||||
Log.write(`ERROR - EDSM.request(): ${err}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------- getSystemValue ---- */
|
|
||||||
|
|
||||||
static async getSystemValue(system: System): Promise<systemEstimatedValue|undefined> {
|
|
||||||
const url = 'https://www.edsm.net/api-system-v1/estimated-value';
|
|
||||||
const response = await EDSM.#request(url, {systemName: system.name});
|
|
||||||
return (response as systemEstimatedValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,81 +1,79 @@
|
||||||
import type { completeFsdJump, location, navRouteSystem } from '../@types/journalLines';
|
import type {completeFsdJump, location, navRouteSystem} from '../@types/journalLines';
|
||||||
|
|
||||||
import * as _ from 'lodash-es';
|
import * as _ from 'lodash-es';
|
||||||
|
|
||||||
import { Body } from './Body';
|
import {Body} from './Body';
|
||||||
import { EDSM } from './EDSM';
|
import {EDSM} from './EDSM';
|
||||||
|
|
||||||
export class System {
|
export class System {
|
||||||
name: string;
|
name: string;
|
||||||
SystemAddress?: number;
|
SystemAddress?: number;
|
||||||
StarClass?: string;
|
StarClass?: string;
|
||||||
charted: boolean;
|
charted: boolean;
|
||||||
bodies: Body[];
|
bodies: Body[];
|
||||||
|
|
||||||
// ESDM data
|
// EDSM data
|
||||||
estimatedValue?: number;
|
estimatedValue?: number;
|
||||||
estimatedValueMapped?: number;
|
estimatedValueMapped?: number;
|
||||||
valuableBodies?: Body[];
|
valuableBodies?: Body[];
|
||||||
|
|
||||||
constructor(line?: navRouteSystem|completeFsdJump|location) {
|
constructor(line?: navRouteSystem | completeFsdJump | location) {
|
||||||
// In future, this is where we preform EDSM lookup
|
if (!line) {
|
||||||
|
this.name = 'Unknown';
|
||||||
|
} else {
|
||||||
|
this.name = line.StarSystem;
|
||||||
|
this.SystemAddress = line.SystemAddress;
|
||||||
|
|
||||||
if (!line) {
|
if ('StarClass' in line) {
|
||||||
this.name = 'Unknown';
|
this.StarClass = line.StarClass;
|
||||||
} else {
|
}
|
||||||
this.name = line.StarSystem;
|
|
||||||
this.SystemAddress = line.SystemAddress;
|
|
||||||
|
|
||||||
if ('StarClass' in line) {
|
|
||||||
this.StarClass = line.StarClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set this to true initially, since it likely is and the system is technically inserted
|
|
||||||
// into the UI before it's appraised.
|
|
||||||
this.charted = true;
|
|
||||||
this.bodies = [];
|
|
||||||
|
|
||||||
if (this.name !== 'Unknown') {
|
|
||||||
this.#getValue();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------- #getValue ---- */
|
// Set this to true initially, since it likely is and the system is technically inserted
|
||||||
|
// into the UI before it's appraised.
|
||||||
|
this.charted = true;
|
||||||
|
this.bodies = [];
|
||||||
|
|
||||||
async #getValue() {
|
if (this.name !== 'Unknown') {
|
||||||
// display estimatedValueMapped
|
this.#getValue();
|
||||||
const data = await EDSM.getSystemValue(this);
|
|
||||||
|
|
||||||
if (data) {
|
|
||||||
this.estimatedValue = data.estimatedValue;
|
|
||||||
this.estimatedValueMapped = data.estimatedValueMapped;
|
|
||||||
|
|
||||||
// If EDSM doesn't have an estimate, then it's likely undiscovered.
|
|
||||||
this.charted = this.estimatedValue > 0;
|
|
||||||
|
|
||||||
// Save valuable bodies in system, if any.
|
|
||||||
if (data.valuableBodies.length > 0) {
|
|
||||||
this.valuableBodies = [];
|
|
||||||
|
|
||||||
data.valuableBodies.forEach((body) => {
|
|
||||||
this.valuableBodies?.push(new Body(body));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let the UI know it needs to update.
|
|
||||||
EDSM.connect().emit('SYSTEM_APPRAISED', this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- sortBodies ---- */
|
/* --------------------------------------------------------------------------- #getValue ---- */
|
||||||
|
|
||||||
sortBodies(): void {
|
async #getValue() {
|
||||||
this.bodies = _.orderBy(
|
// display estimatedValueMapped
|
||||||
this.bodies,
|
const data = await EDSM.getSystemValue(this);
|
||||||
['mappedValue', 'DistanceFromArrivalLS'],
|
|
||||||
['desc', 'desc'],
|
if (data) {
|
||||||
);
|
this.estimatedValue = data.estimatedValue;
|
||||||
|
this.estimatedValueMapped = data.estimatedValueMapped;
|
||||||
|
|
||||||
|
// If EDSM doesn't have an estimate, then it's likely undiscovered.
|
||||||
|
this.charted = this.estimatedValue > 0;
|
||||||
|
|
||||||
|
// Save valuable bodies in system, if any.
|
||||||
|
if (data.valuableBodies.length > 0) {
|
||||||
|
this.valuableBodies = [];
|
||||||
|
|
||||||
|
data.valuableBodies.forEach((body) => {
|
||||||
|
this.valuableBodies?.push(new Body(body));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let the UI know it needs to update.
|
||||||
|
EDSM.connect().emit('SYSTEM_APPRAISED', this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- sortBodies ---- */
|
||||||
|
|
||||||
|
sortBodies(): void {
|
||||||
|
this.bodies = _.orderBy(
|
||||||
|
this.bodies,
|
||||||
|
['mappedValue', 'DistanceFromArrivalLS'],
|
||||||
|
['desc', 'desc'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
72
test/EDSM.test.ts
Normal file
72
test/EDSM.test.ts
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
import {expect, jest} from '@jest/globals';
|
||||||
|
import {EDSM} from '../src/models/EDSM';
|
||||||
|
import {System} from '../src/models/System';
|
||||||
|
|
||||||
|
describe('EDSM', () => {
|
||||||
|
describe('connect()', () => {
|
||||||
|
it('should create new instance', () => {
|
||||||
|
expect(EDSM.connect()).toBeInstanceOf(EDSM);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get previously created instance', () => {
|
||||||
|
const edsm = EDSM.connect();
|
||||||
|
expect(EDSM.connect()).toBeInstanceOf(EDSM);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getSystemValue()', () => {
|
||||||
|
const mockFetch = (data?: {[i: string]: any}, ok: boolean = true) => {
|
||||||
|
global.fetch = jest.fn(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
ok: ok,
|
||||||
|
json: () => Promise.resolve(data),
|
||||||
|
} as Response),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
it('should get system info', async () => {
|
||||||
|
const system = new System({ "StarSystem":"LHS 3447", "SystemAddress":0, "StarPos":[0,0,0], "StarClass":"M" });
|
||||||
|
const data = {
|
||||||
|
"id": 13153,
|
||||||
|
"id64": 5306465653474,
|
||||||
|
"name": "LHS 3447",
|
||||||
|
"url": "https://www.edsm.net/en/system/bodies/id/13153/name/LHS+3447",
|
||||||
|
"estimatedValue": 353968,
|
||||||
|
"estimatedValueMapped": 1164029,
|
||||||
|
"valuableBodies": [
|
||||||
|
{
|
||||||
|
"bodyId": 3195879,
|
||||||
|
"bodyName": "LHS 3447 A 5",
|
||||||
|
"distance": 92282,
|
||||||
|
"valueMax": 879114
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
mockFetch(data);
|
||||||
|
|
||||||
|
const result = await EDSM.getSystemValue(system);
|
||||||
|
expect(result).toEqual(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not get system info if system name is invalid', async () => {
|
||||||
|
const system = new System();
|
||||||
|
const data = {};
|
||||||
|
mockFetch({});
|
||||||
|
|
||||||
|
const result = await EDSM.getSystemValue(system);
|
||||||
|
expect(result).toEqual(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined if response is not ok', async () => {
|
||||||
|
const system = new System();
|
||||||
|
mockFetch({}, false);
|
||||||
|
|
||||||
|
const result = await EDSM.getSystemValue(system);
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.restoreAllMocks();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue