Files
Docwell/Packages/editor/src/suggest.test.ts
T
tajniak81andClaude Opus 5.5 c98b127f1e
CI / go (Media) (push) Successful in 13m40s
CI / go (Server) (push) Failing after 23m20s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 10m35s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / desktop (ubuntu-latest) (push) Failing after 3h4m42s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / mobile (push) Failing after 9m19s
CI / docker (push) Successful in 2h2m57s
Add columns, suggesting mode, verified pages, scanned PDF search and server translations
- Columns: /columns puts 2 to 4 columns side by side, with widths dragged
  between them; they stack on phones. HTML export and import keep them
  (also Notion column lists and Confluence page layouts).
- Suggesting mode: edits become suggested insertions and deletions that
  editors accept or reject one by one or all at once. Text only.
- Verified pages: editors verify a page for a while or without end; the
  verifier is notified when it runs out (migration 1764700000).
- Scanned PDFs attached to pages are read by AI for search where a
  workspace turns it on.
- The server writes error messages, emails, digests and phone
  notifications in English, Polish, German, French and Spanish; the apps
  send their language with each request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 08:38:08 +01:00

138 lines
5.0 KiB
TypeScript

// SPDX-License-Identifier: AGPL-3.0-or-later
import { Editor } from "@tiptap/core";
import { afterEach, describe, expect, it } from "vitest";
import { docwellExtensions } from "./extensions";
import { suggestionsOf } from "./suggest";
let editor: Editor | undefined;
afterEach(() => editor?.destroy());
const ana = { id: "ana", name: "Ana" };
const ben = { id: "ben", name: "Ben" };
function setup(html: string) {
editor = new Editor({ extensions: docwellExtensions({}), content: html });
return editor;
}
/** The first paragraph's text with suggestions shown as [+ins] and [-del]. */
function shown(e: Editor): string {
let out = "";
e.state.doc.firstChild!.forEach((n) => {
const t = n.text ?? "";
const ins = n.marks.some((m) => m.type.name === "suggestInsert");
const del = n.marks.some((m) => m.type.name === "suggestDelete");
out += ins && del ? `[+-${t}]` : ins ? `[+${t}]` : del ? `[-${t}]` : t;
});
return out;
}
/** Types like the keyboard: one transaction per call, at the caret. */
const type = (e: Editor, text: string) => e.view.dispatch(e.state.tr.insertText(text));
const backspace = (e: Editor) => {
const { from } = e.state.selection;
e.view.dispatch(e.state.tr.delete(from - 1, from));
};
describe("suggesting mode", () => {
it("marks typed text as a suggestion and keeps adjacent typing in one", () => {
const e = setup("<p>Hello world</p>");
e.commands.setSuggesting(ana);
e.commands.setTextSelection(6);
type(e, ",");
type(e, " dear");
expect(shown(e)).toBe("Hello[+, dear] world");
expect(suggestionsOf(e.state.doc)).toHaveLength(1);
expect(suggestionsOf(e.state.doc)[0]).toMatchObject({ author: "ana", name: "Ana", inserted: ", dear" });
});
it("keeps deleted text struck through, and Backspace walks left over it", () => {
const e = setup("<p>Hello world</p>");
e.commands.setSuggesting(ana);
e.commands.setTextSelection(12);
backspace(e);
backspace(e);
expect(shown(e)).toBe("Hello wor[-ld]");
expect(e.state.selection.from).toBe(10);
expect(suggestionsOf(e.state.doc)).toHaveLength(1);
});
it("replaces a selection with a deletion followed by an insertion", () => {
const e = setup("<p>Hello world</p>");
e.commands.setSuggesting(ana);
e.commands.setTextSelection({ from: 7, to: 12 });
e.view.dispatch(e.state.tr.insertText("there"));
expect(shown(e)).toBe("Hello [-world][+there]");
expect(e.state.selection.from).toBe(17);
});
it("really deletes the author's own suggested text, not someone else's", () => {
const e = setup("<p>Hi</p>");
e.commands.setSuggesting(ana);
e.commands.setTextSelection(3);
type(e, "!!");
backspace(e);
expect(shown(e)).toBe("Hi[+!]");
e.commands.setSuggesting(ben);
backspace(e);
expect(shown(e)).toBe("Hi[+-!]");
});
it("accepts and rejects suggestions", () => {
const e = setup("<p>Hello world</p>");
e.commands.setSuggesting(ana);
e.commands.setTextSelection({ from: 7, to: 12 });
e.view.dispatch(e.state.tr.insertText("there"));
const [s] = suggestionsOf(e.state.doc);
// Same id for the deletion and the insertion of one replacement? Two suggestions are fine too.
const all = suggestionsOf(e.state.doc);
expect(all.map((x) => x.deleted + "|" + x.inserted).join(",")).toContain("world");
e.commands.setSuggesting(null);
expect(e.commands.acceptAllSuggestions()).toBe(true);
expect(shown(e)).toBe("Hello there");
expect(s).toBeDefined();
const f = setup("<p>Hello world</p>");
f.commands.setSuggesting(ana);
f.commands.setTextSelection({ from: 7, to: 12 });
f.view.dispatch(f.state.tr.insertText("there"));
f.commands.rejectAllSuggestions();
expect(shown(f)).toBe("Hello world");
});
it("accepts one suggestion by id and leaves the others", () => {
const e = setup("<p>one two</p>");
e.commands.setSuggesting(ana);
e.commands.setTextSelection(4);
type(e, "A");
e.commands.setTextSelection(9);
type(e, "B");
const [a, b] = suggestionsOf(e.state.doc);
e.commands.rejectSuggestion(a!.id);
expect(shown(e)).toBe("one two[+B]");
e.commands.acceptSuggestion(b!.id);
expect(shown(e)).toBe("one twoB");
});
it("edits directly when suggesting is off, and lets paragraphs join", () => {
const e = setup("<p>Hi</p><p>there</p>");
type(e, "x");
expect(shown(e)).toBe("xHi");
e.commands.setSuggesting(ana);
// Backspace at the start of the second paragraph joins them: structure is not tracked.
e.commands.setTextSelection(6);
e.commands.joinBackward();
expect(e.state.doc.firstChild!.textContent).toBe("xHithere");
});
it("reads suggestions back from HTML", () => {
const e = setup(
'<p>a<ins data-suggestion="s1" data-author="ana" data-name="Ana" data-at="5">b</ins><del data-suggestion="s2" data-author="ana">c</del></p>',
);
expect(shown(e)).toBe("a[+b][-c]");
expect(suggestionsOf(e.state.doc).map((s) => s.id)).toEqual(["s1", "s2"]);
expect(e.getHTML()).toContain('<ins class="dw-suggest-ins" data-suggestion="s1"');
});
});