feat: context, not config

This commit is contained in:
Barrett Ruth 2025-09-24 18:21:34 -04:00
parent a0171ee81e
commit 9e84d57b8a
15 changed files with 209 additions and 328 deletions

View file

@ -9,9 +9,6 @@ describe('Error boundary handling', function()
log = function(msg, level)
table.insert(logged_messages, { msg = msg, level = level })
end,
progress = function(msg)
table.insert(logged_messages, { msg = msg, level = vim.log.levels.INFO })
end,
set_config = function() end,
}
package.loaded['cp.log'] = mock_logger

View file

@ -88,20 +88,22 @@ describe('Panel integration', function()
state.set_contest_id('2146')
state.set_problem_id('b')
local problem = require('cp.problem')
local config_module = require('cp.config')
local processed_config = config_module.setup({
contests = { codeforces = { cpp = { extension = 'cpp' } } },
})
local ctx = problem.create_context('codeforces', '2146', 'b', processed_config)
local cp_state = require('cp.state')
cp_state.set_platform('codeforces')
cp_state.set_contest_id('2146')
cp_state.set_problem_id('b')
assert.has_no_errors(function()
run.load_test_cases(ctx, state)
run.load_test_cases(state)
end)
local fake_state_data = { platform = 'codeforces', contest_id = '2146', problem_id = 'b' }
assert.has_errors(function()
run.load_test_cases(ctx, fake_state_data)
run.load_test_cases(fake_state_data)
end)
end)
end)

View file

@ -1,146 +0,0 @@
describe('cp.problem', function()
local problem
local spec_helper = require('spec.spec_helper')
before_each(function()
spec_helper.setup()
problem = require('cp.problem')
end)
after_each(function()
spec_helper.teardown()
end)
describe('create_context', function()
local base_config = {
contests = {
atcoder = {
default_language = 'cpp',
cpp = { extension = 'cpp' },
python = { extension = 'py' },
},
codeforces = {
default_language = 'cpp',
cpp = { extension = 'cpp' },
},
},
}
it('creates basic context with required fields', function()
local context = problem.create_context('atcoder', 'abc123', 'a', base_config)
assert.equals('atcoder', context.contest)
assert.equals('abc123', context.contest_id)
assert.equals('a', context.problem_id)
assert.equals('abc123a', context.problem_name)
assert.equals('abc123a.cpp', context.source_file)
assert.equals('build/abc123a.run', context.binary_file)
assert.equals('io/abc123a.cpin', context.input_file)
assert.equals('io/abc123a.cpout', context.output_file)
assert.equals('io/abc123a.expected', context.expected_file)
end)
it('handles context without problem_id', function()
local context = problem.create_context('codeforces', '1933', nil, base_config)
assert.equals('codeforces', context.contest)
assert.equals('1933', context.contest_id)
assert.is_nil(context.problem_id)
assert.equals('1933', context.problem_name)
assert.equals('1933.cpp', context.source_file)
assert.equals('build/1933.run', context.binary_file)
end)
it('uses default language from contest config', function()
local context = problem.create_context('atcoder', 'abc123', 'a', base_config)
assert.equals('abc123a.cpp', context.source_file)
end)
it('respects explicit language parameter', function()
local context = problem.create_context('atcoder', 'abc123', 'a', base_config, 'python')
assert.equals('abc123a.py', context.source_file)
end)
it('uses custom filename function when provided', function()
local config_with_custom = vim.tbl_deep_extend('force', base_config, {
filename = function(contest, contest_id, problem_id)
return contest .. '_' .. contest_id .. (problem_id and ('_' .. problem_id) or '')
end,
})
local context = problem.create_context('atcoder', 'abc123', 'a', config_with_custom)
assert.equals('atcoder_abc123_a.cpp', context.source_file)
assert.equals('atcoder_abc123_a', context.problem_name)
end)
it('validates required parameters', function()
assert.has_error(function()
problem.create_context(nil, 'abc123', 'a', base_config)
end)
assert.has_error(function()
problem.create_context('atcoder', nil, 'a', base_config)
end)
assert.has_error(function()
problem.create_context('atcoder', 'abc123', 'a', nil)
end)
end)
it('validates contest exists in config', function()
assert.has_error(function()
problem.create_context('invalid_contest', 'abc123', 'a', base_config)
end)
end)
it('validates language exists in contest config', function()
assert.has_error(function()
problem.create_context('atcoder', 'abc123', 'a', base_config, 'invalid_language')
end)
end)
it('validates default language exists', function()
local bad_config = {
contests = {
test_contest = {
default_language = 'nonexistent',
},
},
}
assert.has_error(function()
problem.create_context('test_contest', 'abc123', 'a', bad_config)
end)
end)
it('validates language extension is configured', function()
local bad_config = {
contests = {
test_contest = {
default_language = 'cpp',
cpp = {},
},
},
}
assert.has_error(function()
problem.create_context('test_contest', 'abc123', 'a', bad_config)
end)
end)
it('handles complex contest and problem ids', function()
local context = problem.create_context('atcoder', 'arc123', 'f', base_config)
assert.equals('arc123f', context.problem_name)
assert.equals('arc123f.cpp', context.source_file)
assert.equals('build/arc123f.run', context.binary_file)
end)
it('generates correct io file paths', function()
local context = problem.create_context('atcoder', 'abc123', 'a', base_config)
assert.equals('io/abc123a.cpin', context.input_file)
assert.equals('io/abc123a.cpout', context.output_file)
assert.equals('io/abc123a.expected', context.expected_file)
end)
end)
end)

View file

@ -6,9 +6,6 @@ local mock_logger = {
log = function(msg, level)
table.insert(M.logged_messages, { msg = msg, level = level })
end,
progress = function(msg)
table.insert(M.logged_messages, { msg = msg, level = vim.log.levels.INFO })
end,
set_config = function() end,
}
@ -83,10 +80,11 @@ end
function M.mock_scraper_success()
package.loaded['cp.scrape'] = {
scrape_problem = function(ctx)
scrape_problem = function()
local state = require('cp.state')
return {
success = true,
problem_id = ctx.problem_id,
problem_id = state.get_problem_id(),
test_cases = {
{ input = '1 2', expected = '3' },
{ input = '3 4', expected = '7' },