LiveAtlas/test/util.test.ts

466 lines
11 KiB
TypeScript
Raw Normal View History

2022-06-21 22:26:32 +00:00
/*
* Copyright 2022 James Lyne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-06-23 14:01:34 +00:00
import {getGlobalMessages, getMessages, getUrlForLocation, guessWorldDimension, parseUrl} from "@/util";
import {LiveAtlasDimension, LiveAtlasGlobalMessageConfig, LiveAtlasMessageConfig, LiveAtlasParsedUrl} from "@/index";
import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition";
import {globalMessages, serverMessages} from "../messages";
2022-06-21 22:26:32 +00:00
const validURLs: [string, URL, LiveAtlasParsedUrl][] = [
[
'Dynmap - complete',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?worldname=world&mapname=surface&zoom=4&x=6885&y=64&z=24608'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: {x: 6885, y: 64, z: 24608},
zoom: 4,
legacy: true
}
],
[
'Dynmap - no location',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?worldname=world&mapname=surface&zoom=4'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: undefined,
zoom: 4,
legacy: true
}
],
[
'Dynmap - no zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?worldname=world&mapname=surface&x=6885&y=64&z=24608'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: {x: 6885, y: 64, z: 24608},
zoom: undefined,
legacy: true
}
],
[
'Dynmap - no zoom or location',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?worldname=world&mapname=surface'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: undefined,
zoom: undefined,
legacy: true
}
],
[
'Dynmap - no map, zoom or location',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?worldname=world'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: undefined,
location: undefined,
zoom: undefined,
legacy: true
}
],
[
'Pl3xmap - complete',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?world=world&zoom=4&x=6885&z=24608'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: undefined,
location: {x: 6885, y: 64, z: 24608},
zoom: 4,
legacy: true
}
],
[
'Pl3xmap - no location',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?world=world&zoom=4'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: undefined,
location: undefined,
zoom: 4,
legacy: true
}
],
[
'Pl3xmap - no zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?world=world&x=6885&z=24608'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: undefined,
location: {x: 6885, y: 64, z: 24608},
zoom: undefined,
legacy: true
}
],
[
'Pl3xmap - no zoom or location',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/build?world=world'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: undefined,
location: undefined,
zoom: undefined,
legacy: true
}
],
2022-06-21 23:11:56 +00:00
[
'Overviewer - complete',
new URL('https://example.com/#/-269/64/244/-3/world/map'),
{
world: 'world',
map: 'map',
location: {x: -269, y: 64, z:244},
zoom: undefined,
legacy: false
}
],
2022-06-21 22:26:32 +00:00
[
'LiveAtlas - complete',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat;168,64,1914;2'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: {x: 168, y: 64, z: 1914},
zoom: 2,
legacy: false
}
],
[
'LiveAtlas - no zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat;168,64,1914'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: {x: 168, y: 64, z: 1914},
zoom: undefined,
legacy: false
}
],
[
'LiveAtlas - no location or zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: undefined,
zoom: undefined,
legacy: false
}
],
[
'LiveAtlas - no map, location or zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: undefined,
location: undefined,
zoom: undefined,
legacy: false
}
]
]
const invalidURLs: [string, URL, LiveAtlasParsedUrl|null][] = [
[
'LiveAtlas - negative zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat;168,64,1914;-2'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: {x: 168, y: 64, z: 1914},
zoom: undefined,
legacy: false
}
],
[
'LiveAtlas - NaN zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat;168,64,1914;ewsjtuiewshfoes'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: {x: 168, y: 64, z: 1914},
zoom: undefined,
legacy: false
}
],
[
'LiveAtlas - missing z coordinate',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat;168,64;2'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: undefined,
zoom: 2,
legacy: false
}
],
[
'LiveAtlas - NaN z coordinate',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat;168,64,j;2'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: undefined,
zoom: 2,
legacy: false
}
],
[
'LiveAtlas - missing y and z coordinates',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat;168;2'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: undefined,
zoom: 2,
legacy: false
}
],
[
'LiveAtlas - invalid location',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#world;flat;23wteyuisezujsezr'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'flat',
location: undefined,
zoom: undefined,
legacy: false
}
],
[
'Dynmap - Negative zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival?worldname=world&mapname=surface&zoom=-2&x=6885&y=64&z=24608'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: {x: 6885, y: 64, z: 24608},
zoom: undefined,
legacy: true
}
],
[
'Dynmap - NaN zoom',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival?worldname=world&mapname=surface&zoom=fsghrtdrh&x=6885&y=64&z=24608'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: {x: 6885, y: 64, z: 24608},
zoom: undefined,
legacy: true
}
],
[
'Dynmap - Missing z coordinate',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival?worldname=world&mapname=surface&zoom=2&x=6885&y=64'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: undefined,
zoom: 2,
legacy: true
}
],
[
'Dynmap - NaN z coordinate',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival?worldname=world&mapname=surface&zoom=2&x=6885&y=64&z=j'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: undefined,
zoom: 2,
legacy: true
}
],
[
'Dynmap - Missing y and z coordinates',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival?worldname=world&mapname=surface&zoom=2&x=6885'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: undefined,
zoom: 2,
legacy: true
}
],
[
'Dynmap - Invalid/missing coordinates',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival?worldname=world&mapname=surface&x=3wresfsg'),
2022-06-21 22:26:32 +00:00
{
world: 'world',
map: 'surface',
location: undefined,
zoom: undefined,
legacy: true
}
],
[
'Dynmap - Missing world',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival?mapname=surface&x=3wresfsg'),
2022-06-21 22:26:32 +00:00
null
],
2022-06-21 23:11:56 +00:00
[
'Pl3xmap - Negative zoom',
new URL('https://example.com/map/survival?world=world&zoom=-2&x=6885&z=24608'),
{
world: 'world',
map: undefined,
location: {x: 6885, y: 64, z: 24608},
zoom: undefined,
legacy: true
}
],
[
'Pl3xmap - NaN zoom',
new URL('https://example.com/map/survival?world=world&zoom=fsghrtdrh&x=6885&z=24608'),
{
world: 'world',
map: undefined,
location: {x: 6885, y: 64, z: 24608},
zoom: undefined,
legacy: true
}
],
[
'Pl3xmap - Missing z coordinate',
new URL('https://example.com/map/survival?world=world&zoom=2&x=6885'),
{
world: 'world',
map: undefined,
location: undefined,
zoom: 2,
legacy: true
}
],
[
'Pl3xmap - NaN z coordinate',
new URL('https://example.com/map/survival?worldname=world&mapname=surface&zoom=2&x=6885&z=j'),
{
world: 'world',
map: 'surface',
location: undefined,
zoom: 2,
legacy: true
}
],
[
'Pl3xmap - Invalid/missing coordinates',
new URL('https://example.com/map/survival?world=world&x=3wresfsg'),
{
world: 'world',
map: undefined,
location: undefined,
zoom: undefined,
legacy: true
}
],
2022-06-21 22:26:32 +00:00
[
'empty hash',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival#'),
2022-06-21 22:26:32 +00:00
null
],
[
'empty query',
2022-06-21 23:11:56 +00:00
new URL('https://example.com/map/survival?'),
null
],
[
'invalid query',
new URL('https://example.com/map/survival?invalid=aaa&bbb=54'),
2022-06-21 22:26:32 +00:00
null
],
];
describe("parseURL", () => {
test.each(validURLs)("Valid URL - %s", (name: string, url: URL, expected: LiveAtlasParsedUrl) => {
expect(parseUrl(url)).toEqual(expected);
});
test.each(invalidURLs)("Invalid or incomplete URL - %s", (name: string, url: URL, expected: LiveAtlasParsedUrl|null) => {
expect(parseUrl(url)).toEqual(expected);
});
});
2022-06-23 14:01:34 +00:00
describe("guessWorldDimension", () => {
const worlds: [string, LiveAtlasDimension][] = [
['world', 'overworld'],
['smp2022', 'overworld'],
['DIM-1', 'nether'],
['world_nether', 'nether'],
['smp_nether', 'nether'],
['smpnether', 'nether'],
['nether_smp', 'nether'],
['DIM1', 'end'],
['world_the_end', 'end'],
['end', 'end'],
['smp_end', 'end'],
['smpend', 'overworld'], //Would have too many false positives
['end_smp', 'end'],
];
test.each(worlds)('%s -> %s', (input: string, expected: LiveAtlasDimension) =>
expect(guessWorldDimension(input)).toBe(expected))
})
test("getUrlForLocation", () => {
const map = new LiveAtlasMapDefinition({
name: 'test_map',
world: {
name: 'test_world',
displayName: 'Test World',
dimension: 'overworld',
seaLevel: 64,
maps: new Set()
},
baseUrl: 'test/',
tileSize: 128,
imageFormat: 'png',
nativeZoomLevels: 1
});
expect(getUrlForLocation(map, {x: 100, y: 68.5, z: -2300.3}, 3))
.toEqual('#test_world;test_map;100,69,-2300;3')
});
test("getMessages", () => {
const expectedGlobal: LiveAtlasGlobalMessageConfig = globalMessages.reduce((result: any, key) => {
result[key] = `Missing message: ${key}`;
return result;
}, {});
const expectedMessages: LiveAtlasMessageConfig = Object.assign({}, expectedGlobal, serverMessages.reduce((result: any, key) => {
result[key] = `Missing message: ${key}`;
return result;
}, {}));
const input: any = {
extraProperty: true, //Invalid message key
serversHeading: undefined, // Valid message key but undefined
mapTitle: null, // Valid message key but null
loginTitle: '', // Valid message key but empty string
registerConfirmPasswordLabel: {invalid: true}, //Valid message key but not a string,
chatErrorDisabled: 'test defined global message', // Valid global message
chatPlayerQuit: 'test defined server message' // Valid server message
};
expectedGlobal.chatErrorDisabled = expectedMessages.chatErrorDisabled = input.chatErrorDisabled;
expectedMessages.chatPlayerQuit = input.chatPlayerQuit;
expect(getMessages(input)).toEqual(expectedMessages);
expect(getGlobalMessages(input)).toEqual(expectedGlobal);
});