Files
jiapuapp/tests/g12-generation-poem-unicode-contract-runtime-smoke.js
T
2026-07-23 17:21:33 +08:00

74 lines
2.1 KiB
JavaScript

const assert = require("node:assert/strict");
// This is an adversarial executable check for the OpenAPI text contract only.
// It is not the production G12 normalizer or coordinator.
const boundaryAndControlPattern = /^(?!.*[\u0000-\u001F\u007F-\u009F\u061C\u200B-\u200F\u2028-\u202E\u2060\u2066-\u2069\uFEFF])\S(?:[\s\S]*\S)?$/u;
function hasWellFormedUtf16(value) {
for (let index = 0; index < value.length; index += 1) {
const unit = value.charCodeAt(index);
if (unit >= 0xd800 && unit <= 0xdbff) {
const next = value.charCodeAt(index + 1);
if (!(next >= 0xdc00 && next <= 0xdfff)) return false;
index += 1;
continue;
}
if (unit >= 0xdc00 && unit <= 0xdfff) return false;
}
return true;
}
function validatesDeclaredGenerationText(value) {
return (
typeof value === "string" &&
hasWellFormedUtf16(value) &&
value === value.normalize("NFC") &&
[...value].length >= 1 &&
[...value].length <= 50 &&
boundaryAndControlPattern.test(value)
);
}
const hanDe = "\u5FB7";
const hanCheng = "\u627F";
const supplementary = String.fromCodePoint(0x20000);
for (const valid of [
`${hanDe}${hanCheng}`,
`${hanDe}${hanDe}`,
`${supplementary}${hanCheng}`,
supplementary.repeat(50),
]) {
assert.equal(validatesDeclaredGenerationText(valid), true, `valid sample rejected: ${JSON.stringify(valid)}`);
}
for (const invalid of [
"",
supplementary.repeat(51),
"e\u0301",
"\ud800",
"\udc00",
` ${hanDe}`,
`${hanDe} `,
`\u00A0${hanDe}`,
`${hanDe}\u00A0`,
`${hanDe}\t${hanCheng}`,
`${hanDe}\r${hanCheng}`,
`${hanDe}\n${hanCheng}`,
`${hanDe}\u0085${hanCheng}`,
`${hanDe}\u061C${hanCheng}`,
`${hanDe}\u200B${hanCheng}`,
`${hanDe}\u200E${hanCheng}`,
`${hanDe}\u200F${hanCheng}`,
`${hanDe}\u2028${hanCheng}`,
`${hanDe}\u2029${hanCheng}`,
`${hanDe}\u202E${hanCheng}`,
`${hanDe}\u2060${hanCheng}`,
`${hanDe}\u2066${hanCheng}`,
`${hanDe}\uFEFF${hanCheng}`,
]) {
assert.equal(validatesDeclaredGenerationText(invalid), false, `invalid sample accepted: ${JSON.stringify(invalid)}`);
}
console.log("G12-GENERATION-POEM-UNICODE-RUNTIME-SMOKE PASS");