Files
Docwell/Server/internal/ai/audio_test.go
T
tajniak81andClaude Opus 5.5 c239d859b8 Transcribe long recordings, read scanned PDFs, and bring AI writing to phones
Follow-ups to Phase E (ADR-15):

- Recordings over 25 MB are cut into pieces and transcribed in turn:
  with ffmpeg (now in the server and all-in-one images) any format is
  re-encoded to 16 kHz mono MP3 in 20-minute pieces; without it, MP3 is
  cut at frame boundaries and WAV at sample boundaries in Go.
- PDFs without a text layer are written out by a document-capable chat
  model (Anthropic document input, OpenAI file input); Ollama is skipped.
- Phone app: AI answers render as Markdown with tappable citations; the
  editing toolbar opens the writing actions on the selection; audio
  files get Transcribe. Editor bridge protocol 4 adds setAi,
  getSelection, insertMarkdown and transcribe/transcribed.
- The transcript HTML builder moves to @docwell/editor, shared by the
  web app and the phone editor.
- An integration test runs chat, Transcribe and writing actions on a
  device against a live server; the editor test no longer expects
  headings to have only a level.

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

125 lines
3.6 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package ai
import (
"bytes"
"context"
"encoding/binary"
"os/exec"
"testing"
)
// fakeMP3 is an ID3 tag followed by n MPEG-1 Layer III frames (128 kb/s,
// 44.1 kHz, 417 bytes each).
func fakeMP3(n int) []byte {
var b bytes.Buffer
b.Write([]byte{'I', 'D', '3', 4, 0, 0, 0, 0, 0, 5})
b.Write([]byte("TAG!!"))
for i := range n {
frame := make([]byte, 417)
copy(frame, []byte{0xFF, 0xFB, 0x90, 0x00})
frame[4] = byte(i)
b.Write(frame)
}
return b.Bytes()
}
func TestSplitMP3(t *testing.T) {
saved := ffmpegPath
ffmpegPath = ""
defer func() { ffmpegPath = saved }()
if n := mp3FrameLen([]byte{0xFF, 0xFB, 0x90, 0x00}); n != 417 {
t.Fatalf("frame length = %d", n)
}
pieces, err := splitAudio(context.Background(), "call.mp3", fakeMP3(100), 417*30+10)
if err != nil {
t.Fatal(err)
}
if len(pieces) != 4 {
t.Fatalf("pieces = %d", len(pieces))
}
total := 0
for i, p := range pieces {
if len(p.Data) > 417*30+10 || len(p.Data)%417 != 0 || p.Data[0] != 0xFF || p.Data[1] != 0xFB {
t.Errorf("piece %d: %d bytes, starts %x", i, len(p.Data), p.Data[:2])
}
total += len(p.Data) / 417
}
if total != 100 || pieces[0].Name != "call-001.mp3" {
t.Errorf("frames = %d, name %q", total, pieces[0].Name)
}
if one, _ := splitAudio(context.Background(), "a.mp3", fakeMP3(3), 1<<20); len(one) != 1 || one[0].Name != "a.mp3" {
t.Errorf("small file = %+v", one)
}
}
func fakeWAV(samples int) []byte {
var b bytes.Buffer
data := make([]byte, samples*2)
for i := range data {
data[i] = byte(i)
}
b.WriteString("RIFF")
_ = binary.Write(&b, binary.LittleEndian, uint32(36+len(data)))
b.WriteString("WAVEfmt ")
_ = binary.Write(&b, binary.LittleEndian, uint32(16))
_ = binary.Write(&b, binary.LittleEndian, []uint16{1, 1})
_ = binary.Write(&b, binary.LittleEndian, []uint32{16000, 32000})
_ = binary.Write(&b, binary.LittleEndian, []uint16{2, 16})
b.WriteString("data")
_ = binary.Write(&b, binary.LittleEndian, uint32(len(data)))
b.Write(data)
return b.Bytes()
}
func TestSplitWAV(t *testing.T) {
saved := ffmpegPath
ffmpegPath = ""
defer func() { ffmpegPath = saved }()
pieces, err := splitAudio(context.Background(), "memo.wav", fakeWAV(10000), 5000)
if err != nil {
t.Fatal(err)
}
got := 0
for _, p := range pieces {
if len(p.Data) > 5000 || string(p.Data[:4]) != "RIFF" || string(p.Data[8:16]) != "WAVEfmt " {
t.Fatalf("bad piece: %d bytes", len(p.Data))
}
size := int(binary.LittleEndian.Uint32(p.Data[40:]))
if size%2 != 0 || 44+size != len(p.Data) {
t.Fatalf("data size %d in %d bytes", size, len(p.Data))
}
got += size / 2
}
if got != 10000 {
t.Fatalf("samples = %d", got)
}
if _, err := splitAudio(context.Background(), "x.m4a", make([]byte, 100), 10); err != errNeedsFFmpeg {
t.Fatalf("m4a without ffmpeg = %v", err)
}
}
func TestSplitWithFFmpeg(t *testing.T) {
ffmpeg, err := exec.LookPath("ffmpeg")
if err != nil {
t.Skip("no ffmpeg")
}
// 30 seconds of tone as WAV, made by ffmpeg itself.
wav, err := exec.Command(ffmpeg, "-loglevel", "error", "-f", "lavfi", "-i", "sine=frequency=440:duration=30",
"-ar", "44100", "-ac", "2", "-f", "wav", "-").Output()
if err != nil {
t.Fatal(err)
}
pieces, err := splitWithFFmpeg(context.Background(), ffmpeg, "tone.wav", wav, 1<<20)
if err != nil {
t.Fatal(err)
}
if len(pieces) != 1 || pieces[0].Name != "tone-001.mp3" || len(pieces[0].Data) > 200_000 {
t.Fatalf("pieces = %d, first %q %d bytes", len(pieces), pieces[0].Name, len(pieces[0].Data))
}
if mp3FrameLen(pieces[0].Data[bytes.Index(pieces[0].Data, []byte{0xFF}):]) == 0 {
t.Error("the piece is not MP3")
}
}