mapcomplete/test/Models/ThemeConfig/Conversion/PrepareTheme.spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-03-18 13:04:12 +01:00
import { LayoutConfigJson } from "../../../../Models/ThemeConfig/Json/LayoutConfigJson"
import { LayerConfigJson } from "../../../../Models/ThemeConfig/Json/LayerConfigJson"
import { PrepareTheme } from "../../../../Models/ThemeConfig/Conversion/PrepareTheme"
import { TagRenderingConfigJson } from "../../../../Models/ThemeConfig/Json/TagRenderingConfigJson"
import LayoutConfig from "../../../../Models/ThemeConfig/LayoutConfig"
2023-02-09 00:10:59 +01:00
import bookcaseLayer from "../../../../assets/generated/layers/public_bookcase.json"
2022-03-18 13:04:12 +01:00
import LayerConfig from "../../../../Models/ThemeConfig/LayerConfig"
2022-03-21 02:00:50 +01:00
import { ExtractImages } from "../../../../Models/ThemeConfig/Conversion/FixImages"
2023-02-09 00:10:59 +01:00
import cyclofix from "../../../../assets/generated/themes/cyclofix.json"
2022-04-06 03:06:28 +02:00
import { Tag } from "../../../../Logic/Tags/Tag"
import { DesugaringContext } from "../../../../Models/ThemeConfig/Conversion/Conversion"
2022-06-20 03:14:44 +02:00
import { And } from "../../../../Logic/Tags/And"
2023-02-03 04:48:32 +01:00
import { describe, expect, it } from "vitest"
2022-03-18 13:04:12 +01:00
const themeConfigJson: LayoutConfigJson = {
description: "Descr",
icon: "",
layers: [
{
builtin: "public_bookcase",
override: {
source: {
geoJson: "xyz",
},
},
},
],
startLat: 0,
startLon: 0,
startZoom: 0,
title: {
en: "Title",
},
id: "test",
}
describe("PrepareTheme", () => {
2022-04-06 03:06:28 +02:00
it("should substitute layers", () => {
const sharedLayers = new Map<string, LayerConfigJson>()
2023-02-09 00:10:59 +01:00
sharedLayers.set("public_bookcase", bookcaseLayer)
2022-04-06 03:06:28 +02:00
const theme = { ...themeConfigJson, layers: ["public_bookcase"] }
const prepareStep = new PrepareTheme({
tagRenderings: new Map<string, TagRenderingConfigJson>(),
sharedLayers: sharedLayers,
})
let themeConfigJsonPrepared = prepareStep.convert(theme, "test").result
const themeConfig = new LayoutConfig(themeConfigJsonPrepared)
const layerUnderTest = <LayerConfig>(
themeConfig.layers.find((l) => l.id === "public_bookcase")
2022-06-20 03:14:44 +02:00
)
2023-02-03 04:48:32 +01:00
expect(layerUnderTest.source.osmTags).toEqual(
2022-06-20 03:14:44 +02:00
new And([new Tag("amenity", "public_bookcase")])
2022-09-08 21:40:48 +02:00
)
2022-04-06 03:06:28 +02:00
})
2022-03-18 13:04:12 +01:00
2022-04-06 03:06:28 +02:00
it("should apply override", () => {
2022-03-18 13:04:12 +01:00
const sharedLayers = new Map<string, LayerConfigJson>()
2023-02-09 00:10:59 +01:00
sharedLayers.set("public_bookcase", bookcaseLayer)
2022-03-18 13:04:12 +01:00
let themeConfigJsonPrepared = new PrepareTheme({
tagRenderings: new Map<string, TagRenderingConfigJson>(),
sharedLayers: sharedLayers,
}).convert(themeConfigJson, "test").result
const themeConfig = new LayoutConfig(themeConfigJsonPrepared)
const layerUnderTest = <LayerConfig>(
themeConfig.layers.find((l) => l.id === "public_bookcase")
2022-09-08 21:40:48 +02:00
)
2023-02-03 04:48:32 +01:00
expect(layerUnderTest.source.geojsonSource).toBe("xyz")
2022-03-18 13:04:12 +01:00
})
2022-04-06 03:06:28 +02:00
it("should apply override", () => {
const sharedLayers = new Map<string, LayerConfigJson>()
2023-02-09 00:10:59 +01:00
sharedLayers.set("public_bookcase", bookcaseLayer)
2022-04-06 03:06:28 +02:00
let themeConfigJsonPrepared = new PrepareTheme({
tagRenderings: new Map<string, TagRenderingConfigJson>(),
sharedLayers: sharedLayers,
}).convert(
2022-09-08 21:40:48 +02:00
{
2022-04-06 03:06:28 +02:00
...themeConfigJson,
overrideAll: { source: { geoJson: "https://example.com/data.geojson" } },
2022-09-08 21:40:48 +02:00
},
2022-04-06 03:06:28 +02:00
"test"
).result
const themeConfig = new LayoutConfig(themeConfigJsonPrepared)
const layerUnderTest = <LayerConfig>(
themeConfig.layers.find((l) => l.id === "public_bookcase")
2022-09-08 21:40:48 +02:00
)
2023-02-03 04:48:32 +01:00
expect(layerUnderTest.source.geojsonSource).toBe("https://example.com/data.geojson")
2022-04-06 03:06:28 +02:00
})
2022-09-08 21:40:48 +02:00
it("should remove names which are overriden with null", () => {
const testLayer: LayerConfigJson = {
source: {
osmTags: "x=y",
},
id: "layer-example",
name: {
en: "Test layer - please ignore",
},
titleIcons: [],
mapRendering: null,
2022-09-08 21:40:48 +02:00
}
const ctx: DesugaringContext = {
sharedLayers: new Map<string, LayerConfigJson>([["layer-example", testLayer]]),
2022-04-06 03:06:28 +02:00
tagRenderings: new Map<string, TagRenderingConfigJson>(),
2022-09-08 21:40:48 +02:00
}
const layout: LayoutConfigJson = {
description: "A testing theme",
icon: "",
2022-09-08 21:40:48 +02:00
id: "",
layers: [
"layer-example",
2022-09-08 21:40:48 +02:00
{
builtin: "layer-example",
2022-03-18 13:04:12 +01:00
override: {
name: null,
minzoom: 18,
2022-09-08 21:40:48 +02:00
},
},
],
startLat: 0,
startLon: 0,
startZoom: 0,
title: "Test theme",
}
const rewritten = new PrepareTheme(ctx, {
skipDefaultLayers: true,
}).convertStrict(layout, "test")
2023-02-03 04:48:32 +01:00
expect(rewritten.layers[0]).toEqual(testLayer)
expect(rewritten.layers[1]).toEqual({
source: {
osmTags: "x=y",
},
id: "layer-example",
name: null,
minzoom: 18,
mapRendering: null,
titleIcons: [],
})
})
})
2022-03-21 02:00:50 +01:00
describe("ExtractImages", () => {
it("should find all images in a themefile", () => {
2023-02-09 00:10:59 +01:00
const images = new Set<string>(
new ExtractImages(true, new Set<string>())
.convertStrict(<any>cyclofix, "test")
.map((x) => x.path)
2022-09-08 21:40:48 +02:00
)
2022-03-21 02:00:50 +01:00
const expectedValues = [
"./assets/layers/bike_repair_station/repair_station.svg",
"./assets/layers/bike_repair_station/repair_station_pump.svg",
"./assets/layers/bike_repair_station/broken_pump.svg",
"./assets/layers/bike_repair_station/pump.svg",
"./assets/themes/cyclofix/fietsambassade_gent_logo_small.svg",
"./assets/layers/bike_repair_station/pump_example_manual.jpg",
"./assets/layers/bike_repair_station/pump_example.png",
"./assets/layers/bike_repair_station/pump_example_round.jpg",
"./assets/layers/bike_repair_station/repair_station_example_2.jpg",
"close",
]
for (const expected of expectedValues) {
2023-02-09 00:10:59 +01:00
if (!images.has(expected)) {
expect.fail(
"Image " + expected + " not found (has:" + Array.from(images).join(",") + ")"
)
}
2022-03-21 02:00:50 +01:00
}
})
})