5dc709865a
- DigitalMessageText.vue: 为角色预览与聊天记录提供统一的受限文本展示 - content.vue: 增加首次问候、备用开场、示例对白的实时预览与格式说明 - users.vue: 按相同规则展示角色聊天记录,保留用户纯文本 - digital-message-format.ts、tests、README.md: 同步前台格式实现、兼容用例与目录说明
108 lines
3.8 KiB
JavaScript
108 lines
3.8 KiB
JavaScript
/** 对白、叙述、旧消息与累计流式文本的格式回归,不依赖浏览器或真实模型。 */
|
||
import test from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import { parseDigitalMessage as parse } from "../src/utils/digital-message-format.ts";
|
||
|
||
test("标准消息按剧情顺序拆分叙述、对白与心理,隐藏星号并保留引号", () => {
|
||
assert.deepEqual(parse("*她合上书。*\n\n“你来了。”\n\n*她终于放松下来。*"), [
|
||
{ kind: "narration", text: "她合上书。" },
|
||
{ kind: "dialogue", text: "“你来了。”" },
|
||
{ kind: "narration", text: "她终于放松下来。" }
|
||
]);
|
||
});
|
||
|
||
test("旧消息的成对括号与括号外对白兼容,内层括号不会提前关闭叙述", () => {
|
||
assert.deepEqual(
|
||
parse(
|
||
"(她抱起书(封面有些旧),笑了笑)你来了。\n(她心里有些高兴)坐吧。"
|
||
),
|
||
[
|
||
{ kind: "narration", text: "她抱起书(封面有些旧),笑了笑" },
|
||
{ kind: "dialogue", text: "你来了。" },
|
||
{ kind: "narration", text: "她心里有些高兴" },
|
||
{ kind: "dialogue", text: "坐吧。" }
|
||
]
|
||
);
|
||
});
|
||
|
||
test("旧开场白的无标记叙述与引号对白分别展示,英文撇号不作为对白", () => {
|
||
assert.deepEqual(
|
||
parse('She picked up her book.\n"You’re early. Don\'t wait outside."'),
|
||
[
|
||
{ kind: "narration", text: "She picked up her book." },
|
||
{ kind: "dialogue", text: '"You’re early. Don\'t wait outside."' }
|
||
]
|
||
);
|
||
for (const [open, close] of [
|
||
["“", "”"],
|
||
["「", "」"],
|
||
["『", "』"],
|
||
["«", "»"],
|
||
[""", """]
|
||
]) {
|
||
assert.deepEqual(parse(`${open}你好${close}`), [
|
||
{ kind: "dialogue", text: `${open}你好${close}` }
|
||
]);
|
||
}
|
||
});
|
||
|
||
test("纯文本、普通备注、乘法与不完整的历史标记保留原文", () => {
|
||
for (const text of [
|
||
"普通文本",
|
||
"说明(选填)",
|
||
"3 * 4 = 12",
|
||
"file*name.txt",
|
||
"*未闭合",
|
||
"(未闭合"
|
||
]) {
|
||
assert.deepEqual(parse(text), [{ kind: "plain", text }]);
|
||
}
|
||
assert.deepEqual(parse(" \n\r\n"), []);
|
||
assert.deepEqual(parse("第一段\r\n\r\n第二段"), [
|
||
{ kind: "plain", text: "第一段" },
|
||
{ kind: "plain", text: "第二段" }
|
||
]);
|
||
});
|
||
|
||
test("显式叙述里的引号保持为故事文本,不再二次推断", () => {
|
||
assert.deepEqual(parse("*她想到“明天”这个词,轻轻笑了。*"), [
|
||
{ kind: "narration", text: "她想到“明天”这个词,轻轻笑了。" }
|
||
]);
|
||
assert.deepEqual(parse("**“你好。”**"), [
|
||
{ kind: "dialogue", text: "“你好。”" }
|
||
]);
|
||
assert.deepEqual(parse("\\*字面星号\\*"), [
|
||
{ kind: "plain", text: "*字面星号*" }
|
||
]);
|
||
});
|
||
|
||
test("累计流式输入的每个字符前缀都隐藏未闭合星号,完成态排版相同", () => {
|
||
const source = "*她推开门。*\n\n“你来了。”\n\n*她松了口气。*";
|
||
for (let end = 1; end <= source.length; end++) {
|
||
const blocks = parse(source.slice(0, end), true);
|
||
assert.equal(
|
||
blocks.some(block => block.text.includes("*")),
|
||
false,
|
||
`前缀 ${end}`
|
||
);
|
||
}
|
||
assert.deepEqual(parse(source, true), parse(source));
|
||
assert.deepEqual(parse("*她推开门", true), [
|
||
{ kind: "narration", text: "她推开门" }
|
||
]);
|
||
assert.deepEqual(parse("(她推开门", true), [
|
||
{ kind: "narration", text: "她推开门" }
|
||
]);
|
||
assert.deepEqual(parse("“你来了", true), [
|
||
{ kind: "dialogue", text: "“你来了" }
|
||
]);
|
||
assert.deepEqual(parse("**Hello", true), [
|
||
{ kind: "dialogue", text: "Hello" }
|
||
]);
|
||
});
|
||
|
||
test("HTML 与脚本内容只作为文本保留,格式解析不会生成任何标签", () => {
|
||
const text = "<script>globalThis.injected = true</script>";
|
||
assert.deepEqual(parse(text), [{ kind: "plain", text }]);
|
||
});
|