103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
import { afterEach, expect, it } from "vitest";
|
||
import { 浏览器缓冲 } from "../../src/功能/正文写作/编辑缓冲";
|
||
import { 创建编辑会话 } from "../../src/功能/正文写作/编辑会话";
|
||
import type { 正文草稿, 正文记录 } from "../../src/功能/正文写作/数据";
|
||
|
||
afterEach(() => localStorage.clear());
|
||
|
||
it("NC-editor-buffer-late-owner:旧会话迟到成功不清除重开后的新稿", async () => {
|
||
const scope = {
|
||
author_id: "author",
|
||
work_id: "work",
|
||
chapter_id: "a",
|
||
branch_id: "main",
|
||
};
|
||
const draft = (text: string): 正文草稿 => ({
|
||
format_version: 1,
|
||
paragraphs: [
|
||
{ paragraph_id: "p1", content: [{ type: "text", text, marks: [] }] },
|
||
],
|
||
});
|
||
const record: 正文记录 = {
|
||
...scope,
|
||
document_id: "doc",
|
||
revision: 1,
|
||
state: "saved",
|
||
document: draft("原稿"),
|
||
visible_text: "原稿",
|
||
document_hash: "h1",
|
||
visible_text_hash: "t1",
|
||
};
|
||
let finish!: (v: {
|
||
revision: number;
|
||
document_hash: string;
|
||
receipt_id: string;
|
||
}) => void;
|
||
const old = 创建编辑会话(
|
||
scope,
|
||
record,
|
||
() =>
|
||
new Promise((r) => {
|
||
finish = r;
|
||
}),
|
||
浏览器缓冲(scope),
|
||
);
|
||
old.change(draft("旧会话提交"));
|
||
const request = old.save();
|
||
old.dispose();
|
||
const next = 创建编辑会话(
|
||
scope,
|
||
record,
|
||
async () => ({ revision: 2, document_hash: "h2", receipt_id: "r2" }),
|
||
浏览器缓冲(scope),
|
||
浏览器缓冲(scope).read(),
|
||
);
|
||
next.change(draft("重开后新增文字"));
|
||
finish({ revision: 2, document_hash: "h2", receipt_id: "r2" });
|
||
await request;
|
||
expect(浏览器缓冲(scope).read()?.draft).toEqual(draft("重开后新增文字"));
|
||
});
|
||
it("NC-editor-buffer-scope:刷新读取私人缓冲且切章不混用基线", () => {
|
||
const a = {
|
||
author_id: "author",
|
||
work_id: "work",
|
||
chapter_id: "a",
|
||
branch_id: "main",
|
||
};
|
||
const b = { ...a, chapter_id: "b" };
|
||
const bufferA = 浏览器缓冲(a);
|
||
const draft = {
|
||
format_version: 1,
|
||
paragraphs: [{ paragraph_id: "p", content: [] }],
|
||
};
|
||
bufferA.write({
|
||
scope: a,
|
||
base_revision: 3,
|
||
base_hash: "h3",
|
||
draft,
|
||
updated_at: 1,
|
||
});
|
||
浏览器缓冲(b).write({
|
||
scope: b,
|
||
base_revision: 8,
|
||
base_hash: "h8",
|
||
draft,
|
||
updated_at: 2,
|
||
});
|
||
expect(浏览器缓冲(a).read()?.base_revision).toBe(3);
|
||
expect(浏览器缓冲({ ...a, author_id: "another" }).read()).toBeUndefined();
|
||
bufferA.write({
|
||
scope: a,
|
||
base_revision: 4,
|
||
base_hash: "h4",
|
||
draft,
|
||
updated_at: 3,
|
||
});
|
||
expect(浏览器缓冲(a).read()?.base_revision).toBe(4);
|
||
expect(localStorage.length).toBe(2);
|
||
bufferA.clear();
|
||
bufferA.write({scope:a,base_revision:5,base_hash:"h5",draft,updated_at:4});
|
||
expect(bufferA.read()?.base_revision).toBe(5);
|
||
expect(浏览器缓冲(b).read()?.base_revision).toBe(8);
|
||
});
|