feat(foundry): add composable subject detail profiles

This commit is contained in:
Codex
2026-07-22 19:06:33 +03:00
parent dd2febe7cf
commit dc82ae4c07
21 changed files with 1007 additions and 140 deletions
+67 -17
View File
@@ -1,10 +1,10 @@
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { buildMapSubjectCardModel, normalizeTelemetryReadings } from "../apps/catalog/src/mapSubjectCard.mjs";
import { buildMapSubjectCardModel, DEFAULT_MAP_SUBJECT_DETAIL_PROFILE, normalizeTelemetryReadings } from "../apps/catalog/src/mapSubjectCard.mjs";
const fact = {
sourceId: "867236078012345",
sourceId: "gelios-unit-555590",
semanticType: "map.moving_object",
presentationStatus: "current",
observedAt: "2026-07-22T08:42:03.000Z",
@@ -31,26 +31,32 @@ const fact = {
};
test("subject card exposes the full provider-neutral telemetry projection", () => {
const profile = structuredClone(DEFAULT_MAP_SUBJECT_DETAIL_PROFILE);
profile.tabs.find((tab) => tab.id === "telemetry").sections[0].fields[0].allowedReadingIds = ["sensor.temperature"];
const card = buildMapSubjectCardModel(fact, {
bindingId: "trike-current-positions",
dataProductId: "fleet.positions.current.v5",
profile,
});
assert.ok(card);
assert.equal(card.title, "555590 B2 867236078012345");
assert.equal(card.sourceId, fact.sourceId);
const values = new Map(card.sections.flatMap((section) => section.rows.map((row) => [row.key, row.value])));
assert.equal(values.get("source_id"), fact.sourceId);
assert.equal(values.get("data_product_id"), "fleet.positions.current.v5");
assert.equal(values.get("binding_id"), "trike-current-positions");
assert.equal(values.get("latitude"), "55,756");
assert.equal(values.get("longitude"), "37,618");
assert.equal(values.get("signal_state"), "На связи");
assert.equal(values.get("movement_state"), "В движении");
assert.equal(values.get("speed_kph"), "17,4 км/ч");
assert.equal(values.get("mileage_km"), "4 832,25 км");
assert.equal(values.get("engine_hours"), "912,5 ч");
assert.equal(card.readings[0]?.value, "21,5 °C");
assert.equal(card.defaultTabId, "overview");
assert.deepEqual(card.tabs.map((tab) => tab.id), ["overview", "position", "telemetry", "counters", "equipment", "settings", "maintenance", "diagnostics"]);
const values = new Map(card.tabs.flatMap((tab) => tab.sections.flatMap((section) => section.rows.map((row) => [row.key, row.value]))));
assert.equal(values.get("diagnostics.source-id"), fact.sourceId);
assert.equal(values.get("diagnostics.product"), "fleet.positions.current.v5");
assert.equal(values.get("position.latitude"), "55,75580");
assert.equal(values.get("position.longitude"), "37,61760");
assert.equal(values.get("overview.signal"), "На связи");
assert.equal(values.get("overview.movement"), "В движении");
assert.equal(values.get("overview.speed"), "17,4 км");
assert.equal(values.get("counters.mileage"), "4 832,25 км");
assert.equal(values.get("counters.engine-hours"), "912,5 ч");
const readings = card.tabs.find((tab) => tab.id === "telemetry").sections[0].readings;
assert.equal(readings[0]?.value, "21,5 °C");
assert.equal(card.tabs.find((tab) => tab.id === "equipment").empty, true);
});
test("sensor readings remain bounded and reject duplicate, complex and restricted values", () => {
@@ -60,7 +66,8 @@ test("sensor readings remain bounded and reject duplicate, complex and restricte
{ id: "sensor.imei", label: "IMEI", value: "forbidden" },
{ id: "sensor.payload", label: "Payload", value: { nested: true } },
{ id: "Sensor Bad", label: "Bad id", value: 1 },
]);
{ id: "sensor.account", label: "Аккаунт", value: "+79991234567" },
], ["sensor.voltage", "sensor.imei", "sensor.payload", "sensor.account"]);
assert.deepEqual(readings, [{ id: "sensor.voltage", label: "Напряжение", value: "48,2 V", observedAt: undefined }]);
});
@@ -76,13 +83,55 @@ test("card model never renders provider secrets or direct identifiers from attri
raw_params: "forbidden",
},
});
const keys = card.sections.flatMap((section) => section.rows.map((row) => row.key));
const keys = card.tabs.flatMap((tab) => tab.sections.flatMap((section) => section.rows.map((row) => row.key)));
assert.ok(!keys.includes("api_key"));
assert.ok(!keys.includes("imei"));
assert.ok(!keys.includes("phone"));
assert.ok(!keys.includes("raw_params"));
});
test("unknown fields are fail-closed instead of becoming an automatic Other section", () => {
const card = buildMapSubjectCardModel({
...fact,
attributes: { ...fact.attributes, unclassified_payload: { arbitrary: true }, surprise_value: "must not render" },
});
const keys = card.tabs.flatMap((tab) => tab.sections.flatMap((section) => section.rows.map((row) => row.key)));
assert.ok(!keys.includes("unclassified_payload"));
assert.ok(!keys.includes("surprise_value"));
assert.equal(card.tabs.some((tab) => tab.id === "other"), false);
});
test("subject details compose declared aspect bindings by stable source id", () => {
const profile = {
id: "map.subject-detail.composed",
version: "1.0.0",
title: "Composed",
semanticTypes: ["map.moving_object"],
defaultTabId: "equipment",
tabs: [{
id: "equipment",
label: "Оборудование",
emptyMessage: "Нет данных",
sections: [{
id: "identity",
label: "Профиль",
fields: [{ id: "equipment.kind", aspectId: "technical", source: "attribute", field: "equipment_kind", label: "Тип", format: "text" }],
}],
}],
};
const card = buildMapSubjectCardModel(fact, {
profile,
aspects: {
technical: {
bindingId: "trike-technical-profile",
dataProductId: "fleet.technical-profile.current.v1",
fact: { sourceId: fact.sourceId, semanticType: "asset.technical_profile", attributes: { equipment_kind: "cargo_trike" } },
},
},
});
assert.equal(card.tabs[0].sections[0].rows[0].value, "cargo_trike");
});
test("Cesium pin and label share one entity selection that opens the subject card", async () => {
const [renderer, preview] = await Promise.all([
readFile(new URL("../apps/catalog/src/CesiumMapRenderer.tsx", import.meta.url), "utf8"),
@@ -95,5 +144,6 @@ test("Cesium pin and label share one entity selection that opens the subject car
assert.match(preview, /const handleSelect = useCallback/);
assert.match(preview, /setSubjectCardOpen\(true\)/);
assert.match(preview, /title=\{selectedSubjectCard\.title\}/);
assert.match(preview, /Телеметрия:/);
assert.match(preview, /Карточка объекта:/);
assert.match(preview, /SegmentedControl/);
});