164 lines
6.2 KiB
JavaScript
164 lines
6.2 KiB
JavaScript
import { test } from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import { buildAnalysis, numericValue } from "../shared/requirements.mjs";
|
||
import {
|
||
empty,
|
||
keyword,
|
||
constraint,
|
||
sampleAnalysis,
|
||
sampleText,
|
||
} from "./analysis-fixtures.mjs";
|
||
const run = (raw, text = sampleText) =>
|
||
buildAnalysis(raw, [{ role: "user", text }]);
|
||
test("canonical sample generates the five backend fields and scope-specific numbers", () => {
|
||
const { request, analysis } = run(sampleAnalysis());
|
||
assert.deepEqual(Object.keys(request), [
|
||
"userInput",
|
||
"requirements",
|
||
"requiredParts",
|
||
"robotRequirements",
|
||
"partRequirements",
|
||
]);
|
||
assert.deepEqual(request.robotRequirements, {
|
||
degreesOfFreedom: 20,
|
||
functionDescription: "跑步",
|
||
massKg: 10,
|
||
operators: { massKg: "lte" },
|
||
});
|
||
assert.deepEqual(request.partRequirements, [
|
||
{ category: "TORSO_0", material: "铝合金" },
|
||
{ category: "ARM_R", torqueNm: 20, operators: { torqueNm: "gte" } },
|
||
]);
|
||
assert.equal(request.requiredParts.length, 11);
|
||
assert.equal(analysis.canSubmit, true);
|
||
});
|
||
test("bird soft compatibility retains flight and bird intent without adding hard flight fields", () => {
|
||
const text = "我想要一个仿生鸟类的机器";
|
||
const raw = {
|
||
...empty(),
|
||
keywords: [
|
||
keyword("仿生", text),
|
||
keyword("鸟类", text),
|
||
keyword("飞行", text, "inferred"),
|
||
],
|
||
robotTypes: [
|
||
{
|
||
type: "biped",
|
||
source: "inferred",
|
||
evidence: text,
|
||
reason: "鸟类通常双足",
|
||
},
|
||
],
|
||
unmodeled: [
|
||
{ text: "鸟类飞行能力", evidence: text, reason: "候选模板不保证飞行" },
|
||
],
|
||
};
|
||
const { request, analysis } = run(raw, text);
|
||
for (const term of ["鸟类", "飞行", "双足", "鸟类飞行能力"])
|
||
assert.ok(request.requirements.includes(term));
|
||
assert.deepEqual(request.robotRequirements, {});
|
||
assert.equal(
|
||
analysis.keywords.find((k) => k.term === "双足").source,
|
||
"inferred",
|
||
);
|
||
assert.ok(analysis.warnings.length);
|
||
assert.equal(request.requiredParts.length, 11);
|
||
});
|
||
test("explicit exclusions override templates and conflicting constraints block matching", () => {
|
||
const text = sampleText + ",不要左臂";
|
||
const raw = sampleAnalysis();
|
||
raw.excludedCategories = [{ category: "ARM_L", evidence: "不要左臂" }];
|
||
assert.ok(!run(raw, text).request.requiredParts.includes("ARM_L"));
|
||
raw.constraints.push(
|
||
constraint("massKg", "10kg", "质量不超过10kg", "lte", ["ARM_L"]),
|
||
);
|
||
assert.equal(run(raw, text).analysis.canSubmit, false);
|
||
});
|
||
test("unit conversion preserves zero, Chinese numbers, grams and N·cm", () => {
|
||
for (const [field, value, result] of [
|
||
["massKg", "500克", 0.5],
|
||
["massKg", "二点五公斤", 2.5],
|
||
["degreesOfFreedom", "二十", 20],
|
||
["degreesOfFreedom", "0", 0],
|
||
["torqueNm", "200N·cm", 2],
|
||
])
|
||
assert.equal(numericValue(field, value), result);
|
||
for (const [field, value] of [
|
||
["massKg", "10"],
|
||
["torqueNm", "-1N·m"],
|
||
["degreesOfFreedom", "1.5"],
|
||
["massKg", "一百二公斤"],
|
||
])
|
||
assert.throws(() => numericValue(field, value));
|
||
const text =
|
||
"双足机器人整体二十自由度,质量不超过五百克,右臂扭矩至少200N·cm";
|
||
const raw = sampleAnalysis();
|
||
raw.keywords = [keyword("双足", text)];
|
||
raw.robotTypes[0].evidence = "双足机器人";
|
||
raw.partCategories = [];
|
||
raw.constraints = [
|
||
constraint("degreesOfFreedom", "二十", "整体二十自由度"),
|
||
constraint("massKg", "五百克", "质量不超过五百克", "lte"),
|
||
constraint("torqueNm", "200N·cm", "右臂扭矩至少200N·cm", "gte", ["ARM_R"]),
|
||
];
|
||
const result = run(raw, text);
|
||
assert.equal(result.analysis.canSubmit, true);
|
||
assert.equal(result.request.robotRequirements.massKg, 0.5);
|
||
assert.equal(result.request.partRequirements[0].torqueNm, 2);
|
||
});
|
||
test("conflicting intervals and unsupported strict comparisons remain explicit, never approximate", () => {
|
||
const text = sampleText + ",质量至少12kg";
|
||
const raw = sampleAnalysis();
|
||
raw.constraints.push(constraint("massKg", "12kg", "质量至少12kg", "gte"));
|
||
const result = run(raw, text);
|
||
assert.equal(result.analysis.canSubmit, false);
|
||
assert.equal(result.request.robotRequirements.massKg, undefined);
|
||
assert.ok(result.analysis.unmodeled.length);
|
||
raw.constraints = [constraint("massKg", "10kg", "质量不超过10kg", "lt")];
|
||
assert.equal(run(raw).analysis.canSubmit, false);
|
||
raw.constraints = [
|
||
constraint("massKg", "10kg", "质量不超过10kg", "range", [], "10kg"),
|
||
];
|
||
assert.equal(run(raw).request.robotRequirements.massKg, 10);
|
||
const bounds = sampleAnalysis();
|
||
bounds.constraints = [
|
||
constraint("massKg", "5kg", "5kg到10kg", "range", [], "10kg"),
|
||
];
|
||
assert.equal(
|
||
run(bounds, sampleText + ",5kg到10kg").analysis.canSubmit,
|
||
false,
|
||
);
|
||
});
|
||
test("fabricated evidence, invented categories, coordinate fields and invented numeric values cannot pass", () => {
|
||
const raw = sampleAnalysis();
|
||
raw.keywords[0].evidence = "用户没说的";
|
||
assert.throws(() => run(raw), /证据/);
|
||
const wrong = sampleAnalysis();
|
||
wrong.partCategories[0].category = "wing_l";
|
||
assert.throws(() => run(wrong));
|
||
const coords = sampleAnalysis();
|
||
coords.xyz = [1, 2, 3];
|
||
assert.throws(() => run(coords), /允许/);
|
||
const numeric = sampleAnalysis();
|
||
numeric.constraints[0].value = "42";
|
||
const result = run(numeric);
|
||
assert.equal(result.analysis.canSubmit, false);
|
||
assert.equal(result.request.robotRequirements.degreesOfFreedom, undefined);
|
||
});
|
||
test("unknown types stay unresolved and latest-turn values use full verbatim user history", () => {
|
||
const raw = empty();
|
||
const text = "我要四足履带机器";
|
||
raw.keywords = [keyword("四足", text)];
|
||
assert.equal(run(raw, text).analysis.canSubmit, false);
|
||
const next = sampleAnalysis();
|
||
next.constraints = next.constraints.filter((c) => c.field !== "massKg");
|
||
next.constraints.push(constraint("massKg", "5kg", "改成5kg以内", "lte"));
|
||
const result = buildAnalysis(next, [
|
||
{ role: "user", text: sampleText },
|
||
{ role: "assistant", text: "旧值10kg" },
|
||
{ role: "user", text: "改成5kg以内" },
|
||
]);
|
||
assert.equal(result.request.robotRequirements.massKg, 5);
|
||
assert.equal(result.request.userInput, sampleText + "\n改成5kg以内");
|
||
});
|