Files
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

180 lines
6.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package review
import (
"context"
"encoding/json"
"strings"
"testing"
"time"
"adam-french.co.uk/usaco-site/internal/claude"
"adam-french.co.uk/usaco-site/internal/explain"
"adam-french.co.uk/usaco-site/internal/index"
"adam-french.co.uk/usaco-site/internal/transcribe"
)
var transcript = &transcribe.Transcript{
Text: "Um, sort it. Then two pointers.",
Duration: 75,
Segments: []transcribe.Segment{
{Start: 0, End: 3, Text: " Um, sort it."},
{Start: 6, End: 9, Text: " Then two pointers. "},
{Start: 9, End: 9, Text: " "},
},
}
var metrics = transcribe.Metrics{
Duration: 75, Words: 6, WPM: 4.8,
Pauses: []transcribe.Pause{{At: 3, Length: 3}},
Fillers: []transcribe.Filler{{Word: "um", Count: 1}},
FillerCount: 1, ComplexityAt: -1,
}
func TestPrompt(t *testing.T) {
p := Prompt(Input{
Name: "two-sum",
Statement: "# Two Sum\n\nFind a pair.",
Examples: []explain.Example{{In: "1 2\n", Out: "3\n"}},
Code: &index.File{Name: "main.cpp", Content: "int main() {}\n"},
Note: " mock interview ",
Transcript: transcript,
Metrics: metrics,
})
for _, want := range []string{
"<problem name=\"two-sum\">\n# Two Sum",
"<example n=\"1\">\n<input>\n1 2\n</input>\n<output>\n3\n</output>",
"<my_code file=\"main.cpp\">\nint main() {}\n</my_code>",
"<context>\nmock interview\n</context>",
"- Length 01:15, 6 words, 5 words per minute\n",
"- 1 pauses of 2s or more; longest 3.0s at [00:03]\n- Pauses: [00:03] 3.0s\n",
"- Filler words: 1 (0.8 per minute): \"um\" × 1\n",
"- Complexity never mentioned by name\n",
"<transcript>\n[00:00] Um, sort it.\n[00:06] Then two pointers.\n</transcript>",
} {
if !strings.Contains(p, want) {
t.Errorf("prompt is missing %q:\n%s", want, p)
}
}
if q := Prompt(Input{Name: "x", Statement: "s", Transcript: &transcribe.Transcript{Text: "hi"}, Metrics: transcribe.Metrics{ComplexityAt: 4}}); strings.Contains(q, "my_code") || strings.Contains(q, "<context>") ||
!strings.Contains(q, "<transcript>\nhi\n</transcript>") || !strings.Contains(q, "first mentioned at [00:04]") || !strings.Contains(q, "No pauses") {
t.Errorf("minimal prompt:\n%s", q)
}
}
func TestLongestPausesKeepsOrder(t *testing.T) {
var ps []transcribe.Pause
for i := range 12 {
ps = append(ps, transcribe.Pause{At: float64(i * 10), Length: float64(2 + i%4)})
}
got := longestPauses(ps, 3)
if len(got) != 3 || got[0].At != 30 || got[1].At != 70 || got[2].At != 110 {
t.Errorf("longest = %+v", got)
}
}
func TestClock(t *testing.T) {
for sec, want := range map[float64]string{0: "00:00", 59.6: "01:00", 754: "12:34", 3725: "1:02:05"} {
if got := Clock(sec); got != want {
t.Errorf("Clock(%v) = %q, want %q", sec, got, want)
}
}
}
func TestRender(t *testing.T) {
r := Result{
Verdict: "partially_correct",
Summary: "Sort, then two pointers.",
Approach: "Right idea; sorting loses the indices.",
Gaps: []string{"Returns values, not indices.", " "},
Delivery: "Clear, but complexity came late.",
Scores: Scores{
Understanding: Score{4, "Restated it."},
Approach: Score{3, "Sorting | hashing\nboth work."},
Correctness: Score{2, "Indices lost."},
Complexity: Score{1, "Never stated."},
Communication: Score{4, "Easy to follow."},
},
Strengths: []string{"Named the brute force."},
Improvements: []string{"State complexity early."},
}
in := Input{Transcript: transcript, Metrics: metrics}
got := Render(r, in, "claude-opus-5-5", Meta{Now: time.Date(2026, 10, 20, 15, 30, 0, 0, time.UTC), Transcriber: "whisper-1", Audio: "two-sum/2026-10-20-153000.mp3"})
want := "<!-- review verdict=partially_correct audio=two-sum/2026-10-20-153000.mp3 -->\n" +
"_Reviewed by claude-opus-5-5 on 20 October 2026 at 15:30: a 01:15 recording, transcribed by whisper-1._\n" +
"\n## Verdict\n\n**Partly correct.** Sort, then two pointers.\n" +
"\n## Scores\n\n| Area | Score | Why |\n| --- | --- | --- |\n" +
"| Understanding | 4/5 | Restated it. |\n" +
"| Approach | 3/5 | Sorting \\| hashing both work. |\n" +
"| Correctness | 2/5 | Indices lost. |\n" +
"| Complexity | 1/5 | Never stated. |\n" +
"| Communication | 4/5 | Easy to follow. |\n" +
"\n## Approach\n\nRight idea; sorting loses the indices.\n" +
"\n## Gaps\n\n- Returns values, not indices.\n" +
"\n## Delivery\n\nClear, but complexity came late.\n" +
"\n## Strengths\n\n- Named the brute force.\n" +
"\n## To work on\n\n- State complexity early.\n" +
"\n## Metrics\n\n- Length 01:15, 6 words, 5 words per minute\n" +
"- 1 pauses of 2s or more; longest 3.0s at [00:03]\n- Pauses: [00:03] 3.0s\n" +
"- Filler words: 1 (0.8 per minute): \"um\" × 1\n- Complexity never mentioned by name\n" +
"\n## Transcript\n\n[00:00] Um, sort it.\n\n[00:06] Then two pointers.\n"
if got != want {
t.Errorf("Render =\n%s\nwant\n%s", got, want)
}
r.Gaps = nil
if md := Render(r, in, "m", Meta{Now: time.Now(), Transcriber: "w"}); strings.Contains(md, "audio=") || strings.Contains(md, "## Gaps") {
t.Errorf("no audio and no gaps should leave both out:\n%s", md)
}
}
// The schema must describe exactly Result, or the reply would not decode.
func TestSchemaMatchesResult(t *testing.T) {
props := Schema["properties"].(map[string]any)
b, _ := json.Marshal(Result{})
var fields map[string]any
json.Unmarshal(b, &fields)
for f := range fields {
if _, ok := props[f]; !ok {
t.Errorf("Result field %q is not in the schema", f)
}
}
if len(props) != len(fields) || len(Schema["required"].([]string)) != len(fields) {
t.Errorf("schema has %d properties (%d required), Result has %d fields", len(props), len(Schema["required"].([]string)), len(fields))
}
scores := props["scores"].(map[string]any)["properties"].(map[string]any)
b, _ = json.Marshal(Scores{})
var areas map[string]any
json.Unmarshal(b, &areas)
if len(scores) != len(areas) {
t.Errorf("schema has %d scores, Scores has %d", len(scores), len(areas))
}
for _, v := range props["verdict"].(map[string]any)["enum"].([]string) {
if Verdicts[v] == "" {
t.Errorf("verdict %q has no label", v)
}
}
}
type fakeAsker struct{ req claude.Request }
func (f *fakeAsker) Model() string { return "fake" }
func (f *fakeAsker) JSON(_ context.Context, req claude.Request, out any) error {
f.req = req
*out.(*Result) = Result{Verdict: "correct", Summary: "Hash map."}
return nil
}
func TestGenerate(t *testing.T) {
a := &fakeAsker{}
md, err := Generate(context.Background(), a, Input{Name: "x", Statement: "s", Transcript: transcript}, Meta{Now: time.Now(), Transcriber: "whisper-1"})
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(md, "<!-- review verdict=correct -->\n_Reviewed by fake on") || !strings.Contains(md, "## Verdict\n\n**Correct.** Hash map.") {
t.Errorf("unexpected markdown:\n%s", md)
}
if a.req.System != System || a.req.MaxTokens == 0 || a.req.Schema == nil || !strings.Contains(a.req.User, "[00:06] Then two pointers.") {
t.Errorf("request not filled in: %+v", a.req)
}
}