Files
breakpilot-lehrer/school-service/internal/services/timetable_exports_test.go
T
Benjamin Admin 306886a42b Phase 8: CSV + ICS export, print view, MkDocs docs, SBOM + dev-mode auth
Auth (Test-Mode):
  - middleware.AuthMiddleware now takes a devMode flag. In dev,
    requests without Authorization fall back to a deterministic dev
    UUID (00000000-...-001) and role=teacher. ENVIRONMENT=production
    re-enables the strict 401 path.
  - main.go wires devMode = cfg.Environment != "production".
  - page.tsx replaces the red 'Anmeldung noch nicht integriert' banner
    with a softer Testumgebung notice; the manual-token form moves
    behind a nested details block.

Export endpoints (school-service):
  - LoadExportLessons joins tt_lesson with tt_period for wall-clock
    times; one query feeds both CSV and ICS.
  - WriteCSV streams 10 columns including pinned flag.
  - WriteICS emits one VEVENT per lesson anchored to a Monday — caller
    overridable via ?start=YYYY-MM-DD. RFC 5545 escapes for ',', ';',
    '\n' in icsEscape().
  - NextMonday helper for the default anchor.
  - GET /timetable/solutions/:id/export.{csv,ics} handlers attach
    Content-Disposition: attachment so browsers download instead of
    rendering.

Frontend:
  - lib/stundenplan/api.ts downloadSolutionExport() fetches as blob,
    triggers a synthetic <a download> click, and forwards the JWT when
    present.
  - PlanView gains CSV / ICS / Drucken buttons next to the perspective
    selector. The toolbar carries class 'no-print' so window.print()
    yields only the grid.
  - globals.css @media print rule hides chrome, forces white
    background, gives the table proper borders for A4.

Docs:
  - docs-src/services/stundenplan/{index,architecture,constraints,
    solver-tuning,export}.md with nav entry in mkdocs.yml under
    Services → Stundenplaner.
  - sbom/stundenplan/README.md lists manually-verified key dependencies
    and the policy reference. scripts/stundenplan-sbom.sh generates
    full machine-readable inventories via go-licenses + pip-licenses
    + license-checker when those tools are available.

Tests:
  - internal/services/timetable_exports_test.go: 4 unit tests covering
    CSV column layout + quoting, ICS structure + DTSTART formatting,
    icsEscape special chars, NextMonday weekday math.
  - studio-v2/e2e/stundenplan-export.spec.ts split out of the main spec
    file (LOC budget) — 3 tests for button render, CSV download,
    ICS download.
  - mockSchoolApi extended with export.csv + export.ics routes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 08:57:07 +02:00

106 lines
3.0 KiB
Go

package services
import (
"bytes"
"strings"
"testing"
"time"
)
func sampleLessons() []LessonExport {
return []LessonExport{
{DayOfWeek: 1, PeriodIndex: 1, StartTime: "08:00", EndTime: "08:45",
ClassName: "5a", SubjectName: "Mathe", SubjectCode: "M",
TeacherName: "Schmidt, Anna", RoomName: "A101", Pinned: false},
{DayOfWeek: 2, PeriodIndex: 1, StartTime: "08:00", EndTime: "08:45",
ClassName: "5a", SubjectName: "Deutsch, Klasse 5", SubjectCode: "D",
TeacherName: "Mueller, Bob", RoomName: "", Pinned: true},
}
}
func TestWriteCSV_HeaderAndRows(t *testing.T) {
var buf bytes.Buffer
if err := WriteCSV(&buf, sampleLessons()); err != nil {
t.Fatalf("WriteCSV failed: %v", err)
}
out := buf.String()
wantHeader := "day_of_week,period_index,start_time,end_time,class,subject,subject_code,teacher,room,pinned"
if !strings.Contains(out, wantHeader) {
t.Errorf("CSV missing header line; got:\n%s", out)
}
if !strings.Contains(out, "1,1,08:00,08:45,5a,Mathe,M,\"Schmidt, Anna\",A101,false") {
t.Errorf("CSV missing first row; got:\n%s", out)
}
// Commas inside subject name must be quoted.
if !strings.Contains(out, "\"Deutsch, Klasse 5\"") {
t.Errorf("CSV should quote comma in subject name; got:\n%s", out)
}
if !strings.Contains(out, ",true") {
t.Errorf("Pinned flag should serialise as 'true'; got:\n%s", out)
}
}
func TestWriteICS_StructureAndDates(t *testing.T) {
weekStart := time.Date(2026, 8, 24, 0, 0, 0, 0, time.UTC) // a Monday
var buf bytes.Buffer
if err := WriteICS(&buf, sampleLessons(), weekStart, "Schuljahr 26/27"); err != nil {
t.Fatalf("WriteICS failed: %v", err)
}
out := buf.String()
for _, want := range []string{
"BEGIN:VCALENDAR\r\n",
"VERSION:2.0\r\n",
"PRODID:-//BreakPilot//Timetable//DE\r\n",
"BEGIN:VEVENT\r\n",
"END:VEVENT\r\n",
"END:VCALENDAR\r\n",
"DTSTART:20260824T080000",
"DTSTART:20260825T080000",
"SUMMARY:Mathe (5a)",
"LOCATION:A101",
"Schuljahr 26/27",
} {
if !strings.Contains(out, want) {
t.Errorf("ICS missing %q in output", want)
}
}
}
func TestICSEscape_SpecialChars(t *testing.T) {
tests := []struct {
in, want string
}{
{"plain", "plain"},
{"a,b", "a\\,b"},
{"x;y", "x\\;y"},
{"line1\nline2", "line1\\nline2"},
}
for _, tt := range tests {
got := icsEscape(tt.in)
if got != tt.want {
t.Errorf("icsEscape(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}
func TestNextMonday(t *testing.T) {
// Verify the offset arithmetic for every weekday — 2026-08-22 is a Saturday.
cases := []struct {
ref time.Time
wantMo string
}{
{time.Date(2026, 8, 24, 0, 0, 0, 0, time.UTC), "2026-08-24"}, // Mon → same day
{time.Date(2026, 8, 25, 0, 0, 0, 0, time.UTC), "2026-08-31"}, // Tue → next Mon
{time.Date(2026, 8, 23, 0, 0, 0, 0, time.UTC), "2026-08-24"}, // Sun → next Mon
{time.Date(2026, 8, 22, 0, 0, 0, 0, time.UTC), "2026-08-24"}, // Sat → next Mon
}
for _, tt := range cases {
got := NextMonday(tt.ref).Format("2006-01-02")
if got != tt.wantMo {
t.Errorf("NextMonday(%s) = %s, want %s", tt.ref.Format("2006-01-02"), got, tt.wantMo)
}
}
}