Files
Schulcloud-MCP/test/render.test.ts
MechaCat02 5ae2210459 Close the gaps an audit of courses, tasks, files and grades turned up
Every area — courses, rooms, boards, topics, tasks, files, quizzes, teams,
groups, submissions, grades — was checked for data the instance has and the
tools did not show.

Grades and feedback. A teacher's /homework page is a different page from a
student's: grade and comment live in the grading form, one block per
submission, so a teacher account reported every graded submission as having
neither. parseTeacherGrading reads the form, and list_submissions can now
include the written feedback and who handed the work in.

Names. /api/v1 is partly served: courses, users and classes survive in the
deployment's ingress table, and users/{id} is the only route from an id to a
name. Submitters, file creators and course teachers resolve through it, and
degrade to "not visible to this account" where a student may not read them.

Courses, rooms and classes. get_course adds the description, teachers,
member count and weekly timetable from /api/v1/courses. list_classes is new.
get_room reports what the account may do — allowedOperations is an object of
booleans, not the list it was typed as — and applicants and invitation links
where it may manage them.

Board and topic content. Link descriptions, image alt text, drawing and
video-conference titles, the ids behind external tools and H5P content (the
only thing resembling a quiz), and what a deleted element used to be. Topic
Etherpad pads are read like board pads, and htmlToText keeps table columns
apart and drops template indentation.

Files. A scan with no text layer falls back to the preview endpoint, whose
width and outputFormat are undocumented enums, so Claude gets a picture of
the page; list_files reports counts and sizes. Teams stay documented as
unreadable at any API version; their files come later.

What the crawl missed. Tasks attached to topics (18 of 60 on the live
account), each course's own file area, and — behind INDEX_PERSONAL_FILES —
personal files and submissions with their grade comments, so search and
what_changed cover grading. A submission hit points at get_task.

The local instance's preview profile gets an ImageMagick policy that allows
the coders its 7.1.2 build needs; the image's own denies them all.

110 tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-16 20:19:16 +02:00

125 lines
4.6 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 &amp;lt; stays literal', () => {
assert.equal(htmlToText('<p>a &amp;lt; b &lt; c</p>'), 'a &lt; b < c');
});
it('collapses runs of spaces, including the non-breaking ones', () => {
// Schulcloud content is full of &nbsp; used as padding; keeping it would
// reproduce that padding in the plain-text output for no benefit.
assert.equal(htmlToText('<p>a &nbsp;b</p>'), 'a b');
});
it('strips the source template\'s indentation from every line', () => {
// Paragraphs still separate with a blank line; what goes is the leading
// run of spaces the server-side template left on each line.
const html = '<p>\n first line<br>\n second line</p>';
assert.equal(htmlToText(html), 'first line\nsecond line');
});
it('separates table cells so columns do not run together', () => {
const html = '<table><tr><td>Bestandteil</td><td>Funktion</td></tr>' +
'<tr><td>Gehirn</td><td>steuert</td></tr></table>';
assert.equal(htmlToText(html), 'Bestandteil | Funktion\nGehirn | steuert');
});
it('keeps a cell that wraps its text in a paragraph on one row', () => {
const html = '<table><tr><td><p>Bestandteil</p></td><td>Gehirn</td></tr></table>';
assert.equal(htmlToText(html), 'Bestandteil | Gehirn');
});
it('separates paragraphs with a blank line', () => {
assert.equal(htmlToText('<p>first</p>\n <p>second</p>'), 'first\n\nsecond');
});
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);
});
});
describe('decodeEntities', () => {
it('decodes German umlauts, which the legacy pages emit as named entities', () => {
assert.equal(htmlToText('<p>vollst&auml;ndig und nachvollziehbar</p>'), 'vollständig und nachvollziehbar');
assert.equal(htmlToText('<p>Gr&ouml;&szlig;e, &Uuml;bung</p>'), 'Größe, Übung');
});
it('decodes decimal and hex numeric references', () => {
assert.equal(htmlToText('<p>&#8364; &#x20AC; &#228;</p>'), '€ € ä');
});
it('does not double-decode: &amp;auml; stays literal text', () => {
assert.equal(htmlToText('<p>&amp;auml;</p>'), '&auml;');
});
it('leaves unknown entities alone rather than mangling them', () => {
assert.equal(htmlToText('<p>&notarealentity; &amp;</p>'), '&notarealentity; &');
});
it('ignores out-of-range numeric references', () => {
assert.equal(htmlToText('<p>&#1114112;</p>'), '&#1114112;');
});
});