Files
DeepTutor/web/tests/reading-selection.test.ts
Bingxi Zhao (Frank) 7c6bcfaef7 release: v1.5.14
Immersive Reading, self-configuration, and a two-way IMA library.

- Immersive Reading: a new capability that opens a document beside the
  thread. New reading engine (PDF pages / slides / page-sized sections),
  five reading tools, a deterministic locate pre-pass that grounds the
  turn without a second LLM call, /api/v1/reading, and a reader pane with
  selection, highlights, notes, and export to an annotated PDF or Markdown.
- Setup capability: inspect the install, apply a setting, install a
  parsing engine, or request a credential from a chat turn. New settings
  spec table; a candidate value is probed before it is committed, and API
  keys never enter the model's context.
- Tencent IMA: pipeline split into transport/envelope/models/sources/
  notes/media/inventory, an additive capability with five tools, and
  connected libraries that can finally be enumerated (bounded listings
  are reported as a lower bound).
- Retrieval: supports_rag_retrieval() replaces is_connected_kb() for the
  "can I sweep this?" question, so linked / lightrag_server / ima sources
  stop being silently dropped from every book.
- Notebooks: their own console at /notebook, per-notebook locks and atomic
  writes, copy / move / export routes, damaged files surfaced instead of
  dropped, and renaming a record no longer clears its knowledge-base link.
- Settings persistence: one lock and one atomic replace shared by every
  writer of interface.json; the PUT endpoints stop writing the
  defaults-merged view back to disk.

Release notes: assets/releases/ver1-5-14.md
2026-08-19 17:34:52 +08:00

112 lines
3.5 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
cleanQuote,
locatorOfSelection,
mergeRectsByLine,
normaliseRects,
unionRect,
} from "../lib/reading-selection";
const box = (left: number, top: number, width: number, height: number) => ({
left,
top,
width,
height,
});
const PAGE = box(100, 200, 800, 1000);
test("merges rects that share a text line into one", () => {
const merged = mergeRectsByLine([
box(10, 10, 50, 14),
box(60, 10, 40, 14),
box(10, 30, 90, 14),
]);
assert.equal(merged.length, 2);
assert.deepEqual(merged[0], box(10, 10, 90, 14));
});
test("treats vertically overlapping rects of different heights as one line", () => {
// A superscript sits higher and is shorter, but belongs to the same line.
const merged = mergeRectsByLine([box(10, 10, 50, 14), box(60, 8, 8, 8)]);
assert.equal(merged.length, 1);
});
test("drops sliver rects the browser emits at range edges", () => {
const merged = mergeRectsByLine([box(10, 10, 50, 14), box(61, 10, 1, 14)]);
assert.equal(merged.length, 1);
assert.deepEqual(merged[0], box(10, 10, 50, 14));
});
test("no usable rects yields nothing rather than a degenerate box", () => {
assert.deepEqual(mergeRectsByLine([]), []);
assert.deepEqual(mergeRectsByLine([box(0, 0, 0, 0)]), []);
assert.deepEqual(mergeRectsByLine([box(5, 5, 1, 1)]), []);
});
test("normalises to 0..1 of the container box", () => {
const rects = normaliseRects([box(100, 200, 400, 100)], PAGE);
assert.deepEqual(rects, [[0, 0, 0.5, 0.1]]);
});
test("normalisation is scroll-independent because both rects are viewport-space", () => {
const scrolled = normaliseRects(
[box(100 - 300, 200 - 300, 400, 100)],
box(100 - 300, 200 - 300, 800, 1000),
);
assert.deepEqual(scrolled, [[0, 0, 0.5, 0.1]]);
});
test("clips a selection dragged off the page instead of discarding it", () => {
const rects = normaliseRects([box(0, 100, 2000, 200)], PAGE);
assert.equal(rects.length, 1);
const [x0, y0, x1, y1] = rects[0];
assert.ok(x0 >= 0 && y0 >= 0 && x1 <= 1 && y1 <= 1);
assert.ok(x1 - x0 > 0);
});
test("a zero-sized container yields no rects rather than dividing by zero", () => {
assert.deepEqual(normaliseRects([box(0, 0, 10, 10)], box(0, 0, 0, 0)), []);
});
test("drops rects that normalise to nothing", () => {
// Entirely above the page: clamps to a zero-height sliver, so it is dropped.
assert.deepEqual(normaliseRects([box(100, 0, 400, 4)], PAGE), []);
});
test("unionRect wraps every rect", () => {
assert.deepEqual(
unionRect([
[0.1, 0.1, 0.4, 0.2],
[0.05, 0.3, 0.6, 0.35],
]),
[0.05, 0.1, 0.6, 0.35],
);
assert.equal(unionRect([]), null);
});
test("cleanQuote collapses the hard wraps a pdf text layer introduces", () => {
assert.equal(
cleanQuote("Transformers use scaled\n dot-product\tattention today."),
"Transformers use scaled dot-product attention today.",
);
});
test("cleanQuote bounds an enormous selection", () => {
assert.equal(cleanQuote("x".repeat(5000)).length, 2000);
assert.equal(cleanQuote("x".repeat(50), 10).length, 10);
});
test("cleanQuote tolerates empty input", () => {
assert.equal(cleanQuote(""), "");
assert.equal(cleanQuote(" \n "), "");
});
test("a cross-page selection is attributed to where it started", () => {
assert.equal(locatorOfSelection([3, 4]), 3);
assert.equal(locatorOfSelection([null, 4]), 4);
assert.equal(locatorOfSelection([null, null]), null);
assert.equal(locatorOfSelection([0, 2]), 2);
});