Files
cat-haven/test/e2e/edge_cases_spec.lua
T
2026-07-23 21:23:01 +00:00

266 lines
7.3 KiB
Lua

local test_helper = require('test.test_helper')
local mocks = test_helper.mocks
local utils = test_helper.utils
describe('Edge Cases', function()
local core, cat, house, ui, voxel, json
setup(function()
mocks.setupFilesystem()
core = require('src.core')
cat = require('src.cat.init')
house = require('src.house.init')
ui = require('src.ui.init')
voxel = require('src.voxel.init')
json = require('src.json')
end)
it('should handle zero cats', function()
core.state.cats = {}
core.calculateHouseHappiness()
assert.are.equal(0, core.state.house.totalHappiness)
end)
it('should handle zero distance between cats', function()
local c1 = utils.createMockCat({ position = { x = 0, y = 0, z = 0 } })
local c2 = utils.createMockCat({ position = { x = 0, y = 0, z = 0 } })
c1:updateNeeds()
c2:updateNeeds()
end)
it('should handle nil personality', function()
local c = cat.createCat(1, 'Test', nil)
assert.are.equal('social', c.personality.id)
end)
it('should handle invalid JSON', function()
local decoded, err = json.decode('{ invalid json }')
assert.are_equal(nil, decoded)
assert.are_not_equal(nil, err)
end)
it('should handle max capacity', function()
core.state.cats = {}
for i = 1, 8 do
core.spawnCat()
end
assert.are.equal(8, #core.state.cats)
-- Should not exceed max
local initialCount = #core.state.cats
core.spawnCat()
assert.are.equal(initialCount, #core.state.cats)
end)
it('should handle furniture at grid bounds', function()
local canPlace, reason = house.canPlaceFurniture({ x = 2.25, y = 0, z = 2.25 }, { 0.5, 1.5, 0.5 })
assert.is_true(canPlace)
end)
it('should handle low happiness departure', function()
local catObj = utils.createMockCat({
needs = { comfort = 0.05, hunger = 0.5 }
})
table.insert(core.state.cats, catObj)
local initialCount = #core.state.cats
core.checkCatDepartures()
assert.is_true(#core.state.cats < initialCount)
end)
it('should handle furniture upgrades', function()
local furn = utils.createMockFurniture()
furn:upgrade()
assert.are.equal(2, furn.upgradeLevel)
furn:upgrade()
assert.are.equal(3, furn.upgradeLevel)
local result = furn:upgrade()
assert.is_false(result)
end)
it('should handle personality compatibility', function()
local c1 = utils.createMockCat({
personality = { id = 'lone_wolf', compatibility = { ['social'] = -0.5 } }
})
local c2 = utils.createMockCat({
personality = { id = 'social' },
position = { x = 0.5, y = 0, z = 0 }
})
local oldSocial = c1.needs.social
c1:updateNeeds()
assert.is_true(c1.needs.social < oldSocial)
end)
it('should handle audio generation', function()
local purr = audio.createPurrSound()
assert.are_not_equal(nil, purr)
local meow = audio.createMeowSound()
assert.are_not_equal(nil, meow)
local click = audio.createClickSound()
assert.are_not_equal(nil, click)
local ambient = audio.createAmbientSound()
assert.are_not_equal(nil, ambient)
end)
it('should handle serialization edge cases', function()
local c = utils.createMockCat()
c.personality = nil
local serialized = c:serialize()
assert.are_equal('social', serialized.personalityId)
end)
it('should handle rapid updates', function()
core.state.cats = {}
for i = 1, 8 do
table.insert(core.state.cats, utils.createMockCat())
end
for i = 1, 100 do
core.update(0.016)
end
assert.is_true(#core.state.cats > 0)
end)
it('should handle 25+ furniture items', function()
core.state.furniture = {}
for i = 1, 25 do
local furn = utils.createMockFurniture({
position = { x = i * 0.5, y = 0, z = 0 }
})
table.insert(core.state.furniture, furn)
end
assert.are.equal(25, #core.state.furniture)
end)
it('should handle multiple save/load cycles', function()
for i = 1, 5 do
core.state.cats = {}
core.state.furniture = {}
core.spawnCat()
core.addFurniture('bed', { x = 0, y = 0, z = 0 })
core.saveGame()
core.loadGame()
end
end)
it('should handle nil core in all modules', function()
_G.core = nil
cat.updateCats(0.1)
house.checkCollision({ 0, 0, 0 }, { 1, 1, 1 })
house.canPlaceFurniture({ 0, 0, 0 }, { 1, 1, 1 })
house.placeFurniture('bed', { 0, 0, 0 })
house.interactWithFurniture({ 0, 0, 0 })
ui.renderHUD(test_helper.utils.createMockPass())
ui.renderInventory(test_helper.utils.createMockPass(), 1280, 720, 0.72)
ui.renderCatLog(test_helper.utils.createMockPass(), 1280, 0.72)
ui.handleMousePress(100, 100, 1)
voxel.renderWorld(test_helper.utils.createMockPass())
voxel.renderFurniture(test_helper.utils.createMockPass())
voxel.renderCats(test_helper.utils.createMockPass())
voxel.raycast({ 0, 0, 0 }, { 0, 0, -1 }, 10)
audio.playPurr({ 0, 0, 0 })
audio.playMeow({ 0, 0, 0 })
audio.playClick()
end)
it('should handle nil house module', function()
_G.house = nil
core.addFurniture('bed', { 0, 0, 0 })
end)
it('should handle nil ui module', function()
_G.ui = nil
core.update(0.1)
end)
it('should handle nil voxel module', function()
_G.voxel = nil
core.update(0.1)
end)
it('should handle nil audio module', function()
_G.audio = nil
core.update(0.1)
end)
it('should handle nil cat module', function()
_G.cat = nil
core.update(0.1)
end)
it('should handle zero distance in movement', function()
local c = utils.createMockCat({
position = { x = 0, y = 0, z = 0 },
targetPosition = { x = 0, y = 0, z = 0 }
})
c.state = 'moving'
c:update(0.1)
end)
it('should handle nil furniture in raycast', function()
core.state.furniture = nil
local results = voxel.raycast({ 0, 0, 0 }, { 0, 0, -1 }, 10)
assert.are.equal('table', type(results))
end)
it('should handle nil cats in raycast', function()
core.state.cats = nil
local results = voxel.raycast({ 0, 0, 0 }, { 0, 0, -1 }, 10)
assert.are.equal('table', type(results))
end)
it('should handle nil core in JSON encoding', function()
local encoded = json.encode({ core = core })
assert.are_not_equal(nil, encoded:find('version'))
end)
it('should handle nil core in JSON decoding', function()
local decoded = json.decode(encoded)
assert.are_not_equal(nil, decoded)
end)
it('should handle empty core state', function()
core.state = {}
core.calculateHouseHappiness()
assert.are.equal(0, core.state.house.totalHappiness)
end)
it('should handle nil core.state', function()
core.state = nil
core.update(0.1)
end)
it('should handle nil core.CONFIG', function()
core.CONFIG = nil
core.update(0.1)
end)
it('should handle nil core.personalities', function()
core.personalities = nil
local c = cat.createCat(1, 'Test', nil)
assert.are_equal('social', c.personality.id)
end)
it('should handle nil core.furnitureTypes', function()
core.furnitureTypes = nil
local furn = house.placeFurniture('bed', { 0, 0, 0 })
assert.are_equal(nil, furn)
end)
end)