Files
next-tjwater-agent/tests/frontendAction/command.test.ts
T
jiang 109abee0ca
Generic Container CI/CD / test-build-publish (push) Successful in 1m21s
Agent CI/CD / build-test-publish-and-deploy (push) Successful in 1m21s
fix(agent): describe typed historical comparison charts
Use time_comparison for current curves and historical point sets. Document shared time slots and aligned real sample timestamps; synchronize the generated chart contracts.
2026-11-08 03:04:14 +08:00

63 lines
4.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "bun:test";
import presentView from "../../.opencode/tools/present_view.js";
import { taskPublicationCommandSchema } from "../../.opencode/tools/task-publication-schema.js";
import { viewCommandSchema } from "../../src/frontendAction/command.js";
import { taskPresentationCommandSchema } from "../../src/product/task-presentation.js";
import { frontendActionResultSchema } from "../../src/frontendAction/coordinator.js";
const data = { type: "FeatureCollection", features: [{ type: "Feature", geometry: { type: "Point", coordinates: [121, 31] }, properties: { value: 0.4 } }] };
const chart = { title: "压力", chart_type: "line", x_data: ["10:00"], series: [{ name: "MP01", data: [0.4] }] };
const commands = [
{ action: "locate_features", layer: "pipes", feature_ids: ["P1"] },
{ action: "zoom_to_map", center: [121, 31], zoom: 14 },
{ action: "highlight_features", layer: "pipes", feature_ids: ["P1"], effect: "pulse" },
{ action: "clear_highlights" },
{ action: "open_chart", chart_id: "pressure", chart, evidence_refs: ["E1"] },
{ action: "close_chart", chart_id: "pressure" },
{ action: "set_overlay", overlay_id: "points", title: "证据", mode: "geometry", evidence_id: "E1", data },
{ action: "clear_overlay", overlay_id: "points" },
{ action: "render_scada_analysis", items: [{ device_id: "MP01", level: "high" }] },
{ action: "clear_scada_analysis" },
];
describe("unified view commands", () => {
it("uses identical native and host command definitions for every action", () => {
for (const command of commands) {
expect(presentView.args.command.parse(command)).toEqual(viewCommandSchema.parse(command));
expect(taskPublicationCommandSchema.parse(command)).toEqual(taskPresentationCommandSchema.parse(command));
}
});
it("requires resolved chat overlays and evidence-backed task charts", () => {
const { data: _data, ...unresolved } = commands[6];
expect(viewCommandSchema.safeParse(unresolved).success).toBeFalse();
expect(taskPresentationCommandSchema.safeParse(unresolved).success).toBeTrue();
expect(viewCommandSchema.safeParse({ ...commands[4], evidence_refs: [] }).success).toBeTrue();
expect(taskPresentationCommandSchema.safeParse({ ...commands[4], evidence_refs: [] }).success).toBeFalse();
});
it("passes aligned historical scatter points through native and publication contracts", () => {
const command = { action: "open_chart", chart_id: "pressure-comparison", evidence_refs: ["E1"], chart: {
title: "压力七天对比", chart_type: "time_comparison", y_axis_name: "压力(m)",
x_data: ["19:30", "19:45", "20:00"], series: [
{ name: "当前", type: "line", data: [32, null, 31],
sample_times: ["2026-06-14T19:30:00+08:00", null, "2026-06-14T20:00:00+08:00"] },
{ name: "前一天", type: "scatter", data: [null, 32, 33],
sample_times: [null, "2026-06-13T19:45:00+08:00", "2026-06-13T20:00:00+08:00"] },
],
} };
for (const schema of [presentView.args.command, viewCommandSchema, taskPublicationCommandSchema, taskPresentationCommandSchema]) {
expect(schema.parse(command)).toEqual(command);
for (const sample_times of [[null], ["2026-06-13T19:30:00+08:00", null, null]]) {
expect(schema.safeParse({ ...command, chart: { ...command.chart, series: [{ ...command.chart.series[1], sample_times }] } }).success).toBeFalse();
}
}
});
it("rejects old envelopes, unknown fields, scripts, nonfinite numbers and old receipts", () => {
for (const command of [
{ name: "locate_features", params: { layer: "pipes", feature_ids: ["P1"] } },
{ action: "show_chart", ...chart },
{ ...commands[1], zoom: Infinity },
{ ...commands[6], script: "alert(1)" },
]) expect(viewCommandSchema.safeParse(command).success).toBeFalse();
expect(frontendActionResultSchema.safeParse({ version: "frontend-action-result@1", actionId: "a", status: "succeeded", completedAt: Date.now() }).success).toBeFalse();
});
});