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()
local spec_helper = require('spec.spec_helper')
local cp
local state
local logged_messages
before_each(function()
logged_messages = {}
local mock_logger = {
log = function(msg, level)
table.insert(logged_messages, { msg = msg, level = level })
end,
set_config = function() end,
}
package.loaded['cp.log'] = mock_logger
spec_helper.setup_full()
spec_helper.mock_scraper_success()
-- Reset state completely
state = require('cp.state')
state.reset()
@ -30,30 +23,26 @@ describe('Panel integration', function()
end)
after_each(function()
package.loaded['cp.log'] = nil
spec_helper.teardown()
if state then
state.reset()
end
end)
it('should handle run command with properly set contest context', function()
-- First set up a contest context
cp.handle_command({ fargs = { 'codeforces', '2146', 'b' } })
-- Verify state was set correctly
local context = cp.get_current_context()
assert.equals('codeforces', context.platform)
assert.equals('2146', context.contest_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()
cp.handle_command({ fargs = { 'run' } })
end)
-- Should log panel opened or no test cases found, but NOT a validation error
local has_validation_error = false
for _, log_entry in ipairs(logged_messages) do
for _, log_entry in ipairs(spec_helper.logged_messages) do
if
log_entry.level == vim.log.levels.ERROR
and log_entry.msg:match('expected string, got nil')
@ -65,29 +54,25 @@ describe('Panel integration', function()
assert.is_false(has_validation_error)
end)
it('should catch state module vs state object contract violations', function()
-- This test specifically verifies that runner functions receive the right data type
it('should handle state module interface correctly', function()
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_contest_id('2146')
state.set_problem_id('b')
-- Create a proper context
local ctx = problem.create_context('codeforces', '2146', 'b', config.defaults)
local problem = require('cp.problem')
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()
run.load_test_cases(ctx, state)
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' }
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)

View file

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