package explain import ( "context" "encoding/json" "strings" "testing" "time" "adam-french.co.uk/usaco-site/internal/claude" "adam-french.co.uk/usaco-site/internal/index" ) func TestRender(t *testing.T) { r := Result{ Observation: "The answer is monotone in the effort limit.", Hints: []string{"Fix a limit.", "Binary search it, or run Dijkstra on max-edge."}, Approach: "Dijkstra where a path costs its largest step.", Walkthrough: []Step{ {Caption: "Start at (0,0).", SVG: ``}, {Caption: "Relax neighbours.", SVG: "```"}, }, Complexity: "O(RC log RC).", Pitfalls: []string{"Effort is the max, not the sum.", ""}, Sketch: "dist[0][0] = 0\npop smallest", } got := Render(r, "claude-opus-5-5", time.Date(2026, 10, 19, 0, 0, 0, 0, time.UTC)) want := "_Explained by claude-opus-5-5 on 19 October 2026._\n" + "\n## Key observation\n\nThe answer is monotone in the effort limit.\n" + "\n## Hint 1\n\nFix a limit.\n" + "\n## Hint 2\n\nBinary search it, or run Dijkstra on max-edge.\n" + "\n## Approach\n\nDijkstra where a path costs its largest step.\n" + "\n## Walkthrough\n\n**Step 1.** Start at (0,0).\n\n```svg\n\n```\n\n" + "**Step 2.** Relax neighbours.\n\n```svg\n\n```\n" + "\n## Complexity\n\nO(RC log RC).\n" + "\n## Pitfalls\n\n- Effort is the max, not the sum.\n" + "\n## Solution sketch\n\n```text\ndist[0][0] = 0\npop smallest\n```\n" if got != want { t.Errorf("Render =\n%s\nwant\n%s", got, want) } } func TestPrompt(t *testing.T) { p := Prompt(Input{ Name: "min-effort-path", Statement: "# Path With Minimum Effort\n\nA hiker...", Note: " my BFS is wrong ", Code: &index.File{Name: "main.rs", Content: "fn main() {}\n"}, Examples: []Example{{In: "1 2\n", Out: "3\n"}, {In: "5\n"}}, }) for _, want := range []string{ "\n# Path With Minimum Effort", "\n\n1 2\n\n\n3\n", "\n\n5\n\n", "\nfn main() {}\n", "\nmy BFS is wrong\n", } { if !strings.Contains(p, want) { t.Errorf("prompt is missing %q:\n%s", want, p) } } if q := Prompt(Input{Name: "x", Statement: "s"}); strings.Contains(q, "my_attempt") || strings.Contains(q, "stuck") { t.Errorf("empty code and note should leave their blocks out:\n%s", q) } } // 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{Hints: []string{}, Walkthrough: []Step{{}}, Pitfalls: []string{}}) 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)) } } 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{Observation: "obs"} return nil } func TestGenerate(t *testing.T) { a := &fakeAsker{} md, err := Generate(context.Background(), a, Input{Name: "x", Statement: "s"}, time.Now()) if err != nil { t.Fatal(err) } if !strings.Contains(md, "_Explained by fake on") || !strings.Contains(md, "## Key observation\n\nobs") { t.Errorf("unexpected markdown:\n%s", md) } if a.req.System != System || a.req.MaxTokens == 0 || a.req.Schema == nil { t.Errorf("request not filled in: %+v", a.req) } } func TestJobsOneAtATime(t *testing.T) { var j Jobs if s := j.Get("a"); s.State != Idle { t.Fatalf("fresh state = %q, want idle", s.State) } release := make(chan struct{}) if !j.Start("a", func() error { <-release; return nil }) { t.Fatal("first start refused") } if j.Start("a", func() error { return nil }) { t.Fatal("second start while running accepted") } if !j.Start("b", func() error { return context.Canceled }) { t.Fatal("another problem's job refused") } close(release) wait := func(name string) Status { for range 200 { if s := j.Get(name); s.State != Running { return s } time.Sleep(5 * time.Millisecond) } t.Fatalf("%s still running", name) return Status{} } if s := wait("a"); s.State != Done || s.Finished == nil { t.Errorf("a = %+v, want done", s) } if s := wait("b"); s.State != Failed || s.Error != context.Canceled.Error() { t.Errorf("b = %+v, want the error", s) } }