LiveAtlas/test/util.test.ts

395 lines
8.3 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.
*/
import {parseUrl} from "@/util";
import {LiveAtlasParsedUrl} from "@/index";
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);
});
});