test(danmaku): lock 8-segment p contract and parse tolerance
This commit is contained in:
parent
517b4b1ef6
commit
b80b1e1fbf
|
|
@ -4,12 +4,51 @@ import { openCommentsToXml, parseDanmakuXml, serializeDanmakuXml } from "./xml.j
|
|||
describe("xml round-trip", () => {
|
||||
it("parse then serialize preserves time/mode/text", () => {
|
||||
const xml = openCommentsToXml([{ p: "1.5,1,25,16777215,0,0,u,1", m: "你好<hello>" }]);
|
||||
expect(xml).toContain('p="1.5,1,25,16777215,0,0,u,1"');
|
||||
const list = parseDanmakuXml(xml);
|
||||
expect(list).toHaveLength(1);
|
||||
expect(list[0]?.time).toBeCloseTo(1.5);
|
||||
expect(list[0]?.mode).toBe(1);
|
||||
expect(list[0]?.text).toBe("你好<hello>");
|
||||
const again = parseDanmakuXml(serializeDanmakuXml(list));
|
||||
expect(again[0]?.time).toBeCloseTo(1.5);
|
||||
expect(again[0]?.mode).toBe(1);
|
||||
expect(again[0]?.size).toBe(25);
|
||||
expect(again[0]?.color).toBe(16777215);
|
||||
expect(again[0]?.uid).toBe("u");
|
||||
expect(again[0]?.rowId).toBe("1");
|
||||
expect(again[0]?.text).toBe("你好<hello>");
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseDanmakuXml tolerance", () => {
|
||||
it("4-segment p falls back size/color/uid/rowId to defaults", () => {
|
||||
const list = parseDanmakuXml('<?xml version="1.0"?><i><d p="12.5,1,0,0">x</d></i>');
|
||||
expect(list).toHaveLength(1);
|
||||
expect(list[0]?.time).toBeCloseTo(12.5);
|
||||
expect(list[0]?.mode).toBe(1);
|
||||
expect(list[0]?.size).toBe(0);
|
||||
expect(list[0]?.color).toBe(0);
|
||||
expect(list[0]?.timestamp).toBe(0);
|
||||
expect(list[0]?.pool).toBe(0);
|
||||
expect(list[0]?.uid).toBe("");
|
||||
expect(list[0]?.rowId).toBe("");
|
||||
expect(list[0]?.text).toBe("x");
|
||||
});
|
||||
|
||||
it("shorter p defaults size/color to 0 and mode to 1", () => {
|
||||
const two = parseDanmakuXml('<?xml version="1.0"?><i><d p="1.5,1">y</d></i>');
|
||||
expect(two[0]?.time).toBeCloseTo(1.5);
|
||||
expect(two[0]?.mode).toBe(1);
|
||||
expect(two[0]?.size).toBe(0);
|
||||
expect(two[0]?.color).toBe(0);
|
||||
expect(two[0]?.uid).toBe("");
|
||||
expect(two[0]?.rowId).toBe("");
|
||||
|
||||
const one = parseDanmakuXml('<?xml version="1.0"?><i><d p="3">z</d></i>');
|
||||
expect(one[0]?.time).toBe(3);
|
||||
expect(one[0]?.mode).toBe(1);
|
||||
expect(one[0]?.size).toBe(0);
|
||||
expect(one[0]?.color).toBe(0);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue