The problem list's admin row gains a link box beside "Today's LeetCode":
POST /leetcode/import takes {url} and does what `usaco new
<leetcode-link>` does, through the same lib/leetcode.py run and "Start
<slug>" commit as the daily question, then opens the solve page. A
problem already in the repo is only opened.
leetcode.Slug reads the link as slug_of in lib/leetcode.py does: full
links with /description/ and query strings, www., no scheme, or a bare
slug. Anything else is a 400.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
150 lines
4.9 KiB
Go
150 lines
4.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// fakeLeetCode names slug as the daily question, and imports it by writing a
|
|
// main.cpp, as lib/leetcode.py would (much more fully).
|
|
type fakeLeetCode struct {
|
|
slug string
|
|
err error
|
|
imports int
|
|
}
|
|
|
|
func (f *fakeLeetCode) Daily(context.Context) (string, error) { return f.slug, nil }
|
|
|
|
func (f *fakeLeetCode) Import(_ context.Context, root, slug string) error {
|
|
f.imports++
|
|
if f.err != nil {
|
|
return f.err
|
|
}
|
|
dir := filepath.Join(root, "solving", slug)
|
|
os.MkdirAll(dir, 0o755)
|
|
return os.WriteFile(filepath.Join(dir, "main.cpp"), []byte("class Solution {};\n"), 0o644)
|
|
}
|
|
|
|
type dailyReply struct {
|
|
Name string `json:"name"`
|
|
Created bool `json:"created"`
|
|
}
|
|
|
|
func TestDailyImports(t *testing.T) {
|
|
r, repo, _ := setup(t)
|
|
lc := &fakeLeetCode{slug: "two-sum"}
|
|
repo.srv.LeetCode = lc
|
|
|
|
if rec := do(r, "POST", "/usaco/api/leetcode/daily", "", false); rec.Code != 401 {
|
|
t.Fatalf("non-admin = %d, want 401", rec.Code)
|
|
}
|
|
rec := do(r, "POST", "/usaco/api/leetcode/daily", "", true)
|
|
var got dailyReply
|
|
json.Unmarshal(rec.Body.Bytes(), &got)
|
|
if rec.Code != 200 || got != (dailyReply{"two-sum", true}) {
|
|
t.Fatalf("daily = %d %s", rec.Code, rec.Body)
|
|
}
|
|
if len(repo.msg) != 1 || repo.msg[0] != "Start two-sum" {
|
|
t.Fatalf("commits = %v", repo.msg)
|
|
}
|
|
if _, ok := repo.srv.Index().Problems["two-sum"]; !ok {
|
|
t.Fatal("two-sum is not in the index after the import")
|
|
}
|
|
|
|
// The second press only names it.
|
|
rec = do(r, "POST", "/usaco/api/leetcode/daily", "", true)
|
|
got = dailyReply{}
|
|
json.Unmarshal(rec.Body.Bytes(), &got)
|
|
if rec.Code != 200 || got != (dailyReply{"two-sum", false}) || lc.imports != 1 {
|
|
t.Fatalf("second daily = %d %s after %d imports", rec.Code, rec.Body, lc.imports)
|
|
}
|
|
}
|
|
|
|
// A daily question that is already a problem, whatever its status, is not
|
|
// imported over.
|
|
func TestDailyExisting(t *testing.T) {
|
|
r, repo, _ := setup(t)
|
|
lc := &fakeLeetCode{slug: "moocast"}
|
|
repo.srv.LeetCode = lc
|
|
rec := do(r, "POST", "/usaco/api/leetcode/daily", "", true)
|
|
if rec.Code != 200 || lc.imports != 0 || len(repo.msg) != 0 {
|
|
t.Fatalf("daily = %d %s, %d imports, commits %v", rec.Code, rec.Body, lc.imports, repo.msg)
|
|
}
|
|
}
|
|
|
|
func TestDailyImportFails(t *testing.T) {
|
|
r, repo, _ := setup(t)
|
|
repo.srv.LeetCode = &fakeLeetCode{slug: "paid-only", err: errors.New("LeetCode returned no statement for 'paid-only' (a premium problem?)")}
|
|
rec := do(r, "POST", "/usaco/api/leetcode/daily", "", true)
|
|
if rec.Code != 502 || len(repo.msg) != 0 {
|
|
t.Fatalf("daily = %d %s, commits %v", rec.Code, rec.Body, repo.msg)
|
|
}
|
|
}
|
|
|
|
func TestImportLink(t *testing.T) {
|
|
r, repo, _ := setup(t)
|
|
lc := &fakeLeetCode{}
|
|
repo.srv.LeetCode = lc
|
|
body := `{"url":"https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2"}`
|
|
|
|
if rec := do(r, "POST", "/usaco/api/leetcode/import", body, false); rec.Code != 401 {
|
|
t.Fatalf("non-admin = %d, want 401", rec.Code)
|
|
}
|
|
rec := do(r, "POST", "/usaco/api/leetcode/import", body, true)
|
|
var got dailyReply
|
|
json.Unmarshal(rec.Body.Bytes(), &got)
|
|
if rec.Code != 200 || got != (dailyReply{"two-sum", true}) {
|
|
t.Fatalf("import = %d %s", rec.Code, rec.Body)
|
|
}
|
|
if len(repo.msg) != 1 || repo.msg[0] != "Start two-sum" {
|
|
t.Fatalf("commits = %v", repo.msg)
|
|
}
|
|
if _, ok := repo.srv.Index().Problems["two-sum"]; !ok {
|
|
t.Fatal("two-sum is not in the index after the import")
|
|
}
|
|
|
|
rec = do(r, "POST", "/usaco/api/leetcode/import", body, true)
|
|
got = dailyReply{}
|
|
json.Unmarshal(rec.Body.Bytes(), &got)
|
|
if rec.Code != 200 || got != (dailyReply{"two-sum", false}) || lc.imports != 1 {
|
|
t.Fatalf("second import = %d %s after %d imports", rec.Code, rec.Body, lc.imports)
|
|
}
|
|
}
|
|
|
|
func TestImportExisting(t *testing.T) {
|
|
r, repo, _ := setup(t)
|
|
lc := &fakeLeetCode{}
|
|
repo.srv.LeetCode = lc
|
|
rec := do(r, "POST", "/usaco/api/leetcode/import", `{"url":"https://leetcode.com/problems/moocast/"}`, true)
|
|
if rec.Code != 200 || lc.imports != 0 || len(repo.msg) != 0 {
|
|
t.Fatalf("import = %d %s, %d imports, commits %v", rec.Code, rec.Body, lc.imports, repo.msg)
|
|
}
|
|
}
|
|
|
|
func TestImportBadLink(t *testing.T) {
|
|
r, repo, _ := setup(t)
|
|
lc := &fakeLeetCode{}
|
|
repo.srv.LeetCode = lc
|
|
for _, body := range []string{`{"url":"https://codeforces.com/problemset/problem/1/A"}`, `{"url":""}`, `not json`} {
|
|
if rec := do(r, "POST", "/usaco/api/leetcode/import", body, true); rec.Code != 400 {
|
|
t.Fatalf("import %s = %d %s, want 400", body, rec.Code, rec.Body)
|
|
}
|
|
}
|
|
if lc.imports != 0 || len(repo.msg) != 0 {
|
|
t.Fatalf("%d imports, commits %v", lc.imports, repo.msg)
|
|
}
|
|
}
|
|
|
|
func TestImportFails(t *testing.T) {
|
|
r, repo, _ := setup(t)
|
|
repo.srv.LeetCode = &fakeLeetCode{err: errors.New("LeetCode has no problem 'nope'")}
|
|
rec := do(r, "POST", "/usaco/api/leetcode/import", `{"url":"https://leetcode.com/problems/nope/"}`, true)
|
|
if rec.Code != 502 || len(repo.msg) != 0 {
|
|
t.Fatalf("import = %d %s, commits %v", rec.Code, rec.Body, repo.msg)
|
|
}
|
|
}
|