Moves the reusable half into src/core/ (client, types, board, extract, text, keepalive) and the MCP half into src/mcp/. The layering was already clean — nothing in core imported app code or read process.env — so this is a move, not a redesign, and the smoke suite stayed the oracle throughout. The substantive part is core/crawl.ts. The course->board->card->element ->file traversal previously existed only inside tools/search.ts, and the indexer, what's-new diff and file mirror all need it. It now returns a typed Snapshot with breadcrumbs, sorted so two crawls of unchanged content compare equal. Metadata only: downloading and extracting bytes is an order of magnitude more expensive and only the indexer wants it. core/match.ts holds the keyword matching, which makes it testable without a network, and core/text.ts gains the fold/tokenize/snippet helpers (accent folding is not optional for German). search now finds strictly more than before — 5 hits vs 3 for "Datenschutz" — because the snapshot surfaces file-name matches the old streaming walk skipped. 34 unit tests and 30/30 smoke checks pass. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { describe, it } from 'node:test';
|
|
import { daysUntil, formatDate, htmlToText, joinSections, normalizeObjectId } from '../src/core/text.ts';
|
|
|
|
describe('htmlToText', () => {
|
|
it('unwraps the CKEditor markup Schulcloud stores', () => {
|
|
assert.equal(htmlToText('<p>Hallo <strong>Welt</strong></p>'), 'Hallo Welt');
|
|
});
|
|
|
|
it('keeps the href when the link text differs from it', () => {
|
|
assert.equal(
|
|
htmlToText('<p><a href="https://example.org/x">Beispiel</a></p>'),
|
|
'Beispiel (https://example.org/x)',
|
|
);
|
|
});
|
|
|
|
it('does not duplicate a bare URL used as its own label', () => {
|
|
assert.equal(htmlToText('<a href="https://example.org">https://example.org</a>'), 'https://example.org');
|
|
});
|
|
|
|
it('renders list items as bullets and collapses blank runs', () => {
|
|
assert.equal(htmlToText('<ul><li>eins</li><li>zwei</li></ul>'), '- eins\n- zwei');
|
|
});
|
|
|
|
it('decodes entities, ampersand last so &lt; stays literal', () => {
|
|
assert.equal(htmlToText('<p>a &lt; b < c d</p>'), 'a < b < c d');
|
|
});
|
|
|
|
it('returns an empty string for missing input', () => {
|
|
assert.equal(htmlToText(undefined), '');
|
|
assert.equal(htmlToText(null), '');
|
|
});
|
|
});
|
|
|
|
describe('formatDate', () => {
|
|
it('renders ISO timestamps as minute-precision UTC', () => {
|
|
assert.equal(formatDate('2026-08-17T08:00:00.000Z'), '2026-08-17 08:00');
|
|
});
|
|
|
|
it('passes through unparseable values rather than printing Invalid Date', () => {
|
|
assert.equal(formatDate('not a date'), 'not a date');
|
|
});
|
|
|
|
it('marks absent dates', () => {
|
|
assert.equal(formatDate(null), '—');
|
|
});
|
|
});
|
|
|
|
describe('daysUntil', () => {
|
|
it('is negative for past dates and undefined when unset', () => {
|
|
const yesterday = new Date(Date.now() - 86_400_000).toISOString();
|
|
assert.ok((daysUntil(yesterday) ?? 0) < 0);
|
|
assert.equal(daysUntil(undefined), undefined);
|
|
});
|
|
});
|
|
|
|
describe('joinSections', () => {
|
|
it('drops empty and falsy parts', () => {
|
|
assert.equal(joinSections(['a', '', undefined, false, ' ', 'b']), 'a\n\nb');
|
|
});
|
|
});
|
|
|
|
describe('normalizeObjectId', () => {
|
|
it('converts the buffer shape the legacy lesson API returns', () => {
|
|
const id = { buffer: { type: 'Buffer', data: [106, 130, 219, 101, 127, 25, 207, 115, 254, 60, 242, 13] } };
|
|
assert.equal(normalizeObjectId(id), '6a82db657f19cf73fe3cf20d');
|
|
});
|
|
|
|
it('passes plain strings through and gives up on anything else', () => {
|
|
assert.equal(normalizeObjectId('abc'), 'abc');
|
|
assert.equal(normalizeObjectId({}), undefined);
|
|
});
|
|
});
|