mirror of
https://github.com/morluto/rea.git
synced 2026-11-08 15:56:35 +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>
182 lines
6.2 KiB
TypeScript
182 lines
6.2 KiB
TypeScript
import { execFileSync } from "node:child_process";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { expect } from "vitest";
|
|
|
|
import { readZipView } from "../../src/application/desktop/DesktopZipView.js";
|
|
import type { DesktopTaskCheck } from "../../src/domain/desktop/appTask.js";
|
|
import {
|
|
evaluateTaskChecks,
|
|
type TaskCheckOutcome,
|
|
type TaskFileView,
|
|
} from "../../src/domain/desktop/appTaskChecks.js";
|
|
import {
|
|
PYTHON_HELPERS,
|
|
PYTHON_IMPORTS,
|
|
} from "../../src/domain/desktop/appExportPython.js";
|
|
import { PYTHON_TASK_EVALUATOR } from "../../src/domain/desktop/appTaskPython.js";
|
|
import {
|
|
detectStateFormat,
|
|
textualView,
|
|
} from "../../src/domain/desktop/stateFormats.js";
|
|
import { zipArchive } from "../../src/domain/desktop/zipArchive.fixture.js";
|
|
import type { JsonValue } from "../../src/domain/jsonValue.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
|
|
/** A file as an app saved it: its name under the home and its bytes. */
|
|
export interface SavedFile {
|
|
readonly name: string;
|
|
readonly bytes: Uint8Array;
|
|
}
|
|
|
|
const FIXTURES = join(import.meta.dirname, "../fixtures");
|
|
|
|
/**
|
|
* A fixture's two saves, before and after its edit, read from
|
|
* `tests/fixtures/FOLDER/{before,after}/FILE` as the file `name`, each
|
|
* wrapped, such as a Writer `content.xml` zipped into an `.odt`.
|
|
*/
|
|
export const savedEnds = (
|
|
folder: string,
|
|
file: string,
|
|
name: string = file,
|
|
wrap: (bytes: Uint8Array) => Uint8Array = (bytes) => bytes,
|
|
): Promise<SavedFile[]> =>
|
|
Promise.all(
|
|
(["before", "after"] as const).map(async (end) => ({
|
|
name,
|
|
bytes: wrap(await readFile(join(FIXTURES, folder, end, file))),
|
|
})),
|
|
);
|
|
|
|
/** A Writer `content.xml` zipped into an `.odt` after its stored `mimetype`, as Writer saves one. */
|
|
export const odtOf = (content: Uint8Array): Uint8Array =>
|
|
zipArchive([
|
|
{
|
|
name: "mimetype",
|
|
data: "application/vnd.oasis.opendocument.text",
|
|
method: 0,
|
|
},
|
|
{ name: "content.xml", data: content },
|
|
]);
|
|
|
|
/** The exported module's standard library imports, its helpers and the verifier's evaluator, as one program. */
|
|
const PYTHON = `${PYTHON_IMPORTS.split("\n")
|
|
.filter((line) => !line.includes("agentenv_protocol"))
|
|
.join("\n")}${PYTHON_HELPERS}${PYTHON_TASK_EVALUATOR}`;
|
|
|
|
/** Run a driver after the exported module's helpers and evaluator, with JSON on its stdin; its stdout. */
|
|
export const exportedPython = (driver: string, input: unknown): string =>
|
|
execFileSync("python3", ["-I", "-B", "-c", `${PYTHON}\n${driver}`], {
|
|
input: JSON.stringify(input),
|
|
encoding: "utf8",
|
|
maxBuffer: 64 * 1024 * 1024,
|
|
});
|
|
|
|
/** One JSON value per line the driver printed. */
|
|
const printed = <Value>(output: string): Value[] =>
|
|
output
|
|
.trim()
|
|
.split("\n")
|
|
.map((line) => JSON.parse(line) as Value);
|
|
|
|
/** Write each file into a fresh folder, as data/get finds it under the home, keeping its suffix; their paths. */
|
|
export const writeFiles = async (files: readonly SavedFile[]) => {
|
|
const folder = await createTestTempDirectory("desktop-export-python-");
|
|
return Promise.all(
|
|
files.map(async ({ name, bytes }, index) => {
|
|
const path = join(folder, `${String(index)}-${name}`);
|
|
await writeFile(path, bytes);
|
|
return path;
|
|
}),
|
|
);
|
|
};
|
|
|
|
/** The exported module's data/get entry for each file: its format and view, or its error. */
|
|
export const pythonFileViews = async (files: readonly SavedFile[]) =>
|
|
printed<Record<string, JsonValue>>(
|
|
exportedPython(
|
|
`for path in json.load(sys.stdin):
|
|
print(json.dumps(_view(Path(path))))`,
|
|
await writeFiles(files),
|
|
),
|
|
);
|
|
|
|
/** The verifier's results for each file, read by the exported module as the home path `home`. */
|
|
const pythonCheckResults = async (
|
|
home: string,
|
|
checks: readonly DesktopTaskCheck[],
|
|
files: readonly SavedFile[],
|
|
) =>
|
|
printed<{ id: string; result: boolean; description: string }[]>(
|
|
exportedPython(
|
|
`request = json.load(sys.stdin)
|
|
for path in request["paths"]:
|
|
print(json.dumps(evaluate(request["checks"], {request["home"]: _view(Path(path))})))`,
|
|
{ home, checks, paths: await writeFiles(files) },
|
|
),
|
|
);
|
|
|
|
/** The exported module's XML view of each text, or null where it refuses one. */
|
|
export const pythonXmlViews = (texts: readonly string[]) =>
|
|
printed<JsonValue>(
|
|
exportedPython(
|
|
`for text in json.load(sys.stdin):
|
|
print(json.dumps(_xml_view(text)))`,
|
|
texts,
|
|
),
|
|
);
|
|
|
|
/** REA's state view of a zip, text, JSON, key file or XML file, in the shape data/get serves it. */
|
|
export const typescriptFileView = async ({
|
|
name,
|
|
bytes,
|
|
}: SavedFile): Promise<TaskFileView> => {
|
|
const format = detectStateFormat(name, bytes);
|
|
if (format === "sqlite" || format === "dconf" || format === "binary")
|
|
throw new Error(`${name} is ${format}, which these tests do not read`);
|
|
const read =
|
|
format === "zip" ? await readZipView(bytes) : textualView(format, bytes);
|
|
return read.ok
|
|
? { format, view: read.value }
|
|
: { format, view: null, error: read.error };
|
|
};
|
|
|
|
/** Both readers give each file the same format and view, or the same error. */
|
|
export const expectSameFileViews = async (files: readonly SavedFile[]) => {
|
|
const fromPython = await pythonFileViews(files);
|
|
for (const [index, file] of files.entries()) {
|
|
const ours = await typescriptFileView(file);
|
|
const theirs = fromPython[index] ?? {};
|
|
expect(theirs.format).toBe(ours.format);
|
|
expect(theirs.view).toEqual(ours.view);
|
|
expect(theirs.error).toBe(ours.error);
|
|
}
|
|
};
|
|
|
|
/** Both evaluators give the same results and descriptions on each file read as the home path `home`; REA's. */
|
|
export const expectSameCheckResults = async (
|
|
home: string,
|
|
checks: readonly DesktopTaskCheck[],
|
|
files: readonly SavedFile[],
|
|
): Promise<(readonly TaskCheckOutcome[])[]> => {
|
|
const fromPython = await pythonCheckResults(home, checks, files);
|
|
return Promise.all(
|
|
files.map(async (file, index) => {
|
|
const ours = evaluateTaskChecks(
|
|
checks,
|
|
new Map([[home, await typescriptFileView(file)]]),
|
|
);
|
|
expect(fromPython[index]).toEqual(
|
|
ours.map(({ id, passed, description }) => ({
|
|
id,
|
|
result: passed,
|
|
description,
|
|
})),
|
|
);
|
|
return ours;
|
|
}),
|
|
);
|
|
};
|