Skip to content

feat: Add offline mode - #410

Open
ericklucioh wants to merge 21 commits into
j178:masterfrom
ericklucioh:master
Open

feat: Add offline mode#410
ericklucioh wants to merge 21 commits into
j178:masterfrom
ericklucioh:master

Conversation

@ericklucioh

Copy link
Copy Markdown

This pull request introduces a new "offline mode" to the CLI tool, allowing users to save and run LeetCode questions and tests locally without network access. It adds mechanisms to persist question metadata and test artifacts, as well as new commands and interfaces to support running tests offline for supported languages. The implementation includes code changes across the command layer, configuration, language backends, and new tests to ensure correctness.

Offline mode support:

  • Added a new --offline flag to the leetgo test command, enabling users to run tests using only locally saved artifacts. This mode is mutually exclusive with other test modes (--local, --both, --submit, --force). [1] [2] [3]
  • Implemented logic to save question metadata and file paths to a new offline state file (offline.json) when picking a question, enabling later retrieval for offline testing. [1] [2] [3]
  • Added a new runOfflineQuestion function to handle offline test execution, including validation and error handling for unsupported question types (e.g., system design).

Configuration and state management:

  • Introduced new types and functions in config/offline.go to manage offline questions and state, including saving, loading, and resolving offline questions based on language and slug/frontend ID. [1] [2]
  • Added a new method to retrieve the offline state file path from the config.

Language backend support for offline testing:

  • Defined a new OfflineTestable interface in lang/base.go for languages that support offline testing, and implemented RunOfflineTest for Go, C++, Python, and Rust backends. [1] [2] [3] [4] [5]
  • Added a helper to construct a GenerateResult from offline metadata, enabling language backends to locate code and test files for offline runs.

Testing and utilities:

  • Added comprehensive unit tests for offline question resolution, offline test case parsing, and GenerateResult construction from offline data.
  • Refactored and extended helpers for file path management and test execution to support offline scenarios. [1] [2]

Dependency and import updates:

  • Updated imports and dependencies to include new packages required for offline support and logging.

These changes together enable robust offline workflows for users who want to practice or test LeetCode problems locally, improving usability in environments with limited or no network access.

@ericklucioh

Copy link
Copy Markdown
Author

hello. i fixed the lint

ericklucioh and others added 2 commits May 22, 2026 19:31
* Add support for custom OpenAI model in fix command

* Use Openai Offcial SDK and disable thinking for model
@j178

j178 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Thanks for working on this. I agree the offline use case is useful, but I don't think we should add it as a separate --offline mode with a second execution path.

leetgo test -L already means "run locally": it builds and executes the generated local test harness and reads the local testcases.txt. The current gap is narrower: the command still goes through question resolution / q.Fulfill(), so it may need network access when the question metadata is not already available. I think we should fix that gap in the existing -L path instead of introducing a new mode.

The current PR adds a lot of parallel machinery:

  • a new offline.json state file
  • a new OfflineQuestion model
  • a new OfflineTestable interface
  • per-language RunOfflineTest implementations
  • a separate offline test runner and testcase parser

Most of that duplicates behavior that already exists in LocalTestable, RunLocalTest, GeneratePaths, and runTest.

It also creates some behavioral risks:

  • pick now assumes every generated result has a TestCasesFile, but many supported languages still use the base generator and do not generate local test artifacts.
  • The new offline runner handles testcases.txt differently from the existing local runner. For example, existing -L skips cases without expected output, while the new offline parser rejects them.
  • offline.json only records questions generated by pick, so it does not naturally cover existing generated files or contest-generated questions, even though those are local artifacts too.
  • Adding a second local-test path means future fixes to testcase parsing, judging, output handling, timeout behavior, or language-specific execution would need to be kept in sync twice.

I would prefer a smaller design:

  1. Keep leetgo test -L as the user-facing command for local execution.
  2. Make the local-test path prefer cached question metadata when possible.
  3. Only hit the network when the required metadata is missing.
  4. Reuse the existing RunLocalTest / runTest implementation instead of adding RunOfflineTest.
  5. Avoid adding offline.json unless there is a concrete case the existing question cache and generated file paths cannot represent.

So I don't think this PR should be merged in its current shape. The direction I would support is a smaller change that makes -L work better without network access when the question has already been cached/generated.

@ericklucioh

Copy link
Copy Markdown
Author

@j178 Thanks for the feedback. I reworked the PR around the existing local test flow:

  • Removed the separate --offline mode and all duplicated offline execution machinery.
  • Kept leetgo test -L as the only local execution path.
  • Removed the local dependency on q.Fulfill().
  • Reused RunLocalTest, GeneratePaths, runTest, and the existing testcase handling.
  • Added persisted question metadata to the existing project state so generated questions can be resolved locally.
  • Added saved contest question ordering so generated contests can resolve last/1, last/, and aliases such as w330/1 without contacting LeetCode.
  • Preserved compatibility with older projects that do not have saved metadata by falling back to literal output comparison.

I also added tests for local testcase parsing without metadata, saved question resolution, and saved contest resolution.

@j178

j178 commented Jul 23, 2026

Copy link
Copy Markdown
Owner

Thank you for your work on this, and sorry for the delayed response. I haven’t been actively working on this project lately and don’t have much time or focus to dedicate to it right now, so I can’t guarantee when this will be merged. However, I’ll come back to it when I can and take care of any changes that may be needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants