Files
Docwell/Server/internal/ai/media_test.go
T
tajniak81andClaude Opus 5.5 8c39b0aacb
CI / go (Media) (push) Successful in 11m59s
CI / go (Server) (push) Failing after 18m26s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 8m35s
CI / desktop (ubuntu-latest) (push) Successful in 1h10m6s
CI / mobile (push) Failing after 3m43s
CI / docker (push) Successful in 1h2m44s
Docs / build (push) Successful in 11m58s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Run ffmpeg in its own docwell-media service
ffmpeg parses untrusted uploads, so it moves out of the server image
into a container of its own (ADR-15):

- Media/: docwell-media, a small Go wrapper around ffmpeg.
  POST /v1/split takes a recording and returns the 16 kHz mono MP3
  pieces as multipart/mixed. ffmpeg may only open local files and
  common audio/video demuxers (no playlists); optional shared token,
  a job limit, a timeout and a 1 GB cap.
- Server: DOCWELL_MEDIA_URL and DOCWELL_MEDIA_TOKEN send recordings
  over 25 MB to the service. When it is blank or unreachable, the
  server uses a local ffmpeg if it has one, else cuts MP3 and WAV in
  Go. The server image no longer installs ffmpeg; the all-in-one
  image keeps it to stay one container.
- Compose: a media service in every stack, read-only, no
  capabilities, on an internal network only docwell-server joins.
- CI tests the new module and checks that only the server reaches
  media; the release workflow publishes docwell-media.
- Docs, ADR-15 and the .env examples.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:52:34 +01:00

84 lines
2.7 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package ai
import (
"context"
"io"
"log/slog"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/textproto"
"strings"
"testing"
"docwell/server/internal/apperr"
)
func TestSplitRecordingWithMediaService(t *testing.T) {
saved := ffmpegPath
ffmpegPath = ""
defer func() { ffmpegPath = saved }()
var gotName, gotAuth string
var gotLen int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotName, gotAuth = r.URL.Query().Get("name"), r.Header.Get("Authorization")
b, _ := io.ReadAll(r.Body)
gotLen = len(b)
if strings.HasSuffix(gotName, ".bad") {
http.Error(w, "The recording could not be read: junk", http.StatusUnprocessableEntity)
return
}
mw := multipart.NewWriter(w)
w.Header().Set("Content-Type", "multipart/mixed; boundary="+mw.Boundary())
for _, piece := range [][]byte{fakeMP3(5), fakeMP3(40)} {
p, _ := mw.CreatePart(textproto.MIMEHeader{"Content-Type": {"audio/mpeg"}})
_, _ = p.Write(piece)
}
_ = mw.Close()
}))
defer srv.Close()
s := &Service{log: slog.New(slog.DiscardHandler), media: newMediaClient(srv.URL+"/", "tok")}
rec := make([]byte, 20_000) // an .m4a the server could not cut itself
pieces, err := s.splitRecording(context.Background(), "talk.m4a", rec, 417*30+10)
if err != nil {
t.Fatal(err)
}
if gotName != "talk.m4a" || gotAuth != "Bearer tok" || gotLen != len(rec) {
t.Fatalf("request: name %q auth %q len %d", gotName, gotAuth, gotLen)
}
// The second piece (40 frames) is over max and is cut again.
if len(pieces) != 3 || pieces[0].Name != "talk-001.mp3" || pieces[1].Name != "talk-002-001.mp3" {
names := []string{}
for _, p := range pieces {
names = append(names, p.Name)
}
t.Fatalf("pieces = %v", names)
}
// Small recordings never leave the server.
gotName = ""
if p, err := s.splitRecording(context.Background(), "a.m4a", rec, len(rec)); err != nil || len(p) != 1 || gotName != "" {
t.Fatalf("small: %v %d %q", err, len(p), gotName)
}
// ffmpeg's complaint reaches the user.
_, err = s.splitRecording(context.Background(), "x.bad", rec, 10)
if e, ok := apperr.As(err); !ok || !strings.Contains(e.Error(), "could not be read") {
t.Fatalf("unreadable: %v", err)
}
// Unreachable: MP3 is still cut on the server, other formats get a clear error.
srv.Close()
if p, err := s.splitRecording(context.Background(), "a.mp3", fakeMP3(100), 417*30+10); err != nil || len(p) != 4 {
t.Fatalf("fallback mp3: %v %d", err, len(p))
}
_, err = s.splitRecording(context.Background(), "a.m4a", rec, 10)
if e, ok := apperr.As(err); !ok || !strings.Contains(e.Error(), "not reachable") {
t.Fatalf("fallback m4a: %v", err)
}
}