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

156 lines
4.9 KiB
Lua

local test_helper = require('test.test_helper')
local mocks = test_helper.mocks
local utils = test_helper.utils
describe('Cat Module', function()
local cat
setup(function()
cat = require('src.cat.init')
end)
it('should create cat with default personality', function()
local c = cat.createCat(1, 'Test Cat', nil)
assert.are.equal(1, c.id)
assert.are.equal('Test Cat', c.name)
assert.are.equal('social', c.personality.id)
end)
it('should create cat with custom personality', function()
local personality = { id = 'lone_wolf', color = { 0.5, 0.5, 0.6 } }
local c = cat.createCat(2, 'Lone Cat', personality)
assert.are.equal('lone_wolf', c.personality.id)
assert.are.equal(0.5, c.personality.color[1])
end)
it('should update cat needs over time', function()
local c = cat.createCat(1, 'Test Cat', nil)
local oldComfort = c.needs.comfort
c:update(1.0)
assert.is_true(c.needs.comfort < oldComfort)
end)
it('should transition to sleep state', function()
local c = cat.createCat(1, 'Test Cat', nil)
c.needs.comfort = 0.1
c:update(1.0)
assert.is_true(c.isSleeping)
assert.are.equal('sleep', c.state)
end)
it('should wake up after sleep timer', function()
local c = cat.createCat(1, 'Test Cat', nil)
c.needs.comfort = 0.1
c.isSleeping = true
c.sleepTimer = 0.5
c:update(1.0)
assert.is_false(c.isSleeping)
assert.are.equal('idle', c.state)
end)
it('should move toward target position', function()
local c = cat.createCat(1, 'Test Cat', nil)
c.targetPosition = { x = 1, y = 0, z = 1 }
local oldX = c.position.x
local oldZ = c.position.z
c.state = 'moving'
c:update(0.1)
assert.is_true(c.position.x ~= oldX or c.position.z ~= oldZ)
end)
it('should handle zero distance movement', function()
local c = cat.createCat(1, 'Test Cat', nil)
c.targetPosition = { x = 0, y = 0, z = 0 }
c.position = { x = 0, y = 0, z = 0 }
c.state = 'moving'
c:update(0.1)
assert.is_true(true) -- Should not crash
end)
it('should seek furniture based on needs', function()
mocks.setupFilesystem()
require('src.core')
local c = cat.createCat(1, 'Test Cat', nil)
c.needs.comfort = 0.1
c:aiBehavior()
-- Should set targetPosition if furniture found
end)
it('should check personality compatibility', function()
mocks.setupFilesystem()
require('src.core')
local c1 = cat.createCat(1, 'Test Cat 1', { id = 'social', compatibility = { ['social'] = 0.5 } })
local c2 = cat.createCat(2, 'Test Cat 2', { id = 'social', compatibility = { ['social'] = 0.5 } })
c1.position = { x = 0, y = 0, z = 0 }
c2.position = { x = 0.5, y = 0, z = 0 }
c1:updateNeeds()
assert.is_true(c1.needs.social >= 0.7)
end)
it('should handle incompatible cats', function()
mocks.setupFilesystem()
require('src.core')
local c1 = cat.createCat(1, 'Test Cat 1', { id = 'lone_wolf', compatibility = { ['social'] = -0.5 } })
local c2 = cat.createCat(2, 'Test Cat 2', { id = 'social' })
c1.position = { x = 0, y = 0, z = 0 }
c2.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 pet cat and increase mood', function()
local c = cat.createCat(1, 'Test Cat', nil)
local oldMood = c.mood
c:pet()
assert.is_true(c.mood > oldMood)
end)
it('should serialize cat', function()
local c = cat.createCat(1, 'Test Cat', nil)
local serialized = c:serialize()
assert.are.equal(1, serialized.id)
assert.are.equal('Test Cat', serialized.name)
assert.are.equal('social', serialized.personalityId)
end)
it('should deserialize cat', function()
mocks.setupFilesystem()
require('src.core')
local original = cat.createCat(1, 'Test Cat', nil)
local serialized = original:serialize()
local deserialized = cat.deserialize(serialized)
assert.are.equal(original.id, deserialized.id)
assert.are.equal(original.name, deserialized.name)
end)
it('should handle nil personality in deserialize', function()
local data = {
id = 1,
name = 'Test',
personalityId = 'invalid',
position = { x = 0, y = 0, z = 0 }
}
local c = cat.deserialize(data)
assert.are_not.equal(nil, c)
assert.are.equal('social', c.personality.id)
end)
it('should get cat animation', function()
local c = cat.createCat(1, 'Test Cat', nil)
local anim = cat.getCatAnimation(c, 0)
assert.are.equal('table', type(anim))
assert.are.equal('number', type(anim.bounce))
end)
it('should handle nil cat in animation', function()
local anim = cat.getCatAnimation(nil, 0)
assert.are.equal(0, anim.bounce)
end)
it('should handle nil input in updateCats', function()
cat.updateCats(0.1)
assert.is_true(true) -- Should not crash
end)
end)