Files
usaco/site/internal/audiostore/audiostore_test.go
T
adamfandClaude Opus 5.5 63608f130c
Test and publish the site / test (push) Successful in 2m4s
Test and publish the site / publish (push) Successful in 1m44s
Review recorded explanations of a solution on the site
The admin can upload a recording of themselves explaining a problem's
solution out loud (mp3, m4a, wav, webm, ogg or flac, up to 25 MB) and
get interview-style feedback, committed as reviews/<id>.md so every
attempt is kept.

Claude can't take audio, so the recording is transcribed first by an
OpenAI-compatible endpoint (whisper-1 by default; TRANSCRIBE_API_KEY,
with TRANSCRIBE_URL for a self-hosted server). Pace, pauses, filler
words and when complexity first comes up are measured from the word
timestamps and given to Claude with the timestamped transcript, the
statement and optionally the code. The reply covers the verdict, scores
in five areas, the approach and its complexity, missed edge cases,
delivery and what to work on.

Recordings are kept for playback in an S3 bucket (Garage) when the
S3_* settings are given, and are thrown away otherwise. Reviews are
admin-only. The job runs in the background like Explain, and the
statement fetch and save-error handling are now shared between the two.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 01:56:55 +01:00

74 lines
2.1 KiB
Go

package audiostore
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewNeedsAllOrNothing(t *testing.T) {
if s, err := New(Config{}); s != nil || err != nil {
t.Errorf("empty config = %v, %v; want nil, nil", s, err)
}
if _, err := New(Config{Endpoint: "http://garage:3900"}); err == nil {
t.Error("half a config should be an error")
}
}
// A fake S3 that holds objects in a map, addressed by path as Garage is.
func TestPutGetDelete(t *testing.T) {
objs := map[string][]byte{}
types := map[string]string{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPut:
b, _ := io.ReadAll(r.Body)
objs[r.URL.Path], types[r.URL.Path] = b, r.Header.Get("Content-Type")
case http.MethodGet:
b, ok := objs[r.URL.Path]
if !ok {
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(404)
io.WriteString(w, `<?xml version="1.0"?><Error><Code>NoSuchKey</Code><Message>gone</Message></Error>`)
return
}
w.Header().Set("Content-Type", types[r.URL.Path])
w.Write(b)
case http.MethodDelete:
delete(objs, r.URL.Path)
w.WriteHeader(204)
}
}))
defer srv.Close()
s, err := New(Config{Endpoint: srv.URL, Region: "garage", Bucket: "usaco-audio", AccessKeyID: "GK1", SecretAccessKey: "s"})
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
if err := s.Put(ctx, "moocast/2026-10-20-153000.mp3", "audio/mpeg", []byte("ID3")); err != nil {
t.Fatal(err)
}
if string(objs["/usaco-audio/moocast/2026-10-20-153000.mp3"]) != "ID3" {
t.Fatalf("objects = %v", objs)
}
o, err := s.Get(ctx, "moocast/2026-10-20-153000.mp3")
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(o.Body)
o.Body.Close()
if string(b) != "ID3" || o.ContentType != "audio/mpeg" {
t.Errorf("get = %q %q", b, o.ContentType)
}
if err := s.Delete(ctx, "moocast/2026-10-20-153000.mp3"); err != nil {
t.Fatal(err)
}
if _, err := s.Get(ctx, "moocast/2026-10-20-153000.mp3"); !errors.Is(err, ErrNotFound) {
t.Errorf("get after delete: %v, want ErrNotFound", err)
}
}