Files
stack/tests/focus/anchor.test.mjs
2026-11-14 11:12:38 -05:00

131 lines
3.7 KiB
JavaScript

import { test } from "node:test";
import { equal as eq } from "node:assert/strict";
import { CONTEXT_CHARS, reanchor, selectionSelectors } from "../../src/llm/web/focus_anchor.mjs";
test("a unique quote round-trips through its offsets", () => {
const text = "alpha we support the proposal beta";
const start = text.indexOf("we support the proposal");
const sel = selectionSelectors(text, start, start + "we support the proposal".length);
eq(sel.quote, "we support the proposal");
eq(sel.prefix, "alpha ");
eq(sel.suffix, " beta");
eq(sel.start_char, start);
const hit = reanchor(text, sel);
eq(hit.orphaned, false);
eq(hit.how, "position");
eq(hit.start, start);
eq(text.slice(hit.start, hit.end), sel.quote);
});
test("prefix and suffix stay within 32 characters", () => {
const text = "0123456789".repeat(6) + "QUOTE" + "abcdefghij".repeat(6);
const start = text.indexOf("QUOTE");
const sel = selectionSelectors(text, start, start + "QUOTE".length);
eq(CONTEXT_CHARS, 32);
eq(sel.prefix.length, 32);
eq(sel.suffix.length, 32);
eq(sel.prefix, text.slice(start - 32, start));
eq(sel.suffix, text.slice(start + 5, start + 5 + 32));
});
test("a repeated quote is disambiguated by prefix and suffix", () => {
const text = "before LEFT QUOTE after middle RIGHT QUOTE end";
const start = text.lastIndexOf("QUOTE");
const sel = selectionSelectors(text, start, start + "QUOTE".length);
const hit = reanchor(text, {
quote: "QUOTE",
prefix: sel.prefix,
suffix: sel.suffix,
start_char: null,
end_char: null,
});
eq(hit.orphaned, false);
eq(hit.how, "quote");
eq(hit.start, start);
eq(text.slice(hit.start, hit.end), "QUOTE");
});
test("stale offsets fall through to the quote", () => {
const text = "xxxx hello yyyy";
const hit = reanchor(text, {
quote: "hello",
prefix: "xxxx ",
suffix: " yyyy",
start_char: 0,
end_char: 5,
});
eq(hit.how, "quote");
eq(hit.start, text.indexOf("hello"));
});
test("whitespace differences still count as the quote", () => {
const text = "we\nsupport the proposal today";
const hit = reanchor(text, {
quote: "we support the proposal",
prefix: "",
suffix: " today",
start_char: null,
end_char: null,
});
eq(hit.orphaned, false);
eq(hit.how, "quote");
eq(text.slice(hit.start, hit.end).replace(/\s+/g, " "), "we support the proposal");
});
test("a slight text change is a fuzzy match", () => {
const text = "we suport the proposal today";
const hit = reanchor(text, {
quote: "we support the proposal",
prefix: "",
suffix: " today",
start_char: null,
end_char: null,
});
eq(hit.orphaned, false);
eq(hit.how, "fuzzy");
eq(text.slice(hit.start, hit.end), "we suport the proposal");
});
test("an absent quote is orphaned", () => {
const hit = reanchor("nothing here", {
quote: "missing sentence",
prefix: "no ",
suffix: " where",
start_char: 0,
end_char: 4,
});
eq(hit.orphaned, true);
eq(hit.how, "orphaned");
eq(hit.start, null);
eq(hit.end, null);
});
test("unicode quotes round-trip", () => {
const text = "The naïve café — 提案 stands.";
const quote = "naïve café — 提案";
const start = text.indexOf(quote);
const sel = selectionSelectors(text, start, start + quote.length);
eq(sel.quote, quote);
const hit = reanchor(text, {
quote,
prefix: sel.prefix,
suffix: sel.suffix,
start_char: null,
end_char: null,
});
eq(hit.orphaned, false);
eq(text.slice(hit.start, hit.end), quote);
});
test("a bad range is rejected", () => {
let threw = false;
try {
selectionSelectors("abc", 2, 1);
} catch (e) {
threw = true;
eq(e.message, "bad range");
}
eq(threw, true);
});