mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 11:01:11 +00:00
* feat(desktop): read XML with a plain DOCTYPE, and check an element's whole text - Both readers accept a DOCTYPE with only a name and an optional PUBLIC or SYSTEM identifier, record it as the view's doctype, and never fetch the DTD or expand entities beyond XML's five; an internal subset, an external entity and a parameter entity stay refused alike. - text_equals and text_contains compare the concatenated text of the element (or text node or attribute) a pointer reaches, as XPath string() does, with optional normalize_whitespace; both evaluators agree, and a text check on a truncated element is undecided. - Whitespace-only text between elements is dropped only when the element holds no other text, so text split into runs keeps its spaces. - The draft suggests text_equals for an element whose text changed. So an AbiWord .abw reads as XML, and its paragraph's text is checked whole however AbiWord splits it into <c> runs, as Writer's spans are. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(desktop): draft whole-text checks on the paragraph, read only text as text, and skip asctime stamps - A changed value is read at both ends, so text that became a run still marks its paragraph, and the drafted text_equals moves up out of mixed content to the nearest ancestor named by an attribute, such as AbiWord's p by its xid. - Text checks accept only a text node or an attribute value; element names and doctype fields fail, in both evaluators. - isTimestamp also recognises asctime dates (Www Mmm dd hh:mm:ss yyyy). - XML files are drafted with the keyed diff the archive entries use, so no suggestion reaches an element by position; the shared logic moves to appTaskDraftNamed.ts. - Text-check tests assert facts, not whole sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(desktop): keep checks on a changed attribute inside a paragraph whose text check replaces its runs' A drafted whole-text check on a paragraph hid every change inside it, so a link whose label and href both changed drafted only the paragraph's text and the new destination went unchecked. The text check now covers text and runs added or removed whole; a changed name or attribute of an element both versions hold keeps its own check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(desktop): read an attribute called name or attributes as an attribute when the draft keeps its check The owner of `.../attributes/name` was taken as `.../attributes`, so a changed `name` attribute inside a paragraph with a text check was dropped again. The owner is now read by segment, attributes first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(desktop): draft text checks from text in runs added whole, and name a text element by its own id before its container Text typed into a run the diff lists whole was never read, so the draft kept only a presence check on the run, which wrong text also passed. Such text now marks the nearest element both ends hold, or, when none can take the check, the added element's own text. The climb also names an element by an identifier of its own (id, xml:id, xid) before a larger container, so a headline under a long layer keeps a whole-text check that survives re-split runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(desktop): expect the whole-text check where #8988's pairing test meets text checks On this branch a changed element's text is drafted as `text_equals` on the element, so the identity pairing test now expects that check, under the same `[@role=body]` key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(desktop): keep checks for elements without text beside a text edit, and normalize DOCTYPE system id line endings A review found two gaps. - A paragraph's whole-text check covered every element added or removed inside it, including ones holding no text: an image frame, a line break. A solver who typed the text but never inserted the image passed. Such an element now keeps its own presence or absence check. - The Python reader kept a DOCTYPE system literal's CRLF where TypeScript normalizes it to LF. Both now report LF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(desktop): import textSyntaxFormat once in the state format tests main's mixed-case JSON suffix fix and this branch both added the import, and the merge kept both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
329 lines
10 KiB
TypeScript
329 lines
10 KiB
TypeScript
import { readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
|
|
import type { DesktopApplicationCapture } from "../../domain/desktop/desktopCapture.js";
|
|
import { desktopApplicationStateSchema } from "../../domain/desktop/desktopState.js";
|
|
import {
|
|
odtArchive,
|
|
zipArchive,
|
|
} from "../../domain/desktop/zipArchive.fixture.js";
|
|
import { exportDesktopApplication } from "./DesktopApplicationExport.js";
|
|
import { inspectDesktopApplicationState } from "./DesktopApplicationState.js";
|
|
import { DESKTOP_OPERATION } from "./DesktopCaptureRefusals.js";
|
|
import { draftDesktopApplicationTask } from "./DesktopTaskDraft.js";
|
|
import {
|
|
HOME,
|
|
capture,
|
|
change,
|
|
readers,
|
|
retained,
|
|
writeCaptureFolder,
|
|
} from "./desktopCapture.fixture.js";
|
|
|
|
const FREECAD = join(
|
|
import.meta.dirname,
|
|
"../../../tests/fixtures/freecad/1.0",
|
|
);
|
|
const PLATE = "Documents/plate.FCStd";
|
|
const LIST = "Documents/list.odt";
|
|
/** The list's paragraphs, by their prefixed element names. */
|
|
const LIST_TEXT =
|
|
"/entries/[name=content.xml]/view/root/children/[name=office:body]/children/[name=office:text]/children";
|
|
|
|
/** FreeCAD's save of the plate, before and after the user cut the hole. */
|
|
const plate = async (end: "before" | "after") =>
|
|
zipArchive([
|
|
{
|
|
name: "Document.xml",
|
|
data: await readFile(join(FREECAD, end, "Document.xml")),
|
|
},
|
|
{ name: "thumbnails/Thumbnail.png", data: Buffer.from([0x89, 0x50, 0]) },
|
|
]);
|
|
|
|
/** The notes capture with a FreeCAD document and an OpenDocument list the user also edited, both named in window titles. */
|
|
const archiveCapture = async (
|
|
list: readonly [Buffer, Buffer] = [
|
|
odtArchive("Buy bread"),
|
|
odtArchive("Buy milk"),
|
|
],
|
|
) => {
|
|
const base = capture();
|
|
const [first, second] = base.state.checkpoints;
|
|
if (first === undefined || second === undefined) throw new Error("fixture");
|
|
const files = {
|
|
[`${HOME}/${PLATE}`]: [await plate("before"), await plate("after")],
|
|
[`${HOME}/${LIST}`]: list,
|
|
} as const;
|
|
const captured: DesktopApplicationCapture = {
|
|
...base,
|
|
session: {
|
|
...base.session,
|
|
windows: base.session.windows.map((window) => ({
|
|
...window,
|
|
titles: [...window.titles, "plate.FCStd - Notes", "list.odt - Notes"],
|
|
})),
|
|
},
|
|
state: {
|
|
...base.state,
|
|
checkpoints: [
|
|
{
|
|
...first,
|
|
retained: [
|
|
...first.retained,
|
|
...Object.entries(files).map(([path, [before]]) =>
|
|
retained(path, before),
|
|
),
|
|
],
|
|
},
|
|
{
|
|
...second,
|
|
changes: [
|
|
...second.changes,
|
|
...Object.entries(files).map(([path, [before, after]]) =>
|
|
change(path, { before, after }),
|
|
),
|
|
],
|
|
retained: [
|
|
...second.retained,
|
|
...Object.entries(files).map(([path, [, after]]) =>
|
|
retained(path, after),
|
|
),
|
|
],
|
|
},
|
|
],
|
|
},
|
|
};
|
|
return writeCaptureFolder(
|
|
captured,
|
|
DESKTOP_OPERATION,
|
|
Object.values(files).flat(),
|
|
);
|
|
};
|
|
|
|
const HOLE = {
|
|
kind: "value_equals",
|
|
file: PLATE,
|
|
pointer:
|
|
"/entries/[name=Document.xml]/view/root/children/[name=ObjectData]/children/[name=Object][@name=Hole]/children/[name=Properties]/children/[name=Property][@name=Radius]/children/[name=Float]/attributes/value",
|
|
value: 5,
|
|
tolerance: 0.01,
|
|
};
|
|
|
|
/** The entries of the list document's last version in the capture's state view. */
|
|
const endEntries = async (folder: string) => {
|
|
const evidence = await inspectDesktopApplicationState(folder, readers);
|
|
if (!("normalized_result" in evidence)) throw evidence;
|
|
const state = desktopApplicationStateSchema.parse(evidence.normalized_result);
|
|
const end = state.paths
|
|
.find((entry) => entry.home_path === LIST)
|
|
?.versions.at(-1)?.view;
|
|
return (
|
|
(end as { entries: Record<string, unknown>[] } | undefined)?.entries ?? []
|
|
);
|
|
};
|
|
|
|
const exportTask = async (folder: string, checks: unknown[]) => {
|
|
const scratch = await createTestTempDirectory("desktop-archive-task-");
|
|
const path = join(scratch, "task.json");
|
|
await writeFile(
|
|
path,
|
|
JSON.stringify({
|
|
name: "cut-hole",
|
|
prompt: "Cut a 10 mm hole through the plate and save it.",
|
|
checks,
|
|
}),
|
|
);
|
|
const directory = join(scratch, "bundle");
|
|
const result = await exportDesktopApplication(
|
|
{ capture: folder, directory, name: undefined, tasks: [path] },
|
|
readers,
|
|
);
|
|
return { result, directory };
|
|
};
|
|
|
|
describe("documents saved as zip archives", () => {
|
|
it("reads each version into its entries and lists the change by entry and element name", async () => {
|
|
const evidence = await inspectDesktopApplicationState(
|
|
await archiveCapture(),
|
|
readers,
|
|
);
|
|
if (!("normalized_result" in evidence)) throw evidence;
|
|
const state = desktopApplicationStateSchema.parse(
|
|
evidence.normalized_result,
|
|
);
|
|
const path = state.paths.find((entry) => entry.home_path === PLATE);
|
|
|
|
expect(path).toMatchObject({
|
|
role: "document",
|
|
format: "zip",
|
|
versions: [
|
|
{ format: "zip", view: { entry_count: 2, truncated: false } },
|
|
{ format: "zip", view_error: null },
|
|
],
|
|
changes: [{ kind: "values", complete: true }],
|
|
});
|
|
expect(path?.changes[0]?.added).toContain(
|
|
"/entries/[name=Document.xml]/view/root/children/[name=Objects]/children/[name=Object][@name=Hole]",
|
|
);
|
|
});
|
|
|
|
it("exports a task that checks the hole and the list's namespaced XML by name", async () => {
|
|
const folder = await archiveCapture();
|
|
const milk = {
|
|
kind: "value_equals",
|
|
file: LIST,
|
|
pointer: `${LIST_TEXT}/[name=text:p]/children/0`,
|
|
value: "Buy milk",
|
|
};
|
|
|
|
const { result, directory } = await exportTask(folder, [HOLE, milk]);
|
|
if (result instanceof Error) throw result;
|
|
const verifier = await readFile(
|
|
join(directory, "artifacts/cut-hole-check/verify.py"),
|
|
"utf8",
|
|
);
|
|
expect(verifier).toContain(`"tolerance": 0.01`);
|
|
expect(verifier).toContain(JSON.stringify(milk.pointer));
|
|
});
|
|
|
|
it("accepts a check on the whole listing, whose value holds namespaced XML", async () => {
|
|
const folder = await archiveCapture();
|
|
const entries = await endEntries(folder);
|
|
expect(entries).toHaveLength(3);
|
|
|
|
const { result } = await exportTask(folder, [
|
|
{ kind: "value_equals", file: LIST, pointer: "/entries", value: entries },
|
|
]);
|
|
|
|
if (result instanceof Error) throw result;
|
|
});
|
|
|
|
it("exports checks of a namespaced entry's metadata, its absence, and the entry compared whole", async () => {
|
|
const comments = {
|
|
name: "comments.xml",
|
|
data: '<office:annotations xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"/>',
|
|
};
|
|
const folder = await archiveCapture([
|
|
odtArchive("Buy bread", [comments]),
|
|
odtArchive("Buy milk"),
|
|
]);
|
|
const content = (await endEntries(folder)).find(
|
|
(entry) => entry.name === "content.xml",
|
|
);
|
|
|
|
const { result } = await exportTask(folder, [
|
|
{
|
|
kind: "value_equals",
|
|
file: LIST,
|
|
pointer: "/entries/[name=content.xml]/size",
|
|
value: content?.size,
|
|
},
|
|
{
|
|
kind: "value_absent",
|
|
file: LIST,
|
|
pointer: "/entries/[name=comments.xml]",
|
|
},
|
|
]);
|
|
expect(
|
|
result instanceof Error
|
|
? (result as Error & { userMessage: string }).userMessage
|
|
: undefined,
|
|
).toBeUndefined();
|
|
|
|
const whole = await exportTask(folder, [
|
|
{
|
|
kind: "value_equals",
|
|
file: LIST,
|
|
pointer: "/entries/[name=content.xml]",
|
|
value: content,
|
|
},
|
|
]);
|
|
if (whole.result instanceof Error) throw whole.result;
|
|
});
|
|
|
|
it("drafts name-addressed checks of the archive that the export then accepts", async () => {
|
|
const folder = await archiveCapture();
|
|
|
|
const drafted = await draftDesktopApplicationTask(folder, readers);
|
|
if (drafted instanceof Error) throw drafted;
|
|
const task = (
|
|
drafted.normalized_result as {
|
|
task: { checks: { file: string; pointer?: string }[] };
|
|
}
|
|
).task;
|
|
|
|
const archived = task.checks.filter(({ file }) => file === PLATE);
|
|
expect(archived.length).toBeGreaterThan(0);
|
|
expect(
|
|
archived.every(({ pointer }) => pointer?.includes("[name=") === true),
|
|
).toBe(true);
|
|
expect(task.checks.filter(({ file }) => file === LIST)).toEqual([
|
|
expect.objectContaining({
|
|
kind: "text_equals",
|
|
pointer: `${LIST_TEXT}/[name=text:p]`,
|
|
value: "Buy milk",
|
|
}),
|
|
]);
|
|
const { result } = await exportTask(folder, task.checks);
|
|
expect(result).not.toBeInstanceOf(Error);
|
|
});
|
|
});
|
|
|
|
describe("paragraphs split into runs", () => {
|
|
it("drafts and exports a paragraph's whole text once an edit split it into runs, which its first run does not hold", async () => {
|
|
const run = (text: string) =>
|
|
`<text:span text:style-name="T1">${text}</text:span>`;
|
|
const folder = await archiveCapture([
|
|
odtArchive("Buy bread"),
|
|
odtArchive(`Buy ${run("oat")} ${run("milk")}`),
|
|
]);
|
|
const paragraph = `${LIST_TEXT}/[name=text:p]`;
|
|
|
|
const drafted = await draftDesktopApplicationTask(folder, readers);
|
|
if (drafted instanceof Error) throw drafted;
|
|
const { checks } = (
|
|
drafted.normalized_result as { task: { checks: { file: string }[] } }
|
|
).task;
|
|
expect(checks.filter(({ file }) => file === LIST)).toEqual([
|
|
expect.objectContaining({
|
|
kind: "text_equals",
|
|
pointer: paragraph,
|
|
value: "Buy oat milk",
|
|
}),
|
|
]);
|
|
|
|
const whole = await exportTask(folder, [
|
|
HOLE,
|
|
{
|
|
kind: "text_equals",
|
|
file: LIST,
|
|
pointer: paragraph,
|
|
value: "Buy oat milk",
|
|
},
|
|
]);
|
|
if (whole.result instanceof Error) throw whole.result;
|
|
expect(whole.result.normalized_result).toMatchObject({
|
|
task: { failing_on_seed: ["check-1", "check-2"] },
|
|
});
|
|
|
|
const first = await exportTask(folder, [
|
|
HOLE,
|
|
{
|
|
kind: "value_equals",
|
|
file: LIST,
|
|
pointer: `${paragraph}/children/0`,
|
|
value: "Buy oat milk",
|
|
},
|
|
]);
|
|
expect(first.result).toBeInstanceOf(Error);
|
|
// The first run holds only the text before the first span.
|
|
for (const fact of ['"check-2"', JSON.stringify("Buy ")])
|
|
expect(
|
|
(first.result as Error & { userMessage: string }).userMessage,
|
|
).toContain(fact);
|
|
});
|
|
});
|