67 lines
2.5 KiB
JavaScript
67 lines
2.5 KiB
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const path = require("path");
|
|
const { normalizeAreaConfig } = require("./lib/area-config");
|
|
const { digestGltf } = require("./glb-digest");
|
|
const { evaluateGlbBudget, BUDGETS } = require("./lib/stage-manifest");
|
|
|
|
const gltf = {
|
|
nodes: [
|
|
{ name: "Building_1", mesh: 0 },
|
|
{ name: "Shapespark_tree-01", mesh: 1 },
|
|
],
|
|
meshes: [
|
|
{ name: "Building", primitives: [{ indices: 0, attributes: { POSITION: 1 } }] },
|
|
{ name: "Tree", primitives: [{ indices: 2, attributes: { POSITION: 3 } }] },
|
|
],
|
|
accessors: [{ count: 12 }, { count: 4 }, { count: 9 }, { count: 3 }],
|
|
images: [{ name: "large", bufferView: 0 }, { name: "small", bufferView: 1 }],
|
|
bufferViews: [{ byteLength: 800 }, { byteLength: 200 }],
|
|
};
|
|
const digest = digestGltf(gltf, { file: "fixture.glb", fileBytes: 1500 });
|
|
assert.equal(digest.counts.triangles, 7);
|
|
assert.equal(digest.counts.renderTriangles, 7);
|
|
assert.equal(digest.embeddedImageBytes, 1000);
|
|
assert.deepEqual(digest.sourceSummary.map((entry) => [entry.source, entry.triangles]), [["buildings", 4], ["vegetation", 3]]);
|
|
assert.deepEqual(digest.images.map((image) => image.bytes), [800, 200]);
|
|
|
|
const budget = { ...BUDGETS, glbBytes: 1000 };
|
|
assert.equal(evaluateGlbBudget(digest, budget).violations[0].key, "glbBytes");
|
|
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "asset-budget-"));
|
|
const input = path.join(tempDir, "input.osm");
|
|
fs.writeFileSync(input, "<osm/>");
|
|
const base = { id: "test-area", input, outputRoot: tempDir };
|
|
assert.equal(
|
|
normalizeAreaConfig(base).outputs.trafficSignals,
|
|
path.join(tempDir, "test-area", "osm2streets_web_out", "traffic_signals.json"),
|
|
);
|
|
assert.equal(
|
|
normalizeAreaConfig(base).outputs.trafficSignalAssemblies,
|
|
path.join(tempDir, "test-area", "osm2streets_web_out", "traffic_signal_assemblies.geojson"),
|
|
);
|
|
assert.equal(normalizeAreaConfig({ ...base, budget: { nodes: 800 } }).budget.glbNodes, 800);
|
|
assert.throws(
|
|
() => normalizeAreaConfig({ ...base, budget: { nodes: 1200 } }),
|
|
/budget.reason is required/,
|
|
);
|
|
assert.throws(
|
|
() => normalizeAreaConfig({ ...base, budget: { triangles: 0 } }),
|
|
/budget.triangles must be a positive integer/,
|
|
);
|
|
assert.throws(
|
|
() => normalizeAreaConfig({ ...base, budget: "large" }),
|
|
/budget must be an object/,
|
|
);
|
|
assert.equal(
|
|
normalizeAreaConfig({ ...base, budget: { nodes: 1200, reason: "Dense campus vegetation" } }).budget.glbNodes,
|
|
1200,
|
|
);
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
|
|
console.log("Asset budget tests passed.");
|