fix(test): typing

This commit is contained in:
Barrett Ruth 2025-09-22 20:21:20 -04:00
parent 23310eed53
commit 80c7697340
2 changed files with 22 additions and 35 deletions

View file

@ -1,19 +1,12 @@
describe('Panel integration', function() describe('Panel integration', function()
local spec_helper = require('spec.spec_helper')
local cp local cp
local state local state
local logged_messages
before_each(function() before_each(function()
logged_messages = {} spec_helper.setup_full()
local mock_logger = { spec_helper.mock_scraper_success()
log = function(msg, level)
table.insert(logged_messages, { msg = msg, level = level })
end,
set_config = function() end,
}
package.loaded['cp.log'] = mock_logger
-- Reset state completely
state = require('cp.state') state = require('cp.state')
state.reset() state.reset()
@ -30,30 +23,26 @@ describe('Panel integration', function()
end) end)
after_each(function() after_each(function()
package.loaded['cp.log'] = nil spec_helper.teardown()
if state then if state then
state.reset() state.reset()
end end
end) end)
it('should handle run command with properly set contest context', function() it('should handle run command with properly set contest context', function()
-- First set up a contest context
cp.handle_command({ fargs = { 'codeforces', '2146', 'b' } }) cp.handle_command({ fargs = { 'codeforces', '2146', 'b' } })
-- Verify state was set correctly
local context = cp.get_current_context() local context = cp.get_current_context()
assert.equals('codeforces', context.platform) assert.equals('codeforces', context.platform)
assert.equals('2146', context.contest_id) assert.equals('2146', context.contest_id)
assert.equals('b', context.problem_id) assert.equals('b', context.problem_id)
-- Now try to run the panel - this should NOT crash with "contest_id: expected string, got nil"
assert.has_no_errors(function() assert.has_no_errors(function()
cp.handle_command({ fargs = { 'run' } }) cp.handle_command({ fargs = { 'run' } })
end) end)
-- Should log panel opened or no test cases found, but NOT a validation error
local has_validation_error = false local has_validation_error = false
for _, log_entry in ipairs(logged_messages) do for _, log_entry in ipairs(spec_helper.logged_messages) do
if if
log_entry.level == vim.log.levels.ERROR log_entry.level == vim.log.levels.ERROR
and log_entry.msg:match('expected string, got nil') and log_entry.msg:match('expected string, got nil')
@ -65,29 +54,25 @@ describe('Panel integration', function()
assert.is_false(has_validation_error) assert.is_false(has_validation_error)
end) end)
it('should catch state module vs state object contract violations', function() it('should handle state module interface correctly', function()
-- This test specifically verifies that runner functions receive the right data type
local run = require('cp.runner.run') local run = require('cp.runner.run')
local problem = require('cp.problem')
local config = require('cp.config')
-- Set up state properly
state.set_platform('codeforces') state.set_platform('codeforces')
state.set_contest_id('2146') state.set_contest_id('2146')
state.set_problem_id('b') state.set_problem_id('b')
-- Create a proper context local problem = require('cp.problem')
local ctx = problem.create_context('codeforces', '2146', 'b', config.defaults) local ctx = problem.create_context('codeforces', '2146', 'b', {
contests = { codeforces = { cpp = { extension = 'cpp' } } },
})
-- This should work - passing the state MODULE (not state data)
assert.has_no_errors(function() assert.has_no_errors(function()
run.load_test_cases(ctx, state) run.load_test_cases(ctx, state)
end) end)
-- This would break if we passed state data instead of state module
local fake_state_data = { platform = 'codeforces', contest_id = '2146', problem_id = 'b' } local fake_state_data = { platform = 'codeforces', contest_id = '2146', problem_id = 'b' }
assert.has_errors(function() assert.has_errors(function()
run.load_test_cases(ctx, fake_state_data) -- This should fail because no get_* methods run.load_test_cases(ctx, fake_state_data)
end) end)
end) end)
end) end)

View file

@ -141,20 +141,22 @@ describe('cp.picker', function()
it('falls back to scraping when cache miss', function() it('falls back to scraping when cache miss', function()
local cache = require('cp.cache') local cache = require('cp.cache')
local scrape = require('cp.scrape')
cache.load = function() end cache.load = function() end
cache.get_contest_data = function(_, _) cache.get_contest_data = function(_, _)
return nil return nil
end end
scrape.scrape_contest_metadata = function(_, _)
return { package.loaded['cp.scrape'] = {
success = true, scrape_contest_metadata = function(_, _)
problems = { return {
{ id = 'x', name = 'Problem X' }, success = true,
}, problems = {
} { id = 'x', name = 'Problem X' },
end },
}
end,
}
local problems = picker.get_problems_for_contest('test_platform', 'test_contest') local problems = picker.get_problems_for_contest('test_platform', 'test_contest')
assert.is_table(problems) assert.is_table(problems)