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.

232 lines
8.7 KiB
TypeScript
Raw Normal View History

2023-07-15 18:55:03 +02:00
import { LayoutConfigJson } from "../../../../src/Models/ThemeConfig/Json/LayoutConfigJson"
import { LayerConfigJson } from "../../../../src/Models/ThemeConfig/Json/LayerConfigJson"
import { PrepareTheme } from "../../../../src/Models/ThemeConfig/Conversion/PrepareTheme"
import LayoutConfig from "../../../../src/Models/ThemeConfig/LayoutConfig"
import bookcaseLayer from "../../../../src/assets/generated/layers/public_bookcase.json"
import LayerConfig from "../../../../src/Models/ThemeConfig/LayerConfig"
import { ExtractImages } from "../../../../src/Models/ThemeConfig/Conversion/FixImages"
import cyclofix from "../../../../src/assets/generated/themes/cyclofix.json"
import { Tag } from "../../../../src/Logic/Tags/Tag"
2023-11-02 04:35:32 +01:00
import { DesugaringContext } from "../../../../src/Models/ThemeConfig/Conversion/Conversion"
2023-07-15 18:55:03 +02:00
import { And } from "../../../../src/Logic/Tags/And"
2023-02-03 04:48:32 +01:00
import { describe, expect, it } from "vitest"
import { QuestionableTagRenderingConfigJson } from "../../../../src/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
import Constants from "../../../../src/Models/Constants"
2023-11-02 04:35:32 +01:00
import { ConversionContext } from "../../../../src/Models/ThemeConfig/Conversion/ConversionContext"
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",
}
function constructSharedLayers(): Map<string, LayerConfigJson> {
const sharedLayers = new Map<string, LayerConfigJson>()
sharedLayers.set("selected_element", <LayerConfigJson>{
id: "selected_element",
pointRendering: null,
tagRenderings: null,
lineRendering: null,
title: null,
source: "special",
})
for (const defaultLayer of Constants.added_by_default) {
sharedLayers.set(defaultLayer, <LayerConfigJson>{
id: defaultLayer,
pointRendering: null,
tagRenderings: null,
lineRendering: null,
title: null,
source: "special",
})
}
return sharedLayers
}
2022-03-18 13:04:12 +01:00
describe("PrepareTheme", () => {
2022-04-06 03:06:28 +02:00
it("should substitute layers", () => {
const sharedLayers = constructSharedLayers()
sharedLayers.set("public_bookcase", <any>bookcaseLayer)
2022-04-06 03:06:28 +02:00
const theme = { ...themeConfigJson, layers: ["public_bookcase"] }
const prepareStep = new PrepareTheme({
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
sharedLayers,
2023-06-20 10:40:27 +02:00
publicLayers: new Set<string>(),
2022-04-06 03:06:28 +02:00
})
let themeConfigJsonPrepared = prepareStep.convertStrict(theme, ConversionContext.test())
2022-04-06 03:06:28 +02:00
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", () => {
const sharedLayers = constructSharedLayers()
sharedLayers.set("public_bookcase", <any>bookcaseLayer)
2022-03-18 13:04:12 +01:00
let themeConfigJsonPrepared = new PrepareTheme({
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
sharedLayers,
2023-06-20 10:40:27 +02:00
publicLayers: new Set<string>(),
}).convertStrict(themeConfigJson, ConversionContext.test())
2022-03-18 13:04:12 +01:00
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 = constructSharedLayers()
sharedLayers.set("public_bookcase", <any>bookcaseLayer)
2022-04-06 03:06:28 +02:00
let themeConfigJsonPrepared = new PrepareTheme({
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
sharedLayers,
2023-06-20 10:40:27 +02:00
publicLayers: new Set<string>(),
}).convertStrict(
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
},
ConversionContext.test()
)
2022-04-06 03:06:28 +02:00
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: [],
pointRendering: [
{
location: ["point"],
label: "xyz",
iconBadges: [
{
if: "_favourite=yes",
then: <any>{
id: "circlewhiteheartred",
render: "circle:white;heart:red",
},
},
],
},
],
lineRendering: [{ width: 1 }],
2022-09-08 21:40:48 +02:00
}
const sharedLayers = constructSharedLayers()
sharedLayers.set("layer-example", testLayer)
const ctx: DesugaringContext = {
sharedLayers,
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
2023-06-20 10:40:27 +02:00
publicLayers: new Set<string>(),
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, ConversionContext.test())
2023-02-03 04:48:32 +01:00
expect(rewritten.layers[0]).toEqual(testLayer)
expect(rewritten.layers[1]).toEqual({
2024-02-20 13:33:38 +01:00
_basedOn: "layer-example",
source: {
osmTags: "x=y",
},
id: "layer-example",
name: null,
minzoom: 18,
pointRendering: [
{
location: ["point"],
label: "xyz",
iconBadges: [
{
if: "_favourite=yes",
then: {
id: "circlewhiteheartred",
render: "circle:white;heart:red",
},
},
],
},
],
lineRendering: [{ width: 1 }],
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, ConversionContext.test())
2023-02-09 00:10:59 +01:00
.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
}
})
})