#!/usr/bin/env node "use strict"; const assert = require("assert"); const fs = require("fs"); const os = require("os"); const path = require("path"); const { cesiumPreviewHtml } = require("./lib/area-preview"); const { makeVehicleGltf } = require("./lib/vehicle-model"); const { buildVehicleRoute } = require("./lib/vehicle-route"); const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "preview-assets-")); const osmPath = path.join(tempDir, "fixture.osm"); fs.writeFileSync(osmPath, ` `); const route = buildVehicleRoute(osmPath); assert.equal(route.source, osmPath); assert.deepEqual(route.bounds, { minLon: 120, minLat: 30, maxLon: 120.01, maxLat: 30.01, }); assert.equal(route.speedMetersPerSecond, 8); assert.equal(route.loop, true); assert.equal(route.segments.length, 2); assert.deepEqual(route.segments.map((segment) => segment.id), ["major-road", "minor-road"]); assert.equal(route.segments[0].name, "Major Road"); assert.equal(route.segments[0].oneWay, "yes"); assert.equal(route.segments[0].laneOffsetMeters, 1.3); assert.notDeepEqual(route.segments[0].coordinates, route.segments[0].centerlineCoordinates); assert.ok(route.segments[0].lengthMeters > route.segments[1].lengthMeters); const vehicle = makeVehicleGltf(); assert.equal(vehicle.asset.version, "2.0"); assert.equal(vehicle.asset.generator, "osm-asset-pipeline vehicle preview"); assert.equal(vehicle.scene, 0); assert.equal(vehicle.meshes.length, vehicle.nodes.length); assert.equal(vehicle.scenes[0].nodes.length, vehicle.nodes.length); assert.deepEqual( vehicle.materials.map((material) => material.name), ["paint red", "dark roof", "glass", "tire", "wheel hub", "headlight", "tail light"], ); assert.ok(vehicle.meshes.some((mesh) => mesh.name === "body")); assert.ok(vehicle.meshes.some((mesh) => mesh.name === "wheel_-1.55_-1.02")); assert.match(vehicle.buffers[0].uri, /^data:application\/octet-stream;base64,/); assert.equal( Buffer.from(vehicle.buffers[0].uri.split(",")[1], "base64").length, vehicle.buffers[0].byteLength, ); const html = cesiumPreviewHtml( "scene<&>.glb", "scene.json", "route.json", "vehicle.gltf", "north<&>\u2028valley", ); assert.match(html, /north<&>\u2028valley Cesium Preview<\/title>/); assert.match(html, /Loading scene<&>\.glb/); assert.match(html, /"areaId":"north\\u003c\\u0026\\u003e\\u2028valley"/); assert.match(html, /"glbName":"scene\\u003c\\u0026\\u003e\.glb"/); assert.match(html, /id="viewMode"/); assert.match(html, /data-view-mode="inspect"/); assert.match(html, /id="semanticToggles" class="control-subgroup hidden"/); fs.rmSync(tempDir, { recursive: true, force: true }); console.log("Preview asset tests passed.");