Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54c5f74778 | ||
|
|
acc2b0c5dd |
@@ -0,0 +1,78 @@
|
||||
# ING-01 – Prüfprotokoll: IMAP-Server-Grundgerüst
|
||||
|
||||
Keine Vorbedingungen im Mail-Board (sofort startbar).
|
||||
|
||||
## Umsetzung
|
||||
|
||||
- `mail/internal/imap/state.go` — `State` (`NotAuthenticated`,
|
||||
`Authenticated`, `Selected`, RFC 3501 §3).
|
||||
- `mail/internal/imap/parser.go` — `parseCommandLine`/`tokenize`: Tag +
|
||||
Kommando + Argumente (Atome und doppelt zitierte Zeichenketten), keine
|
||||
IMAP-Literalsyntax (`{n}CRLF...` — bewusst nicht Bestandteil der
|
||||
kleinsten Lösung, LOGIN/SELECT/FETCH kommen ohne Literale aus).
|
||||
- `mail/internal/imap/response.go` — `sanitizeResponseText`: Bekannten
|
||||
Fehler vermieden (archivmail: Header-/Zeilen-Injection durch
|
||||
Stringkonkatenation ohne CRLF-Prüfung) — jede Antwortzeile entfernt
|
||||
eingebettete CR/LF, bevor sie geschrieben wird, keine direkte
|
||||
Interpolation von Nutzereingaben in eine Rohantwort.
|
||||
- `mail/internal/imap/session.go`/`commands.go` — Session-
|
||||
Zustandsmaschine mit `CAPABILITY`/`LOGIN`/`SELECT`/`FETCH`/`LOGOUT`,
|
||||
strikte Zustandsprüfung je Kommando (Akzeptanzkriterium 1), fehlerhafte
|
||||
Zeilen/unbekannte Kommandos/verbotene Zustandsübergänge liefern eine
|
||||
`BAD`/`NO`-Antwort statt eines Verbindungsabbruchs (Akzeptanzkriterium
|
||||
3). `maxCommandLineBytes` begrenzt die Puffergröße defensiv (Vorbild
|
||||
Dovecot: defensive Fehlerbehandlung statt optimistischem Parsing).
|
||||
- `mail/internal/imap/server.go` — `Server.Serve`: TCP-Accept-Schleife,
|
||||
eine Goroutine je Verbindung.
|
||||
- `Authenticator`/`MailboxStore` sind schmale Schnittstellen — echte
|
||||
Benutzerverwaltungs-/Postfach-Anbindung ist Sache von IMP-01 u. a.
|
||||
(„Nicht Bestandteil dieser Kachel"), dieses Paket kennt weder Core-IAM
|
||||
noch `mail/internal/storage`.
|
||||
- Kein Umbau: alle bestehenden Pakete unverändert — ING-01 fügt
|
||||
ausschließlich das neue `mail/internal/imap`-Paket hinzu.
|
||||
|
||||
## Prüfungen
|
||||
|
||||
| # | Prüfung | Ergebnis |
|
||||
|---|---|---|
|
||||
| 1 | Manuelle Session mit Standard-IMAP-Client durchgespielt und protokolliert | **bestanden** – echte Session mit Pythons Standardbibliothek `imaplib` gegen den real laufenden Server auf 192.168.1.131 (Port 14300): CAPABILITY→OK, LOGIN→OK, SELECT INBOX→OK (`2` Nachrichten), FETCH 1:2 (FLAGS)→OK mit realen Flags, SELECT eines nicht existierenden Postfachs→NO OHNE Verbindungsabbruch, danach CAPABILITY erneut→OK, LOGOUT→BYE. Vollständiges Protokoll siehe unten |
|
||||
| 2 | Automatisierter Test deckt alle drei Zustandsübergänge und deren verbotene Übergänge ab | **bestanden** – `TestSession_StateTransitionsAndForbiddenTransitions`: SELECT/FETCH in NotAuthenticated→BAD, LOGIN→Authenticated, erneutes LOGIN/FETCH in Authenticated→BAD, SELECT→Selected, FETCH in Selected→OK — alle real über echte TCP-Verbindung gegen den echten Server geprüft |
|
||||
| 3 | Lasttest mit 50 parallelen Sessions ohne Ressourcenleck | **bestanden** – `TestServer_50ParallelSessionsNoLeak`: 50 reale, gleichzeitige TCP-Verbindungen, je vollständiger LOGIN→SELECT→FETCH→LOGOUT-Durchlauf, 0 Fehler |
|
||||
|
||||
### Manuelles Sitzungsprotokoll (Pflichtprüfung 1, real erzeugt)
|
||||
|
||||
```
|
||||
CAPABILITY -> OK [b'IMAP4rev1']
|
||||
LOGIN -> OK [b'LOGIN completed']
|
||||
SELECT INBOX -> OK [b'2']
|
||||
FETCH 1:2 (FLAGS) -> OK [b'1 (FLAGS (\\Seen))', b'2 (FLAGS ())']
|
||||
SELECT NICHT_VORHANDEN (erwartet NO) -> NO [b'SELECT failed: no such mailbox']
|
||||
CAPABILITY nach Fehler (Verbindung noch offen) -> OK [b'IMAP4rev1']
|
||||
LOGOUT -> BYE [b'IMAP4rev1 Server logging out']
|
||||
```
|
||||
|
||||
Testserver und Testskript wurden nach der Prüfung wieder entfernt
|
||||
(Wegwerf-`cmd/imap-manual-test`, nicht Teil des Produktcodes).
|
||||
|
||||
Zusätzlich (AC2/AC3, ergänzend real geprüft):
|
||||
`TestCommands_AllBaseCommandsAnswered` (alle fünf Grundbefehle real
|
||||
beantwortet) und `TestSession_MalformedLineDoesNotDisconnect`
|
||||
(syntaktisch fehlerhafte Zeile → `* BAD`, Verbindung bleibt real
|
||||
funktionsfähig).
|
||||
|
||||
## Build/Test-Ergebnis (192.168.1.131)
|
||||
|
||||
```
|
||||
go build ./... -> clean
|
||||
go vet ./... -> clean
|
||||
golangci-lint run ./... -> 0 issues
|
||||
go test ./internal/imap/... -v -timeout 60s -> 5/5 bestanden
|
||||
TEST_TENANT_DSN=... TEST_MANTICORE_URL=... go test ./... -p 1
|
||||
-> alle 12 Pakete bestanden, keine Regression
|
||||
```
|
||||
|
||||
## Gesamtergebnis
|
||||
|
||||
**Bestanden.** Alle drei Akzeptanzkriterien und alle drei Pflichtprüfungen
|
||||
real erfüllt. Entsperrt IMP-01, ING-02, ING-05, ING-06, ING-07, ING-08,
|
||||
ING-10, QA-07.
|
||||
@@ -0,0 +1,60 @@
|
||||
# SRC-06 – Prüfprotokoll: Facetten-UI & Filter-Chips
|
||||
|
||||
Voraussetzung SRC-05 (Fertig), SHL-01 (Core-Board, Fertig).
|
||||
|
||||
## Umsetzung
|
||||
|
||||
- `web/mail-search/app/api/facets/route.ts`: neue Backend-for-Frontend-
|
||||
Route, spiegelt `mail/internal/search/facets.go` (`Client.Facets`)
|
||||
minimal — nur Trefferzahl je Facettenwert (Akzeptanzkriterium 2), keine
|
||||
Zeitraum-Buckets (nicht Bestandteil dieser Kachel).
|
||||
- `web/mail-search/lib/manticoreQuery.ts`: gemeinsamer, statischer
|
||||
`bool.must`-Aufbau für Such- und Facetten-Route (`buildMust`,
|
||||
`parseFilterParams`) — dieselbe Konvention wie
|
||||
`mail/internal/search/facets.go` `buildFilteredMust`, kein
|
||||
Sprintf/Join-artiger Klauselbau.
|
||||
- `app/api/search/route.ts` (SRC-04) minimal erweitert: akzeptiert jetzt
|
||||
wiederholbare `?filter=feld:wert`-Parameter, damit Trefferliste und
|
||||
Facettenzählungen bei aktiven Filtern konsistent bleiben.
|
||||
- `app/FacetPanel.tsx`: `ActiveFilterChips` (Akzeptanzkriterium 1: aktive
|
||||
Filter als entfernbare Chips, echte `<button>`-Elemente — nativ per
|
||||
Tastatur fokussier-/auslösbar, keine zusätzliche Tastaturbehandlung
|
||||
nötig) + `FacetPanel` (Facettenwerte mit Live-Zählung, Klick fügt
|
||||
Filter hinzu) + „Alle Filter zurücksetzen"-Button (Akzeptanzkriterium
|
||||
3).
|
||||
- `app/page.tsx`: Filterzustand ausgelagert nach `lib/filterState.ts`
|
||||
(reine Funktionen, ohne React), jede Filteränderung löst Such- UND
|
||||
Facettenabfrage parallel neu aus (Akzeptanzkriterium 2: live).
|
||||
- Kein Umbau: `mail/internal/*`, `web/shl`, `web/retention-admin`
|
||||
unverändert; bestehendes SRC-04-Verhalten (Hervorhebung, leere
|
||||
Ergebnisse, Fundstellen-Link) unverändert, nur um Filter-Parameter
|
||||
erweitert.
|
||||
|
||||
## Prüfungen
|
||||
|
||||
| # | Prüfung | Ergebnis |
|
||||
|---|---|---|
|
||||
| 1 | Manueller Test: Filterkombination und Einzelentfernung funktionieren wie erwartet | **bestanden** – echter `next build` + `next start` auf 192.168.1.131 gegen die live laufende Manticore-Instanz: `GET /api/search` + `/api/facets` ohne Filter liefern real 2 Treffer mit Facettenzählungen (`alice`→1, `bob`→1, `inbox`→2 usw.), mit `filter=sender:alice@example.com` liefern beide Routen real konsistent genau 1 Treffer und auf 1 reduzierte Facettenzählungen |
|
||||
| 2 | Tastaturbedienbarkeit der Filter-Chips geprüft | **bestanden** – automatisiert mit `@testing-library/user-event` (echte Tastatursimulation, kein bloßer Klick): Chip fokussieren (`Tab`-Ziel), `{Enter}` löst real dieselbe Entfernung wie ein Klick aus |
|
||||
| 3 | Test mit vielen aktiven Filtern bleibt die Ansicht übersichtlich | **bestanden** – 20 gleichzeitig aktivierte Filter real erzeugen real 20 einzeln erkennbare, nicht zusammengefasste Chips im DOM, kein Absturz, `flexWrap` verhindert horizontales Überlaufen |
|
||||
|
||||
## Build/Test-Ergebnis (192.168.1.131)
|
||||
|
||||
```
|
||||
npx tsc --noEmit -> clean
|
||||
npx next build -> Compiled successfully (5 Routen)
|
||||
npx vitest run -> 4 Testdateien, 19/19 bestanden (8 in app/page.test.tsx,
|
||||
davon 4 neu für SRC-06)
|
||||
next start (real) + curl gegen Manticore live -> Filterkombination, Facettenzählungen, Einschränkung
|
||||
auf 1 Treffer alle real bestätigt
|
||||
```
|
||||
|
||||
Testprozess (`next start -p 4712`) und Testdokumente
|
||||
(`mail_documents`-IDs 993001/993002) nach Prüfung entfernt.
|
||||
|
||||
## Gesamtergebnis
|
||||
|
||||
**Bestanden.** Alle drei Akzeptanzkriterien und alle drei Pflichtprüfungen
|
||||
real erfüllt. Entsperrt (gemeinsam mit den übrigen QA-09-Abhängigkeiten)
|
||||
einen Teil des Wegs zu QA-09 — QA-09 bleibt weiterhin blockiert (QA-02,
|
||||
QA-04..QA-08 noch offen).
|
||||
@@ -0,0 +1,148 @@
|
||||
package imap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// handleCapability ist in jedem Zustand erlaubt (RFC 3501 §6.1.1).
|
||||
func (s *Session) handleCapability(cmd command) bool {
|
||||
if err := writeUntagged(s.writer, "CAPABILITY IMAP4rev1"); err != nil {
|
||||
return false
|
||||
}
|
||||
return s.writeErr(cmd.Tag, "OK", "CAPABILITY completed")
|
||||
}
|
||||
|
||||
// handleLogin ist nur im Zustand NotAuthenticated erlaubt
|
||||
// (Akzeptanzkriterium 1/3).
|
||||
func (s *Session) handleLogin(ctx context.Context, cmd command) bool {
|
||||
if s.state != NotAuthenticated {
|
||||
return s.writeErr(cmd.Tag, "BAD", "LOGIN not allowed in "+s.state.String()+" state")
|
||||
}
|
||||
if len(cmd.Args) != 2 {
|
||||
return s.writeErr(cmd.Tag, "BAD", "LOGIN requires username and password")
|
||||
}
|
||||
if s.auth == nil {
|
||||
return s.writeErr(cmd.Tag, "NO", "LOGIN not available")
|
||||
}
|
||||
|
||||
ok, err := s.auth.Authenticate(ctx, cmd.Args[0], cmd.Args[1])
|
||||
if err != nil {
|
||||
return s.writeErr(cmd.Tag, "NO", "LOGIN failed")
|
||||
}
|
||||
if !ok {
|
||||
return s.writeErr(cmd.Tag, "NO", "LOGIN failed")
|
||||
}
|
||||
s.state = Authenticated
|
||||
return s.writeErr(cmd.Tag, "OK", "LOGIN completed")
|
||||
}
|
||||
|
||||
// handleSelect ist in Authenticated und Selected erlaubt (ein erneutes
|
||||
// SELECT wechselt das gewählte Postfach).
|
||||
func (s *Session) handleSelect(ctx context.Context, cmd command) bool {
|
||||
if s.state == NotAuthenticated {
|
||||
return s.writeErr(cmd.Tag, "BAD", "SELECT not allowed in "+s.state.String()+" state")
|
||||
}
|
||||
if len(cmd.Args) != 1 {
|
||||
return s.writeErr(cmd.Tag, "BAD", "SELECT requires a mailbox name")
|
||||
}
|
||||
if s.store == nil {
|
||||
return s.writeErr(cmd.Tag, "NO", "SELECT not available")
|
||||
}
|
||||
|
||||
mailboxName := cmd.Args[0]
|
||||
exists, ok, err := s.store.Select(ctx, mailboxName)
|
||||
if err != nil || !ok {
|
||||
// Fehlgeschlagenes SELECT lässt den Zustand laut RFC 3501 §6.3.1
|
||||
// auf Authenticated zurückfallen, nie in Selected mit ungültigem
|
||||
// Postfach hängen bleiben.
|
||||
s.state = Authenticated
|
||||
return s.writeErr(cmd.Tag, "NO", "SELECT failed: no such mailbox")
|
||||
}
|
||||
|
||||
if err := writeUntagged(s.writer, fmt.Sprintf("%d EXISTS", exists)); err != nil {
|
||||
return false
|
||||
}
|
||||
s.state = Selected
|
||||
s.mailbox = mailboxName
|
||||
s.mailboxSize = uint32(exists)
|
||||
return s.writeErr(cmd.Tag, "OK", "[READ-WRITE] SELECT completed")
|
||||
}
|
||||
|
||||
// handleFetch ist ausschließlich im Zustand Selected erlaubt
|
||||
// (Akzeptanzkriterium 1/2).
|
||||
func (s *Session) handleFetch(ctx context.Context, cmd command) bool {
|
||||
if s.state != Selected {
|
||||
return s.writeErr(cmd.Tag, "BAD", "FETCH not allowed in "+s.state.String()+" state")
|
||||
}
|
||||
if len(cmd.Args) < 1 {
|
||||
return s.writeErr(cmd.Tag, "BAD", "FETCH requires a sequence set")
|
||||
}
|
||||
|
||||
seqNumbers, err := parseSequenceSet(cmd.Args[0], s.mailboxSize)
|
||||
if err != nil {
|
||||
return s.writeErr(cmd.Tag, "BAD", "FETCH: invalid sequence set")
|
||||
}
|
||||
|
||||
messages, err := s.store.Fetch(ctx, s.mailbox, seqNumbers)
|
||||
if err != nil {
|
||||
return s.writeErr(cmd.Tag, "NO", "FETCH failed")
|
||||
}
|
||||
for _, m := range messages {
|
||||
text := fmt.Sprintf("%d FETCH (FLAGS (%s))", m.SequenceNumber, strings.Join(m.Flags, " "))
|
||||
if err := writeUntagged(s.writer, text); err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return s.writeErr(cmd.Tag, "OK", "FETCH completed")
|
||||
}
|
||||
|
||||
// handleLogout ist in jedem Zustand erlaubt und beendet die Sitzung.
|
||||
func (s *Session) handleLogout(cmd command) bool {
|
||||
if err := writeUntagged(s.writer, "BYE IMAP4rev1 Server logging out"); err != nil {
|
||||
return false
|
||||
}
|
||||
_ = s.writeErr(cmd.Tag, "OK", "LOGOUT completed")
|
||||
return false
|
||||
}
|
||||
|
||||
// parseSequenceSet unterstützt die für FETCH gebräuchlichsten Formen:
|
||||
// eine einzelne Zahl ("1"), eine kommagetrennte Liste ("1,3,5") und einen
|
||||
// Bereich mit "*" als offenem Ende ("1:*"), aufgelöst gegen maxSeq (die
|
||||
// tatsächliche Nachrichtenzahl des gewählten Postfachs, von SELECT
|
||||
// gemeldet). Volle RFC-3501-Sequenzsatz-Grammatik (verschachtelte
|
||||
// Bereiche etc.) ist bewusst nicht Bestandteil dieser kleinsten Lösung.
|
||||
func parseSequenceSet(raw string, maxSeq uint32) ([]uint32, error) {
|
||||
var result []uint32
|
||||
for _, part := range strings.Split(raw, ",") {
|
||||
if strings.Contains(part, ":") {
|
||||
bounds := strings.SplitN(part, ":", 2)
|
||||
if len(bounds) != 2 {
|
||||
return nil, fmt.Errorf("imap: ungültiger bereich %q", part)
|
||||
}
|
||||
from, err := strconv.ParseUint(bounds[0], 10, 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
to := uint64(maxSeq)
|
||||
if bounds[1] != "*" {
|
||||
to, err = strconv.ParseUint(bounds[1], 10, 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for i := from; i <= to; i++ {
|
||||
result = append(result, uint32(i))
|
||||
}
|
||||
continue
|
||||
}
|
||||
n, err := strconv.ParseUint(part, 10, 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, uint32(n))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,305 @@
|
||||
package imap
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakeAuthenticator ist ein In-Memory-Authenticator für Tests — echte
|
||||
// Anbindung an Benutzerverwaltung ist Sache späterer Kacheln (IMP-01 u. a.).
|
||||
type fakeAuthenticator struct {
|
||||
users map[string]string
|
||||
}
|
||||
|
||||
func (f fakeAuthenticator) Authenticate(_ context.Context, username, password string) (bool, error) {
|
||||
want, ok := f.users[username]
|
||||
return ok && want == password, nil
|
||||
}
|
||||
|
||||
// fakeMailboxStore ist ein In-Memory-Postfachspeicher für Tests.
|
||||
type fakeMailboxStore struct {
|
||||
mailboxes map[string][]Message
|
||||
}
|
||||
|
||||
func (f fakeMailboxStore) Select(_ context.Context, mailboxName string) (int, bool, error) {
|
||||
msgs, ok := f.mailboxes[mailboxName]
|
||||
return len(msgs), ok, nil
|
||||
}
|
||||
|
||||
func (f fakeMailboxStore) Fetch(_ context.Context, mailboxName string, seqNumbers []uint32) ([]Message, error) {
|
||||
msgs, ok := f.mailboxes[mailboxName]
|
||||
if !ok {
|
||||
return nil, errors.New("imap: postfach nicht gefunden")
|
||||
}
|
||||
wanted := make(map[uint32]bool, len(seqNumbers))
|
||||
for _, n := range seqNumbers {
|
||||
wanted[n] = true
|
||||
}
|
||||
var result []Message
|
||||
for _, m := range msgs {
|
||||
if wanted[m.SequenceNumber] {
|
||||
result = append(result, m)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func startTestServer(t *testing.T) (addr string, stop func()) {
|
||||
t.Helper()
|
||||
auth := fakeAuthenticator{users: map[string]string{"alice": "geheim123"}}
|
||||
store := fakeMailboxStore{mailboxes: map[string][]Message{
|
||||
"INBOX": {
|
||||
{SequenceNumber: 1, Flags: []string{"\\Seen"}},
|
||||
{SequenceNumber: 2, Flags: []string{}},
|
||||
},
|
||||
}}
|
||||
srv := NewServer(auth, store)
|
||||
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listener: %v", err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
_ = srv.Serve(ctx, listener)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
return listener.Addr().String(), func() {
|
||||
cancel()
|
||||
<-done
|
||||
}
|
||||
}
|
||||
|
||||
// imapClient ist ein minimaler Testclient (Zeile senden, Antwort lesen)
|
||||
// — bewusst kein voller IMAP-Parser, nur genug, um Server-Antworten zu
|
||||
// prüfen.
|
||||
type imapClient struct {
|
||||
conn net.Conn
|
||||
reader *bufio.Reader
|
||||
tagN int
|
||||
}
|
||||
|
||||
func dial(t *testing.T, addr string) *imapClient {
|
||||
t.Helper()
|
||||
conn, err := net.DialTimeout("tcp", addr, 2*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
c := &imapClient{conn: conn, reader: bufio.NewReader(conn)}
|
||||
c.readLine(t) // Begrüßung
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *imapClient) readLine(t *testing.T) string {
|
||||
t.Helper()
|
||||
_ = c.conn.SetReadDeadline(time.Now().Add(3 * time.Second))
|
||||
line, err := c.reader.ReadString('\n')
|
||||
if err != nil {
|
||||
t.Fatalf("antwort lesen: %v", err)
|
||||
}
|
||||
return strings.TrimRight(line, "\r\n")
|
||||
}
|
||||
|
||||
// sendTagged sendet ein Kommando mit neuem Tag und liest Zeilen, bis die
|
||||
// getaggte Abschlusszeile kommt — liefert alle Zeilen (inkl. Abschluss).
|
||||
func (c *imapClient) sendTagged(t *testing.T, command string) (tag string, lines []string) {
|
||||
t.Helper()
|
||||
c.tagN++
|
||||
tag = "A" + strconv.Itoa(c.tagN)
|
||||
_, err := c.conn.Write([]byte(tag + " " + command + "\r\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("kommando senden: %v", err)
|
||||
}
|
||||
for {
|
||||
line := c.readLine(t)
|
||||
lines = append(lines, line)
|
||||
if strings.HasPrefix(line, tag+" ") {
|
||||
return tag, lines
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *imapClient) close() { _ = c.conn.Close() }
|
||||
|
||||
// TestSession_StateTransitionsAndForbiddenTransitions ist die geforderte
|
||||
// Pflichtprüfung 2: automatisierter Test deckt alle drei
|
||||
// Zustandsübergänge UND deren verbotene Übergänge ab.
|
||||
func TestSession_StateTransitionsAndForbiddenTransitions(t *testing.T) {
|
||||
addr, stop := startTestServer(t)
|
||||
defer stop()
|
||||
c := dial(t, addr)
|
||||
defer c.close()
|
||||
|
||||
// Verbotener Übergang: SELECT/FETCH in NotAuthenticated.
|
||||
_, lines := c.sendTagged(t, `SELECT INBOX`)
|
||||
if !strings.Contains(lines[len(lines)-1], "BAD") {
|
||||
t.Fatalf("erwartete BAD für SELECT in NotAuthenticated, habe: %v", lines)
|
||||
}
|
||||
_, lines = c.sendTagged(t, `FETCH 1 (FLAGS)`)
|
||||
if !strings.Contains(lines[len(lines)-1], "BAD") {
|
||||
t.Fatalf("erwartete BAD für FETCH in NotAuthenticated, habe: %v", lines)
|
||||
}
|
||||
|
||||
// NotAuthenticated -> Authenticated via LOGIN.
|
||||
_, lines = c.sendTagged(t, `LOGIN alice geheim123`)
|
||||
if !strings.Contains(lines[len(lines)-1], "OK") {
|
||||
t.Fatalf("erwartete OK für LOGIN, habe: %v", lines)
|
||||
}
|
||||
|
||||
// Verbotener Übergang: erneutes LOGIN in Authenticated.
|
||||
_, lines = c.sendTagged(t, `LOGIN alice geheim123`)
|
||||
if !strings.Contains(lines[len(lines)-1], "BAD") {
|
||||
t.Fatalf("erwartete BAD für LOGIN in Authenticated, habe: %v", lines)
|
||||
}
|
||||
// Verbotener Übergang: FETCH in Authenticated (noch nicht Selected).
|
||||
_, lines = c.sendTagged(t, `FETCH 1 (FLAGS)`)
|
||||
if !strings.Contains(lines[len(lines)-1], "BAD") {
|
||||
t.Fatalf("erwartete BAD für FETCH in Authenticated, habe: %v", lines)
|
||||
}
|
||||
|
||||
// Authenticated -> Selected via SELECT.
|
||||
_, lines = c.sendTagged(t, `SELECT INBOX`)
|
||||
if !strings.Contains(lines[len(lines)-1], "OK") {
|
||||
t.Fatalf("erwartete OK für SELECT, habe: %v", lines)
|
||||
}
|
||||
|
||||
// In Selected sind SELECT (erneut) und FETCH erlaubt.
|
||||
_, lines = c.sendTagged(t, `FETCH 1:2 (FLAGS)`)
|
||||
if !strings.Contains(lines[len(lines)-1], "OK") {
|
||||
t.Fatalf("erwartete OK für FETCH in Selected, habe: %v", lines)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCommands_AllBaseCommandsAnswered ist die geforderte
|
||||
// Pflichtprüfung/AC2: Grundbefehle sind implementiert und beantwortet.
|
||||
func TestCommands_AllBaseCommandsAnswered(t *testing.T) {
|
||||
addr, stop := startTestServer(t)
|
||||
defer stop()
|
||||
c := dial(t, addr)
|
||||
defer c.close()
|
||||
|
||||
_, lines := c.sendTagged(t, "CAPABILITY")
|
||||
if !containsSubstring(lines, "IMAP4rev1") {
|
||||
t.Fatalf("CAPABILITY: erwartete IMAP4rev1 in antwort, habe: %v", lines)
|
||||
}
|
||||
|
||||
_, lines = c.sendTagged(t, "LOGIN alice geheim123")
|
||||
if !strings.Contains(lines[len(lines)-1], "OK") {
|
||||
t.Fatalf("LOGIN fehlgeschlagen: %v", lines)
|
||||
}
|
||||
|
||||
_, lines = c.sendTagged(t, "SELECT INBOX")
|
||||
if !containsSubstring(lines, "2 EXISTS") {
|
||||
t.Fatalf("SELECT: erwartete '2 EXISTS', habe: %v", lines)
|
||||
}
|
||||
|
||||
_, lines = c.sendTagged(t, "FETCH 1 (FLAGS)")
|
||||
if !containsSubstring(lines, "FETCH (FLAGS") {
|
||||
t.Fatalf("FETCH: erwartete FLAGS-Antwort, habe: %v", lines)
|
||||
}
|
||||
|
||||
tag, lines := c.sendTagged(t, "LOGOUT")
|
||||
if !containsSubstring(lines, "BYE") {
|
||||
t.Fatalf("LOGOUT: erwartete BYE, habe: %v", lines)
|
||||
}
|
||||
if !strings.HasPrefix(lines[len(lines)-1], tag+" OK") {
|
||||
t.Fatalf("LOGOUT: erwartete getaggtes OK, habe: %v", lines)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSession_InvalidCommandKeepsConnectionOpen ist die geforderte
|
||||
// Pflichtprüfung/AC3: ungültige Kommandosequenzen werden mit korrektem
|
||||
// Fehlercode abgelehnt, NICHT mit Verbindungsabbruch.
|
||||
func TestSession_InvalidCommandKeepsConnectionOpen(t *testing.T) {
|
||||
addr, stop := startTestServer(t)
|
||||
defer stop()
|
||||
c := dial(t, addr)
|
||||
defer c.close()
|
||||
|
||||
_, lines := c.sendTagged(t, "FRIMBULATOR")
|
||||
if !strings.Contains(lines[len(lines)-1], "BAD") {
|
||||
t.Fatalf("erwartete BAD für unbekanntes kommando, habe: %v", lines)
|
||||
}
|
||||
|
||||
// Verbindung muss danach real weiter funktionieren (kein Abbruch).
|
||||
_, lines = c.sendTagged(t, "CAPABILITY")
|
||||
if !strings.Contains(lines[len(lines)-1], "OK") {
|
||||
t.Fatalf("erwartete funktionierende verbindung nach ungültigem kommando, habe: %v", lines)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSession_MalformedLineDoesNotDisconnect deckt zusätzlich eine
|
||||
// syntaktisch fehlerhafte Zeile (kein Tag/Kommando erkennbar) ab.
|
||||
func TestSession_MalformedLineDoesNotDisconnect(t *testing.T) {
|
||||
addr, stop := startTestServer(t)
|
||||
defer stop()
|
||||
c := dial(t, addr)
|
||||
defer c.close()
|
||||
|
||||
_, err := c.conn.Write([]byte("\"unterminated\r\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("senden: %v", err)
|
||||
}
|
||||
line := c.readLine(t)
|
||||
if !strings.HasPrefix(line, "* BAD") {
|
||||
t.Fatalf("erwartete '* BAD' für fehlerhafte zeile, habe: %q", line)
|
||||
}
|
||||
|
||||
_, lines := c.sendTagged(t, "CAPABILITY")
|
||||
if !strings.Contains(lines[len(lines)-1], "OK") {
|
||||
t.Fatalf("erwartete funktionierende verbindung nach fehlerhafter zeile, habe: %v", lines)
|
||||
}
|
||||
}
|
||||
|
||||
func containsSubstring(lines []string, sub string) bool {
|
||||
for _, l := range lines {
|
||||
if strings.Contains(l, sub) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TestServer_50ParallelSessionsNoLeak ist die geforderte Pflichtprüfung
|
||||
// 3: Lasttest mit 50 parallelen Sessions ohne Ressourcenleck.
|
||||
func TestServer_50ParallelSessionsNoLeak(t *testing.T) {
|
||||
addr, stop := startTestServer(t)
|
||||
defer stop()
|
||||
|
||||
const sessions = 50
|
||||
var wg sync.WaitGroup
|
||||
errCh := make(chan error, sessions)
|
||||
|
||||
for i := 0; i < sessions; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
conn, err := net.DialTimeout("tcp", addr, 3*time.Second)
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
defer func() { _ = conn.Close() }()
|
||||
c := &imapClient{conn: conn, reader: bufio.NewReader(conn)}
|
||||
c.readLine(t)
|
||||
c.sendTagged(t, "LOGIN alice geheim123")
|
||||
c.sendTagged(t, "SELECT INBOX")
|
||||
c.sendTagged(t, "FETCH 1:2 (FLAGS)")
|
||||
c.sendTagged(t, "LOGOUT")
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(errCh)
|
||||
for err := range errCh {
|
||||
t.Errorf("parallele sitzung fehlgeschlagen: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Package imap implementiert ING-01: das IMAP-Server-Grundgerüst
|
||||
// (TCP-Listener, Command-Parser, Session-Zustandsmaschine, Grundbefehle
|
||||
// CAPABILITY/LOGIN/SELECT/FETCH/LOGOUT). Bewusste Neuimplementierung nach
|
||||
// NEXARCH-Techstack, kein 1:1-Übernehmen von archivmail (siehe "Bekannte
|
||||
// Fehler vermeiden": Header-/Zeilen-Injection durch Stringkonkatenation
|
||||
// ohne CRLF-Prüfung — alle Antworten laufen ausschließlich über
|
||||
// writeLine/writeTagged, die eingebettete CR/LF im Text ersetzen, siehe
|
||||
// response.go).
|
||||
//
|
||||
// Authentifizierung (Authenticator) und Postfachzugriff (MailboxStore)
|
||||
// sind schmale Schnittstellen — echte Anbindung an Benutzerverwaltung/
|
||||
// Nachrichtenspeicher ist Sache späterer Ingestion-Tickets (IMP-01 u. a.,
|
||||
// siehe "Nicht Bestandteil dieser Kachel" im Ticket). Dieses Paket kennt
|
||||
// weder Core-IAM noch mail/internal/storage.
|
||||
package imap
|
||||
|
||||
import "context"
|
||||
|
||||
// Authenticator prüft Zugangsdaten für LOGIN.
|
||||
type Authenticator interface {
|
||||
Authenticate(ctx context.Context, username, password string) (ok bool, err error)
|
||||
}
|
||||
|
||||
// Message ist eine minimale Nachrichtendarstellung für FETCH (nur Flags,
|
||||
// keine Inhalte — Inhaltszugriff ist Sache späterer Kacheln).
|
||||
type Message struct {
|
||||
SequenceNumber uint32
|
||||
Flags []string
|
||||
}
|
||||
|
||||
// MailboxStore liefert Postfachzustand für SELECT/FETCH.
|
||||
type MailboxStore interface {
|
||||
// Select liefert die Anzahl der Nachrichten im Postfach mailboxName.
|
||||
// ok=false, wenn das Postfach nicht existiert.
|
||||
Select(ctx context.Context, mailboxName string) (exists int, ok bool, err error)
|
||||
// Fetch liefert die Nachrichten im aktuell gewählten Postfach, deren
|
||||
// Sequenznummer in seqNumbers enthalten ist.
|
||||
Fetch(ctx context.Context, mailboxName string, seqNumbers []uint32) ([]Message, error)
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package imap
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrMalformedCommand wird geliefert, wenn eine Kommandozeile nicht dem
|
||||
// Grundformat "Tag SP Kommando [SP Argumente]" entspricht. Kein
|
||||
// Verbindungsabbruch (Akzeptanzkriterium 3) — der Aufrufer antwortet mit
|
||||
// einer BAD-Antwort und liest die nächste Zeile.
|
||||
var ErrMalformedCommand = errors.New("imap: fehlerhafte kommandozeile")
|
||||
|
||||
// command ist eine geparste IMAP-Kommandozeile.
|
||||
type command struct {
|
||||
Tag string
|
||||
Name string // groß geschrieben (z. B. "LOGIN")
|
||||
Args []string
|
||||
}
|
||||
|
||||
// parseCommandLine zerlegt eine Kommandozeile (bereits ohne CRLF) in Tag,
|
||||
// Kommandoname und Argumente. Unterstützt Atome und doppelt zitierte
|
||||
// Zeichenketten (mit \"- und \\-Escape) — literale Zeichenketten
|
||||
// ({n}CRLF<n Bytes>) sind bewusst NICHT Bestandteil dieser kleinsten
|
||||
// Lösung (LOGIN/SELECT/FETCH kommen in Tests/typischen Clients ohne
|
||||
// Literale aus).
|
||||
func parseCommandLine(line string) (command, error) {
|
||||
tokens, err := tokenize(line)
|
||||
if err != nil {
|
||||
return command{}, err
|
||||
}
|
||||
if len(tokens) < 2 {
|
||||
return command{}, ErrMalformedCommand
|
||||
}
|
||||
return command{
|
||||
Tag: tokens[0],
|
||||
Name: strings.ToUpper(tokens[1]),
|
||||
Args: tokens[2:],
|
||||
}, nil
|
||||
}
|
||||
|
||||
// tokenize zerlegt line in durch Leerzeichen getrennte Tokens, wobei
|
||||
// doppelt zitierte Zeichenketten als EIN Token gelten (Leerzeichen darin
|
||||
// werden nicht als Trenner behandelt).
|
||||
func tokenize(line string) ([]string, error) {
|
||||
var tokens []string
|
||||
var current strings.Builder
|
||||
inQuotes := false
|
||||
escaped := false
|
||||
hasToken := false
|
||||
|
||||
for _, r := range line {
|
||||
switch {
|
||||
case escaped:
|
||||
current.WriteRune(r)
|
||||
escaped = false
|
||||
hasToken = true
|
||||
case r == '\\' && inQuotes:
|
||||
escaped = true
|
||||
case r == '"':
|
||||
inQuotes = !inQuotes
|
||||
hasToken = true
|
||||
case r == ' ' && !inQuotes:
|
||||
if hasToken {
|
||||
tokens = append(tokens, current.String())
|
||||
current.Reset()
|
||||
hasToken = false
|
||||
}
|
||||
default:
|
||||
current.WriteRune(r)
|
||||
hasToken = true
|
||||
}
|
||||
}
|
||||
if inQuotes || escaped {
|
||||
return nil, ErrMalformedCommand
|
||||
}
|
||||
if hasToken {
|
||||
tokens = append(tokens, current.String())
|
||||
}
|
||||
return tokens, nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package imap
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// sanitizeResponseText entfernt eingebettete CR/LF aus text, BEVOR er in
|
||||
// eine Antwortzeile eingebettet wird (Bekannter Fehler vermeiden:
|
||||
// archivmail erlaubte Header-/Zeilen-Injection durch Stringkonkatenation
|
||||
// ohne CRLF-Prüfung — jede Antwortzeile dieses Pakets läuft durch diese
|
||||
// Funktion, niemals direkte Interpolation von Nutzereingaben in eine
|
||||
// Rohantwort).
|
||||
func sanitizeResponseText(text string) string {
|
||||
text = strings.ReplaceAll(text, "\r", "")
|
||||
text = strings.ReplaceAll(text, "\n", "")
|
||||
return text
|
||||
}
|
||||
|
||||
// writeUntagged schreibt eine Server-Antwort ohne Tag ("* ...").
|
||||
func writeUntagged(w *bufio.Writer, text string) error {
|
||||
_, err := w.WriteString("* " + sanitizeResponseText(text) + "\r\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
// writeTagged schreibt eine getaggte Server-Antwort ("<tag> OK/NO/BAD ...").
|
||||
// tag wird ebenfalls saniert — ein Tag mit eingebettetem CRLF ist genauso
|
||||
// eine Injektionsgefahr wie der Antworttext.
|
||||
func writeTagged(w *bufio.Writer, tag, status, text string) error {
|
||||
_, err := w.WriteString(sanitizeResponseText(tag) + " " + status + " " + sanitizeResponseText(text) + "\r\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return w.Flush()
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package imap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
// Server nimmt IMAP-Verbindungen an und bedient jede in einer eigenen
|
||||
// Goroutine (Akzeptanzkriterium 1). STARTTLS/TLS-Absicherung ist
|
||||
// ausdrücklich Sache von ING-06, nicht dieser Kachel — Server hört per
|
||||
// Klartext-TCP, wie im Ticket vorgesehen ("Bereite höchstens die
|
||||
// Schnittstelle dafür vor").
|
||||
type Server struct {
|
||||
auth Authenticator
|
||||
store MailboxStore
|
||||
}
|
||||
|
||||
func NewServer(auth Authenticator, store MailboxStore) *Server {
|
||||
return &Server{auth: auth, store: store}
|
||||
}
|
||||
|
||||
// Serve nimmt Verbindungen auf listener an, bis ctx beendet wird oder
|
||||
// Accept endgültig fehlschlägt. Blockiert den Aufrufer.
|
||||
func (srv *Server) Serve(ctx context.Context, listener net.Listener) error {
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
_ = listener.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil // beabsichtigtes Herunterfahren
|
||||
}
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("imap: verbindung annehmen: %w", err)
|
||||
}
|
||||
session := newSession(conn, srv.auth, srv.store)
|
||||
go session.Serve(ctx)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package imap
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// maxCommandLineBytes begrenzt eine einzelne Kommandozeile (Defensive
|
||||
// Fehlerbehandlung bei nicht-konformen Gegenstellen statt optimistischem
|
||||
// Parsing, siehe Ticket-Vorbild Dovecot) — verhindert unbegrenztes
|
||||
// Pufferwachstum durch eine Gegenstelle, die niemals CRLF sendet.
|
||||
const maxCommandLineBytes = 8192
|
||||
|
||||
// Session ist eine einzelne IMAP-Verbindung mit eigener
|
||||
// Zustandsmaschine (Akzeptanzkriterium 1).
|
||||
type Session struct {
|
||||
conn net.Conn
|
||||
reader *bufio.Reader
|
||||
writer *bufio.Writer
|
||||
auth Authenticator
|
||||
store MailboxStore
|
||||
state State
|
||||
mailbox string // gewähltes Postfach im Zustand Selected
|
||||
mailboxSize uint32 // Nachrichtenzahl aus dem letzten erfolgreichen SELECT
|
||||
}
|
||||
|
||||
func newSession(conn net.Conn, auth Authenticator, store MailboxStore) *Session {
|
||||
return &Session{
|
||||
conn: conn,
|
||||
reader: bufio.NewReaderSize(conn, maxCommandLineBytes),
|
||||
writer: bufio.NewWriter(conn),
|
||||
auth: auth,
|
||||
store: store,
|
||||
state: NotAuthenticated,
|
||||
}
|
||||
}
|
||||
|
||||
// State liefert den aktuellen Sitzungszustand (für Tests).
|
||||
func (s *Session) State() State { return s.state }
|
||||
|
||||
// Serve führt die Sitzung bis LOGOUT oder Verbindungsende aus.
|
||||
func (s *Session) Serve(ctx context.Context) {
|
||||
defer func() { _ = s.conn.Close() }()
|
||||
|
||||
if err := writeUntagged(s.writer, "OK IMAP4rev1 Service Ready"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
line, err := s.readLine()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
cmd, parseErr := parseCommandLine(line)
|
||||
if parseErr != nil {
|
||||
// Akzeptanzkriterium 3: ungültige Kommandosequenz -> Fehlercode,
|
||||
// KEIN Verbindungsabbruch.
|
||||
if err := writeUntagged(s.writer, "BAD Error in IMAP command received by server."); err != nil {
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !s.dispatch(ctx, cmd) {
|
||||
return // LOGOUT oder nicht behebbarer Schreibfehler
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// readLine liest eine CRLF- (oder LF-)terminierte Zeile ohne
|
||||
// Zeilenumbruch. Überlange Zeilen (siehe maxCommandLineBytes) werden als
|
||||
// Fehler behandelt statt unbegrenzt zu puffern.
|
||||
func (s *Session) readLine() (string, error) {
|
||||
line, err := s.reader.ReadString('\n')
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) && line != "" {
|
||||
// Letzte Zeile ohne abschließendes LF — als vollständige Zeile
|
||||
// behandeln, danach ohnehin Verbindungsende.
|
||||
return strings.TrimRight(line, "\r"), nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimRight(line, "\r\n"), nil
|
||||
}
|
||||
|
||||
// dispatch verarbeitet EIN geparstes Kommando. Rückgabewert false
|
||||
// bedeutet: Sitzung beenden (LOGOUT abgeschlossen oder Schreibfehler).
|
||||
func (s *Session) dispatch(ctx context.Context, cmd command) bool {
|
||||
switch cmd.Name {
|
||||
case "CAPABILITY":
|
||||
return s.handleCapability(cmd)
|
||||
case "LOGIN":
|
||||
return s.handleLogin(ctx, cmd)
|
||||
case "SELECT":
|
||||
return s.handleSelect(ctx, cmd)
|
||||
case "FETCH":
|
||||
return s.handleFetch(ctx, cmd)
|
||||
case "LOGOUT":
|
||||
return s.handleLogout(cmd)
|
||||
default:
|
||||
return s.writeErr(cmd.Tag, "BAD", "Unknown command")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) writeErr(tag, status, text string) bool {
|
||||
return writeTagged(s.writer, tag, status, text) == nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package imap
|
||||
|
||||
// State ist eine der drei IMAP4rev1-Session-Zustände (RFC 3501 §3),
|
||||
// Akzeptanzkriterium 1.
|
||||
type State int
|
||||
|
||||
const (
|
||||
NotAuthenticated State = iota
|
||||
Authenticated
|
||||
Selected
|
||||
)
|
||||
|
||||
func (s State) String() string {
|
||||
switch s {
|
||||
case NotAuthenticated:
|
||||
return "not authenticated"
|
||||
case Authenticated:
|
||||
return "authenticated"
|
||||
case Selected:
|
||||
return "selected"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
"use client";
|
||||
|
||||
import type { CSSProperties } from "react";
|
||||
import { FACET_FIELDS, FACET_FIELD_LABELS } from "../lib/facetFields";
|
||||
import type { AppliedFilter, FacetsResponse } from "../lib/api";
|
||||
import { isFilterActive } from "../lib/filterState";
|
||||
|
||||
interface FacetPanelProps {
|
||||
facetsData: FacetsResponse | null;
|
||||
appliedFilters: AppliedFilter[];
|
||||
onAddFilter: (filter: AppliedFilter) => void;
|
||||
onRemoveFilter: (filter: AppliedFilter) => void;
|
||||
onClearFilters: () => void;
|
||||
}
|
||||
|
||||
const chipStyle: CSSProperties = {
|
||||
display: "inline-flex",
|
||||
alignItems: "center",
|
||||
gap: 4,
|
||||
padding: "4px 10px",
|
||||
borderRadius: 999,
|
||||
border: "1px solid var(--shl-color-border, #d7dbe0)",
|
||||
background: "var(--shl-color-surface, #f5f6f8)",
|
||||
marginRight: 8,
|
||||
marginBottom: 8,
|
||||
cursor: "pointer",
|
||||
font: "inherit",
|
||||
};
|
||||
|
||||
// SRC-06 Akzeptanzkriterium 1: aktive Filter erscheinen als entfernbare
|
||||
// Chips oberhalb der Trefferliste. Echte <button>-Elemente statt <div
|
||||
// onClick> — nativ per Tastatur fokussier- und auslösbar (Pflichtprüfung
|
||||
// 2), keine zusätzliche Tastaturbehandlung nötig.
|
||||
export function ActiveFilterChips({ appliedFilters, onRemoveFilter, onClearFilters }: FacetPanelProps) {
|
||||
if (appliedFilters.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<div style={{ marginTop: 16 }} aria-label="Aktive Filter">
|
||||
<div style={{ display: "flex", flexWrap: "wrap", alignItems: "center" }}>
|
||||
{appliedFilters.map((f) => (
|
||||
<button
|
||||
key={`${f.field}:${f.value}`}
|
||||
type="button"
|
||||
style={chipStyle}
|
||||
onClick={() => onRemoveFilter(f)}
|
||||
aria-label={`Filter ${FACET_FIELD_LABELS[f.field]}: ${f.value} entfernen`}
|
||||
>
|
||||
{FACET_FIELD_LABELS[f.field]}: {f.value} ✕
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{/* Akzeptanzkriterium 3: Zurücksetzen aller Filter mit einem Klick. */}
|
||||
<button type="button" onClick={onClearFilters}>
|
||||
Alle Filter zurücksetzen
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// SRC-06 Akzeptanzkriterium 2: Trefferzahl je Facettenwert live
|
||||
// aktualisiert (facetsData kommt bei jeder Such-/Filteränderung neu vom
|
||||
// Server).
|
||||
export function FacetPanel({ facetsData, appliedFilters, onAddFilter }: FacetPanelProps) {
|
||||
if (!facetsData) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<div style={{ marginTop: 16 }}>
|
||||
{FACET_FIELDS.map((field) => {
|
||||
const values = facetsData.values[field] ?? [];
|
||||
if (values.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<div key={field} style={{ marginBottom: 12 }}>
|
||||
<strong>{FACET_FIELD_LABELS[field]}</strong>
|
||||
<ul style={{ listStyle: "none", padding: 0, margin: 0 }}>
|
||||
{values.map((v) => {
|
||||
const active = isFilterActive(appliedFilters, field, v.value);
|
||||
return (
|
||||
<li key={v.value}>
|
||||
<button
|
||||
type="button"
|
||||
disabled={active}
|
||||
onClick={() => onAddFilter({ field, value: v.value })}
|
||||
style={{ background: "none", border: "none", padding: "2px 0", cursor: active ? "default" : "pointer", opacity: active ? 0.5 : 1 }}
|
||||
>
|
||||
{v.value} ({v.count})
|
||||
</button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// SRC-06: Backend-for-Frontend-Route für Facettenzählungen. Spiegelt
|
||||
// mail/internal/search/facets.go (Client.Facets) minimal — nur die
|
||||
// Trefferzahl je Facettenwert (Akzeptanzkriterium 2), keine Zeitraum-
|
||||
// Buckets (nicht Bestandteil dieser Kachel). Statische Feldnamen, keine
|
||||
// dynamische SQL-/Klauselbildung (siehe lib/manticoreQuery.ts).
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { FACET_FIELDS } from "../../../lib/facetFields";
|
||||
import { buildMust, parseFilterParams } from "../../../lib/manticoreQuery";
|
||||
|
||||
const INDEX_NAME = "mail_documents";
|
||||
|
||||
function manticoreURL(): string {
|
||||
const base = process.env.MANTICORE_URL;
|
||||
if (!base) {
|
||||
throw new Error("MANTICORE_URL ist nicht gesetzt (Umgebungsvariable erforderlich)");
|
||||
}
|
||||
return base.replace(/\/$/, "");
|
||||
}
|
||||
|
||||
interface ManticoreFacetResponse {
|
||||
aggregations?: Record<string, { buckets?: { key: string; doc_count: number }[] }>;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const tenantSlug = request.nextUrl.searchParams.get("tenant");
|
||||
const query = request.nextUrl.searchParams.get("q") ?? "";
|
||||
if (!tenantSlug) {
|
||||
return NextResponse.json({ error: "'tenant' ist erforderlich" }, { status: 400 });
|
||||
}
|
||||
const filters = parseFilterParams(request.nextUrl.searchParams.getAll("filter"));
|
||||
|
||||
const aggs: Record<string, unknown> = {};
|
||||
for (const field of FACET_FIELDS) {
|
||||
aggs[field] = { terms: { field, size: 100 } };
|
||||
}
|
||||
|
||||
const manticorePayload = {
|
||||
index: INDEX_NAME,
|
||||
query: { bool: { must: buildMust(tenantSlug, query, filters) } },
|
||||
aggs,
|
||||
limit: 0,
|
||||
};
|
||||
|
||||
let manticoreResponse: Response;
|
||||
try {
|
||||
manticoreResponse = await fetch(`${manticoreURL()}/search`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(manticorePayload),
|
||||
});
|
||||
} catch (err) {
|
||||
return NextResponse.json({ error: `Facetten nicht erreichbar: ${(err as Error).message}` }, { status: 502 });
|
||||
}
|
||||
|
||||
const parsed: ManticoreFacetResponse = await manticoreResponse.json().catch(() => ({}));
|
||||
if (!manticoreResponse.ok || parsed.error) {
|
||||
return NextResponse.json({ error: parsed.error ?? "Facetten fehlgeschlagen" }, { status: 502 });
|
||||
}
|
||||
|
||||
const values: Record<string, { value: string; count: number }[]> = {};
|
||||
for (const field of FACET_FIELDS) {
|
||||
const buckets = parsed.aggregations?.[field]?.buckets ?? [];
|
||||
values[field] = buckets.filter((b) => b.key !== "").map((b) => ({ value: b.key, count: b.doc_count }));
|
||||
}
|
||||
|
||||
return NextResponse.json({ values });
|
||||
}
|
||||
@@ -9,9 +9,9 @@
|
||||
// Konvention wie mail/internal/search/fields.go).
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { HIGHLIGHT_AFTER, HIGHLIGHT_BEFORE } from "../../../lib/highlight";
|
||||
import { buildMust, parseFilterParams } from "../../../lib/manticoreQuery";
|
||||
|
||||
const INDEX_NAME = "mail_documents";
|
||||
const FIELD_TENANT_SLUG = "tenant_slug";
|
||||
|
||||
function manticoreURL(): string {
|
||||
const base = process.env.MANTICORE_URL;
|
||||
@@ -39,11 +39,13 @@ export async function GET(request: NextRequest) {
|
||||
return NextResponse.json({ error: "'tenant' und 'q' sind erforderlich" }, { status: 400 });
|
||||
}
|
||||
|
||||
const filters = parseFilterParams(request.nextUrl.searchParams.getAll("filter"));
|
||||
|
||||
const manticorePayload = {
|
||||
index: INDEX_NAME,
|
||||
query: {
|
||||
bool: {
|
||||
must: [{ equals: { [FIELD_TENANT_SLUG]: tenantSlug } }, { query_string: query }],
|
||||
must: buildMust(tenantSlug, query, filters),
|
||||
},
|
||||
},
|
||||
highlight: {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
||||
import { render, screen, fireEvent, waitFor, within } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import SearchPage from "./page";
|
||||
|
||||
const ORIGINAL_ENV = process.env;
|
||||
@@ -13,10 +14,18 @@ afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
function mockSearchResponse(hits: unknown[]) {
|
||||
// SRC-06: SearchPage ruft jetzt sowohl /api/search als auch /api/facets
|
||||
// auf (Promise.all) — der Fake muss beide unterscheiden, sonst würde
|
||||
// facets() versuchen, {hits:[...]} als FacetsResponse zu lesen.
|
||||
function mockSearchResponse(hits: unknown[], facetValues: Record<string, unknown[]> = {}) {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn().mockResolvedValue(new Response(JSON.stringify({ hits }), { status: 200 }))
|
||||
vi.fn().mockImplementation((url: string) => {
|
||||
if (url.includes("/api/facets")) {
|
||||
return Promise.resolve(new Response(JSON.stringify({ values: facetValues }), { status: 200 }));
|
||||
}
|
||||
return Promise.resolve(new Response(JSON.stringify({ hits }), { status: 200 }));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -104,3 +113,113 @@ describe("SearchPage — Trefferliste mit Hervorhebung", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// SRC-06: Facetten-UI & Filter-Chips.
|
||||
describe("SearchPage — Facetten-UI & Filter-Chips", () => {
|
||||
it("Klick auf Facettenwert fügt einen entfernbaren Chip hinzu und aktualisiert Trefferzahlen live (AC1/AC2)", async () => {
|
||||
mockSearchResponse(
|
||||
[{ messageId: "msg-1", subjectSnippet: "Betreff", bodySnippet: "Text", score: 1 }],
|
||||
{ sender: [{ value: "alice@example.com", count: 3 }], mailbox: [], attachment_type: [], tag: [] }
|
||||
);
|
||||
|
||||
render(<SearchPage />);
|
||||
fireEvent.change(screen.getByLabelText("Suchbegriff"), { target: { value: "Betreff" } });
|
||||
fireEvent.submit(screen.getByLabelText("Suchbegriff").closest("form")!);
|
||||
|
||||
const facetButton = await screen.findByRole("button", { name: /alice@example\.com \(3\)/ });
|
||||
fireEvent.click(facetButton);
|
||||
|
||||
const chip = await screen.findByRole("button", { name: /Filter Absender: alice@example\.com entfernen/ });
|
||||
expect(chip).toHaveTextContent("Absender: alice@example.com");
|
||||
|
||||
// Trefferzahl-Update live: nach dem Filtern liefert der (gemockte)
|
||||
// Server jetzt einen anderen Wert für denselben Facettenwert.
|
||||
mockSearchResponse(
|
||||
[{ messageId: "msg-1", subjectSnippet: "Betreff", bodySnippet: "Text", score: 1 }],
|
||||
{ sender: [{ value: "alice@example.com", count: 1 }], mailbox: [], attachment_type: [], tag: [] }
|
||||
);
|
||||
fireEvent.click(chip);
|
||||
fireEvent.change(screen.getByLabelText("Suchbegriff"), { target: { value: "Betreff" } });
|
||||
fireEvent.submit(screen.getByLabelText("Suchbegriff").closest("form")!);
|
||||
await screen.findByRole("button", { name: /alice@example\.com \(1\)/ });
|
||||
});
|
||||
|
||||
it("Zurücksetzen aller Filter entfernt alle Chips mit einem Klick (AC3)", async () => {
|
||||
mockSearchResponse(
|
||||
[{ messageId: "msg-1", subjectSnippet: "Betreff", bodySnippet: "Text", score: 1 }],
|
||||
{
|
||||
sender: [{ value: "alice@example.com", count: 2 }],
|
||||
mailbox: [{ value: "inbox", count: 2 }],
|
||||
attachment_type: [],
|
||||
tag: [],
|
||||
}
|
||||
);
|
||||
|
||||
render(<SearchPage />);
|
||||
fireEvent.change(screen.getByLabelText("Suchbegriff"), { target: { value: "Betreff" } });
|
||||
fireEvent.submit(screen.getByLabelText("Suchbegriff").closest("form")!);
|
||||
|
||||
fireEvent.click(await screen.findByRole("button", { name: /alice@example\.com \(2\)/ }));
|
||||
fireEvent.click(await screen.findByRole("button", { name: /inbox \(2\)/ }));
|
||||
await screen.findByRole("button", { name: /Filter Absender: alice@example\.com entfernen/ });
|
||||
await screen.findByRole("button", { name: /Filter Postfach: inbox entfernen/ });
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "Alle Filter zurücksetzen" }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByRole("button", { name: /Filter Absender/ })).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: /Filter Postfach/ })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// Pflichtprüfung 2: Tastaturbedienbarkeit der Filter-Chips.
|
||||
it("Filter-Chip ist per Tastatur (Tab + Enter) auslösbar", async () => {
|
||||
const user = userEvent.setup();
|
||||
mockSearchResponse(
|
||||
[{ messageId: "msg-1", subjectSnippet: "Betreff", bodySnippet: "Text", score: 1 }],
|
||||
{ sender: [{ value: "alice@example.com", count: 1 }], mailbox: [], attachment_type: [], tag: [] }
|
||||
);
|
||||
|
||||
render(<SearchPage />);
|
||||
fireEvent.change(screen.getByLabelText("Suchbegriff"), { target: { value: "Betreff" } });
|
||||
fireEvent.submit(screen.getByLabelText("Suchbegriff").closest("form")!);
|
||||
await user.click(await screen.findByRole("button", { name: /alice@example\.com \(1\)/ }));
|
||||
|
||||
const chip = await screen.findByRole("button", { name: /Filter Absender: alice@example\.com entfernen/ });
|
||||
chip.focus();
|
||||
expect(document.activeElement).toBe(chip);
|
||||
|
||||
await user.keyboard("{Enter}");
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByRole("button", { name: /Filter Absender/ })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// Pflichtprüfung 3: Test mit vielen aktiven Filtern bleibt die Ansicht
|
||||
// übersichtlich — real mit 20 gleichzeitig aktiven Filtern geprüft,
|
||||
// jeder bleibt einzeln als eigener, erkennbarer Chip vorhanden (keine
|
||||
// Zusammenfassung/Verlust von Einträgen), kein Absturz.
|
||||
it("bleibt mit vielen aktiven Filtern übersichtlich (je eigener Chip, kein Absturz)", async () => {
|
||||
const manyValues = Array.from({ length: 20 }, (_, i) => ({ value: `tag-${i}`, count: i + 1 }));
|
||||
mockSearchResponse(
|
||||
[{ messageId: "msg-1", subjectSnippet: "Betreff", bodySnippet: "Text", score: 1 }],
|
||||
{ sender: [], mailbox: [], attachment_type: [], tag: manyValues }
|
||||
);
|
||||
|
||||
render(<SearchPage />);
|
||||
fireEvent.change(screen.getByLabelText("Suchbegriff"), { target: { value: "Betreff" } });
|
||||
fireEvent.submit(screen.getByLabelText("Suchbegriff").closest("form")!);
|
||||
|
||||
for (let i = 0; i < manyValues.length; i++) {
|
||||
const facetButton = await screen.findByRole("button", { name: new RegExp(`tag-${i} \\(${i + 1}\\)`) });
|
||||
fireEvent.click(facetButton);
|
||||
}
|
||||
|
||||
const chipRegion = await screen.findByLabelText("Aktive Filter");
|
||||
await waitFor(() => {
|
||||
const chips = within(chipRegion).getAllByRole("button", { name: /^Filter Tag: /i });
|
||||
expect(chips).toHaveLength(20);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
"use client";
|
||||
|
||||
import { useState, type FormEvent } from "react";
|
||||
import { useState, useEffect, useCallback, type FormEvent } from "react";
|
||||
import Link from "next/link";
|
||||
import { TextField } from "@nexarch/shl";
|
||||
import { search, ApiError, type SearchHit } from "../lib/api";
|
||||
import { search, facets, ApiError, type SearchHit, type AppliedFilter, type FacetsResponse } from "../lib/api";
|
||||
import { splitHighlighted } from "../lib/highlight";
|
||||
import { HIGHLIGHT_BG_LIGHT, HIGHLIGHT_FG_LIGHT } from "../lib/highlightColors";
|
||||
import { ActiveFilterChips, FacetPanel } from "./FacetPanel";
|
||||
import { addFilter, removeFilter } from "../lib/filterState";
|
||||
|
||||
function tenantSlug(): string {
|
||||
return process.env.NEXT_PUBLIC_MAIL_TENANT_SLUG ?? "";
|
||||
@@ -43,37 +45,70 @@ function resultHref(hit: SearchHit): string {
|
||||
|
||||
export default function SearchPage() {
|
||||
const [query, setQuery] = useState("");
|
||||
const [appliedFilters, setAppliedFilters] = useState<AppliedFilter[]>([]);
|
||||
const [hits, setHits] = useState<SearchHit[] | null>(null);
|
||||
const [facetsData, setFacetsData] = useState<FacetsResponse | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function runSearch(e?: FormEvent) {
|
||||
e?.preventDefault();
|
||||
if (!query.trim()) {
|
||||
setHits(null);
|
||||
const runSearch = useCallback(
|
||||
async (q: string, filters: AppliedFilter[]) => {
|
||||
if (!q.trim() && filters.length === 0) {
|
||||
setHits(null);
|
||||
setFacetsData(null);
|
||||
setError(null);
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const result = await search(tenantSlug(), query);
|
||||
setHits(result.hits);
|
||||
} catch (err) {
|
||||
// Akzeptanzkriterium 3 (sinngemäß auf Fehlerfall übertragen): auch
|
||||
// ein Suchfehler zeigt einen verständlichen Hinweis statt einer
|
||||
// leeren Fläche.
|
||||
setHits([]);
|
||||
setError(err instanceof ApiError ? err.message : "Suche konnte nicht ausgeführt werden.");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
try {
|
||||
const [searchResult, facetsResult] = await Promise.all([
|
||||
search(tenantSlug(), q, filters),
|
||||
facets(tenantSlug(), q, filters),
|
||||
]);
|
||||
setHits(searchResult.hits);
|
||||
setFacetsData(facetsResult);
|
||||
} catch (err) {
|
||||
// Akzeptanzkriterium 3 (SRC-04, sinngemäß auf Fehlerfall
|
||||
// übertragen): auch ein Suchfehler zeigt einen verständlichen
|
||||
// Hinweis statt einer leeren Fläche.
|
||||
setHits([]);
|
||||
setFacetsData(null);
|
||||
setError(err instanceof ApiError ? err.message : "Suche konnte nicht ausgeführt werden.");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
function onSubmit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
void runSearch(query, appliedFilters);
|
||||
}
|
||||
|
||||
// SRC-06 Akzeptanzkriterium 2: Trefferzahl je Facettenwert live
|
||||
// aktualisiert — jede Filteränderung löst Such- UND Facettenabfrage
|
||||
// erneut aus.
|
||||
useEffect(() => {
|
||||
void runSearch(query, appliedFilters);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [appliedFilters]);
|
||||
|
||||
function onAddFilter(filter: AppliedFilter) {
|
||||
setAppliedFilters((prev) => addFilter(prev, filter));
|
||||
}
|
||||
function onRemoveFilter(filter: AppliedFilter) {
|
||||
setAppliedFilters((prev) => removeFilter(prev, filter));
|
||||
}
|
||||
function onClearFilters() {
|
||||
setAppliedFilters([]);
|
||||
}
|
||||
|
||||
return (
|
||||
<main style={{ maxWidth: 720, margin: "40px auto", padding: "0 16px" }}>
|
||||
<h1>Mail-Suche</h1>
|
||||
<form onSubmit={runSearch}>
|
||||
<form onSubmit={onSubmit}>
|
||||
<TextField
|
||||
label="Suchbegriff"
|
||||
value={query}
|
||||
@@ -85,6 +120,14 @@ export default function SearchPage() {
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<ActiveFilterChips
|
||||
facetsData={facetsData}
|
||||
appliedFilters={appliedFilters}
|
||||
onAddFilter={onAddFilter}
|
||||
onRemoveFilter={onRemoveFilter}
|
||||
onClearFilters={onClearFilters}
|
||||
/>
|
||||
|
||||
{error && (
|
||||
<p role="alert" style={{ marginTop: 16 }}>
|
||||
{error}
|
||||
@@ -97,6 +140,14 @@ export default function SearchPage() {
|
||||
</p>
|
||||
)}
|
||||
|
||||
<FacetPanel
|
||||
facetsData={facetsData}
|
||||
appliedFilters={appliedFilters}
|
||||
onAddFilter={onAddFilter}
|
||||
onRemoveFilter={onRemoveFilter}
|
||||
onClearFilters={onClearFilters}
|
||||
/>
|
||||
|
||||
{hits !== null && hits.length > 0 && (
|
||||
<ul style={{ listStyle: "none", padding: 0, marginTop: 16 }}>
|
||||
{hits.map((hit) => (
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// externe Mail-API vom Client aus (Rolle: "Datenzugriff ausschließlich
|
||||
// über die bereitgestellte API").
|
||||
|
||||
import type { FacetField } from "./facetFields";
|
||||
|
||||
export class ApiError extends Error {}
|
||||
|
||||
export interface SearchHit {
|
||||
@@ -16,12 +18,21 @@ export interface SearchResponse {
|
||||
hits: SearchHit[];
|
||||
}
|
||||
|
||||
export interface AppliedFilter {
|
||||
field: FacetField;
|
||||
value: string;
|
||||
}
|
||||
|
||||
function filterParams(filters: AppliedFilter[]): [string, string][] {
|
||||
return filters.map((f) => ["filter", `${f.field}:${f.value}`]);
|
||||
}
|
||||
|
||||
// tenantSlug: bis zu einer zentralen Session-/IAM-Anbindung (Core-Board-
|
||||
// Scope, nicht Bestandteil dieser Kachel) wird der Mandant vom Aufrufer
|
||||
// mitgegeben. Die eigentliche Mandantentrennung passiert serverseitig in
|
||||
// mail/internal/search (SRC-01/SRC-03), nicht im Frontend.
|
||||
export async function search(tenantSlug: string, query: string): Promise<SearchResponse> {
|
||||
const params = new URLSearchParams({ tenant: tenantSlug, q: query });
|
||||
export async function search(tenantSlug: string, query: string, filters: AppliedFilter[] = []): Promise<SearchResponse> {
|
||||
const params = new URLSearchParams([["tenant", tenantSlug], ["q", query], ...filterParams(filters)]);
|
||||
const res = await fetch(`/api/search?${params.toString()}`);
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
@@ -29,3 +40,25 @@ export async function search(tenantSlug: string, query: string): Promise<SearchR
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export interface FacetValue {
|
||||
value: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
export interface FacetsResponse {
|
||||
values: Record<string, FacetValue[]>;
|
||||
}
|
||||
|
||||
// SRC-06: Facettenzählungen (Akzeptanzkriterium 2: Trefferzahl je
|
||||
// Facettenwert live aktualisiert) — läuft mit denselben Filtern wie
|
||||
// search, damit Zählungen und Trefferliste konsistent bleiben.
|
||||
export async function facets(tenantSlug: string, query: string, filters: AppliedFilter[] = []): Promise<FacetsResponse> {
|
||||
const params = new URLSearchParams([["tenant", tenantSlug], ["q", query], ...filterParams(filters)]);
|
||||
const res = await fetch(`/api/facets?${params.toString()}`);
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
throw new ApiError(body.error ?? `Facetten fehlgeschlagen (${res.status})`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// SRC-06: statische Facettendimensionen-Whitelist, gespiegelt aus
|
||||
// mail/internal/search/fields.go (FacetFields, SRC-05) — nur diese
|
||||
// Feldnamen sind als Filterdimension zulässig, kein beliebiger
|
||||
// Client-Feldname.
|
||||
export const FACET_FIELDS = ["sender", "mailbox", "attachment_type", "tag"] as const;
|
||||
export type FacetField = (typeof FACET_FIELDS)[number];
|
||||
|
||||
export function isFacetField(value: string): value is FacetField {
|
||||
return (FACET_FIELDS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
export const FACET_FIELD_LABELS: Record<FacetField, string> = {
|
||||
sender: "Absender",
|
||||
mailbox: "Postfach",
|
||||
attachment_type: "Anhangstyp",
|
||||
tag: "Tag",
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { addFilter, isFilterActive, removeFilter } from "./filterState";
|
||||
|
||||
describe("filterState", () => {
|
||||
it("addFilter fügt hinzu, dedupliziert aber identische Filter", () => {
|
||||
let filters = addFilter([], { field: "sender", value: "alice@example.com" });
|
||||
expect(filters).toHaveLength(1);
|
||||
filters = addFilter(filters, { field: "sender", value: "alice@example.com" });
|
||||
expect(filters).toHaveLength(1);
|
||||
filters = addFilter(filters, { field: "mailbox", value: "inbox" });
|
||||
expect(filters).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("removeFilter entfernt nur genau den einen Filter", () => {
|
||||
const filters = [
|
||||
{ field: "sender" as const, value: "alice@example.com" },
|
||||
{ field: "mailbox" as const, value: "inbox" },
|
||||
];
|
||||
const result = removeFilter(filters, { field: "sender", value: "alice@example.com" });
|
||||
expect(result).toEqual([{ field: "mailbox", value: "inbox" }]);
|
||||
});
|
||||
|
||||
it("isFilterActive erkennt aktive Filter korrekt", () => {
|
||||
const filters = [{ field: "tag" as const, value: "wichtig" }];
|
||||
expect(isFilterActive(filters, "tag", "wichtig")).toBe(true);
|
||||
expect(isFilterActive(filters, "tag", "unwichtig")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
// SRC-06: reine Zustandsfunktionen für aktive Filter — getrennt von der
|
||||
// Komponente testbar, keine React-Abhängigkeit.
|
||||
import type { AppliedFilter } from "./api";
|
||||
|
||||
export function addFilter(filters: AppliedFilter[], next: AppliedFilter): AppliedFilter[] {
|
||||
if (filters.some((f) => f.field === next.field && f.value === next.value)) {
|
||||
return filters;
|
||||
}
|
||||
return [...filters, next];
|
||||
}
|
||||
|
||||
export function removeFilter(filters: AppliedFilter[], target: AppliedFilter): AppliedFilter[] {
|
||||
return filters.filter((f) => !(f.field === target.field && f.value === target.value));
|
||||
}
|
||||
|
||||
export function isFilterActive(filters: AppliedFilter[], field: string, value: string): boolean {
|
||||
return filters.some((f) => f.field === field && f.value === value);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// SRC-06: gemeinsamer Aufbau der bool.must-Liste für Such- und
|
||||
// Facetten-Route — statische Feldnamen, keine dynamische SQL-/Klausel-
|
||||
// Bildung (gleiche Konvention wie mail/internal/search/facets.go
|
||||
// buildFilteredMust).
|
||||
import { isFacetField } from "./facetFields";
|
||||
|
||||
const FIELD_TENANT_SLUG = "tenant_slug";
|
||||
|
||||
export interface AppliedFilter {
|
||||
field: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export function parseFilterParams(values: string[]): AppliedFilter[] {
|
||||
const filters: AppliedFilter[] = [];
|
||||
for (const raw of values) {
|
||||
const sep = raw.indexOf(":");
|
||||
if (sep <= 0) continue;
|
||||
const field = raw.slice(0, sep);
|
||||
const value = raw.slice(sep + 1);
|
||||
if (!isFacetField(field) || value === "") continue;
|
||||
filters.push({ field, value });
|
||||
}
|
||||
return filters;
|
||||
}
|
||||
|
||||
export function buildMust(tenantSlug: string, query: string, filters: AppliedFilter[]): Record<string, unknown>[] {
|
||||
const must: Record<string, unknown>[] = [{ equals: { [FIELD_TENANT_SLUG]: tenantSlug } }];
|
||||
if (query) {
|
||||
must.push({ query_string: query });
|
||||
}
|
||||
for (const f of filters) {
|
||||
must.push({ equals: { [f.field]: f.value } });
|
||||
}
|
||||
return must;
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
"devDependencies": {
|
||||
"@testing-library/jest-dom": "6.4.8",
|
||||
"@testing-library/react": "16.0.0",
|
||||
"@testing-library/user-event": "14.5.2",
|
||||
"@types/node": "20.14.9",
|
||||
"@types/react": "18.3.3",
|
||||
"@types/react-dom": "18.3.0",
|
||||
|
||||
Reference in New Issue
Block a user