forked from linkong/planet
151 lines
4.8 KiB
JavaScript
151 lines
4.8 KiB
JavaScript
import { describe, expect, it } from "bun:test";
|
|
import {
|
|
addTargetToCompare,
|
|
clearCompareTargets,
|
|
getCompareTargets,
|
|
matchWorkflowBindings,
|
|
normalizeEarthTarget,
|
|
removeTargetFromCompare,
|
|
renderBindingPromptTemplate,
|
|
resetCompareTargetsForTest,
|
|
} from "./workflow-bindings.js";
|
|
|
|
describe("earth workflow bindings matcher, normalizer, and compare store", () => {
|
|
it("normalizes different Earth objects into a consistent target contract", () => {
|
|
const cable = normalizeEarthTarget("cable", {
|
|
cable_id: "c-peace",
|
|
name: "PEACE",
|
|
latitude: 11.5,
|
|
longitude: 43.1,
|
|
owner: "PEACE Cable",
|
|
});
|
|
expect(cable.entity_type).toBe("cable");
|
|
expect(cable.entity_id).toBe("c-peace");
|
|
expect(cable.label).toBe("PEACE");
|
|
expect(cable.latitude).toBe(11.5);
|
|
expect(cable.longitude).toBe(43.1);
|
|
|
|
const customPoint = normalizeEarthTarget("earth_interactable", {
|
|
id: "poi-99",
|
|
label: "ALMA Observatory",
|
|
layer: "science",
|
|
kind: "observatory",
|
|
lat: -23.02,
|
|
lon: -67.75,
|
|
});
|
|
expect(customPoint.entity_type).toBe("earth_interactable");
|
|
expect(customPoint.kind).toBe("observatory");
|
|
expect(customPoint.layer).toBe("science");
|
|
expect(customPoint.latitude).toBe(-23.02);
|
|
});
|
|
|
|
it("matches bound workflows by surface, entity type, interactable kind, target count, and priority", () => {
|
|
const catalog = [
|
|
{
|
|
workflow_id: "default.general",
|
|
name: "默认通用",
|
|
is_builtin: true,
|
|
bindings: {
|
|
enabled: true,
|
|
surfaces: ["info_card", "multi_select"],
|
|
entity_types: ["cable", "vessel", "earth_interactable"],
|
|
interactable_kinds: [],
|
|
min_targets: 1,
|
|
max_targets: 5,
|
|
priority: 0,
|
|
},
|
|
},
|
|
{
|
|
workflow_id: "custom.observatory_only",
|
|
name: "天文台专项",
|
|
is_builtin: false,
|
|
bindings: {
|
|
enabled: true,
|
|
surfaces: ["info_card"],
|
|
entity_types: ["earth_interactable"],
|
|
interactable_kinds: ["observatory"],
|
|
min_targets: 1,
|
|
max_targets: 2,
|
|
priority: 30,
|
|
},
|
|
},
|
|
{
|
|
workflow_id: "custom.cable_vessel_compare",
|
|
name: "海缆+船舶联合排查",
|
|
is_builtin: false,
|
|
bindings: {
|
|
enabled: true,
|
|
surfaces: ["multi_select"],
|
|
entity_types: ["cable", "vessel"],
|
|
interactable_kinds: [],
|
|
min_targets: 2,
|
|
max_targets: 5,
|
|
priority: 50,
|
|
},
|
|
},
|
|
];
|
|
|
|
const obs = normalizeEarthTarget("earth_interactable", {
|
|
id: "obs-1",
|
|
label: "ALMA",
|
|
kind: "observatory",
|
|
});
|
|
const obsMatches = matchWorkflowBindings({ surface: "info_card", targets: [obs], catalog });
|
|
expect(obsMatches.map((w) => w.workflow_id)).toEqual([
|
|
"custom.observatory_only",
|
|
"default.general",
|
|
]);
|
|
|
|
const portPoi = normalizeEarthTarget("earth_interactable", {
|
|
id: "port-1",
|
|
label: "Suez",
|
|
kind: "port",
|
|
});
|
|
const portMatches = matchWorkflowBindings({ surface: "info_card", targets: [portPoi], catalog });
|
|
expect(portMatches.map((w) => w.workflow_id)).toEqual(["default.general"]);
|
|
|
|
const multiMatches = matchWorkflowBindings({
|
|
surface: "multi_select",
|
|
targets: [
|
|
normalizeEarthTarget("cable", { id: "c1", name: "2Africa" }),
|
|
normalizeEarthTarget("vessel", { mmsi: "123456", name: "MV Test" }),
|
|
],
|
|
catalog,
|
|
});
|
|
expect(multiMatches.map((w) => w.workflow_id)).toEqual([
|
|
"custom.cable_vessel_compare",
|
|
"default.general",
|
|
]);
|
|
});
|
|
|
|
it("renders prompt templates safely without throwing on missing fields", () => {
|
|
const out = renderBindingPromptTemplate(
|
|
"检查 {{targets_summary}},重点对象={{target.label}} ({{target.lat}})",
|
|
[{ entity_type: "cable", entity_id: "c1", label: "PEACE", latitude: 10.5, longitude: 20.25 }],
|
|
"默认问题",
|
|
);
|
|
expect(out).toContain("cable「PEACE」(10.50, 20.25)");
|
|
expect(out).toContain("重点对象=PEACE (10.5000)");
|
|
});
|
|
|
|
it("deduplicates and removes compare tray targets in memory", () => {
|
|
const origDoc = globalThis.document;
|
|
globalThis.document = undefined;
|
|
try {
|
|
resetCompareTargetsForTest();
|
|
const t1 = normalizeEarthTarget("cable", { id: "c1", name: "PEACE" });
|
|
const t2 = normalizeEarthTarget("vessel", { mmsi: "999", name: "Ship A" });
|
|
addTargetToCompare(t1);
|
|
addTargetToCompare(t1);
|
|
addTargetToCompare(t2);
|
|
expect(getCompareTargets()).toHaveLength(2);
|
|
removeTargetFromCompare("cable:c1");
|
|
expect(getCompareTargets()).toHaveLength(1);
|
|
clearCompareTargets();
|
|
expect(getCompareTargets()).toHaveLength(0);
|
|
} finally {
|
|
globalThis.document = origDoc;
|
|
}
|
|
});
|
|
});
|