Files
Schulcloud-MCP/test/homework-page.test.ts
MechaCat02 a8badc2fd1 Read submitted text and teacher feedback from the homework page
You were right that this data never reaches the browser as an API call.
The legacy front end calls the Feathers API server-side for
submission.comment, submission.grade and submission.gradeComment and
renders them into GET /homework/{taskId}. That Feathers API is not
exposed publicly — /api/v1/* 404s — so the rendered page is the only way
to reach these fields from outside.

core/homework-page.ts parses it, hooked on the data-testid attributes
the project's own e2e tests use rather than incidental markup. The page
authenticates by jwt *cookie*; an Authorization header is ignored and
redirects to the identity provider. Every field is optional and parse
failures return undefined, so a markup change degrades to "not found"
and cannot break get_task. The wording distinguishes the two: absent
feedback is reported as not found, never as none given.

Measured on one course: 4 of 7 graded submissions carry feedback no API
call can return — "vollständig und nachvollziehbar", "Feedback siehe
Zettel", and so on.

This exposed a bug in a shared utility: htmlToText decoded only six
entities, so any named entity passed through raw. German content makes
that routine — "vollständig" would have reached the model verbatim
from boards and task descriptions too, not just here. It now decodes
named, decimal and hex references in one pass, so ä stays
literal instead of decoding twice, and leaves unknown names alone rather
than mangling them.

Also fixes a documented-recovery bug found while restoring the session:
`docker compose restart` does not re-read env_file, so it silently kept
serving the dead token. `up -d` is correct and the docs said the wrong
thing.

78 tests, 38/38 smoke; verified end to end through Claude Code.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 13:25:35 +02:00

74 lines
3.2 KiB
TypeScript

import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { parseHomeworkPage } from '../src/core/homework-page.ts';
/**
* Fixtures mirror the legacy client's templates (feedback.hbs, submission.hbs)
* as actually served — including the parts that are absent for a student, which
* is what makes every field optional.
*/
const page = (parts: { submission?: string; feedback?: string }) => `
<html><body>
<nav class="nav tab-links"><a href="#activetabid=extended">Details</a></nav>
<section id="extended" class="tab-content">keine Beschreibung vorhanden</section>
<section id="submission" class="tab-content">${parts.submission ?? ''}</section>
${parts.feedback === undefined ? '' : `<section id="feedback" class="tab-content">${parts.feedback}</section>`}
</body></html>`;
const fileCard = (id: string, name: string, size = 1234) =>
`<div class="card file " data-file-name="${name}" data-file-size="${size}" data-file-id="${id}"></div>`;
describe('parseHomeworkPage', () => {
it("reads the teacher's written feedback, which no API exposes", () => {
const html = page({
feedback: `<div class="comment ckcontent" data-testid="feedback-comment"><p>vollst&auml;ndig und nachvollziehbar</p></div>`,
});
assert.equal(parseHomeworkPage(html)?.gradeComment, 'vollständig und nachvollziehbar');
});
it('reads a percentage grade from the feedback tab', () => {
const html = page({ feedback: '<p>Du hast 85% erreicht</p>' });
assert.equal(parseHomeworkPage(html)?.gradePercent, 85);
});
it('reads the student\'s typed answer from the editable textarea', () => {
const html = page({
submission: `<textarea data-testid="submission-text" name="comment"> <p>Meine L&ouml;sung</p> </textarea>`,
});
assert.equal(parseHomeworkPage(html)?.submittedText, 'Meine Lösung');
});
it('reads the typed answer from the read-only form once submission closed', () => {
const html = page({ submission: `<div class="comment"><p>Abgegebener Text</p></div>` });
assert.equal(parseHomeworkPage(html)?.submittedText, 'Abgegebener Text');
});
it('separates submitted files from files the teacher returned', () => {
const html = page({
submission: fileCard('a'.repeat(24), 'meine-abgabe.pdf'),
feedback: fileCard('b'.repeat(24), 'korrektur.pdf'),
});
const detail = parseHomeworkPage(html);
assert.deepEqual(detail?.submittedFiles, [{ id: 'a'.repeat(24), name: 'meine-abgabe.pdf' }]);
assert.deepEqual(detail?.gradingFiles, [{ id: 'b'.repeat(24), name: 'korrektur.pdf' }]);
});
it('returns a result with nothing set when the page carries no submission', () => {
// The common case for an ungraded task: sections present but empty.
const detail = parseHomeworkPage(page({}));
assert.ok(detail);
assert.equal(detail.submittedText, undefined);
assert.equal(detail.gradeComment, undefined);
assert.deepEqual(detail.gradingFiles, []);
});
it('gives up rather than guessing when the markup is not a homework page', () => {
assert.equal(parseHomeworkPage('<html><body>Anmelden</body></html>'), undefined);
});
it('decodes entities in file names', () => {
const html = page({ submission: fileCard('c'.repeat(24), 'A&amp;B &quot;final&quot;.pdf') });
assert.equal(parseHomeworkPage(html)?.submittedFiles[0]?.name, 'A&B "final".pdf');
});
});