test(desktop): bound child-text and reference-index work by counts (#11610)

The child-text selector test counted elapsed time while reading a 4 MiB
child; it now counts the characters read and refuses a whole-string rewrite
of that text. The shared-key reference test counted elapsed time over
60,000 records; it now counts the elements Array.prototype.includes scans,
which grows with the square of the records without the per-tag cap.

Refs #10513
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 08:25:52 +08:00
1 parent 482165d7ce
commit cf1407966e
2 files changed
+91 -27

No files matched your search

+44 -21
View File
@@ -431,7 +431,7 @@ describe("draftTaskFiles values' text and references", () => {
]);
});
it("resolves a key the records of many archive entries share in time that grows with the records, not with their square", () => {
it("resolves a key the records of many archive entries share with work that grows with the records, not with their square", () => {
// Copying the key's list of elements for each record took four seconds for 60,000 here.
const entry = (name: string, text: string) => {
const bytes = new TextEncoder().encode(text);
@@ -467,34 +467,57 @@ describe("draftTaskFiles values' text and references", () => {
const pointer =
"/entries/[name=book.xml]/view/root/children/[name=ope]/attributes/category";
const started = performance.now();
const { values } = draftValues(
{
added: [],
changed: [pointer],
removed: [],
added_count: 0,
changed_count: 1,
removed_count: 0,
// Each record is matched against the few elements its key already lists.
// Matching it against every earlier record makes records^2 comparisons;
// counting the elements `includes` scans stops that at the bound.
const allowed = 64 * records.length * 300;
let scanned = 0;
const array = Array.prototype;
const { includes } = array;
Object.defineProperty(array, "includes", {
configurable: true,
writable: true,
value(this: unknown[], ...args: [unknown, number?]) {
scanned += this.length;
if (scanned > allowed) throw new Error("records matched pairwise");
return includes.apply(this, args);
},
{
ends,
listed: () => true,
noise: () => undefined,
format: () => "xml",
referenceRoots: (view) => (view === null ? [] : [view]),
},
);
const elapsed = performance.now() - started;
});
let drafted: ReturnType<typeof draftValues>;
try {
drafted = draftValues(
{
added: [],
changed: [pointer],
removed: [],
added_count: 0,
changed_count: 1,
removed_count: 0,
},
{
ends,
listed: () => true,
noise: () => undefined,
format: () => "xml",
referenceRoots: (view) => (view === null ? [] : [view]),
},
);
} finally {
Object.defineProperty(array, "includes", {
configurable: true,
writable: true,
value: includes,
});
}
expect(values[0]?.refers_to).toEqual([
expect(scanned).toBeLessThanOrEqual(allowed);
expect(drafted.values[0]?.refers_to).toEqual([
{
attribute: "category",
value: "1",
element: '<cat key="1" name="Rent"/>',
},
]);
expect(elapsed).toBeLessThan(1000);
});
});
+47 -6
View File
@@ -43,14 +43,55 @@ describe("childConditions", () => {
{ name: "body", attributes: {}, children: [text] },
],
});
const huge = element(`a b ${"x".repeat(4 * 1024 * 1024)}`);
const length = 4 * 1024 * 1024;
const huge = element(`a b ${"x".repeat(length)}`);
const started = performance.now();
const conditions = childConditions(huge);
const elapsed = performance.now() - started;
// A selector names a member by at most 64 characters. Count the characters
// read one at a time, and refuse any whole-string rewrite of the long
// text, as collapsing all of it before measuring would do.
const allowed = 1024;
let read = 0;
const string = String.prototype;
const { [Symbol.iterator]: iterate } = string;
const rewrites = ["replace", "replaceAll", "split"] as const;
const originals = rewrites.map((name) => [name, string[name]] as const);
Object.defineProperty(string, Symbol.iterator, {
configurable: true,
writable: true,
*value(this: string) {
for (const character of iterate.call(this)) {
if (++read > allowed) throw new Error("read the whole child text");
yield character;
}
},
});
for (const [name, original] of originals)
Object.defineProperty(string, name, {
configurable: true,
writable: true,
value(this: string, ...args: never[]) {
if (this.length >= length)
throw new Error(`${name} on the long text`);
return (original as (...values: never[]) => unknown).apply(
this,
args,
);
},
});
let conditions: string[];
try {
conditions = childConditions(huge);
} finally {
string[Symbol.iterator] = iterate;
for (const [name, original] of originals)
Object.defineProperty(string, name, {
configurable: true,
writable: true,
value: original,
});
}
expect(conditions).toEqual(["[>title=Budget]"]);
// Building the whole collapsed text took about half a second.
expect(elapsed).toBeLessThan(100);
expect(read).toBeLessThanOrEqual(allowed);
});
});