From 5bf8c8960b7493a15b5fb198b23d26f4f6c6e57f Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 21:28:34 -0400 Subject: [PATCH 01/13] feat: qol improvements --- .github/workflows/ci.yml | 12 ++++ readme.md => README.md | 0 pyproject.toml | 5 ++ tests/cache_spec.lua | 55 ++++++++++++++++ tests/command_parsing_spec.lua | 79 +++++++++++++++++++++++ tests/config_spec.lua | 46 +++++++++++++ tests/execute_spec.lua | 108 +++++++++++++++++++++++++++++++ tests/health_spec.lua | 54 ++++++++++++++++ tests/integration_spec.lua | 114 +++++++++++++++++++++++++++++++++ tests/problem_spec.lua | 81 +++++++++++++++++++++++ tests/scraper_spec.lua | 86 +++++++++++++++++++++++++ tests/test_panel_spec.lua | 106 ++++++++++++++++++++++++++++++ uv.lock | 64 ++++++++++++++++++ 13 files changed, 810 insertions(+) rename readme.md => README.md (100%) create mode 100644 tests/cache_spec.lua create mode 100644 tests/command_parsing_spec.lua create mode 100644 tests/config_spec.lua create mode 100644 tests/execute_spec.lua create mode 100644 tests/health_spec.lua create mode 100644 tests/integration_spec.lua create mode 100644 tests/problem_spec.lua create mode 100644 tests/scraper_spec.lua create mode 100644 tests/test_panel_spec.lua diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ecde609..50c7b00 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,3 +64,15 @@ jobs: run: uv tool install ruff - name: Lint Python files with ruff run: ruff check scrapers/ + + python-typecheck: + name: Python Type Checking + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install uv + uses: astral-sh/setup-uv@v4 + - name: Install dependencies with mypy + run: uv sync --dev + - name: Type check Python files with mypy + run: uv run mypy scrapers/ diff --git a/readme.md b/README.md similarity index 100% rename from readme.md rename to README.md diff --git a/pyproject.toml b/pyproject.toml index 6bd65d5..f14a8f4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,3 +9,8 @@ dependencies = [ "cloudscraper>=1.2.71", "requests>=2.32.5", ] + +[dependency-groups] +dev = [ + "mypy>=1.18.2", +] diff --git a/tests/cache_spec.lua b/tests/cache_spec.lua new file mode 100644 index 0000000..141b3be --- /dev/null +++ b/tests/cache_spec.lua @@ -0,0 +1,55 @@ +-- Unit tests for caching system +describe("cp.cache", function() + local cache + local temp_dir + + before_each(function() + cache = require("cp.cache") + temp_dir = vim.fn.tempname() + vim.fn.mkdir(temp_dir, "p") + -- Mock cache directory + end) + + after_each(function() + -- Clean up temp files + vim.fn.delete(temp_dir, "rf") + end) + + describe("contest metadata caching", function() + it("stores contest metadata correctly", function() + -- Test storing contest data + end) + + it("retrieves cached contest metadata", function() + -- Test retrieving contest data + end) + + it("handles missing cache files gracefully", function() + -- Test missing cache behavior + end) + end) + + describe("test case caching", function() + it("stores test cases for problems", function() + -- Test test case storage + end) + + it("retrieves cached test cases", function() + -- Test test case retrieval + end) + + it("handles cache invalidation", function() + -- Test cache expiry/invalidation + end) + end) + + describe("cache persistence", function() + it("persists cache across sessions", function() + -- Test cache file persistence + end) + + it("handles corrupted cache files", function() + -- Test corrupted cache recovery + end) + end) +end) \ No newline at end of file diff --git a/tests/command_parsing_spec.lua b/tests/command_parsing_spec.lua new file mode 100644 index 0000000..4c657f8 --- /dev/null +++ b/tests/command_parsing_spec.lua @@ -0,0 +1,79 @@ +-- Unit tests for command parsing and validation +describe("cp command parsing", function() + local cp + + before_each(function() + cp = require("cp") + cp.setup() + end) + + describe("platform setup commands", function() + it("parses :CP codeforces correctly", function() + -- Test platform-only command parsing + end) + + it("parses :CP codeforces 1800 correctly", function() + -- Test contest setup command parsing + end) + + it("parses :CP codeforces 1800 A correctly", function() + -- Test full setup command parsing + end) + + it("parses CSES format :CP cses 1068 correctly", function() + -- Test CSES-specific command parsing + end) + end) + + describe("action commands", function() + it("parses :CP test correctly", function() + -- Test test panel command + end) + + it("parses :CP next correctly", function() + -- Test navigation command + end) + + it("parses :CP prev correctly", function() + -- Test navigation command + end) + end) + + describe("language flags", function() + it("parses --lang=cpp correctly", function() + -- Test language flag parsing + end) + + it("parses --debug flag correctly", function() + -- Test debug flag parsing + end) + + it("combines flags correctly", function() + -- Test multiple flag parsing + end) + end) + + describe("error handling", function() + it("handles invalid commands gracefully", function() + -- Test error messages for bad commands + end) + + it("provides helpful error messages", function() + -- Test error message quality + end) + end) + + describe("command completion", function() + it("completes platform names", function() + -- Test tab completion for platforms + end) + + it("completes problem IDs from cached contest", function() + -- Test problem ID completion + end) + + it("completes action names", function() + -- Test action completion + end) + end) +end) \ No newline at end of file diff --git a/tests/config_spec.lua b/tests/config_spec.lua new file mode 100644 index 0000000..2baf3af --- /dev/null +++ b/tests/config_spec.lua @@ -0,0 +1,46 @@ +-- Unit tests for configuration management +describe("cp.config", function() + local config + + before_each(function() + config = require("cp.config") + end) + + describe("setup", function() + it("returns default config when no user config provided", function() + -- Test default configuration values + end) + + it("merges user config with defaults", function() + -- Test config merging behavior + end) + + it("validates contest configurations", function() + -- Test contest config validation + end) + + it("handles invalid config gracefully", function() + -- Test error handling for bad configs + end) + end) + + describe("platform validation", function() + it("accepts valid platforms", function() + -- Test platform validation + end) + + it("rejects invalid platforms", function() + -- Test platform rejection + end) + end) + + describe("language configurations", function() + it("provides correct file extensions for languages", function() + -- Test language -> extension mappings + end) + + it("provides correct compile commands", function() + -- Test compile command generation + end) + end) +end) \ No newline at end of file diff --git a/tests/execute_spec.lua b/tests/execute_spec.lua new file mode 100644 index 0000000..c8260f6 --- /dev/null +++ b/tests/execute_spec.lua @@ -0,0 +1,108 @@ +-- Unit tests for code compilation and execution +describe("cp.execute", function() + local execute + local temp_dir + local test_files = {} + + before_each(function() + execute = require("cp.execute") + temp_dir = vim.fn.tempname() + vim.fn.mkdir(temp_dir, "p") + vim.api.nvim_set_current_dir(temp_dir) + + -- Create sample source files for testing + test_files.cpp = temp_dir .. "/test.cpp" + test_files.python = temp_dir .. "/test.py" + test_files.rust = temp_dir .. "/test.rs" + + -- Write simple test programs + vim.fn.writefile({ + '#include ', + 'int main() { std::cout << "Hello" << std::endl; return 0; }' + }, test_files.cpp) + + vim.fn.writefile({ + 'print("Hello")' + }, test_files.python) + end) + + after_each(function() + vim.fn.delete(temp_dir, "rf") + end) + + describe("compilation", function() + it("compiles C++ code successfully", function() + -- Test C++ compilation + end) + + it("compiles Rust code successfully", function() + -- Test Rust compilation + end) + + it("handles compilation errors", function() + -- Test error handling for bad code + end) + + it("applies optimization flags correctly", function() + -- Test optimization settings + end) + + it("handles debug flag correctly", function() + -- Test debug compilation + end) + end) + + describe("execution", function() + it("runs compiled programs", function() + -- Test program execution + end) + + it("handles runtime errors", function() + -- Test runtime error handling + end) + + it("enforces time limits", function() + -- Test timeout handling + end) + + it("captures output correctly", function() + -- Test stdout/stderr capture + end) + + it("handles large inputs/outputs", function() + -- Test large data handling + end) + end) + + describe("test case execution", function() + it("runs single test case", function() + -- Test individual test case execution + end) + + it("runs multiple test cases", function() + -- Test batch execution + end) + + it("compares outputs correctly", function() + -- Test output comparison logic + end) + + it("handles edge cases in output comparison", function() + -- Test whitespace, newlines, etc. + end) + end) + + describe("platform-specific execution", function() + it("works on Linux", function() + -- Test Linux-specific behavior + end) + + it("works on macOS", function() + -- Test macOS-specific behavior + end) + + it("works on Windows", function() + -- Test Windows-specific behavior + end) + end) +end) \ No newline at end of file diff --git a/tests/health_spec.lua b/tests/health_spec.lua new file mode 100644 index 0000000..503b87a --- /dev/null +++ b/tests/health_spec.lua @@ -0,0 +1,54 @@ +-- Unit tests for health check functionality +describe("cp.health", function() + local health + + before_each(function() + health = require("cp.health") + end) + + describe("system checks", function() + it("detects Neovim version correctly", function() + -- Test Neovim version detection + end) + + it("detects available compilers", function() + -- Test C++, Rust, etc. compiler detection + end) + + it("detects Python installation", function() + -- Test Python availability + end) + + it("checks for required external tools", function() + -- Test curl, wget, etc. availability + end) + end) + + describe("configuration validation", function() + it("validates contest configurations", function() + -- Test config validation + end) + + it("checks directory permissions", function() + -- Test write permissions for directories + end) + + it("validates language configurations", function() + -- Test language setup validation + end) + end) + + describe("health report generation", function() + it("generates comprehensive health report", function() + -- Test :checkhealth cp output + end) + + it("provides actionable recommendations", function() + -- Test that health check gives useful advice + end) + + it("handles partial functionality gracefully", function() + -- Test when some features are unavailable + end) + end) +end) \ No newline at end of file diff --git a/tests/integration_spec.lua b/tests/integration_spec.lua new file mode 100644 index 0000000..569de7e --- /dev/null +++ b/tests/integration_spec.lua @@ -0,0 +1,114 @@ +-- Integration tests for complete workflows +describe("cp.nvim integration", function() + local cp + local temp_dir + + before_each(function() + cp = require("cp") + temp_dir = vim.fn.tempname() + vim.fn.mkdir(temp_dir, "p") + vim.api.nvim_set_current_dir(temp_dir) + + -- Set up with minimal config + cp.setup({ + scrapers = {}, -- Disable scraping for integration tests + contests = { + codeforces = { + dir = temp_dir, + url = "mock://codeforces.com", + languages = { + cpp = { extension = "cpp", compile = "g++ -o %s %s" } + } + } + } + }) + end) + + after_each(function() + vim.fn.delete(temp_dir, "rf") + vim.cmd("silent! %bwipeout!") + end) + + describe("complete problem setup workflow", function() + it("handles :CP codeforces 1800 A workflow", function() + -- Test complete setup from command to file creation + -- 1. Parse command + -- 2. Set up directory structure + -- 3. Create source file + -- 4. Apply template + -- 5. Switch to buffer + end) + + it("handles CSES workflow", function() + -- Test CSES-specific complete workflow + end) + + it("handles language switching", function() + -- Test switching languages for same problem + end) + end) + + describe("problem navigation workflow", function() + it("navigates between problems in contest", function() + -- Test :CP next/:CP prev workflow + -- Requires cached contest metadata + end) + + it("maintains state across navigation", function() + -- Test that work isn't lost when switching problems + end) + end) + + describe("test panel workflow", function() + it("handles complete testing workflow", function() + -- 1. Set up problem + -- 2. Write solution + -- 3. Open test panel (:CP test) + -- 4. Compile and run tests + -- 5. View results + -- 6. Close panel + end) + + it("handles debug workflow", function() + -- Test :CP test --debug workflow + end) + end) + + describe("file system integration", function() + it("maintains proper directory structure", function() + -- Test that files are organized correctly + end) + + it("handles existing files appropriately", function() + -- Test behavior when problem already exists + end) + + it("cleans up temporary files", function() + -- Test cleanup of build artifacts + end) + end) + + describe("error recovery", function() + it("recovers from network failures gracefully", function() + -- Test behavior when scraping fails + end) + + it("recovers from compilation failures", function() + -- Test error handling in compilation + end) + + it("handles corrupted cache gracefully", function() + -- Test cache corruption recovery + end) + end) + + describe("multi-session behavior", function() + it("persists state across Neovim restarts", function() + -- Test that contest/problem state persists + end) + + it("handles concurrent usage", function() + -- Test multiple Neovim instances + end) + end) +end) \ No newline at end of file diff --git a/tests/problem_spec.lua b/tests/problem_spec.lua new file mode 100644 index 0000000..e9740d3 --- /dev/null +++ b/tests/problem_spec.lua @@ -0,0 +1,81 @@ +-- Unit tests for problem context and file management +describe("cp.problem", function() + local problem + local temp_dir + + before_each(function() + problem = require("cp.problem") + temp_dir = vim.fn.tempname() + vim.fn.mkdir(temp_dir, "p") + -- Change to temp directory for testing + vim.api.nvim_set_current_dir(temp_dir) + end) + + after_each(function() + vim.fn.delete(temp_dir, "rf") + end) + + describe("context creation", function() + it("creates context for Codeforces problems", function() + -- Test context creation with proper paths + end) + + it("creates context for CSES problems", function() + -- Test CSES-specific context + end) + + it("generates correct file paths", function() + -- Test source file path generation + end) + + it("generates correct build paths", function() + -- Test build directory structure + end) + end) + + describe("template handling", function() + it("applies language templates correctly", function() + -- Test template application + end) + + it("handles custom templates", function() + -- Test user-defined templates + end) + + it("supports snippet integration", function() + -- Test LuaSnip integration + end) + end) + + describe("file operations", function() + it("creates directory structure", function() + -- Test directory creation (build/, io/) + end) + + it("handles existing files gracefully", function() + -- Test behavior when files exist + end) + + it("sets up input/output files", function() + -- Test I/O file creation + end) + end) + + describe("language support", function() + it("supports C++ compilation", function() + -- Test C++ setup and compilation + end) + + it("supports Python execution", function() + -- Test Python setup + end) + + it("supports Rust compilation", function() + -- Test Rust setup + end) + + it("supports custom language configurations", function() + -- Test user-defined language support + end) + end) +end) \ No newline at end of file diff --git a/tests/scraper_spec.lua b/tests/scraper_spec.lua new file mode 100644 index 0000000..1c44e97 --- /dev/null +++ b/tests/scraper_spec.lua @@ -0,0 +1,86 @@ +-- Unit tests for web scraping functionality +describe("cp.scrape", function() + local scrape + local mock_responses = {} + + before_each(function() + scrape = require("cp.scrape") + + -- Mock HTTP responses for different platforms + mock_responses.codeforces_contest = [[ +
+
Problem A
+
Problem B
+
+ ]] + + mock_responses.codeforces_problem = [[ +
Sample Input
+
Sample Output
+ ]] + end) + + describe("contest metadata scraping", function() + it("scrapes Codeforces contest problems", function() + -- Mock HTTP request, test problem list extraction + end) + + it("scrapes Atcoder contest problems", function() + -- Test Atcoder format + end) + + it("scrapes CSES problem list", function() + -- Test CSES format + end) + + it("handles network errors gracefully", function() + -- Test error handling for failed requests + end) + + it("handles parsing errors gracefully", function() + -- Test error handling for malformed HTML + end) + end) + + describe("problem scraping", function() + it("extracts test cases from Codeforces problems", function() + -- Test test case extraction + end) + + it("handles multiple test cases correctly", function() + -- Test multiple sample inputs/outputs + end) + + it("handles problems with no sample cases", function() + -- Test edge case handling + end) + + it("extracts problem metadata (time limits, etc.)", function() + -- Test metadata extraction + end) + end) + + describe("platform-specific parsing", function() + it("handles Codeforces HTML structure", function() + -- Test Codeforces-specific parsing + end) + + it("handles Atcoder HTML structure", function() + -- Test Atcoder-specific parsing + end) + + it("handles CSES HTML structure", function() + -- Test CSES-specific parsing + end) + end) + + describe("rate limiting and caching", function() + it("respects rate limits", function() + -- Test rate limiting behavior + end) + + it("uses cached results when appropriate", function() + -- Test caching integration + end) + end) +end) \ No newline at end of file diff --git a/tests/test_panel_spec.lua b/tests/test_panel_spec.lua new file mode 100644 index 0000000..022c395 --- /dev/null +++ b/tests/test_panel_spec.lua @@ -0,0 +1,106 @@ +-- UI/buffer tests for the interactive test panel +describe("cp test panel", function() + local cp + + before_each(function() + cp = require("cp") + cp.setup() + -- Set up a clean Neovim environment + vim.cmd("silent! %bwipeout!") + end) + + after_each(function() + -- Clean up test panel state + vim.cmd("silent! %bwipeout!") + end) + + describe("panel creation", function() + it("creates test panel buffers", function() + -- Test buffer creation for tab, expected, actual views + end) + + it("sets up correct window layout", function() + -- Test 3-pane layout creation + end) + + it("applies correct buffer settings", function() + -- Test buffer options (buftype, filetype, etc.) + end) + + it("sets up keymaps correctly", function() + -- Test navigation keymaps (Ctrl+N, Ctrl+P, q) + end) + end) + + describe("test case display", function() + it("renders test case tabs correctly", function() + -- Test tab line rendering with status indicators + end) + + it("displays input correctly", function() + -- Test input pane content + end) + + it("displays expected output correctly", function() + -- Test expected output pane + end) + + it("displays actual output correctly", function() + -- Test actual output pane + end) + + it("shows diff when test fails", function() + -- Test diff mode activation + end) + end) + + describe("navigation", function() + it("navigates to next test case", function() + -- Test Ctrl+N navigation + end) + + it("navigates to previous test case", function() + -- Test Ctrl+P navigation + end) + + it("wraps around at boundaries", function() + -- Test navigation wrapping + end) + + it("updates display on navigation", function() + -- Test content updates when switching tests + end) + end) + + describe("test execution integration", function() + it("compiles and runs tests automatically", function() + -- Test automatic compilation and execution + end) + + it("updates results in real-time", function() + -- Test live result updates + end) + + it("handles compilation failures", function() + -- Test error display when compilation fails + end) + + it("shows execution time", function() + -- Test timing display + end) + end) + + describe("session management", function() + it("saves and restores session correctly", function() + -- Test session save/restore when opening/closing panel + end) + + it("handles multiple panels gracefully", function() + -- Test behavior with multiple test panels + end) + + it("cleans up resources on close", function() + -- Test proper cleanup when closing panel + end) + end) +end) \ No newline at end of file diff --git a/uv.lock b/uv.lock index c6a66b0..873bb31 100644 --- a/uv.lock +++ b/uv.lock @@ -100,6 +100,62 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442, upload-time = "2024-09-15T18:07:37.964Z" }, ] +[[package]] +name = "mypy" +version = "1.18.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "mypy-extensions" }, + { name = "pathspec" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/c0/77/8f0d0001ffad290cef2f7f216f96c814866248a0b92a722365ed54648e7e/mypy-1.18.2.tar.gz", hash = "sha256:06a398102a5f203d7477b2923dda3634c36727fa5c237d8f859ef90c42a9924b", size = 3448846, upload-time = "2025-09-19T00:11:10.519Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/88/87/cafd3ae563f88f94eec33f35ff722d043e09832ea8530ef149ec1efbaf08/mypy-1.18.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:807d9315ab9d464125aa9fcf6d84fde6e1dc67da0b6f80e7405506b8ac72bc7f", size = 12731198, upload-time = "2025-09-19T00:09:44.857Z" }, + { url = "https://files.pythonhosted.org/packages/0f/e0/1e96c3d4266a06d4b0197ace5356d67d937d8358e2ee3ffac71faa843724/mypy-1.18.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:776bb00de1778caf4db739c6e83919c1d85a448f71979b6a0edd774ea8399341", size = 11817879, upload-time = "2025-09-19T00:09:47.131Z" }, + { url = "https://files.pythonhosted.org/packages/72/ef/0c9ba89eb03453e76bdac5a78b08260a848c7bfc5d6603634774d9cd9525/mypy-1.18.2-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:1379451880512ffce14505493bd9fe469e0697543717298242574882cf8cdb8d", size = 12427292, upload-time = "2025-09-19T00:10:22.472Z" }, + { url = "https://files.pythonhosted.org/packages/1a/52/ec4a061dd599eb8179d5411d99775bec2a20542505988f40fc2fee781068/mypy-1.18.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1331eb7fd110d60c24999893320967594ff84c38ac6d19e0a76c5fd809a84c86", size = 13163750, upload-time = "2025-09-19T00:09:51.472Z" }, + { url = "https://files.pythonhosted.org/packages/c4/5f/2cf2ceb3b36372d51568f2208c021870fe7834cf3186b653ac6446511839/mypy-1.18.2-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:3ca30b50a51e7ba93b00422e486cbb124f1c56a535e20eff7b2d6ab72b3b2e37", size = 13351827, upload-time = "2025-09-19T00:09:58.311Z" }, + { url = "https://files.pythonhosted.org/packages/c8/7d/2697b930179e7277529eaaec1513f8de622818696857f689e4a5432e5e27/mypy-1.18.2-cp311-cp311-win_amd64.whl", hash = "sha256:664dc726e67fa54e14536f6e1224bcfce1d9e5ac02426d2326e2bb4e081d1ce8", size = 9757983, upload-time = "2025-09-19T00:10:09.071Z" }, + { url = "https://files.pythonhosted.org/packages/07/06/dfdd2bc60c66611dd8335f463818514733bc763e4760dee289dcc33df709/mypy-1.18.2-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:33eca32dd124b29400c31d7cf784e795b050ace0e1f91b8dc035672725617e34", size = 12908273, upload-time = "2025-09-19T00:10:58.321Z" }, + { url = "https://files.pythonhosted.org/packages/81/14/6a9de6d13a122d5608e1a04130724caf9170333ac5a924e10f670687d3eb/mypy-1.18.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:a3c47adf30d65e89b2dcd2fa32f3aeb5e94ca970d2c15fcb25e297871c8e4764", size = 11920910, upload-time = "2025-09-19T00:10:20.043Z" }, + { url = "https://files.pythonhosted.org/packages/5f/a9/b29de53e42f18e8cc547e38daa9dfa132ffdc64f7250e353f5c8cdd44bee/mypy-1.18.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:5d6c838e831a062f5f29d11c9057c6009f60cb294fea33a98422688181fe2893", size = 12465585, upload-time = "2025-09-19T00:10:33.005Z" }, + { url = "https://files.pythonhosted.org/packages/77/ae/6c3d2c7c61ff21f2bee938c917616c92ebf852f015fb55917fd6e2811db2/mypy-1.18.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:01199871b6110a2ce984bde85acd481232d17413868c9807e95c1b0739a58914", size = 13348562, upload-time = "2025-09-19T00:10:11.51Z" }, + { url = "https://files.pythonhosted.org/packages/4d/31/aec68ab3b4aebdf8f36d191b0685d99faa899ab990753ca0fee60fb99511/mypy-1.18.2-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:a2afc0fa0b0e91b4599ddfe0f91e2c26c2b5a5ab263737e998d6817874c5f7c8", size = 13533296, upload-time = "2025-09-19T00:10:06.568Z" }, + { url = "https://files.pythonhosted.org/packages/9f/83/abcb3ad9478fca3ebeb6a5358bb0b22c95ea42b43b7789c7fb1297ca44f4/mypy-1.18.2-cp312-cp312-win_amd64.whl", hash = "sha256:d8068d0afe682c7c4897c0f7ce84ea77f6de953262b12d07038f4d296d547074", size = 9828828, upload-time = "2025-09-19T00:10:28.203Z" }, + { url = "https://files.pythonhosted.org/packages/5f/04/7f462e6fbba87a72bc8097b93f6842499c428a6ff0c81dd46948d175afe8/mypy-1.18.2-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:07b8b0f580ca6d289e69209ec9d3911b4a26e5abfde32228a288eb79df129fcc", size = 12898728, upload-time = "2025-09-19T00:10:01.33Z" }, + { url = "https://files.pythonhosted.org/packages/99/5b/61ed4efb64f1871b41fd0b82d29a64640f3516078f6c7905b68ab1ad8b13/mypy-1.18.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:ed4482847168439651d3feee5833ccedbf6657e964572706a2adb1f7fa4dfe2e", size = 11910758, upload-time = "2025-09-19T00:10:42.607Z" }, + { url = "https://files.pythonhosted.org/packages/3c/46/d297d4b683cc89a6e4108c4250a6a6b717f5fa96e1a30a7944a6da44da35/mypy-1.18.2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c3ad2afadd1e9fea5cf99a45a822346971ede8685cc581ed9cd4d42eaf940986", size = 12475342, upload-time = "2025-09-19T00:11:00.371Z" }, + { url = "https://files.pythonhosted.org/packages/83/45/4798f4d00df13eae3bfdf726c9244bcb495ab5bd588c0eed93a2f2dd67f3/mypy-1.18.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:a431a6f1ef14cf8c144c6b14793a23ec4eae3db28277c358136e79d7d062f62d", size = 13338709, upload-time = "2025-09-19T00:11:03.358Z" }, + { url = "https://files.pythonhosted.org/packages/d7/09/479f7358d9625172521a87a9271ddd2441e1dab16a09708f056e97007207/mypy-1.18.2-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:7ab28cc197f1dd77a67e1c6f35cd1f8e8b73ed2217e4fc005f9e6a504e46e7ba", size = 13529806, upload-time = "2025-09-19T00:10:26.073Z" }, + { url = "https://files.pythonhosted.org/packages/71/cf/ac0f2c7e9d0ea3c75cd99dff7aec1c9df4a1376537cb90e4c882267ee7e9/mypy-1.18.2-cp313-cp313-win_amd64.whl", hash = "sha256:0e2785a84b34a72ba55fb5daf079a1003a34c05b22238da94fcae2bbe46f3544", size = 9833262, upload-time = "2025-09-19T00:10:40.035Z" }, + { url = "https://files.pythonhosted.org/packages/5a/0c/7d5300883da16f0063ae53996358758b2a2df2a09c72a5061fa79a1f5006/mypy-1.18.2-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:62f0e1e988ad41c2a110edde6c398383a889d95b36b3e60bcf155f5164c4fdce", size = 12893775, upload-time = "2025-09-19T00:10:03.814Z" }, + { url = "https://files.pythonhosted.org/packages/50/df/2cffbf25737bdb236f60c973edf62e3e7b4ee1c25b6878629e88e2cde967/mypy-1.18.2-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:8795a039bab805ff0c1dfdb8cd3344642c2b99b8e439d057aba30850b8d3423d", size = 11936852, upload-time = "2025-09-19T00:10:51.631Z" }, + { url = "https://files.pythonhosted.org/packages/be/50/34059de13dd269227fb4a03be1faee6e2a4b04a2051c82ac0a0b5a773c9a/mypy-1.18.2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6ca1e64b24a700ab5ce10133f7ccd956a04715463d30498e64ea8715236f9c9c", size = 12480242, upload-time = "2025-09-19T00:11:07.955Z" }, + { url = "https://files.pythonhosted.org/packages/5b/11/040983fad5132d85914c874a2836252bbc57832065548885b5bb5b0d4359/mypy-1.18.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:d924eef3795cc89fecf6bedc6ed32b33ac13e8321344f6ddbf8ee89f706c05cb", size = 13326683, upload-time = "2025-09-19T00:09:55.572Z" }, + { url = "https://files.pythonhosted.org/packages/e9/ba/89b2901dd77414dd7a8c8729985832a5735053be15b744c18e4586e506ef/mypy-1.18.2-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:20c02215a080e3a2be3aa50506c67242df1c151eaba0dcbc1e4e557922a26075", size = 13514749, upload-time = "2025-09-19T00:10:44.827Z" }, + { url = "https://files.pythonhosted.org/packages/25/bc/cc98767cffd6b2928ba680f3e5bc969c4152bf7c2d83f92f5a504b92b0eb/mypy-1.18.2-cp314-cp314-win_amd64.whl", hash = "sha256:749b5f83198f1ca64345603118a6f01a4e99ad4bf9d103ddc5a3200cc4614adf", size = 9982959, upload-time = "2025-09-19T00:10:37.344Z" }, + { url = "https://files.pythonhosted.org/packages/87/e3/be76d87158ebafa0309946c4a73831974d4d6ab4f4ef40c3b53a385a66fd/mypy-1.18.2-py3-none-any.whl", hash = "sha256:22a1748707dd62b58d2ae53562ffc4d7f8bcc727e8ac7cbc69c053ddc874d47e", size = 2352367, upload-time = "2025-09-19T00:10:15.489Z" }, +] + +[[package]] +name = "mypy-extensions" +version = "1.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a2/6e/371856a3fb9d31ca8dac321cda606860fa4548858c0cc45d9d1d4ca2628b/mypy_extensions-1.1.0.tar.gz", hash = "sha256:52e68efc3284861e772bbcd66823fde5ae21fd2fdb51c62a211403730b916558", size = 6343, upload-time = "2025-04-22T14:54:24.164Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/79/7b/2c79738432f5c924bef5071f933bcc9efd0473bac3b4aa584a6f7c1c8df8/mypy_extensions-1.1.0-py3-none-any.whl", hash = "sha256:1be4cccdb0f2482337c4743e60421de3a356cd97508abadd57d47403e94f5505", size = 4963, upload-time = "2025-04-22T14:54:22.983Z" }, +] + +[[package]] +name = "pathspec" +version = "0.12.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/ca/bc/f35b8446f4531a7cb215605d100cd88b7ac6f44ab3fc94870c120ab3adbf/pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712", size = 51043, upload-time = "2023-12-10T22:30:45Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/cc/20/ff623b09d963f88bfde16306a54e12ee5ea43e9b597108672ff3a408aad6/pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08", size = 31191, upload-time = "2023-12-10T22:30:43.14Z" }, +] + [[package]] name = "pyparsing" version = "3.2.3" @@ -146,6 +202,11 @@ dependencies = [ { name = "requests" }, ] +[package.dev-dependencies] +dev = [ + { name = "mypy" }, +] + [package.metadata] requires-dist = [ { name = "beautifulsoup4", specifier = ">=4.13.5" }, @@ -153,6 +214,9 @@ requires-dist = [ { name = "requests", specifier = ">=2.32.5" }, ] +[package.metadata.requires-dev] +dev = [{ name = "mypy", specifier = ">=1.18.2" }] + [[package]] name = "soupsieve" version = "2.8" From 28182e1a5f2fd04942d810fef3039be21adfc8b1 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 21:29:31 -0400 Subject: [PATCH 02/13] feat(test): last spec --- tests/snippets_spec.lua | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/snippets_spec.lua diff --git a/tests/snippets_spec.lua b/tests/snippets_spec.lua new file mode 100644 index 0000000..465017c --- /dev/null +++ b/tests/snippets_spec.lua @@ -0,0 +1,68 @@ +-- Unit tests for snippet/template functionality +describe("cp.snippets", function() + local snippets + + before_each(function() + snippets = require("cp.snippets") + end) + + describe("template loading", function() + it("loads default templates correctly", function() + -- Test default template loading + end) + + it("loads platform-specific templates", function() + -- Test Codeforces vs CSES templates + end) + + it("loads language-specific templates", function() + -- Test C++ vs Python vs Rust templates + end) + + it("handles custom user templates", function() + -- Test user-defined template integration + end) + end) + + describe("LuaSnip integration", function() + it("registers snippets with LuaSnip", function() + -- Test snippet registration + end) + + it("provides platform-language combinations", function() + -- Test snippet triggers like cp.nvim/codeforces.cpp + end) + + it("handles missing LuaSnip gracefully", function() + -- Test fallback when LuaSnip not available + end) + end) + + describe("template expansion", function() + it("expands templates with correct content", function() + -- Test template content expansion + end) + + it("handles template variables", function() + -- Test variable substitution in templates + end) + + it("maintains cursor positioning", function() + -- Test cursor placement after expansion + end) + end) + + describe("template management", function() + it("allows template customization", function() + -- Test user template override + end) + + it("supports template inheritance", function() + -- Test template extending/modification + end) + + it("validates template syntax", function() + -- Test template validation + end) + end) +end) \ No newline at end of file From a5cf5cb5d2888cd434bc6dc3a37bd7ba267e774e Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 21:30:03 -0400 Subject: [PATCH 03/13] fix(ci): formatting; --- tests/cache_spec.lua | 32 ++++++++--------- tests/command_parsing_spec.lua | 46 ++++++++++++------------ tests/config_spec.lua | 28 +++++++-------- tests/execute_spec.lua | 62 ++++++++++++++++---------------- tests/health_spec.lua | 32 ++++++++--------- tests/integration_spec.lua | 64 +++++++++++++++++----------------- tests/problem_spec.lua | 46 ++++++++++++------------ tests/scraper_spec.lua | 42 +++++++++++----------- tests/snippets_spec.lua | 40 ++++++++++----------- tests/test_panel_spec.lua | 60 +++++++++++++++---------------- 10 files changed, 226 insertions(+), 226 deletions(-) diff --git a/tests/cache_spec.lua b/tests/cache_spec.lua index 141b3be..ce8ad5c 100644 --- a/tests/cache_spec.lua +++ b/tests/cache_spec.lua @@ -1,55 +1,55 @@ -- Unit tests for caching system -describe("cp.cache", function() +describe('cp.cache', function() local cache local temp_dir before_each(function() - cache = require("cp.cache") + cache = require('cp.cache') temp_dir = vim.fn.tempname() - vim.fn.mkdir(temp_dir, "p") + vim.fn.mkdir(temp_dir, 'p') -- Mock cache directory end) after_each(function() -- Clean up temp files - vim.fn.delete(temp_dir, "rf") + vim.fn.delete(temp_dir, 'rf') end) - describe("contest metadata caching", function() - it("stores contest metadata correctly", function() + describe('contest metadata caching', function() + it('stores contest metadata correctly', function() -- Test storing contest data end) - it("retrieves cached contest metadata", function() + it('retrieves cached contest metadata', function() -- Test retrieving contest data end) - it("handles missing cache files gracefully", function() + it('handles missing cache files gracefully', function() -- Test missing cache behavior end) end) - describe("test case caching", function() - it("stores test cases for problems", function() + describe('test case caching', function() + it('stores test cases for problems', function() -- Test test case storage end) - it("retrieves cached test cases", function() + it('retrieves cached test cases', function() -- Test test case retrieval end) - it("handles cache invalidation", function() + it('handles cache invalidation', function() -- Test cache expiry/invalidation end) end) - describe("cache persistence", function() - it("persists cache across sessions", function() + describe('cache persistence', function() + it('persists cache across sessions', function() -- Test cache file persistence end) - it("handles corrupted cache files", function() + it('handles corrupted cache files', function() -- Test corrupted cache recovery end) end) -end) \ No newline at end of file +end) diff --git a/tests/command_parsing_spec.lua b/tests/command_parsing_spec.lua index 4c657f8..0abef2b 100644 --- a/tests/command_parsing_spec.lua +++ b/tests/command_parsing_spec.lua @@ -1,79 +1,79 @@ -- Unit tests for command parsing and validation -describe("cp command parsing", function() +describe('cp command parsing', function() local cp before_each(function() - cp = require("cp") + cp = require('cp') cp.setup() end) - describe("platform setup commands", function() - it("parses :CP codeforces correctly", function() + describe('platform setup commands', function() + it('parses :CP codeforces correctly', function() -- Test platform-only command parsing end) - it("parses :CP codeforces 1800 correctly", function() + it('parses :CP codeforces 1800 correctly', function() -- Test contest setup command parsing end) - it("parses :CP codeforces 1800 A correctly", function() + it('parses :CP codeforces 1800 A correctly', function() -- Test full setup command parsing end) - it("parses CSES format :CP cses 1068 correctly", function() + it('parses CSES format :CP cses 1068 correctly', function() -- Test CSES-specific command parsing end) end) - describe("action commands", function() - it("parses :CP test correctly", function() + describe('action commands', function() + it('parses :CP test correctly', function() -- Test test panel command end) - it("parses :CP next correctly", function() + it('parses :CP next correctly', function() -- Test navigation command end) - it("parses :CP prev correctly", function() + it('parses :CP prev correctly', function() -- Test navigation command end) end) - describe("language flags", function() - it("parses --lang=cpp correctly", function() + describe('language flags', function() + it('parses --lang=cpp correctly', function() -- Test language flag parsing end) - it("parses --debug flag correctly", function() + it('parses --debug flag correctly', function() -- Test debug flag parsing end) - it("combines flags correctly", function() + it('combines flags correctly', function() -- Test multiple flag parsing end) end) - describe("error handling", function() - it("handles invalid commands gracefully", function() + describe('error handling', function() + it('handles invalid commands gracefully', function() -- Test error messages for bad commands end) - it("provides helpful error messages", function() + it('provides helpful error messages', function() -- Test error message quality end) end) - describe("command completion", function() - it("completes platform names", function() + describe('command completion', function() + it('completes platform names', function() -- Test tab completion for platforms end) - it("completes problem IDs from cached contest", function() + it('completes problem IDs from cached contest', function() -- Test problem ID completion end) - it("completes action names", function() + it('completes action names', function() -- Test action completion end) end) -end) \ No newline at end of file +end) diff --git a/tests/config_spec.lua b/tests/config_spec.lua index 2baf3af..ae1c46a 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -1,46 +1,46 @@ -- Unit tests for configuration management -describe("cp.config", function() +describe('cp.config', function() local config before_each(function() - config = require("cp.config") + config = require('cp.config') end) - describe("setup", function() - it("returns default config when no user config provided", function() + describe('setup', function() + it('returns default config when no user config provided', function() -- Test default configuration values end) - it("merges user config with defaults", function() + it('merges user config with defaults', function() -- Test config merging behavior end) - it("validates contest configurations", function() + it('validates contest configurations', function() -- Test contest config validation end) - it("handles invalid config gracefully", function() + it('handles invalid config gracefully', function() -- Test error handling for bad configs end) end) - describe("platform validation", function() - it("accepts valid platforms", function() + describe('platform validation', function() + it('accepts valid platforms', function() -- Test platform validation end) - it("rejects invalid platforms", function() + it('rejects invalid platforms', function() -- Test platform rejection end) end) - describe("language configurations", function() - it("provides correct file extensions for languages", function() + describe('language configurations', function() + it('provides correct file extensions for languages', function() -- Test language -> extension mappings end) - it("provides correct compile commands", function() + it('provides correct compile commands', function() -- Test compile command generation end) end) -end) \ No newline at end of file +end) diff --git a/tests/execute_spec.lua b/tests/execute_spec.lua index c8260f6..e48447f 100644 --- a/tests/execute_spec.lua +++ b/tests/execute_spec.lua @@ -1,108 +1,108 @@ -- Unit tests for code compilation and execution -describe("cp.execute", function() +describe('cp.execute', function() local execute local temp_dir local test_files = {} before_each(function() - execute = require("cp.execute") + execute = require('cp.execute') temp_dir = vim.fn.tempname() - vim.fn.mkdir(temp_dir, "p") + vim.fn.mkdir(temp_dir, 'p') vim.api.nvim_set_current_dir(temp_dir) -- Create sample source files for testing - test_files.cpp = temp_dir .. "/test.cpp" - test_files.python = temp_dir .. "/test.py" - test_files.rust = temp_dir .. "/test.rs" + test_files.cpp = temp_dir .. '/test.cpp' + test_files.python = temp_dir .. '/test.py' + test_files.rust = temp_dir .. '/test.rs' -- Write simple test programs vim.fn.writefile({ '#include ', - 'int main() { std::cout << "Hello" << std::endl; return 0; }' + 'int main() { std::cout << "Hello" << std::endl; return 0; }', }, test_files.cpp) vim.fn.writefile({ - 'print("Hello")' + 'print("Hello")', }, test_files.python) end) after_each(function() - vim.fn.delete(temp_dir, "rf") + vim.fn.delete(temp_dir, 'rf') end) - describe("compilation", function() - it("compiles C++ code successfully", function() + describe('compilation', function() + it('compiles C++ code successfully', function() -- Test C++ compilation end) - it("compiles Rust code successfully", function() + it('compiles Rust code successfully', function() -- Test Rust compilation end) - it("handles compilation errors", function() + it('handles compilation errors', function() -- Test error handling for bad code end) - it("applies optimization flags correctly", function() + it('applies optimization flags correctly', function() -- Test optimization settings end) - it("handles debug flag correctly", function() + it('handles debug flag correctly', function() -- Test debug compilation end) end) - describe("execution", function() - it("runs compiled programs", function() + describe('execution', function() + it('runs compiled programs', function() -- Test program execution end) - it("handles runtime errors", function() + it('handles runtime errors', function() -- Test runtime error handling end) - it("enforces time limits", function() + it('enforces time limits', function() -- Test timeout handling end) - it("captures output correctly", function() + it('captures output correctly', function() -- Test stdout/stderr capture end) - it("handles large inputs/outputs", function() + it('handles large inputs/outputs', function() -- Test large data handling end) end) - describe("test case execution", function() - it("runs single test case", function() + describe('test case execution', function() + it('runs single test case', function() -- Test individual test case execution end) - it("runs multiple test cases", function() + it('runs multiple test cases', function() -- Test batch execution end) - it("compares outputs correctly", function() + it('compares outputs correctly', function() -- Test output comparison logic end) - it("handles edge cases in output comparison", function() + it('handles edge cases in output comparison', function() -- Test whitespace, newlines, etc. end) end) - describe("platform-specific execution", function() - it("works on Linux", function() + describe('platform-specific execution', function() + it('works on Linux', function() -- Test Linux-specific behavior end) - it("works on macOS", function() + it('works on macOS', function() -- Test macOS-specific behavior end) - it("works on Windows", function() + it('works on Windows', function() -- Test Windows-specific behavior end) end) -end) \ No newline at end of file +end) diff --git a/tests/health_spec.lua b/tests/health_spec.lua index 503b87a..8f43778 100644 --- a/tests/health_spec.lua +++ b/tests/health_spec.lua @@ -1,54 +1,54 @@ -- Unit tests for health check functionality -describe("cp.health", function() +describe('cp.health', function() local health before_each(function() - health = require("cp.health") + health = require('cp.health') end) - describe("system checks", function() - it("detects Neovim version correctly", function() + describe('system checks', function() + it('detects Neovim version correctly', function() -- Test Neovim version detection end) - it("detects available compilers", function() + it('detects available compilers', function() -- Test C++, Rust, etc. compiler detection end) - it("detects Python installation", function() + it('detects Python installation', function() -- Test Python availability end) - it("checks for required external tools", function() + it('checks for required external tools', function() -- Test curl, wget, etc. availability end) end) - describe("configuration validation", function() - it("validates contest configurations", function() + describe('configuration validation', function() + it('validates contest configurations', function() -- Test config validation end) - it("checks directory permissions", function() + it('checks directory permissions', function() -- Test write permissions for directories end) - it("validates language configurations", function() + it('validates language configurations', function() -- Test language setup validation end) end) - describe("health report generation", function() - it("generates comprehensive health report", function() + describe('health report generation', function() + it('generates comprehensive health report', function() -- Test :checkhealth cp output end) - it("provides actionable recommendations", function() + it('provides actionable recommendations', function() -- Test that health check gives useful advice end) - it("handles partial functionality gracefully", function() + it('handles partial functionality gracefully', function() -- Test when some features are unavailable end) end) -end) \ No newline at end of file +end) diff --git a/tests/integration_spec.lua b/tests/integration_spec.lua index 569de7e..8570d2f 100644 --- a/tests/integration_spec.lua +++ b/tests/integration_spec.lua @@ -1,12 +1,12 @@ -- Integration tests for complete workflows -describe("cp.nvim integration", function() +describe('cp.nvim integration', function() local cp local temp_dir before_each(function() - cp = require("cp") + cp = require('cp') temp_dir = vim.fn.tempname() - vim.fn.mkdir(temp_dir, "p") + vim.fn.mkdir(temp_dir, 'p') vim.api.nvim_set_current_dir(temp_dir) -- Set up with minimal config @@ -15,22 +15,22 @@ describe("cp.nvim integration", function() contests = { codeforces = { dir = temp_dir, - url = "mock://codeforces.com", + url = 'mock://codeforces.com', languages = { - cpp = { extension = "cpp", compile = "g++ -o %s %s" } - } - } - } + cpp = { extension = 'cpp', compile = 'g++ -o %s %s' }, + }, + }, + }, }) end) after_each(function() - vim.fn.delete(temp_dir, "rf") - vim.cmd("silent! %bwipeout!") + vim.fn.delete(temp_dir, 'rf') + vim.cmd('silent! %bwipeout!') end) - describe("complete problem setup workflow", function() - it("handles :CP codeforces 1800 A workflow", function() + describe('complete problem setup workflow', function() + it('handles :CP codeforces 1800 A workflow', function() -- Test complete setup from command to file creation -- 1. Parse command -- 2. Set up directory structure @@ -39,28 +39,28 @@ describe("cp.nvim integration", function() -- 5. Switch to buffer end) - it("handles CSES workflow", function() + it('handles CSES workflow', function() -- Test CSES-specific complete workflow end) - it("handles language switching", function() + it('handles language switching', function() -- Test switching languages for same problem end) end) - describe("problem navigation workflow", function() - it("navigates between problems in contest", function() + describe('problem navigation workflow', function() + it('navigates between problems in contest', function() -- Test :CP next/:CP prev workflow -- Requires cached contest metadata end) - it("maintains state across navigation", function() + it('maintains state across navigation', function() -- Test that work isn't lost when switching problems end) end) - describe("test panel workflow", function() - it("handles complete testing workflow", function() + describe('test panel workflow', function() + it('handles complete testing workflow', function() -- 1. Set up problem -- 2. Write solution -- 3. Open test panel (:CP test) @@ -69,46 +69,46 @@ describe("cp.nvim integration", function() -- 6. Close panel end) - it("handles debug workflow", function() + it('handles debug workflow', function() -- Test :CP test --debug workflow end) end) - describe("file system integration", function() - it("maintains proper directory structure", function() + describe('file system integration', function() + it('maintains proper directory structure', function() -- Test that files are organized correctly end) - it("handles existing files appropriately", function() + it('handles existing files appropriately', function() -- Test behavior when problem already exists end) - it("cleans up temporary files", function() + it('cleans up temporary files', function() -- Test cleanup of build artifacts end) end) - describe("error recovery", function() - it("recovers from network failures gracefully", function() + describe('error recovery', function() + it('recovers from network failures gracefully', function() -- Test behavior when scraping fails end) - it("recovers from compilation failures", function() + it('recovers from compilation failures', function() -- Test error handling in compilation end) - it("handles corrupted cache gracefully", function() + it('handles corrupted cache gracefully', function() -- Test cache corruption recovery end) end) - describe("multi-session behavior", function() - it("persists state across Neovim restarts", function() + describe('multi-session behavior', function() + it('persists state across Neovim restarts', function() -- Test that contest/problem state persists end) - it("handles concurrent usage", function() + it('handles concurrent usage', function() -- Test multiple Neovim instances end) end) -end) \ No newline at end of file +end) diff --git a/tests/problem_spec.lua b/tests/problem_spec.lua index e9740d3..c895be9 100644 --- a/tests/problem_spec.lua +++ b/tests/problem_spec.lua @@ -1,81 +1,81 @@ -- Unit tests for problem context and file management -describe("cp.problem", function() +describe('cp.problem', function() local problem local temp_dir before_each(function() - problem = require("cp.problem") + problem = require('cp.problem') temp_dir = vim.fn.tempname() - vim.fn.mkdir(temp_dir, "p") + vim.fn.mkdir(temp_dir, 'p') -- Change to temp directory for testing vim.api.nvim_set_current_dir(temp_dir) end) after_each(function() - vim.fn.delete(temp_dir, "rf") + vim.fn.delete(temp_dir, 'rf') end) - describe("context creation", function() - it("creates context for Codeforces problems", function() + describe('context creation', function() + it('creates context for Codeforces problems', function() -- Test context creation with proper paths end) - it("creates context for CSES problems", function() + it('creates context for CSES problems', function() -- Test CSES-specific context end) - it("generates correct file paths", function() + it('generates correct file paths', function() -- Test source file path generation end) - it("generates correct build paths", function() + it('generates correct build paths', function() -- Test build directory structure end) end) - describe("template handling", function() - it("applies language templates correctly", function() + describe('template handling', function() + it('applies language templates correctly', function() -- Test template application end) - it("handles custom templates", function() + it('handles custom templates', function() -- Test user-defined templates end) - it("supports snippet integration", function() + it('supports snippet integration', function() -- Test LuaSnip integration end) end) - describe("file operations", function() - it("creates directory structure", function() + describe('file operations', function() + it('creates directory structure', function() -- Test directory creation (build/, io/) end) - it("handles existing files gracefully", function() + it('handles existing files gracefully', function() -- Test behavior when files exist end) - it("sets up input/output files", function() + it('sets up input/output files', function() -- Test I/O file creation end) end) - describe("language support", function() - it("supports C++ compilation", function() + describe('language support', function() + it('supports C++ compilation', function() -- Test C++ setup and compilation end) - it("supports Python execution", function() + it('supports Python execution', function() -- Test Python setup end) - it("supports Rust compilation", function() + it('supports Rust compilation', function() -- Test Rust setup end) - it("supports custom language configurations", function() + it('supports custom language configurations', function() -- Test user-defined language support end) end) -end) \ No newline at end of file +end) diff --git a/tests/scraper_spec.lua b/tests/scraper_spec.lua index 1c44e97..679b4f9 100644 --- a/tests/scraper_spec.lua +++ b/tests/scraper_spec.lua @@ -1,10 +1,10 @@ -- Unit tests for web scraping functionality -describe("cp.scrape", function() +describe('cp.scrape', function() local scrape local mock_responses = {} before_each(function() - scrape = require("cp.scrape") + scrape = require('cp.scrape') -- Mock HTTP responses for different platforms mock_responses.codeforces_contest = [[ @@ -20,67 +20,67 @@ describe("cp.scrape", function() ]] end) - describe("contest metadata scraping", function() - it("scrapes Codeforces contest problems", function() + describe('contest metadata scraping', function() + it('scrapes Codeforces contest problems', function() -- Mock HTTP request, test problem list extraction end) - it("scrapes Atcoder contest problems", function() + it('scrapes Atcoder contest problems', function() -- Test Atcoder format end) - it("scrapes CSES problem list", function() + it('scrapes CSES problem list', function() -- Test CSES format end) - it("handles network errors gracefully", function() + it('handles network errors gracefully', function() -- Test error handling for failed requests end) - it("handles parsing errors gracefully", function() + it('handles parsing errors gracefully', function() -- Test error handling for malformed HTML end) end) - describe("problem scraping", function() - it("extracts test cases from Codeforces problems", function() + describe('problem scraping', function() + it('extracts test cases from Codeforces problems', function() -- Test test case extraction end) - it("handles multiple test cases correctly", function() + it('handles multiple test cases correctly', function() -- Test multiple sample inputs/outputs end) - it("handles problems with no sample cases", function() + it('handles problems with no sample cases', function() -- Test edge case handling end) - it("extracts problem metadata (time limits, etc.)", function() + it('extracts problem metadata (time limits, etc.)', function() -- Test metadata extraction end) end) - describe("platform-specific parsing", function() - it("handles Codeforces HTML structure", function() + describe('platform-specific parsing', function() + it('handles Codeforces HTML structure', function() -- Test Codeforces-specific parsing end) - it("handles Atcoder HTML structure", function() + it('handles Atcoder HTML structure', function() -- Test Atcoder-specific parsing end) - it("handles CSES HTML structure", function() + it('handles CSES HTML structure', function() -- Test CSES-specific parsing end) end) - describe("rate limiting and caching", function() - it("respects rate limits", function() + describe('rate limiting and caching', function() + it('respects rate limits', function() -- Test rate limiting behavior end) - it("uses cached results when appropriate", function() + it('uses cached results when appropriate', function() -- Test caching integration end) end) -end) \ No newline at end of file +end) diff --git a/tests/snippets_spec.lua b/tests/snippets_spec.lua index 465017c..ed81205 100644 --- a/tests/snippets_spec.lua +++ b/tests/snippets_spec.lua @@ -1,68 +1,68 @@ -- Unit tests for snippet/template functionality -describe("cp.snippets", function() +describe('cp.snippets', function() local snippets before_each(function() - snippets = require("cp.snippets") + snippets = require('cp.snippets') end) - describe("template loading", function() - it("loads default templates correctly", function() + describe('template loading', function() + it('loads default templates correctly', function() -- Test default template loading end) - it("loads platform-specific templates", function() + it('loads platform-specific templates', function() -- Test Codeforces vs CSES templates end) - it("loads language-specific templates", function() + it('loads language-specific templates', function() -- Test C++ vs Python vs Rust templates end) - it("handles custom user templates", function() + it('handles custom user templates', function() -- Test user-defined template integration end) end) - describe("LuaSnip integration", function() - it("registers snippets with LuaSnip", function() + describe('LuaSnip integration', function() + it('registers snippets with LuaSnip', function() -- Test snippet registration end) - it("provides platform-language combinations", function() + it('provides platform-language combinations', function() -- Test snippet triggers like cp.nvim/codeforces.cpp end) - it("handles missing LuaSnip gracefully", function() + it('handles missing LuaSnip gracefully', function() -- Test fallback when LuaSnip not available end) end) - describe("template expansion", function() - it("expands templates with correct content", function() + describe('template expansion', function() + it('expands templates with correct content', function() -- Test template content expansion end) - it("handles template variables", function() + it('handles template variables', function() -- Test variable substitution in templates end) - it("maintains cursor positioning", function() + it('maintains cursor positioning', function() -- Test cursor placement after expansion end) end) - describe("template management", function() - it("allows template customization", function() + describe('template management', function() + it('allows template customization', function() -- Test user template override end) - it("supports template inheritance", function() + it('supports template inheritance', function() -- Test template extending/modification end) - it("validates template syntax", function() + it('validates template syntax', function() -- Test template validation end) end) -end) \ No newline at end of file +end) diff --git a/tests/test_panel_spec.lua b/tests/test_panel_spec.lua index 022c395..41de1cc 100644 --- a/tests/test_panel_spec.lua +++ b/tests/test_panel_spec.lua @@ -1,106 +1,106 @@ -- UI/buffer tests for the interactive test panel -describe("cp test panel", function() +describe('cp test panel', function() local cp before_each(function() - cp = require("cp") + cp = require('cp') cp.setup() -- Set up a clean Neovim environment - vim.cmd("silent! %bwipeout!") + vim.cmd('silent! %bwipeout!') end) after_each(function() -- Clean up test panel state - vim.cmd("silent! %bwipeout!") + vim.cmd('silent! %bwipeout!') end) - describe("panel creation", function() - it("creates test panel buffers", function() + describe('panel creation', function() + it('creates test panel buffers', function() -- Test buffer creation for tab, expected, actual views end) - it("sets up correct window layout", function() + it('sets up correct window layout', function() -- Test 3-pane layout creation end) - it("applies correct buffer settings", function() + it('applies correct buffer settings', function() -- Test buffer options (buftype, filetype, etc.) end) - it("sets up keymaps correctly", function() + it('sets up keymaps correctly', function() -- Test navigation keymaps (Ctrl+N, Ctrl+P, q) end) end) - describe("test case display", function() - it("renders test case tabs correctly", function() + describe('test case display', function() + it('renders test case tabs correctly', function() -- Test tab line rendering with status indicators end) - it("displays input correctly", function() + it('displays input correctly', function() -- Test input pane content end) - it("displays expected output correctly", function() + it('displays expected output correctly', function() -- Test expected output pane end) - it("displays actual output correctly", function() + it('displays actual output correctly', function() -- Test actual output pane end) - it("shows diff when test fails", function() + it('shows diff when test fails', function() -- Test diff mode activation end) end) - describe("navigation", function() - it("navigates to next test case", function() + describe('navigation', function() + it('navigates to next test case', function() -- Test Ctrl+N navigation end) - it("navigates to previous test case", function() + it('navigates to previous test case', function() -- Test Ctrl+P navigation end) - it("wraps around at boundaries", function() + it('wraps around at boundaries', function() -- Test navigation wrapping end) - it("updates display on navigation", function() + it('updates display on navigation', function() -- Test content updates when switching tests end) end) - describe("test execution integration", function() - it("compiles and runs tests automatically", function() + describe('test execution integration', function() + it('compiles and runs tests automatically', function() -- Test automatic compilation and execution end) - it("updates results in real-time", function() + it('updates results in real-time', function() -- Test live result updates end) - it("handles compilation failures", function() + it('handles compilation failures', function() -- Test error display when compilation fails end) - it("shows execution time", function() + it('shows execution time', function() -- Test timing display end) end) - describe("session management", function() - it("saves and restores session correctly", function() + describe('session management', function() + it('saves and restores session correctly', function() -- Test session save/restore when opening/closing panel end) - it("handles multiple panels gracefully", function() + it('handles multiple panels gracefully', function() -- Test behavior with multiple test panels end) - it("cleans up resources on close", function() + it('cleans up resources on close', function() -- Test proper cleanup when closing panel end) end) -end) \ No newline at end of file +end) From 002b75b0ab6861f826f7809c757fed97d4d82785 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 21:31:56 -0400 Subject: [PATCH 04/13] fix: typing --- vim.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vim.toml b/vim.toml index f43cc6f..9a4528d 100644 --- a/vim.toml +++ b/vim.toml @@ -16,3 +16,9 @@ any = true [it] any = true + +[before_each] +any = true + +[after_each] +any = true From ca6f8417c056e48d27cf115eaa8d46993317a65c Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 21:49:25 -0400 Subject: [PATCH 05/13] feat: scraper cleanup --- pyproject.toml | 2 + scrapers/atcoder.py | 146 +++++++++++++++++++++++------------------ scrapers/codeforces.py | 143 ++++++++++++++++++++++++---------------- scrapers/cses.py | 142 +++++++++++++++++++++++---------------- uv.lock | 41 +++++++++++- 5 files changed, 296 insertions(+), 178 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f14a8f4..6c796e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,4 +13,6 @@ dependencies = [ [dependency-groups] dev = [ "mypy>=1.18.2", + "types-beautifulsoup4>=4.12.0.20250516", + "types-requests>=2.32.4.20250913", ] diff --git a/scrapers/atcoder.py b/scrapers/atcoder.py index 3c86565..860ada9 100644 --- a/scrapers/atcoder.py +++ b/scrapers/atcoder.py @@ -2,9 +2,10 @@ import json import sys +from typing import Any import requests -from bs4 import BeautifulSoup +from bs4 import BeautifulSoup, Tag def parse_problem_url(contest_id: str, problem_letter: str) -> str: @@ -23,7 +24,6 @@ def extract_problem_from_row(row, contest_id: str) -> dict[str, str] | None: task_name = task_link.get_text(strip=True) task_href = task_link.get("href", "") - if not task_href: return None @@ -50,13 +50,11 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]: soup = BeautifulSoup(response.text, "html.parser") task_table = soup.find("table", class_="table") - - if not task_table: + if not task_table or not isinstance(task_table, Tag): return [] - rows = task_table.find_all("tr")[1:] - problems = [] - + rows = task_table.find_all("tr")[1:] # skip header + problems: list[dict[str, str]] = [] for row in rows: problem = extract_problem_from_row(row, contest_id) if problem: @@ -92,7 +90,6 @@ def extract_test_case_from_headers(sample_headers, i: int) -> tuple[str, str] | input_text = input_pre.get_text().strip().replace("\r", "") output_text = output_pre.get_text().strip().replace("\r", "") - if not input_text or not output_text: return None @@ -109,19 +106,17 @@ def scrape(url: str) -> list[tuple[str, str]]: response.raise_for_status() soup = BeautifulSoup(response.text, "html.parser") - sample_headers = soup.find_all( "h3", string=lambda x: x and "sample" in x.lower() if x else False ) - tests = [] + tests: list[tuple[str, str]] = [] i = 0 - while i < len(sample_headers): test_case = extract_test_case_from_headers(sample_headers, i) if test_case: tests.append(test_case) - i += 2 + i += 2 # move from "Sample Input n" to after "Sample Output n" else: i += 1 @@ -134,88 +129,111 @@ def scrape(url: str) -> list[tuple[str, str]]: def main() -> None: if len(sys.argv) < 2: - result: dict[str, str | bool] = { - "success": False, - "error": "Usage: atcoder.py metadata OR atcoder.py tests ", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: atcoder.py metadata OR atcoder.py tests ", + } + ) + ) sys.exit(1) mode: str = sys.argv[1] if mode == "metadata": if len(sys.argv) != 3: - result = { - "success": False, - "error": "Usage: atcoder.py metadata ", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: atcoder.py metadata ", + } + ) + ) sys.exit(1) contest_id: str = sys.argv[2] problems: list[dict[str, str]] = scrape_contest_problems(contest_id) if not problems: - result = { - "success": False, - "error": f"No problems found for contest {contest_id}", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"No problems found for contest {contest_id}", + } + ) + ) sys.exit(1) - result = { - "success": True, - "contest_id": contest_id, - "problems": problems, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": True, + "contest_id": contest_id, + "problems": problems, + } + ) + ) elif mode == "tests": if len(sys.argv) != 4: - result = { - "success": False, - "error": "Usage: atcoder.py tests ", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: atcoder.py tests ", + } + ) + ) sys.exit(1) - contest_id: str = sys.argv[2] + test_contest_id: str = sys.argv[2] problem_letter: str = sys.argv[3] - problem_id: str = contest_id + problem_letter.lower() + problem_id: str = f"{test_contest_id}_{problem_letter.lower()}" - url: str = parse_problem_url(contest_id, problem_letter) + url: str = parse_problem_url(test_contest_id, problem_letter) print(f"Scraping: {url}", file=sys.stderr) tests: list[tuple[str, str]] = scrape(url) - if not tests: - result = { - "success": False, - "error": f"No tests found for {contest_id} {problem_letter}", - "problem_id": problem_id, - "url": url, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"No tests found for {test_contest_id} {problem_letter}", + "problem_id": problem_id, + "url": url, + } + ) + ) sys.exit(1) - test_list: list[dict[str, str]] = [] - for input_data, output_data in tests: - test_list.append({"input": input_data, "expected": output_data}) + test_list: list[dict[str, str]] = [ + {"input": i, "expected": o} for i, o in tests + ] - result = { - "success": True, - "problem_id": problem_id, - "url": url, - "tests": test_list, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": True, + "problem_id": problem_id, + "url": url, + "tests": test_list, + } + ) + ) else: - result = { - "success": False, - "error": f"Unknown mode: {mode}. Use 'metadata' or 'tests'", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"Unknown mode: {mode}. Use 'metadata' or 'tests'", + } + ) + ) sys.exit(1) diff --git a/scrapers/codeforces.py b/scrapers/codeforces.py index f8e2290..87272d8 100644 --- a/scrapers/codeforces.py +++ b/scrapers/codeforces.py @@ -2,9 +2,10 @@ import json import sys +from typing import Any import cloudscraper -from bs4 import BeautifulSoup +from bs4 import BeautifulSoup, Tag def scrape(url: str) -> list[tuple[str, str]]: @@ -17,12 +18,12 @@ def scrape(url: str) -> list[tuple[str, str]]: input_sections = soup.find_all("div", class_="input") output_sections = soup.find_all("div", class_="output") - individual_inputs = {} - individual_outputs = {} + individual_inputs: dict[str, list[str]] = {} + individual_outputs: dict[str, list[str]] = {} for inp_section in input_sections: inp_pre = inp_section.find("pre") - if not inp_pre: + if not inp_pre or not isinstance(inp_pre, Tag): continue test_line_divs = inp_pre.find_all( @@ -51,7 +52,7 @@ def scrape(url: str) -> list[tuple[str, str]]: for out_section in output_sections: out_pre = out_section.find("pre") - if not out_pre: + if not out_pre or not isinstance(out_pre, Tag): continue test_line_divs = out_pre.find_all( @@ -95,12 +96,12 @@ def scrape(url: str) -> list[tuple[str, str]]: for inp_section in input_sections: inp_pre = inp_section.find("pre") - if not inp_pre: + if not inp_pre or not isinstance(inp_pre, Tag): continue divs = inp_pre.find_all("div") if divs: - lines = [div.get_text().strip() for div in divs] + lines = [div.get_text().strip() for div in divs if isinstance(div, Tag)] text = "\n".join(lines) else: text = inp_pre.get_text().replace("\r", "").strip() @@ -108,12 +109,12 @@ def scrape(url: str) -> list[tuple[str, str]]: for out_section in output_sections: out_pre = out_section.find("pre") - if not out_pre: + if not out_pre or not isinstance(out_pre, Tag): continue divs = out_pre.find_all("div") if divs: - lines = [div.get_text().strip() for div in divs] + lines = [div.get_text().strip() for div in divs if isinstance(div, Tag)] text = "\n".join(lines) else: text = out_pre.get_text().replace("\r", "").strip() @@ -152,7 +153,9 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]: ) for link in problem_links: - href: str = link.get("href", "") + if not isinstance(link, Tag): + continue + href: str = str(link.get("href", "")) if f"/contest/{contest_id}/problem/" in href: problem_letter: str = href.split("/")[-1].lower() problem_name: str = link.get_text(strip=True) @@ -183,86 +186,110 @@ def scrape_sample_tests(url: str) -> list[tuple[str, str]]: def main() -> None: if len(sys.argv) < 2: - result: dict[str, str | bool] = { - "success": False, - "error": "Usage: codeforces.py metadata OR codeforces.py tests ", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: codeforces.py metadata OR codeforces.py tests ", + } + ) + ) sys.exit(1) mode: str = sys.argv[1] if mode == "metadata": if len(sys.argv) != 3: - result: dict[str, str | bool] = { - "success": False, - "error": "Usage: codeforces.py metadata ", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: codeforces.py metadata ", + } + ) + ) sys.exit(1) contest_id: str = sys.argv[2] problems: list[dict[str, str]] = scrape_contest_problems(contest_id) if not problems: - result: dict[str, str | bool] = { - "success": False, - "error": f"No problems found for contest {contest_id}", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"No problems found for contest {contest_id}", + } + ) + ) sys.exit(1) - result: dict[str, str | bool | list] = { - "success": True, - "contest_id": contest_id, - "problems": problems, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": True, + "contest_id": contest_id, + "problems": problems, + } + ) + ) elif mode == "tests": if len(sys.argv) != 4: - result: dict[str, str | bool] = { - "success": False, - "error": "Usage: codeforces.py tests ", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: codeforces.py tests ", + } + ) + ) sys.exit(1) - contest_id: str = sys.argv[2] + tests_contest_id: str = sys.argv[2] problem_letter: str = sys.argv[3] - problem_id: str = contest_id + problem_letter.lower() + problem_id: str = tests_contest_id + problem_letter.lower() - url: str = parse_problem_url(contest_id, problem_letter) + url: str = parse_problem_url(tests_contest_id, problem_letter) tests: list[tuple[str, str]] = scrape_sample_tests(url) if not tests: - result: dict[str, str | bool] = { - "success": False, - "error": f"No tests found for {contest_id} {problem_letter}", - "problem_id": problem_id, - "url": url, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"No tests found for {tests_contest_id} {problem_letter}", + "problem_id": problem_id, + "url": url, + } + ) + ) sys.exit(1) test_list: list[dict[str, str]] = [] for input_data, output_data in tests: test_list.append({"input": input_data, "expected": output_data}) - result: dict[str, str | bool | list] = { - "success": True, - "problem_id": problem_id, - "url": url, - "tests": test_list, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": True, + "problem_id": problem_id, + "url": url, + "tests": test_list, + } + ) + ) else: - result: dict[str, str | bool] = { - "success": False, - "error": f"Unknown mode: {mode}. Use 'metadata' or 'tests'", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"Unknown mode: {mode}. Use 'metadata' or 'tests'", + } + ) + ) sys.exit(1) diff --git a/scrapers/cses.py b/scrapers/cses.py index c91f671..6e6fa62 100755 --- a/scrapers/cses.py +++ b/scrapers/cses.py @@ -2,6 +2,7 @@ import json import sys +from typing import Any import requests from bs4 import BeautifulSoup @@ -16,7 +17,9 @@ def parse_problem_url(problem_input: str) -> str | None: def process_problem_element( - element, current_category: str, all_categories: dict + element, + current_category: str | None, + all_categories: dict[str, list[dict[str, str]]], ) -> str | None: if element.name == "h1": category_name = element.get_text().strip() @@ -52,7 +55,7 @@ def scrape_all_problems() -> dict[str, list[dict[str, str]]]: response.raise_for_status() soup = BeautifulSoup(response.text, "html.parser") - all_categories = {} + all_categories: dict[str, list[dict[str, str]]] = {} problem_links = soup.find_all( "a", href=lambda x: x and "/problemset/task/" in x @@ -127,59 +130,79 @@ def scrape(url: str) -> list[tuple[str, str]]: def main() -> None: if len(sys.argv) < 2: - result: dict[str, str | bool] = { - "success": False, - "error": "Usage: cses.py metadata OR cses.py tests ", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: cses.py metadata OR cses.py tests ", + } + ) + ) sys.exit(1) mode: str = sys.argv[1] if mode == "metadata": if len(sys.argv) != 2: - result = { - "success": False, - "error": "Usage: cses.py metadata", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: cses.py metadata", + } + ) + ) sys.exit(1) all_categories: dict[str, list[dict[str, str]]] = scrape_all_problems() if not all_categories: - result = { - "success": False, - "error": "Failed to scrape CSES problem categories", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Failed to scrape CSES problem categories", + } + ) + ) sys.exit(1) - result = { - "success": True, - "categories": all_categories, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": True, + "categories": all_categories, + } + ) + ) elif mode == "tests": if len(sys.argv) != 3: - result = { - "success": False, - "error": "Usage: cses.py tests ", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": "Usage: cses.py tests ", + } + ) + ) sys.exit(1) problem_input: str = sys.argv[2] url: str | None = parse_problem_url(problem_input) if not url: - result = { - "success": False, - "error": f"Invalid problem input: {problem_input}. Use either problem ID (e.g., 1068) or full URL", - "problem_id": problem_input if problem_input.isdigit() else None, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"Invalid problem input: {problem_input}. Use either problem ID (e.g., 1068) or full URL", + "problem_id": problem_input + if problem_input.isdigit() + else None, + } + ) + ) sys.exit(1) tests: list[tuple[str, str]] = scrape(url) @@ -189,33 +212,42 @@ def main() -> None: ) if not tests: - result = { - "success": False, - "error": f"No tests found for {problem_input}", - "problem_id": problem_id, - "url": url, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"No tests found for {problem_input}", + "problem_id": problem_id, + "url": url, + } + ) + ) sys.exit(1) - test_list: list[dict[str, str]] = [] - for input_data, output_data in tests: - test_list.append({"input": input_data, "expected": output_data}) + test_list: list[dict[str, str]] = [ + {"input": i, "expected": o} for i, o in tests + ] - result = { - "success": True, - "problem_id": problem_id, - "url": url, - "tests": test_list, - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": True, + "problem_id": problem_id, + "url": url, + "tests": test_list, + } + ) + ) else: - result = { - "success": False, - "error": f"Unknown mode: {mode}. Use 'metadata' or 'tests'", - } - print(json.dumps(result)) + print( + json.dumps( + { + "success": False, + "error": f"Unknown mode: {mode}. Use 'metadata' or 'tests'", + } + ) + ) sys.exit(1) diff --git a/uv.lock b/uv.lock index 873bb31..dfef7b6 100644 --- a/uv.lock +++ b/uv.lock @@ -205,6 +205,8 @@ dependencies = [ [package.dev-dependencies] dev = [ { name = "mypy" }, + { name = "types-beautifulsoup4" }, + { name = "types-requests" }, ] [package.metadata] @@ -215,7 +217,11 @@ requires-dist = [ ] [package.metadata.requires-dev] -dev = [{ name = "mypy", specifier = ">=1.18.2" }] +dev = [ + { name = "mypy", specifier = ">=1.18.2" }, + { name = "types-beautifulsoup4", specifier = ">=4.12.0.20250516" }, + { name = "types-requests", specifier = ">=2.32.4.20250913" }, +] [[package]] name = "soupsieve" @@ -226,6 +232,39 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/14/a0/bb38d3b76b8cae341dad93a2dd83ab7462e6dbcdd84d43f54ee60a8dc167/soupsieve-2.8-py3-none-any.whl", hash = "sha256:0cc76456a30e20f5d7f2e14a98a4ae2ee4e5abdc7c5ea0aafe795f344bc7984c", size = 36679, upload-time = "2025-08-27T15:39:50.179Z" }, ] +[[package]] +name = "types-beautifulsoup4" +version = "4.12.0.20250516" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "types-html5lib" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/6d/d1/32b410f6d65eda94d3dfb0b3d0ca151f12cb1dc4cef731dcf7cbfd8716ff/types_beautifulsoup4-4.12.0.20250516.tar.gz", hash = "sha256:aa19dd73b33b70d6296adf92da8ab8a0c945c507e6fb7d5db553415cc77b417e", size = 16628, upload-time = "2025-05-16T03:09:09.93Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7c/79/d84de200a80085b32f12c5820d4fd0addcbe7ba6dce8c1c9d8605e833c8e/types_beautifulsoup4-4.12.0.20250516-py3-none-any.whl", hash = "sha256:5923399d4a1ba9cc8f0096fe334cc732e130269541d66261bb42ab039c0376ee", size = 16879, upload-time = "2025-05-16T03:09:09.051Z" }, +] + +[[package]] +name = "types-html5lib" +version = "1.1.11.20250917" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/68/4b/a970718e8bd9324ee8fb8eaf02ff069f6d03c20d4523bb4232892ecc3d06/types_html5lib-1.1.11.20250917.tar.gz", hash = "sha256:7b52743377f33f9b4fd7385afbd2d457b8864ee51f90ff2a795ad9e8c053373a", size = 16868, upload-time = "2025-09-17T02:47:41.18Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/78/8a/da91a9c64dcb5e69beb567519857411996d8ecae9f6f128bcef8260e7a8d/types_html5lib-1.1.11.20250917-py3-none-any.whl", hash = "sha256:b294fd06d60da205daeb2f615485ca4d475088d2eff1009cf427f4a80fcd5346", size = 22908, upload-time = "2025-09-17T02:47:40.39Z" }, +] + +[[package]] +name = "types-requests" +version = "2.32.4.20250913" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "urllib3" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/36/27/489922f4505975b11de2b5ad07b4fe1dca0bca9be81a703f26c5f3acfce5/types_requests-2.32.4.20250913.tar.gz", hash = "sha256:abd6d4f9ce3a9383f269775a9835a4c24e5cd6b9f647d64f88aa4613c33def5d", size = 23113, upload-time = "2025-09-13T02:40:02.309Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/2a/20/9a227ea57c1285986c4cf78400d0a91615d25b24e257fd9e2969606bdfae/types_requests-2.32.4.20250913-py3-none-any.whl", hash = "sha256:78c9c1fffebbe0fa487a418e0fa5252017e9c60d1a2da394077f1780f655d7e1", size = 20658, upload-time = "2025-09-13T02:40:01.115Z" }, +] + [[package]] name = "typing-extensions" version = "4.14.1" From 51fd6e3676c291841805b148954bfee349b26ed8 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 22:01:40 -0400 Subject: [PATCH 06/13] feat(ci): pytest --- .github/workflows/ci.yml | 16 ++++++- pyproject.toml | 5 ++ scrapers/__init__.py | 0 tests/scrapers/conftest.py | 41 ++++++++++++++++ tests/scrapers/test_atcoder.py | 51 ++++++++++++++++++++ tests/scrapers/test_codeforces.py | 54 ++++++++++++++++++++++ tests/scrapers/test_cses.py | 47 +++++++++++++++++++ uv.lock | 77 +++++++++++++++++++++++++++++++ 8 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 scrapers/__init__.py create mode 100644 tests/scrapers/conftest.py create mode 100644 tests/scrapers/test_atcoder.py create mode 100644 tests/scrapers/test_codeforces.py create mode 100644 tests/scrapers/test_cses.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50c7b00..e8a6034 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,7 +51,7 @@ jobs: - name: Install ruff run: uv tool install ruff - name: Check Python formatting with ruff - run: ruff format --check scrapers/ + run: ruff format --check scrapers/ tests/scrapers/ python-lint: name: Python Linting @@ -75,4 +75,16 @@ jobs: - name: Install dependencies with mypy run: uv sync --dev - name: Type check Python files with mypy - run: uv run mypy scrapers/ + run: uv run mypy scrapers/ tests/scrapers/ + + python-test: + name: Python Testing + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install uv + uses: astral-sh/setup-uv@v4 + - name: Install dependencies with pytest + run: uv sync --dev + - name: Run Python tests + run: uv run pytest tests/scrapers/ -v diff --git a/pyproject.toml b/pyproject.toml index 6c796e3..00b350b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,4 +15,9 @@ dev = [ "mypy>=1.18.2", "types-beautifulsoup4>=4.12.0.20250516", "types-requests>=2.32.4.20250913", + "pytest>=8.0.0", + "pytest-mock>=3.12.0", ] + +[tool.pytest.ini_options] +pythonpath = ["."] diff --git a/scrapers/__init__.py b/scrapers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/scrapers/conftest.py b/tests/scrapers/conftest.py new file mode 100644 index 0000000..3248ec2 --- /dev/null +++ b/tests/scrapers/conftest.py @@ -0,0 +1,41 @@ +import pytest + + +@pytest.fixture +def mock_codeforces_html(): + return """ +
+
+            
3
+
1 2 3
+
+
+
+
+            
6
+
+
+ """ + + +@pytest.fixture +def mock_atcoder_html(): + return """ +

Sample Input 1

+
3
+1 2 3
+

Sample Output 1

+
6
+ """ + + +@pytest.fixture +def mock_cses_html(): + return """ +

Example

+

Input:

+
3
+1 2 3
+

Output:

+
6
+ """ diff --git a/tests/scrapers/test_atcoder.py b/tests/scrapers/test_atcoder.py new file mode 100644 index 0000000..6b0fa40 --- /dev/null +++ b/tests/scrapers/test_atcoder.py @@ -0,0 +1,51 @@ +from unittest.mock import Mock +import pytest +from scrapers.atcoder import scrape, scrape_contest_problems + + +def test_scrape_success(mocker, mock_atcoder_html): + mock_response = Mock() + mock_response.text = mock_atcoder_html + + mocker.patch("scrapers.atcoder.requests.get", return_value=mock_response) + + result = scrape("https://atcoder.jp/contests/abc350/tasks/abc350_a") + + assert len(result) == 1 + assert result[0][0] == "3\n1 2 3" + assert result[0][1] == "6" + + +def test_scrape_contest_problems(mocker): + mock_response = Mock() + mock_response.text = """ + + + + + + + + + + +
TaskName
A - Water Tank
B - Dentist Aoki
+ """ + + mocker.patch("scrapers.atcoder.requests.get", return_value=mock_response) + + result = scrape_contest_problems("abc350") + + assert len(result) == 2 + assert result[0] == {"id": "a", "name": "A - Water Tank"} + assert result[1] == {"id": "b", "name": "B - Dentist Aoki"} + + +def test_scrape_network_error(mocker): + mocker.patch( + "scrapers.atcoder.requests.get", side_effect=Exception("Network error") + ) + + result = scrape("https://atcoder.jp/contests/abc350/tasks/abc350_a") + + assert result == [] diff --git a/tests/scrapers/test_codeforces.py b/tests/scrapers/test_codeforces.py new file mode 100644 index 0000000..381cc93 --- /dev/null +++ b/tests/scrapers/test_codeforces.py @@ -0,0 +1,54 @@ +import json +from unittest.mock import Mock +import pytest +from scrapers.codeforces import scrape, scrape_contest_problems + + +def test_scrape_success(mocker, mock_codeforces_html): + mock_scraper = Mock() + mock_response = Mock() + mock_response.text = mock_codeforces_html + mock_scraper.get.return_value = mock_response + + mocker.patch( + "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper + ) + + result = scrape("https://codeforces.com/contest/1900/problem/A") + + assert len(result) == 1 + assert result[0][0] == "1\n3\n1 2 3" + assert result[0][1] == "6" + + +def test_scrape_contest_problems(mocker): + mock_scraper = Mock() + mock_response = Mock() + mock_response.text = """ + A. Problem A + B. Problem B + """ + mock_scraper.get.return_value = mock_response + + mocker.patch( + "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper + ) + + result = scrape_contest_problems("1900") + + assert len(result) == 2 + assert result[0] == {"id": "a", "name": "A. Problem A"} + assert result[1] == {"id": "b", "name": "B. Problem B"} + + +def test_scrape_network_error(mocker): + mock_scraper = Mock() + mock_scraper.get.side_effect = Exception("Network error") + + mocker.patch( + "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper + ) + + result = scrape("https://codeforces.com/contest/1900/problem/A") + + assert result == [] diff --git a/tests/scrapers/test_cses.py b/tests/scrapers/test_cses.py new file mode 100644 index 0000000..9df8281 --- /dev/null +++ b/tests/scrapers/test_cses.py @@ -0,0 +1,47 @@ +from unittest.mock import Mock +import pytest +from scrapers.cses import scrape, scrape_all_problems + + +def test_scrape_success(mocker, mock_cses_html): + mock_response = Mock() + mock_response.text = mock_cses_html + + mocker.patch("scrapers.cses.requests.get", return_value=mock_response) + + result = scrape("https://cses.fi/problemset/task/1068") + + assert len(result) == 1 + assert result[0][0] == "3\n1 2 3" + assert result[0][1] == "6" + + +def test_scrape_all_problems(mocker): + mock_response = Mock() + mock_response.text = """ +

Introductory Problems

+ Weird Algorithm + Missing Number +

Sorting and Searching

+ Apartments + """ + + mocker.patch("scrapers.cses.requests.get", return_value=mock_response) + + result = scrape_all_problems() + + assert "Introductory Problems" in result + assert "Sorting and Searching" in result + assert len(result["Introductory Problems"]) == 2 + assert result["Introductory Problems"][0] == { + "id": "1068", + "name": "Weird Algorithm", + } + + +def test_scrape_network_error(mocker): + mocker.patch("scrapers.cses.requests.get", side_effect=Exception("Network error")) + + result = scrape("https://cses.fi/problemset/task/1068") + + assert result == [] diff --git a/uv.lock b/uv.lock index dfef7b6..bfe4d6d 100644 --- a/uv.lock +++ b/uv.lock @@ -91,6 +91,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/81/97/fc88803a451029688dffd7eb446dc1b529657577aec13aceff1cc9628c5d/cloudscraper-1.2.71-py2.py3-none-any.whl", hash = "sha256:76f50ca529ed2279e220837befdec892626f9511708e200d48d5bb76ded679b0", size = 99652, upload-time = "2023-04-25T23:20:15.974Z" }, ] +[[package]] +name = "colorama" +version = "0.4.6" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697, upload-time = "2022-10-25T02:36:22.414Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" }, +] + [[package]] name = "idna" version = "3.10" @@ -100,6 +109,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442, upload-time = "2024-09-15T18:07:37.964Z" }, ] +[[package]] +name = "iniconfig" +version = "2.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f2/97/ebf4da567aa6827c909642694d71c9fcf53e5b504f2d96afea02718862f3/iniconfig-2.1.0.tar.gz", hash = "sha256:3abbd2e30b36733fee78f9c7f7308f2d0050e88f0087fd25c2645f63c773e1c7", size = 4793, upload-time = "2025-03-19T20:09:59.721Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/2c/e1/e6716421ea10d38022b952c159d5161ca1193197fb744506875fbb87ea7b/iniconfig-2.1.0-py3-none-any.whl", hash = "sha256:9deba5723312380e77435581c6bf4935c94cbfab9b1ed33ef8d238ea168eb760", size = 6050, upload-time = "2025-03-19T20:10:01.071Z" }, +] + [[package]] name = "mypy" version = "1.18.2" @@ -147,6 +165,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/79/7b/2c79738432f5c924bef5071f933bcc9efd0473bac3b4aa584a6f7c1c8df8/mypy_extensions-1.1.0-py3-none-any.whl", hash = "sha256:1be4cccdb0f2482337c4743e60421de3a356cd97508abadd57d47403e94f5505", size = 4963, upload-time = "2025-04-22T14:54:22.983Z" }, ] +[[package]] +name = "packaging" +version = "25.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a1/d4/1fc4078c65507b51b96ca8f8c3ba19e6a61c8253c72794544580a7b6c24d/packaging-25.0.tar.gz", hash = "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f", size = 165727, upload-time = "2025-04-19T11:48:59.673Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469, upload-time = "2025-04-19T11:48:57.875Z" }, +] + [[package]] name = "pathspec" version = "0.12.1" @@ -156,6 +183,24 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/cc/20/ff623b09d963f88bfde16306a54e12ee5ea43e9b597108672ff3a408aad6/pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08", size = 31191, upload-time = "2023-12-10T22:30:43.14Z" }, ] +[[package]] +name = "pluggy" +version = "1.6.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f9/e2/3e91f31a7d2b083fe6ef3fa267035b518369d9511ffab804f839851d2779/pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3", size = 69412, upload-time = "2025-05-15T12:30:07.975Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538, upload-time = "2025-05-15T12:30:06.134Z" }, +] + +[[package]] +name = "pygments" +version = "2.19.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/b0/77/a5b8c569bf593b0140bde72ea885a803b82086995367bf2037de0159d924/pygments-2.19.2.tar.gz", hash = "sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887", size = 4968631, upload-time = "2025-06-21T13:39:12.283Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/c7/21/705964c7812476f378728bdf590ca4b771ec72385c533964653c68e86bdc/pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b", size = 1225217, upload-time = "2025-06-21T13:39:07.939Z" }, +] + [[package]] name = "pyparsing" version = "3.2.3" @@ -165,6 +210,34 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/05/e7/df2285f3d08fee213f2d041540fa4fc9ca6c2d44cf36d3a035bf2a8d2bcc/pyparsing-3.2.3-py3-none-any.whl", hash = "sha256:a749938e02d6fd0b59b356ca504a24982314bb090c383e3cf201c95ef7e2bfcf", size = 111120, upload-time = "2025-03-25T05:01:24.908Z" }, ] +[[package]] +name = "pytest" +version = "8.4.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "colorama", marker = "sys_platform == 'win32'" }, + { name = "iniconfig" }, + { name = "packaging" }, + { name = "pluggy" }, + { name = "pygments" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/a3/5c/00a0e072241553e1a7496d638deababa67c5058571567b92a7eaa258397c/pytest-8.4.2.tar.gz", hash = "sha256:86c0d0b93306b961d58d62a4db4879f27fe25513d4b969df351abdddb3c30e01", size = 1519618, upload-time = "2025-09-04T14:34:22.711Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/a8/a4/20da314d277121d6534b3a980b29035dcd51e6744bd79075a6ce8fa4eb8d/pytest-8.4.2-py3-none-any.whl", hash = "sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79", size = 365750, upload-time = "2025-09-04T14:34:20.226Z" }, +] + +[[package]] +name = "pytest-mock" +version = "3.15.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pytest" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/68/14/eb014d26be205d38ad5ad20d9a80f7d201472e08167f0bb4361e251084a9/pytest_mock-3.15.1.tar.gz", hash = "sha256:1849a238f6f396da19762269de72cb1814ab44416fa73a8686deac10b0d87a0f", size = 34036, upload-time = "2025-09-16T16:37:27.081Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5a/cc/06253936f4a7fa2e0f48dfe6d851d9c56df896a9ab09ac019d70b760619c/pytest_mock-3.15.1-py3-none-any.whl", hash = "sha256:0a25e2eb88fe5168d535041d09a4529a188176ae608a6d249ee65abc0949630d", size = 10095, upload-time = "2025-09-16T16:37:25.734Z" }, +] + [[package]] name = "requests" version = "2.32.5" @@ -205,6 +278,8 @@ dependencies = [ [package.dev-dependencies] dev = [ { name = "mypy" }, + { name = "pytest" }, + { name = "pytest-mock" }, { name = "types-beautifulsoup4" }, { name = "types-requests" }, ] @@ -219,6 +294,8 @@ requires-dist = [ [package.metadata.requires-dev] dev = [ { name = "mypy", specifier = ">=1.18.2" }, + { name = "pytest", specifier = ">=8.0.0" }, + { name = "pytest-mock", specifier = ">=3.12.0" }, { name = "types-beautifulsoup4", specifier = ">=4.12.0.20250516" }, { name = "types-requests", specifier = ">=2.32.4.20250913" }, ] From 0e97e3fffa08457d8c50802da76ccd6176f0b1ba Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 22:01:50 -0400 Subject: [PATCH 07/13] feat(ci): typecheck scrapers --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e8a6034..2a69753 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,7 +63,7 @@ jobs: - name: Install ruff run: uv tool install ruff - name: Lint Python files with ruff - run: ruff check scrapers/ + run: ruff check scrapers/ tests/scrapers/ python-typecheck: name: Python Type Checking From 5c2cc0d97da978d0f087f3e04ad097912952b449 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 22:02:21 -0400 Subject: [PATCH 08/13] feat: ignore missing clourscrapers --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 00b350b..289baf0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,3 +21,6 @@ dev = [ [tool.pytest.ini_options] pythonpath = ["."] + +[tool.mypy] +ignore_missing_imports = true From 8a6b5dc373fef538d119ca1ffbb5667af400ea6c Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 22:03:42 -0400 Subject: [PATCH 09/13] fix(ci): import cleanup --- scrapers/atcoder.py | 1 - scrapers/codeforces.py | 1 - scrapers/cses.py | 1 - tests/scrapers/test_atcoder.py | 1 - tests/scrapers/test_codeforces.py | 2 -- tests/scrapers/test_cses.py | 1 - 6 files changed, 7 deletions(-) diff --git a/scrapers/atcoder.py b/scrapers/atcoder.py index 860ada9..0de9f2b 100644 --- a/scrapers/atcoder.py +++ b/scrapers/atcoder.py @@ -2,7 +2,6 @@ import json import sys -from typing import Any import requests from bs4 import BeautifulSoup, Tag diff --git a/scrapers/codeforces.py b/scrapers/codeforces.py index 87272d8..730800a 100644 --- a/scrapers/codeforces.py +++ b/scrapers/codeforces.py @@ -2,7 +2,6 @@ import json import sys -from typing import Any import cloudscraper from bs4 import BeautifulSoup, Tag diff --git a/scrapers/cses.py b/scrapers/cses.py index 6e6fa62..16a9c18 100755 --- a/scrapers/cses.py +++ b/scrapers/cses.py @@ -2,7 +2,6 @@ import json import sys -from typing import Any import requests from bs4 import BeautifulSoup diff --git a/tests/scrapers/test_atcoder.py b/tests/scrapers/test_atcoder.py index 6b0fa40..5086894 100644 --- a/tests/scrapers/test_atcoder.py +++ b/tests/scrapers/test_atcoder.py @@ -1,5 +1,4 @@ from unittest.mock import Mock -import pytest from scrapers.atcoder import scrape, scrape_contest_problems diff --git a/tests/scrapers/test_codeforces.py b/tests/scrapers/test_codeforces.py index 381cc93..eacc801 100644 --- a/tests/scrapers/test_codeforces.py +++ b/tests/scrapers/test_codeforces.py @@ -1,6 +1,4 @@ -import json from unittest.mock import Mock -import pytest from scrapers.codeforces import scrape, scrape_contest_problems diff --git a/tests/scrapers/test_cses.py b/tests/scrapers/test_cses.py index 9df8281..1dd0096 100644 --- a/tests/scrapers/test_cses.py +++ b/tests/scrapers/test_cses.py @@ -1,5 +1,4 @@ from unittest.mock import Mock -import pytest from scrapers.cses import scrape, scrape_all_problems From ffaec3b9479ded5dd92bf498f9496cc88bb8646d Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 22:14:13 -0400 Subject: [PATCH 10/13] fix(ci): type scrapers --- scrapers/atcoder.py | 4 +- scrapers/codeforces.py | 123 +++++++++++------------------- scrapers/models.py | 41 ++++++++++ tests/scrapers/test_codeforces.py | 9 ++- 4 files changed, 92 insertions(+), 85 deletions(-) create mode 100644 scrapers/models.py diff --git a/scrapers/atcoder.py b/scrapers/atcoder.py index 0de9f2b..b9b39ea 100644 --- a/scrapers/atcoder.py +++ b/scrapers/atcoder.py @@ -52,7 +52,7 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]: if not task_table or not isinstance(task_table, Tag): return [] - rows = task_table.find_all("tr")[1:] # skip header + rows = task_table.find_all("tr")[1:] problems: list[dict[str, str]] = [] for row in rows: problem = extract_problem_from_row(row, contest_id) @@ -115,7 +115,7 @@ def scrape(url: str) -> list[tuple[str, str]]: test_case = extract_test_case_from_headers(sample_headers, i) if test_case: tests.append(test_case) - i += 2 # move from "Sample Input n" to after "Sample Output n" + i += 2 else: i += 1 diff --git a/scrapers/codeforces.py b/scrapers/codeforces.py index 730800a..c193a21 100644 --- a/scrapers/codeforces.py +++ b/scrapers/codeforces.py @@ -2,12 +2,15 @@ import json import sys +from dataclasses import asdict import cloudscraper from bs4 import BeautifulSoup, Tag +from .models import MetadataResult, Problem, TestCase, TestsResult -def scrape(url: str) -> list[tuple[str, str]]: + +def scrape(url: str) -> list[TestCase]: try: scraper = cloudscraper.create_scraper() response = scraper.get(url, timeout=10) @@ -88,7 +91,7 @@ def scrape(url: str) -> list[tuple[str, str]]: input_text = "\n".join(individual_inputs[test_num]) output_text = "\n".join(individual_outputs[test_num]) prefixed_input = "1\n" + input_text - tests.append((prefixed_input, output_text)) + tests.append(TestCase(input=prefixed_input, expected=output_text)) return tests all_inputs = [] all_outputs = [] @@ -124,7 +127,7 @@ def scrape(url: str) -> list[tuple[str, str]]: combined_input = "\n".join(all_inputs) combined_output = "\n".join(all_outputs) - return [(combined_input, combined_output)] + return [TestCase(input=combined_input, expected=combined_output)] except Exception as e: print(f"CloudScraper failed: {e}", file=sys.stderr) @@ -137,7 +140,7 @@ def parse_problem_url(contest_id: str, problem_letter: str) -> str: ) -def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]: +def scrape_contest_problems(contest_id: str) -> list[Problem]: try: contest_url: str = f"https://codeforces.com/contest/{contest_id}" scraper = cloudscraper.create_scraper() @@ -145,7 +148,7 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]: response.raise_for_status() soup = BeautifulSoup(response.text, "html.parser") - problems: list[dict[str, str]] = [] + problems: list[Problem] = [] problem_links = soup.find_all( "a", href=lambda x: x and f"/contest/{contest_id}/problem/" in x @@ -160,15 +163,15 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]: problem_name: str = link.get_text(strip=True) if problem_letter and problem_name: - problems.append({"id": problem_letter, "name": problem_name}) + problems.append(Problem(id=problem_letter, name=problem_name)) - problems.sort(key=lambda x: x["id"]) + problems.sort(key=lambda x: x.id) seen: set[str] = set() - unique_problems: list[dict[str, str]] = [] + unique_problems: list[Problem] = [] for p in problems: - if p["id"] not in seen: - seen.add(p["id"]) + if p.id not in seen: + seen.add(p.id) unique_problems.append(p) return unique_problems @@ -178,71 +181,50 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]: return [] -def scrape_sample_tests(url: str) -> list[tuple[str, str]]: +def scrape_sample_tests(url: str) -> list[TestCase]: print(f"Scraping: {url}", file=sys.stderr) return scrape(url) def main() -> None: if len(sys.argv) < 2: - print( - json.dumps( - { - "success": False, - "error": "Usage: codeforces.py metadata OR codeforces.py tests ", - } - ) + result = MetadataResult( + success=False, + error="Usage: codeforces.py metadata OR codeforces.py tests ", ) + print(json.dumps(asdict(result))) sys.exit(1) mode: str = sys.argv[1] if mode == "metadata": if len(sys.argv) != 3: - print( - json.dumps( - { - "success": False, - "error": "Usage: codeforces.py metadata ", - } - ) + result = MetadataResult( + success=False, error="Usage: codeforces.py metadata " ) + print(json.dumps(asdict(result))) sys.exit(1) contest_id: str = sys.argv[2] - problems: list[dict[str, str]] = scrape_contest_problems(contest_id) + problems: list[Problem] = scrape_contest_problems(contest_id) if not problems: - print( - json.dumps( - { - "success": False, - "error": f"No problems found for contest {contest_id}", - } - ) + result = MetadataResult( + success=False, error=f"No problems found for contest {contest_id}" ) + print(json.dumps(asdict(result))) sys.exit(1) - print( - json.dumps( - { - "success": True, - "contest_id": contest_id, - "problems": problems, - } - ) - ) + result = MetadataResult(success=True, contest_id=contest_id, problems=problems) + print(json.dumps(asdict(result))) elif mode == "tests": if len(sys.argv) != 4: - print( - json.dumps( - { - "success": False, - "error": "Usage: codeforces.py tests ", - } - ) + tests_result = TestsResult( + success=False, + error="Usage: codeforces.py tests ", ) + print(json.dumps(asdict(tests_result))) sys.exit(1) tests_contest_id: str = sys.argv[2] @@ -250,45 +232,28 @@ def main() -> None: problem_id: str = tests_contest_id + problem_letter.lower() url: str = parse_problem_url(tests_contest_id, problem_letter) - tests: list[tuple[str, str]] = scrape_sample_tests(url) + tests: list[TestCase] = scrape_sample_tests(url) if not tests: - print( - json.dumps( - { - "success": False, - "error": f"No tests found for {tests_contest_id} {problem_letter}", - "problem_id": problem_id, - "url": url, - } - ) + tests_result = TestsResult( + success=False, + error=f"No tests found for {tests_contest_id} {problem_letter}", + problem_id=problem_id, + url=url, ) + print(json.dumps(asdict(tests_result))) sys.exit(1) - test_list: list[dict[str, str]] = [] - for input_data, output_data in tests: - test_list.append({"input": input_data, "expected": output_data}) - - print( - json.dumps( - { - "success": True, - "problem_id": problem_id, - "url": url, - "tests": test_list, - } - ) + tests_result = TestsResult( + success=True, problem_id=problem_id, url=url, tests=tests ) + print(json.dumps(asdict(tests_result))) else: - print( - json.dumps( - { - "success": False, - "error": f"Unknown mode: {mode}. Use 'metadata' or 'tests'", - } - ) + result = MetadataResult( + success=False, error=f"Unknown mode: {mode}. Use 'metadata' or 'tests'" ) + print(json.dumps(asdict(result))) sys.exit(1) diff --git a/scrapers/models.py b/scrapers/models.py new file mode 100644 index 0000000..ea0e03e --- /dev/null +++ b/scrapers/models.py @@ -0,0 +1,41 @@ +from dataclasses import dataclass + + +@dataclass +class TestCase: + input: str + expected: str + + +@dataclass +class Problem: + id: str + name: str + + +@dataclass +class ScrapingResult: + success: bool + error: str | None = None + + +@dataclass +class MetadataResult(ScrapingResult): + contest_id: str | None = None + problems: list[Problem] | None = None + categories: dict[str, list[Problem]] | None = None + + def __post_init__(self): + if self.problems is None: + self.problems = [] + + +@dataclass +class TestsResult(ScrapingResult): + problem_id: str = "" + url: str = "" + tests: list[TestCase] | None = None + + def __post_init__(self): + if self.tests is None: + self.tests = [] diff --git a/tests/scrapers/test_codeforces.py b/tests/scrapers/test_codeforces.py index eacc801..e9c9429 100644 --- a/tests/scrapers/test_codeforces.py +++ b/tests/scrapers/test_codeforces.py @@ -1,5 +1,6 @@ from unittest.mock import Mock from scrapers.codeforces import scrape, scrape_contest_problems +from scrapers.models import Problem, TestCase def test_scrape_success(mocker, mock_codeforces_html): @@ -15,8 +16,8 @@ def test_scrape_success(mocker, mock_codeforces_html): result = scrape("https://codeforces.com/contest/1900/problem/A") assert len(result) == 1 - assert result[0][0] == "1\n3\n1 2 3" - assert result[0][1] == "6" + assert result[0].input == "1\n3\n1 2 3" + assert result[0].expected == "6" def test_scrape_contest_problems(mocker): @@ -35,8 +36,8 @@ def test_scrape_contest_problems(mocker): result = scrape_contest_problems("1900") assert len(result) == 2 - assert result[0] == {"id": "a", "name": "A. Problem A"} - assert result[1] == {"id": "b", "name": "B. Problem B"} + assert result[0] == Problem(id="a", name="A. Problem A") + assert result[1] == Problem(id="b", name="B. Problem B") def test_scrape_network_error(mocker): From 7c894720d04998bd44f5b14b592f0cb497271096 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 22:15:06 -0400 Subject: [PATCH 11/13] fix(ci): remove unused import --- tests/scrapers/test_codeforces.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scrapers/test_codeforces.py b/tests/scrapers/test_codeforces.py index e9c9429..67277ea 100644 --- a/tests/scrapers/test_codeforces.py +++ b/tests/scrapers/test_codeforces.py @@ -1,6 +1,6 @@ from unittest.mock import Mock from scrapers.codeforces import scrape, scrape_contest_problems -from scrapers.models import Problem, TestCase +from scrapers.models import Problem def test_scrape_success(mocker, mock_codeforces_html): From 560c8b284636e5d1bf9a93032acd669f5c55389e Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 22:15:48 -0400 Subject: [PATCH 12/13] fix: remove tests for now --- tests/cache_spec.lua | 55 -------------- tests/command_parsing_spec.lua | 79 --------------------- tests/config_spec.lua | 46 ------------ tests/execute_spec.lua | 108 ---------------------------- tests/health_spec.lua | 54 -------------- tests/integration_spec.lua | 114 ------------------------------ tests/problem_spec.lua | 81 --------------------- tests/scraper_spec.lua | 86 ---------------------- tests/scrapers/conftest.py | 41 ----------- tests/scrapers/test_atcoder.py | 50 ------------- tests/scrapers/test_codeforces.py | 53 -------------- tests/scrapers/test_cses.py | 46 ------------ tests/snippets_spec.lua | 68 ------------------ tests/test_panel_spec.lua | 106 --------------------------- 14 files changed, 987 deletions(-) delete mode 100644 tests/cache_spec.lua delete mode 100644 tests/command_parsing_spec.lua delete mode 100644 tests/config_spec.lua delete mode 100644 tests/execute_spec.lua delete mode 100644 tests/health_spec.lua delete mode 100644 tests/integration_spec.lua delete mode 100644 tests/problem_spec.lua delete mode 100644 tests/scraper_spec.lua delete mode 100644 tests/scrapers/conftest.py delete mode 100644 tests/scrapers/test_atcoder.py delete mode 100644 tests/scrapers/test_codeforces.py delete mode 100644 tests/scrapers/test_cses.py delete mode 100644 tests/snippets_spec.lua delete mode 100644 tests/test_panel_spec.lua diff --git a/tests/cache_spec.lua b/tests/cache_spec.lua deleted file mode 100644 index ce8ad5c..0000000 --- a/tests/cache_spec.lua +++ /dev/null @@ -1,55 +0,0 @@ --- Unit tests for caching system -describe('cp.cache', function() - local cache - local temp_dir - - before_each(function() - cache = require('cp.cache') - temp_dir = vim.fn.tempname() - vim.fn.mkdir(temp_dir, 'p') - -- Mock cache directory - end) - - after_each(function() - -- Clean up temp files - vim.fn.delete(temp_dir, 'rf') - end) - - describe('contest metadata caching', function() - it('stores contest metadata correctly', function() - -- Test storing contest data - end) - - it('retrieves cached contest metadata', function() - -- Test retrieving contest data - end) - - it('handles missing cache files gracefully', function() - -- Test missing cache behavior - end) - end) - - describe('test case caching', function() - it('stores test cases for problems', function() - -- Test test case storage - end) - - it('retrieves cached test cases', function() - -- Test test case retrieval - end) - - it('handles cache invalidation', function() - -- Test cache expiry/invalidation - end) - end) - - describe('cache persistence', function() - it('persists cache across sessions', function() - -- Test cache file persistence - end) - - it('handles corrupted cache files', function() - -- Test corrupted cache recovery - end) - end) -end) diff --git a/tests/command_parsing_spec.lua b/tests/command_parsing_spec.lua deleted file mode 100644 index 0abef2b..0000000 --- a/tests/command_parsing_spec.lua +++ /dev/null @@ -1,79 +0,0 @@ --- Unit tests for command parsing and validation -describe('cp command parsing', function() - local cp - - before_each(function() - cp = require('cp') - cp.setup() - end) - - describe('platform setup commands', function() - it('parses :CP codeforces correctly', function() - -- Test platform-only command parsing - end) - - it('parses :CP codeforces 1800 correctly', function() - -- Test contest setup command parsing - end) - - it('parses :CP codeforces 1800 A correctly', function() - -- Test full setup command parsing - end) - - it('parses CSES format :CP cses 1068 correctly', function() - -- Test CSES-specific command parsing - end) - end) - - describe('action commands', function() - it('parses :CP test correctly', function() - -- Test test panel command - end) - - it('parses :CP next correctly', function() - -- Test navigation command - end) - - it('parses :CP prev correctly', function() - -- Test navigation command - end) - end) - - describe('language flags', function() - it('parses --lang=cpp correctly', function() - -- Test language flag parsing - end) - - it('parses --debug flag correctly', function() - -- Test debug flag parsing - end) - - it('combines flags correctly', function() - -- Test multiple flag parsing - end) - end) - - describe('error handling', function() - it('handles invalid commands gracefully', function() - -- Test error messages for bad commands - end) - - it('provides helpful error messages', function() - -- Test error message quality - end) - end) - - describe('command completion', function() - it('completes platform names', function() - -- Test tab completion for platforms - end) - - it('completes problem IDs from cached contest', function() - -- Test problem ID completion - end) - - it('completes action names', function() - -- Test action completion - end) - end) -end) diff --git a/tests/config_spec.lua b/tests/config_spec.lua deleted file mode 100644 index ae1c46a..0000000 --- a/tests/config_spec.lua +++ /dev/null @@ -1,46 +0,0 @@ --- Unit tests for configuration management -describe('cp.config', function() - local config - - before_each(function() - config = require('cp.config') - end) - - describe('setup', function() - it('returns default config when no user config provided', function() - -- Test default configuration values - end) - - it('merges user config with defaults', function() - -- Test config merging behavior - end) - - it('validates contest configurations', function() - -- Test contest config validation - end) - - it('handles invalid config gracefully', function() - -- Test error handling for bad configs - end) - end) - - describe('platform validation', function() - it('accepts valid platforms', function() - -- Test platform validation - end) - - it('rejects invalid platforms', function() - -- Test platform rejection - end) - end) - - describe('language configurations', function() - it('provides correct file extensions for languages', function() - -- Test language -> extension mappings - end) - - it('provides correct compile commands', function() - -- Test compile command generation - end) - end) -end) diff --git a/tests/execute_spec.lua b/tests/execute_spec.lua deleted file mode 100644 index e48447f..0000000 --- a/tests/execute_spec.lua +++ /dev/null @@ -1,108 +0,0 @@ --- Unit tests for code compilation and execution -describe('cp.execute', function() - local execute - local temp_dir - local test_files = {} - - before_each(function() - execute = require('cp.execute') - temp_dir = vim.fn.tempname() - vim.fn.mkdir(temp_dir, 'p') - vim.api.nvim_set_current_dir(temp_dir) - - -- Create sample source files for testing - test_files.cpp = temp_dir .. '/test.cpp' - test_files.python = temp_dir .. '/test.py' - test_files.rust = temp_dir .. '/test.rs' - - -- Write simple test programs - vim.fn.writefile({ - '#include ', - 'int main() { std::cout << "Hello" << std::endl; return 0; }', - }, test_files.cpp) - - vim.fn.writefile({ - 'print("Hello")', - }, test_files.python) - end) - - after_each(function() - vim.fn.delete(temp_dir, 'rf') - end) - - describe('compilation', function() - it('compiles C++ code successfully', function() - -- Test C++ compilation - end) - - it('compiles Rust code successfully', function() - -- Test Rust compilation - end) - - it('handles compilation errors', function() - -- Test error handling for bad code - end) - - it('applies optimization flags correctly', function() - -- Test optimization settings - end) - - it('handles debug flag correctly', function() - -- Test debug compilation - end) - end) - - describe('execution', function() - it('runs compiled programs', function() - -- Test program execution - end) - - it('handles runtime errors', function() - -- Test runtime error handling - end) - - it('enforces time limits', function() - -- Test timeout handling - end) - - it('captures output correctly', function() - -- Test stdout/stderr capture - end) - - it('handles large inputs/outputs', function() - -- Test large data handling - end) - end) - - describe('test case execution', function() - it('runs single test case', function() - -- Test individual test case execution - end) - - it('runs multiple test cases', function() - -- Test batch execution - end) - - it('compares outputs correctly', function() - -- Test output comparison logic - end) - - it('handles edge cases in output comparison', function() - -- Test whitespace, newlines, etc. - end) - end) - - describe('platform-specific execution', function() - it('works on Linux', function() - -- Test Linux-specific behavior - end) - - it('works on macOS', function() - -- Test macOS-specific behavior - end) - - it('works on Windows', function() - -- Test Windows-specific behavior - end) - end) -end) diff --git a/tests/health_spec.lua b/tests/health_spec.lua deleted file mode 100644 index 8f43778..0000000 --- a/tests/health_spec.lua +++ /dev/null @@ -1,54 +0,0 @@ --- Unit tests for health check functionality -describe('cp.health', function() - local health - - before_each(function() - health = require('cp.health') - end) - - describe('system checks', function() - it('detects Neovim version correctly', function() - -- Test Neovim version detection - end) - - it('detects available compilers', function() - -- Test C++, Rust, etc. compiler detection - end) - - it('detects Python installation', function() - -- Test Python availability - end) - - it('checks for required external tools', function() - -- Test curl, wget, etc. availability - end) - end) - - describe('configuration validation', function() - it('validates contest configurations', function() - -- Test config validation - end) - - it('checks directory permissions', function() - -- Test write permissions for directories - end) - - it('validates language configurations', function() - -- Test language setup validation - end) - end) - - describe('health report generation', function() - it('generates comprehensive health report', function() - -- Test :checkhealth cp output - end) - - it('provides actionable recommendations', function() - -- Test that health check gives useful advice - end) - - it('handles partial functionality gracefully', function() - -- Test when some features are unavailable - end) - end) -end) diff --git a/tests/integration_spec.lua b/tests/integration_spec.lua deleted file mode 100644 index 8570d2f..0000000 --- a/tests/integration_spec.lua +++ /dev/null @@ -1,114 +0,0 @@ --- Integration tests for complete workflows -describe('cp.nvim integration', function() - local cp - local temp_dir - - before_each(function() - cp = require('cp') - temp_dir = vim.fn.tempname() - vim.fn.mkdir(temp_dir, 'p') - vim.api.nvim_set_current_dir(temp_dir) - - -- Set up with minimal config - cp.setup({ - scrapers = {}, -- Disable scraping for integration tests - contests = { - codeforces = { - dir = temp_dir, - url = 'mock://codeforces.com', - languages = { - cpp = { extension = 'cpp', compile = 'g++ -o %s %s' }, - }, - }, - }, - }) - end) - - after_each(function() - vim.fn.delete(temp_dir, 'rf') - vim.cmd('silent! %bwipeout!') - end) - - describe('complete problem setup workflow', function() - it('handles :CP codeforces 1800 A workflow', function() - -- Test complete setup from command to file creation - -- 1. Parse command - -- 2. Set up directory structure - -- 3. Create source file - -- 4. Apply template - -- 5. Switch to buffer - end) - - it('handles CSES workflow', function() - -- Test CSES-specific complete workflow - end) - - it('handles language switching', function() - -- Test switching languages for same problem - end) - end) - - describe('problem navigation workflow', function() - it('navigates between problems in contest', function() - -- Test :CP next/:CP prev workflow - -- Requires cached contest metadata - end) - - it('maintains state across navigation', function() - -- Test that work isn't lost when switching problems - end) - end) - - describe('test panel workflow', function() - it('handles complete testing workflow', function() - -- 1. Set up problem - -- 2. Write solution - -- 3. Open test panel (:CP test) - -- 4. Compile and run tests - -- 5. View results - -- 6. Close panel - end) - - it('handles debug workflow', function() - -- Test :CP test --debug workflow - end) - end) - - describe('file system integration', function() - it('maintains proper directory structure', function() - -- Test that files are organized correctly - end) - - it('handles existing files appropriately', function() - -- Test behavior when problem already exists - end) - - it('cleans up temporary files', function() - -- Test cleanup of build artifacts - end) - end) - - describe('error recovery', function() - it('recovers from network failures gracefully', function() - -- Test behavior when scraping fails - end) - - it('recovers from compilation failures', function() - -- Test error handling in compilation - end) - - it('handles corrupted cache gracefully', function() - -- Test cache corruption recovery - end) - end) - - describe('multi-session behavior', function() - it('persists state across Neovim restarts', function() - -- Test that contest/problem state persists - end) - - it('handles concurrent usage', function() - -- Test multiple Neovim instances - end) - end) -end) diff --git a/tests/problem_spec.lua b/tests/problem_spec.lua deleted file mode 100644 index c895be9..0000000 --- a/tests/problem_spec.lua +++ /dev/null @@ -1,81 +0,0 @@ --- Unit tests for problem context and file management -describe('cp.problem', function() - local problem - local temp_dir - - before_each(function() - problem = require('cp.problem') - temp_dir = vim.fn.tempname() - vim.fn.mkdir(temp_dir, 'p') - -- Change to temp directory for testing - vim.api.nvim_set_current_dir(temp_dir) - end) - - after_each(function() - vim.fn.delete(temp_dir, 'rf') - end) - - describe('context creation', function() - it('creates context for Codeforces problems', function() - -- Test context creation with proper paths - end) - - it('creates context for CSES problems', function() - -- Test CSES-specific context - end) - - it('generates correct file paths', function() - -- Test source file path generation - end) - - it('generates correct build paths', function() - -- Test build directory structure - end) - end) - - describe('template handling', function() - it('applies language templates correctly', function() - -- Test template application - end) - - it('handles custom templates', function() - -- Test user-defined templates - end) - - it('supports snippet integration', function() - -- Test LuaSnip integration - end) - end) - - describe('file operations', function() - it('creates directory structure', function() - -- Test directory creation (build/, io/) - end) - - it('handles existing files gracefully', function() - -- Test behavior when files exist - end) - - it('sets up input/output files', function() - -- Test I/O file creation - end) - end) - - describe('language support', function() - it('supports C++ compilation', function() - -- Test C++ setup and compilation - end) - - it('supports Python execution', function() - -- Test Python setup - end) - - it('supports Rust compilation', function() - -- Test Rust setup - end) - - it('supports custom language configurations', function() - -- Test user-defined language support - end) - end) -end) diff --git a/tests/scraper_spec.lua b/tests/scraper_spec.lua deleted file mode 100644 index 679b4f9..0000000 --- a/tests/scraper_spec.lua +++ /dev/null @@ -1,86 +0,0 @@ --- Unit tests for web scraping functionality -describe('cp.scrape', function() - local scrape - local mock_responses = {} - - before_each(function() - scrape = require('cp.scrape') - - -- Mock HTTP responses for different platforms - mock_responses.codeforces_contest = [[ -
-
Problem A
-
Problem B
-
- ]] - - mock_responses.codeforces_problem = [[ -
Sample Input
-
Sample Output
- ]] - end) - - describe('contest metadata scraping', function() - it('scrapes Codeforces contest problems', function() - -- Mock HTTP request, test problem list extraction - end) - - it('scrapes Atcoder contest problems', function() - -- Test Atcoder format - end) - - it('scrapes CSES problem list', function() - -- Test CSES format - end) - - it('handles network errors gracefully', function() - -- Test error handling for failed requests - end) - - it('handles parsing errors gracefully', function() - -- Test error handling for malformed HTML - end) - end) - - describe('problem scraping', function() - it('extracts test cases from Codeforces problems', function() - -- Test test case extraction - end) - - it('handles multiple test cases correctly', function() - -- Test multiple sample inputs/outputs - end) - - it('handles problems with no sample cases', function() - -- Test edge case handling - end) - - it('extracts problem metadata (time limits, etc.)', function() - -- Test metadata extraction - end) - end) - - describe('platform-specific parsing', function() - it('handles Codeforces HTML structure', function() - -- Test Codeforces-specific parsing - end) - - it('handles Atcoder HTML structure', function() - -- Test Atcoder-specific parsing - end) - - it('handles CSES HTML structure', function() - -- Test CSES-specific parsing - end) - end) - - describe('rate limiting and caching', function() - it('respects rate limits', function() - -- Test rate limiting behavior - end) - - it('uses cached results when appropriate', function() - -- Test caching integration - end) - end) -end) diff --git a/tests/scrapers/conftest.py b/tests/scrapers/conftest.py deleted file mode 100644 index 3248ec2..0000000 --- a/tests/scrapers/conftest.py +++ /dev/null @@ -1,41 +0,0 @@ -import pytest - - -@pytest.fixture -def mock_codeforces_html(): - return """ -
-
-            
3
-
1 2 3
-
-
-
-
-            
6
-
-
- """ - - -@pytest.fixture -def mock_atcoder_html(): - return """ -

Sample Input 1

-
3
-1 2 3
-

Sample Output 1

-
6
- """ - - -@pytest.fixture -def mock_cses_html(): - return """ -

Example

-

Input:

-
3
-1 2 3
-

Output:

-
6
- """ diff --git a/tests/scrapers/test_atcoder.py b/tests/scrapers/test_atcoder.py deleted file mode 100644 index 5086894..0000000 --- a/tests/scrapers/test_atcoder.py +++ /dev/null @@ -1,50 +0,0 @@ -from unittest.mock import Mock -from scrapers.atcoder import scrape, scrape_contest_problems - - -def test_scrape_success(mocker, mock_atcoder_html): - mock_response = Mock() - mock_response.text = mock_atcoder_html - - mocker.patch("scrapers.atcoder.requests.get", return_value=mock_response) - - result = scrape("https://atcoder.jp/contests/abc350/tasks/abc350_a") - - assert len(result) == 1 - assert result[0][0] == "3\n1 2 3" - assert result[0][1] == "6" - - -def test_scrape_contest_problems(mocker): - mock_response = Mock() - mock_response.text = """ - - - - - - - - - - -
TaskName
A - Water Tank
B - Dentist Aoki
- """ - - mocker.patch("scrapers.atcoder.requests.get", return_value=mock_response) - - result = scrape_contest_problems("abc350") - - assert len(result) == 2 - assert result[0] == {"id": "a", "name": "A - Water Tank"} - assert result[1] == {"id": "b", "name": "B - Dentist Aoki"} - - -def test_scrape_network_error(mocker): - mocker.patch( - "scrapers.atcoder.requests.get", side_effect=Exception("Network error") - ) - - result = scrape("https://atcoder.jp/contests/abc350/tasks/abc350_a") - - assert result == [] diff --git a/tests/scrapers/test_codeforces.py b/tests/scrapers/test_codeforces.py deleted file mode 100644 index 67277ea..0000000 --- a/tests/scrapers/test_codeforces.py +++ /dev/null @@ -1,53 +0,0 @@ -from unittest.mock import Mock -from scrapers.codeforces import scrape, scrape_contest_problems -from scrapers.models import Problem - - -def test_scrape_success(mocker, mock_codeforces_html): - mock_scraper = Mock() - mock_response = Mock() - mock_response.text = mock_codeforces_html - mock_scraper.get.return_value = mock_response - - mocker.patch( - "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper - ) - - result = scrape("https://codeforces.com/contest/1900/problem/A") - - assert len(result) == 1 - assert result[0].input == "1\n3\n1 2 3" - assert result[0].expected == "6" - - -def test_scrape_contest_problems(mocker): - mock_scraper = Mock() - mock_response = Mock() - mock_response.text = """ - A. Problem A - B. Problem B - """ - mock_scraper.get.return_value = mock_response - - mocker.patch( - "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper - ) - - result = scrape_contest_problems("1900") - - assert len(result) == 2 - assert result[0] == Problem(id="a", name="A. Problem A") - assert result[1] == Problem(id="b", name="B. Problem B") - - -def test_scrape_network_error(mocker): - mock_scraper = Mock() - mock_scraper.get.side_effect = Exception("Network error") - - mocker.patch( - "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper - ) - - result = scrape("https://codeforces.com/contest/1900/problem/A") - - assert result == [] diff --git a/tests/scrapers/test_cses.py b/tests/scrapers/test_cses.py deleted file mode 100644 index 1dd0096..0000000 --- a/tests/scrapers/test_cses.py +++ /dev/null @@ -1,46 +0,0 @@ -from unittest.mock import Mock -from scrapers.cses import scrape, scrape_all_problems - - -def test_scrape_success(mocker, mock_cses_html): - mock_response = Mock() - mock_response.text = mock_cses_html - - mocker.patch("scrapers.cses.requests.get", return_value=mock_response) - - result = scrape("https://cses.fi/problemset/task/1068") - - assert len(result) == 1 - assert result[0][0] == "3\n1 2 3" - assert result[0][1] == "6" - - -def test_scrape_all_problems(mocker): - mock_response = Mock() - mock_response.text = """ -

Introductory Problems

- Weird Algorithm - Missing Number -

Sorting and Searching

- Apartments - """ - - mocker.patch("scrapers.cses.requests.get", return_value=mock_response) - - result = scrape_all_problems() - - assert "Introductory Problems" in result - assert "Sorting and Searching" in result - assert len(result["Introductory Problems"]) == 2 - assert result["Introductory Problems"][0] == { - "id": "1068", - "name": "Weird Algorithm", - } - - -def test_scrape_network_error(mocker): - mocker.patch("scrapers.cses.requests.get", side_effect=Exception("Network error")) - - result = scrape("https://cses.fi/problemset/task/1068") - - assert result == [] diff --git a/tests/snippets_spec.lua b/tests/snippets_spec.lua deleted file mode 100644 index ed81205..0000000 --- a/tests/snippets_spec.lua +++ /dev/null @@ -1,68 +0,0 @@ --- Unit tests for snippet/template functionality -describe('cp.snippets', function() - local snippets - - before_each(function() - snippets = require('cp.snippets') - end) - - describe('template loading', function() - it('loads default templates correctly', function() - -- Test default template loading - end) - - it('loads platform-specific templates', function() - -- Test Codeforces vs CSES templates - end) - - it('loads language-specific templates', function() - -- Test C++ vs Python vs Rust templates - end) - - it('handles custom user templates', function() - -- Test user-defined template integration - end) - end) - - describe('LuaSnip integration', function() - it('registers snippets with LuaSnip', function() - -- Test snippet registration - end) - - it('provides platform-language combinations', function() - -- Test snippet triggers like cp.nvim/codeforces.cpp - end) - - it('handles missing LuaSnip gracefully', function() - -- Test fallback when LuaSnip not available - end) - end) - - describe('template expansion', function() - it('expands templates with correct content', function() - -- Test template content expansion - end) - - it('handles template variables', function() - -- Test variable substitution in templates - end) - - it('maintains cursor positioning', function() - -- Test cursor placement after expansion - end) - end) - - describe('template management', function() - it('allows template customization', function() - -- Test user template override - end) - - it('supports template inheritance', function() - -- Test template extending/modification - end) - - it('validates template syntax', function() - -- Test template validation - end) - end) -end) diff --git a/tests/test_panel_spec.lua b/tests/test_panel_spec.lua deleted file mode 100644 index 41de1cc..0000000 --- a/tests/test_panel_spec.lua +++ /dev/null @@ -1,106 +0,0 @@ --- UI/buffer tests for the interactive test panel -describe('cp test panel', function() - local cp - - before_each(function() - cp = require('cp') - cp.setup() - -- Set up a clean Neovim environment - vim.cmd('silent! %bwipeout!') - end) - - after_each(function() - -- Clean up test panel state - vim.cmd('silent! %bwipeout!') - end) - - describe('panel creation', function() - it('creates test panel buffers', function() - -- Test buffer creation for tab, expected, actual views - end) - - it('sets up correct window layout', function() - -- Test 3-pane layout creation - end) - - it('applies correct buffer settings', function() - -- Test buffer options (buftype, filetype, etc.) - end) - - it('sets up keymaps correctly', function() - -- Test navigation keymaps (Ctrl+N, Ctrl+P, q) - end) - end) - - describe('test case display', function() - it('renders test case tabs correctly', function() - -- Test tab line rendering with status indicators - end) - - it('displays input correctly', function() - -- Test input pane content - end) - - it('displays expected output correctly', function() - -- Test expected output pane - end) - - it('displays actual output correctly', function() - -- Test actual output pane - end) - - it('shows diff when test fails', function() - -- Test diff mode activation - end) - end) - - describe('navigation', function() - it('navigates to next test case', function() - -- Test Ctrl+N navigation - end) - - it('navigates to previous test case', function() - -- Test Ctrl+P navigation - end) - - it('wraps around at boundaries', function() - -- Test navigation wrapping - end) - - it('updates display on navigation', function() - -- Test content updates when switching tests - end) - end) - - describe('test execution integration', function() - it('compiles and runs tests automatically', function() - -- Test automatic compilation and execution - end) - - it('updates results in real-time', function() - -- Test live result updates - end) - - it('handles compilation failures', function() - -- Test error display when compilation fails - end) - - it('shows execution time', function() - -- Test timing display - end) - end) - - describe('session management', function() - it('saves and restores session correctly', function() - -- Test session save/restore when opening/closing panel - end) - - it('handles multiple panels gracefully', function() - -- Test behavior with multiple test panels - end) - - it('cleans up resources on close', function() - -- Test proper cleanup when closing panel - end) - end) -end) From 2704fe6d7273be27bc791da3d6ff2b49cc6841e2 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 18 Sep 2025 22:17:14 -0400 Subject: [PATCH 13/13] fix(ci): include scrapers, though --- tests/scrapers/conftest.py | 41 ++++++++++++++++++++++++ tests/scrapers/test_atcoder.py | 50 +++++++++++++++++++++++++++++ tests/scrapers/test_codeforces.py | 53 +++++++++++++++++++++++++++++++ tests/scrapers/test_cses.py | 46 +++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 tests/scrapers/conftest.py create mode 100644 tests/scrapers/test_atcoder.py create mode 100644 tests/scrapers/test_codeforces.py create mode 100644 tests/scrapers/test_cses.py diff --git a/tests/scrapers/conftest.py b/tests/scrapers/conftest.py new file mode 100644 index 0000000..3248ec2 --- /dev/null +++ b/tests/scrapers/conftest.py @@ -0,0 +1,41 @@ +import pytest + + +@pytest.fixture +def mock_codeforces_html(): + return """ +
+
+            
3
+
1 2 3
+
+
+
+
+            
6
+
+
+ """ + + +@pytest.fixture +def mock_atcoder_html(): + return """ +

Sample Input 1

+
3
+1 2 3
+

Sample Output 1

+
6
+ """ + + +@pytest.fixture +def mock_cses_html(): + return """ +

Example

+

Input:

+
3
+1 2 3
+

Output:

+
6
+ """ diff --git a/tests/scrapers/test_atcoder.py b/tests/scrapers/test_atcoder.py new file mode 100644 index 0000000..5086894 --- /dev/null +++ b/tests/scrapers/test_atcoder.py @@ -0,0 +1,50 @@ +from unittest.mock import Mock +from scrapers.atcoder import scrape, scrape_contest_problems + + +def test_scrape_success(mocker, mock_atcoder_html): + mock_response = Mock() + mock_response.text = mock_atcoder_html + + mocker.patch("scrapers.atcoder.requests.get", return_value=mock_response) + + result = scrape("https://atcoder.jp/contests/abc350/tasks/abc350_a") + + assert len(result) == 1 + assert result[0][0] == "3\n1 2 3" + assert result[0][1] == "6" + + +def test_scrape_contest_problems(mocker): + mock_response = Mock() + mock_response.text = """ + + + + + + + + + + +
TaskName
A - Water Tank
B - Dentist Aoki
+ """ + + mocker.patch("scrapers.atcoder.requests.get", return_value=mock_response) + + result = scrape_contest_problems("abc350") + + assert len(result) == 2 + assert result[0] == {"id": "a", "name": "A - Water Tank"} + assert result[1] == {"id": "b", "name": "B - Dentist Aoki"} + + +def test_scrape_network_error(mocker): + mocker.patch( + "scrapers.atcoder.requests.get", side_effect=Exception("Network error") + ) + + result = scrape("https://atcoder.jp/contests/abc350/tasks/abc350_a") + + assert result == [] diff --git a/tests/scrapers/test_codeforces.py b/tests/scrapers/test_codeforces.py new file mode 100644 index 0000000..67277ea --- /dev/null +++ b/tests/scrapers/test_codeforces.py @@ -0,0 +1,53 @@ +from unittest.mock import Mock +from scrapers.codeforces import scrape, scrape_contest_problems +from scrapers.models import Problem + + +def test_scrape_success(mocker, mock_codeforces_html): + mock_scraper = Mock() + mock_response = Mock() + mock_response.text = mock_codeforces_html + mock_scraper.get.return_value = mock_response + + mocker.patch( + "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper + ) + + result = scrape("https://codeforces.com/contest/1900/problem/A") + + assert len(result) == 1 + assert result[0].input == "1\n3\n1 2 3" + assert result[0].expected == "6" + + +def test_scrape_contest_problems(mocker): + mock_scraper = Mock() + mock_response = Mock() + mock_response.text = """ + A. Problem A + B. Problem B + """ + mock_scraper.get.return_value = mock_response + + mocker.patch( + "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper + ) + + result = scrape_contest_problems("1900") + + assert len(result) == 2 + assert result[0] == Problem(id="a", name="A. Problem A") + assert result[1] == Problem(id="b", name="B. Problem B") + + +def test_scrape_network_error(mocker): + mock_scraper = Mock() + mock_scraper.get.side_effect = Exception("Network error") + + mocker.patch( + "scrapers.codeforces.cloudscraper.create_scraper", return_value=mock_scraper + ) + + result = scrape("https://codeforces.com/contest/1900/problem/A") + + assert result == [] diff --git a/tests/scrapers/test_cses.py b/tests/scrapers/test_cses.py new file mode 100644 index 0000000..1dd0096 --- /dev/null +++ b/tests/scrapers/test_cses.py @@ -0,0 +1,46 @@ +from unittest.mock import Mock +from scrapers.cses import scrape, scrape_all_problems + + +def test_scrape_success(mocker, mock_cses_html): + mock_response = Mock() + mock_response.text = mock_cses_html + + mocker.patch("scrapers.cses.requests.get", return_value=mock_response) + + result = scrape("https://cses.fi/problemset/task/1068") + + assert len(result) == 1 + assert result[0][0] == "3\n1 2 3" + assert result[0][1] == "6" + + +def test_scrape_all_problems(mocker): + mock_response = Mock() + mock_response.text = """ +

Introductory Problems

+ Weird Algorithm + Missing Number +

Sorting and Searching

+ Apartments + """ + + mocker.patch("scrapers.cses.requests.get", return_value=mock_response) + + result = scrape_all_problems() + + assert "Introductory Problems" in result + assert "Sorting and Searching" in result + assert len(result["Introductory Problems"]) == 2 + assert result["Introductory Problems"][0] == { + "id": "1068", + "name": "Weird Algorithm", + } + + +def test_scrape_network_error(mocker): + mocker.patch("scrapers.cses.requests.get", side_effect=Exception("Network error")) + + result = scrape("https://cses.fi/problemset/task/1068") + + assert result == []