Files
Docwell/Server/internal/ai/audio_test.go
T
tajniak81andClaude Opus 5.5 cad42fbe89
CI / desktop (ubuntu-latest) (push) Successful in 2h34m54s
CI / mobile (push) Failing after 4m52s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / docker (push) Successful in 53m34s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / go (Media) (push) Successful in 13m0s
CI / go (Server) (push) Successful in 24m4s
CI / go (Web-App) (push) Successful in 12m35s
CI / frontend (push) Failing after 11m51s
Prepare the 0.3.0 release
CHANGELOG.md gets the 0.3.0 notes: everything since 0.2.0 by area, and
the steps for upgrading a 0.2.0 install (new compose files with a stock
PocketBase on 8070, pb_data at /pb/pb_data, the media service, and the
SMTP password to enter once more). The release workflow now uses the
tag's CHANGELOG section as the Gitea release text. A v0.2.0 compose stack
with data was upgraded in place to this commit and kept its data.

Also fixes what CI would have failed on:
- Server lint (golangci-lint v2.14): errors.Is/As for sentinel errors,
  the bbolt/errors package instead of the deprecated bolt error, one
  needless conversion, and commented nolints for gosec false positives.
- SPDX headers on Database/export-schema.sh and Server/entrypoint.sh.
- Prettier on two files; the generated pbschema/schema.json is ignored.
- The editor-bridge dist page rebuilt to match the phone app's copy.

PLAN.md and the upgrade guide point to the release notes.

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

126 lines
3.6 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package ai
import (
"bytes"
"context"
"encoding/binary"
"errors"
"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); !errors.Is(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")
}
}