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{ "\n# Two Sum", "\n\n1 2\n\n\n3\n", "\nint main() {}\n", "\nmock interview\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[00:00] Um, sort it.\n[00:06] Then two pointers.\n", } { 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, "") || !strings.Contains(q, "\nhi\n") || !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 := "\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, "\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) } }