import {
checkValidConfidenceLimit ,
checkValidCurrentHitPoints ,
checkValidHitPoints ,
checkValidHitPointsWithoutConfidence ,
checkValidMaxHitPoints ,
confidenceLimit ,
damageConfidence ,
damageHealth ,
HitPoints ,
HitPointsWithoutConfidence ,
hitPointsWithoutConfidenceToString ,
hitPointsToString , isValidConfidenceLimit ,
isValidCurrentHitPoints ,
isValidHitPoints ,
isValidHitPointsWithoutConfidence ,
isValidMaxHitPoints ,
recoverConfidence ,
recoverHealth , setCurrentConfidence ,
setCurrentHealth ,
setMaxConfidence ,
setMaxHealth , checkConsistentConfidenceDelta
} from "./HitPoints" ;
import { assertion , AssertionConfig } from "./Assertions" ;
const InvalidHitPoints = { currentHealth : 999 , maxHealth : 99 , maxConfidence : 100 , currentConfidence : 20 } ;
let originalConfig : AssertionConfig.Type
beforeEach ( ( ) = > {
originalConfig = assertion . config
assertion . config = AssertionConfig . Throw
} ) ;
afterEach ( ( ) = > {
assertion . config = originalConfig
} ) ;
describe ( "hitPointsWithoutConfidenceToString" , ( ) = > {
const pairs : [ HitPointsWithoutConfidence , string ] [ ] = [
[ { currentHealth : 99 , maxHealth : 100 , maxConfidence : 200 } , "{Health: 99/100, Confidence: -/-/200}" ] ,
[ { currentHealth : - 25 , maxHealth : 0 , maxConfidence : - 200 } , "{Health: -25/0, Confidence: -/-/-200}" ] ,
]
pairs . forEach ( ( [ hp , expected ] ) = > {
test ( ` for ${ JSON . stringify ( hp ) } = " ${ expected } " ` , ( ) = > {
expect ( hitPointsWithoutConfidenceToString ( hp ) ) . toStrictEqual ( expected )
} )
} )
} ) ;
describe ( "hitPointsToString" , ( ) = > {
const pairs : [ HitPoints , string ] [ ] = [
[ { currentHealth : 99 , maxHealth : 100 , maxConfidence : 200 , currentConfidence : 10 } , "{Health: 99/100, Confidence: 10/198/200}" ] ,
[ { currentHealth : - 25 , maxHealth : 0 , maxConfidence : - 200 , currentConfidence : - 15 } , "{Health: -25/0, Confidence: -15/<err>/-200}" ] ,
]
pairs . forEach ( ( [ hp , expected ] ) = > {
test ( ` for ${ JSON . stringify ( hp ) } = " ${ expected } " ` , ( ) = > {
expect ( hitPointsToString ( hp ) ) . toStrictEqual ( expected )
} )
} )
} ) ;
describe ( "isValidMaxHitPoints" , ( ) = > {
const validValues = [ 1 , 2 , 99999 ]
const invalidValues = [ 0 , - 1 , 1.5 , NaN , Infinity , - Infinity , 100000 , 999999 ]
validValues . forEach ( ( value ) = > {
test ( ` returns true for ${ value } ` , ( ) = > {
expect ( isValidMaxHitPoints ( value ) ) . toBe ( true )
} )
} )
invalidValues . forEach ( ( value ) = > {
test ( ` returns false for ${ value } ` , ( ) = > {
expect ( isValidMaxHitPoints ( value ) ) . toBe ( false )
} )
} )
} ) ;
describe ( "isValidCurrentHitPoints" , ( ) = > {
const validValues : [ number , number , number ] [ ] = [ [ 1 , 1 , 0 ] , [ - 1 , 1 , - 1 ] , [ 0 , 1 , 0 ] , [ 0 , 1 , - 1 ] , [ 50 , 99999 , 0 ] ]
const invalidValues : [ number , number , number ] [ ] = [ [ 2 , 1 , 0 ] , [ - 1 , 1 , 0 ] , [ 0.5 , 1 , 0 ] , [ - 2 , 1 , - 1 ] ]
validValues . forEach ( ( [ current , max , min ] ) = > {
test ( ` returns true for ${ current } between ${ min } and ${ max } ` , ( ) = > {
expect ( isValidCurrentHitPoints ( current , max , min ) ) . toBe ( true )
} )
} )
invalidValues . forEach ( ( [ current , max , min ] ) = > {
test ( ` returns false for ${ current } between ${ min } and ${ max } ` , ( ) = > {
expect ( isValidCurrentHitPoints ( current , max , min ) ) . toBe ( false )
} )
} )
} ) ;
describe ( "isValidHitPointsWithoutConfidence" , ( ) = > {
const validValues : HitPointsWithoutConfidence [ ] = [ {
currentHealth : 100 ,
maxHealth : 100 ,
maxConfidence : 100 ,
} , {
currentHealth : 50 ,
maxHealth : 100 ,
maxConfidence : 25
} , {
currentHealth : 0 ,
maxHealth : 1 ,
maxConfidence : 1 ,
} ]
const invalidValues : HitPointsWithoutConfidence [ ] = [ {
currentHealth : 0 ,
maxHealth : 0 ,
maxConfidence : 99 ,
} , {
currentHealth : 0 ,
maxHealth : 20 ,
maxConfidence : 0 ,
} , {
currentHealth : - 300 ,
maxHealth : 100 ,
maxConfidence : 99 ,
} ]
validValues . forEach ( ( value ) = > {
test ( ` returns true for ${ hitPointsWithoutConfidenceToString ( value ) } ` , ( ) = > {
expect ( isValidHitPointsWithoutConfidence ( value ) ) . toBe ( true )
} )
} )
invalidValues . forEach ( ( value ) = > {
test ( ` returns false for ${ hitPointsWithoutConfidenceToString ( value ) } ` , ( ) = > {
expect ( isValidHitPointsWithoutConfidence ( value ) ) . toBe ( false )
} )
} )
} )
describe ( "isValidHitPoints" , ( ) = > {
const validValues : HitPoints [ ] = [ {
currentHealth : 100 ,
maxHealth : 100 ,
currentConfidence : 100 ,
maxConfidence : 100 ,
} , {
currentHealth : 50 ,
maxHealth : 100 ,
currentConfidence : 30 ,
maxConfidence : 60 ,
} , {
currentHealth : 0 ,
maxHealth : 100 ,
currentConfidence : 0 ,
maxConfidence : 30 ,
} ]
const invalidValues : HitPoints [ ] = [ {
currentHealth : 11 ,
maxHealth : 10 ,
currentConfidence : 99 ,
maxConfidence : 99 ,
} , {
currentHealth : 10 ,
maxHealth : 20 ,
currentConfidence : 20 ,
maxConfidence : 20 ,
} , {
currentHealth : 20 ,
maxHealth : 20 ,
currentConfidence : 25 ,
maxConfidence : 20 ,
} ]
validValues . forEach ( ( value ) = > {
test ( ` returns true for ${ hitPointsToString ( value ) } ` , ( ) = > {
expect ( isValidHitPoints ( value ) ) . toBe ( true )
} )
} )
invalidValues . forEach ( ( value ) = > {
test ( ` returns false for ${ hitPointsToString ( value ) } ` , ( ) = > {
expect ( isValidHitPoints ( value ) ) . toBe ( false )
} )
} )
} )
describe ( "isValidConfidenceLimit" , ( ) = > {
const validValues : [ number , HitPointsWithoutConfidence ] [ ] = [
[ 200 , {
currentHealth : 100 ,
maxHealth : 100 ,
maxConfidence : 200 ,
} ] , [ 0 , {
currentHealth : 50 ,
maxHealth : 100 ,
maxConfidence : 60
} ] , [ 10 , {
currentHealth : 50 ,
maxHealth : 100 ,
maxConfidence : 60
} ] , [ 0 , {
currentHealth : 0 ,
maxHealth : 100 ,
maxConfidence : 100 ,
} ] ]
const invalidValues : [ number , HitPointsWithoutConfidence ] [ ] = [
[ 195 , {
currentHealth : 100 ,
maxHealth : 100 ,
maxConfidence : 200 ,
} ] , [ 25 , {
currentHealth : 0 ,
maxHealth : 100 ,
maxConfidence : 100 ,
} ] ]
validValues . forEach ( ( [ value , hp ] ) = > {
test ( ` returns true for ${ value } and ${ hitPointsWithoutConfidenceToString ( hp ) } ` , ( ) = > {
expect ( isValidConfidenceLimit ( value , hp ) ) . toBe ( true )
} )
} )
invalidValues . forEach ( ( [ value , hp ] ) = > {
test ( ` returns false for ${ value } and ${ hitPointsWithoutConfidenceToString ( hp ) } ` , ( ) = > {
expect ( isValidConfidenceLimit ( value , hp ) ) . toBe ( false )
} )
} )
} )
describe ( "checkValidMaxHitPoints" , ( ) = > {
test ( "returns on valid max HP" , ( ) = > {
expect ( checkValidMaxHitPoints ( 30 ) ) . toEqual ( 30 )
} )
test ( "throws on invalid max HP" , ( ) = > {
expect ( ( ) = > checkValidMaxHitPoints ( - 1 ) ) . toThrow ( )
} )
} )
describe ( "checkValidCurrentHitPoints" , ( ) = > {
test ( "returns on valid current HP" , ( ) = > {
expect ( checkValidCurrentHitPoints ( 30 , 30 , 0 ) ) . toEqual ( 30 )
} )
test ( "throws on invalid current HP" , ( ) = > {
expect ( ( ) = > checkValidCurrentHitPoints ( - 10 , 30 , 0 ) ) . toThrow ( )
} )
} )
describe ( "checkValidHitPointsWithoutConfidence" , ( ) = > {
test ( "returns on valid hit points base" , ( ) = > {
expect ( checkValidHitPointsWithoutConfidence ( { currentHealth : 30 , maxHealth : 40 , maxConfidence : 99 } ) ) . toStrictEqual ( { currentHealth : 30 , maxHealth : 40 , maxConfidence : 99 } )
} )
test ( "throws on invalid hit points base" , ( ) = > {
expect ( ( ) = > checkValidHitPointsWithoutConfidence ( { currentHealth : - 20 , maxHealth : 10 , maxConfidence : 30 } ) ) . toThrow ( )
} )
} )
describe ( "checkValidHitPoints" , ( ) = > {
test ( "returns on valid hit points" , ( ) = > {
expect ( checkValidHitPoints ( { currentHealth : 30 , maxHealth : 40 , maxConfidence : 99 , currentConfidence : 30 } ) ) . toStrictEqual ( { currentHealth : 30 , maxHealth : 40 , maxConfidence : 99 , currentConfidence : 30 } )
} )
test ( "throws on invalid hit points" , ( ) = > {
expect ( ( ) = > checkValidHitPoints ( { currentHealth : - 20 , maxHealth : 10 , maxConfidence : 30 , currentConfidence : 50 } ) ) . toThrow ( )
} )
} )
describe ( "checkValidConfidenceLimit" , ( ) = > {
test ( "returns on valid confidence limit" , ( ) = > {
expect ( checkValidConfidenceLimit ( 30 , { currentHealth : 15 , maxHealth : 20 , maxConfidence : 40 } ) ) . toEqual ( 30 )
} )
test ( "throws on invalid confidence limit" , ( ) = > {
expect ( ( ) = > checkValidConfidenceLimit ( 40 , { currentHealth : 15 , maxHealth : 20 , maxConfidence : 40 } ) ) . toThrow ( )
} )
} )
describe ( "checkConsistentConfidenceDelta" , ( ) = > {
const consistentValues : [ HitPoints , number , number ] [ ] = [
[ { currentHealth : 30 , maxHealth : 30 , currentConfidence : 10 , maxConfidence : 20 } , - 10 , 0 ] ,
[ { currentHealth : 30 , maxHealth : 30 , currentConfidence : 20 , maxConfidence : 20 } , 0 , 0 ] ,
[ { currentHealth : 30 , maxHealth : 30 , currentConfidence : 20 , maxConfidence : 20 } , 10 , 15 ] ,
[ { currentHealth : 7 , maxHealth : 10 , currentConfidence : 17 , maxConfidence : 25 } , 10 , 4 ] ,
[ { currentHealth : 7 , maxHealth : 10 , currentConfidence : 17 , maxConfidence : 25 } , 9 , 4 ] ,
[ { currentHealth : 7 , maxHealth : 10 , currentConfidence : 17 , maxConfidence : 25 } , 25 , 10 ] ,
[ { currentHealth : 7 , maxHealth : 10 , currentConfidence : 17 , maxConfidence : 25 } , 24 , 10 ] ,
]
consistentValues . forEach ( ( [ hp , confidence , health ] ) = > {
const newHealth = { . . . hp , currentConfidence : confidence , currentHealth : health }
test ( ` returns for ${ hitPointsToString ( hp ) } -> ${ hitPointsToString ( newHealth ) } ` , ( ) = > {
expect ( checkConsistentConfidenceDelta ( confidence , "testing" , hp , health ) ) . toEqual ( confidence )
} )
} )
const inconsistentValues : [ HitPoints , number , number ] [ ] = [
[ { currentHealth : 30 , maxHealth : 30 , currentConfidence : 10 , maxConfidence : 20 } , 0 , 0 ] ,
[ { currentHealth : 30 , maxHealth : 30 , currentConfidence : 20 , maxConfidence : 20 } , 20 , 0 ] ,
[ { currentHealth : 30 , maxHealth : 30 , currentConfidence : 20 , maxConfidence : 20 } , 5 , 15 ] ,
[ { currentHealth : 7 , maxHealth : 10 , currentConfidence : 10 , maxConfidence : 25 } , 8 , 4 ] ,
[ { currentHealth : 7 , maxHealth : 10 , currentConfidence : 17 , maxConfidence : 25 } , 23 , 10 ] ,
]
inconsistentValues . forEach ( ( [ hp , confidence , health ] ) = > {
const newHealth = { . . . hp , currentConfidence : confidence , currentHealth : health }
test ( ` throws for ${ hitPointsToString ( hp ) } -> ${ hitPointsToString ( newHealth ) } ` , ( ) = > {
expect ( ( ) = > checkConsistentConfidenceDelta ( confidence , "testing" , hp , health ) ) . toThrow ( )
} )
} )
test ( "throws an error starting with the evaluation of the action function" , ( ) = > {
expect ( ( ) = > checkConsistentConfidenceDelta (
30 , "frozzing the snagler" ,
{ maxHealth : 10 , currentHealth : 0 , maxConfidence : 30 , currentConfidence : 0 } ,
9
) ) . toThrow ( /^frozzing the snagler / )
} )
} )
describe ( "confidenceLimit" , ( ) = > {
test ( "throws on invalid input" , ( ) = > {
expect ( ( ) = > confidenceLimit ( { currentHealth : - 99 , maxHealth : 30 , maxConfidence : 20 } ) ) . toThrow ( )
} )
const values : [ HitPointsWithoutConfidence , number ] [ ] = [
[ { maxConfidence : 99 , maxHealth : 30 , currentHealth : 30 } , 99 ] ,
// Even 1 missing health results in a loss of at least 1 confidence.
[ { maxConfidence : 10 , maxHealth : 99999 , currentHealth : 99998 } , 9 ] ,
// Confidence is 0 when each point of confidence represents multiple points of health and there aren't enough to make 1 point of confidence
[ { maxConfidence : 10 , maxHealth : 50 , currentHealth : 4 } , 0 ] ,
// Confidence is 0 when health is 0
[ { maxConfidence : 99999 , maxHealth : 99 , currentHealth : 0 } , 0 ] ,
// or when it's below 0
[ { maxConfidence : 99999 , maxHealth : 99 , currentHealth : - 50 } , 0 ] ,
]
values . forEach ( ( [ hp , expected ] ) = > {
test ( ` returns ${ expected } for ${ hitPointsWithoutConfidenceToString ( hp ) } ` , ( ) = > {
expect ( confidenceLimit ( hp ) ) . toBe ( expected )
} )
} )
} )
describe ( "damageHealth" , ( ) = > {
const invalidValues : [ HitPoints , number ] [ ] = [
[ InvalidHitPoints , 10 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 10 } , - 1 ] ,
]
invalidValues . forEach ( ( [ hp , damage ] ) = > {
test ( ` for ${ damage } on ${ hitPointsToString ( hp ) } throws ` , ( ) = > {
expect ( ( ) = > damageHealth ( hp , damage , { damageConfidence : false } ) ) . toThrow ( )
} )
} )
const values : [ [ HitPoints , number , { damageConfidence : boolean } ? ] , Partial < HitPoints > ] [ ] = [
[ [ { currentHealth : - 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 0 ] , { } ] ,
[ [ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 0 ] , { } ] ,
[ [ { currentHealth : - 5 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 5 ] , { currentHealth : - 10 } ] ,
[ [ { currentHealth : - 5 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 ] , { currentHealth : - 10 } ] ,
[ [ { currentHealth : 0 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 ] , { currentHealth : - 10 } ] ,
[ [ { currentHealth : 0 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 15 ] , { currentHealth : - 10 } ] ,
[ [ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 15 ] , { currentHealth : - 5 } ] ,
[ [ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 18 } , 5 , { damageConfidence : true } ] , { currentHealth : 5 , currentConfidence : 8 } ] ,
[ [ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 18 } , 5 , { damageConfidence : false } ] , { currentHealth : 5 , currentConfidence : 10 } ] ,
[ [ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 18 } , 15 , { damageConfidence : true } ] , { currentHealth : - 5 , currentConfidence : 0 } ] ,
[ [ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 18 } , 15 , { damageConfidence : false } ] , { currentHealth : - 5 , currentConfidence : 0 } ] ,
[ [ { currentHealth : 100 , maxHealth : 100 , maxConfidence : 20 , currentConfidence : 18 } , 7 , { damageConfidence : true } ] , { currentHealth : 93 , currentConfidence : 16 } ] ,
[ [ { currentHealth : 100 , maxHealth : 100 , maxConfidence : 20 , currentConfidence : 18 } , 7 , { damageConfidence : false } ] , { currentHealth : 93 , currentConfidence : 18 } ] ,
[ [ { currentHealth : 93 , maxHealth : 100 , maxConfidence : 20 , currentConfidence : 18 } , 2 , { damageConfidence : true } ] , { currentHealth : 91 , currentConfidence : 17 } ] ,
[ [ { currentHealth : 93 , maxHealth : 100 , maxConfidence : 20 , currentConfidence : 18 } , 2 , { damageConfidence : false } ] , { currentHealth : 91 , currentConfidence : 18 } ] ,
]
values . forEach ( ( [ [ hp , damage , options = { damageConfidence : true } ] , expectedChanges ] ) = > {
const expected = { . . . hp , . . . expectedChanges }
test ( ` for ${ damage } on ${ hitPointsToString ( hp ) } with damageConfidence ${ options . damageConfidence ? "on" : "off" } results in ${ hitPointsToString ( expected ) } ` , ( ) = > {
expect ( damageHealth ( hp , damage , options ) ) . toStrictEqual ( expected )
} )
} )
} )
describe ( "recoverHealth" , ( ) = > {
const invalidValues : [ HitPoints , number ] [ ] = [
[ InvalidHitPoints , 10 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 10 } , - 1 ] ,
]
invalidValues . forEach ( ( [ hp , healing ] ) = > {
test ( ` for ${ healing } on ${ hitPointsToString ( hp ) } throws ` , ( ) = > {
expect ( ( ) = > recoverHealth ( hp , healing , { recoverConfidence : false } ) ) . toThrow ( )
} )
} )
const values : [ [ HitPoints , number , { recoverConfidence : boolean } ? ] , Partial < HitPoints > ] [ ] = [
[ [ { currentHealth : - 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 0 ] , { } ] ,
[ [ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 0 ] , { } ] ,
[ [ { currentHealth : - 5 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 5 ] , { currentHealth : 0 } ] ,
[ [ { currentHealth : - 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 ] , { currentHealth : 0 } ] ,
[ [ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 ] , { } ] ,
[ [ { currentHealth : 7 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 8 } , 2 , { recoverConfidence : true } ] , { currentHealth : 9 , currentConfidence : 12 } ] ,
[ [ { currentHealth : 7 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 8 } , 2 , { recoverConfidence : false } ] , { currentHealth : 9 , currentConfidence : 8 } ] ,
[ [ { currentHealth : 7 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 8 } , 5 , { recoverConfidence : true } ] , { currentHealth : 10 , currentConfidence : 14 } ] ,
[ [ { currentHealth : 7 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 8 } , 5 , { recoverConfidence : false } ] , { currentHealth : 10 , currentConfidence : 8 } ] ,
[ [ { currentHealth : - 10 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 0 } , 15 , { recoverConfidence : true } ] , { currentHealth : 5 , currentConfidence : 10 } ] ,
[ [ { currentHealth : - 10 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 0 } , 15 , { recoverConfidence : false } ] , { currentHealth : 5 , currentConfidence : 0 } ] ,
[ [ { currentHealth : 0 , maxHealth : 10 , maxConfidence : 25 , currentConfidence : 0 } , 3 , { recoverConfidence : true } ] , { currentHealth : 3 , currentConfidence : 7 } ] ,
[ [ { currentHealth : 3 , maxHealth : 10 , maxConfidence : 25 , currentConfidence : 7 } , 3 , { recoverConfidence : true } ] , { currentHealth : 6 , currentConfidence : 14 } ] ,
[ [ { currentHealth : 0 , maxHealth : 10 , maxConfidence : 25 , currentConfidence : 0 } , 3 , { recoverConfidence : false } ] , { currentHealth : 3 , currentConfidence : 0 } ] ,
]
values . forEach ( ( [ [ hp , damage , options = { recoverConfidence : true } ] , expectedChanges ] ) = > {
const expected = { . . . hp , . . . expectedChanges }
test ( ` for ${ damage } on ${ hitPointsToString ( hp ) } with recoverConfidence ${ options . recoverConfidence ? "on" : "off" } results in ${ hitPointsToString ( expected ) } ` , ( ) = > {
expect ( recoverHealth ( hp , damage , options ) ) . toStrictEqual ( expected )
} )
} )
} )
describe ( "setMaxHealth" , ( ) = > {
const invalidValues : [ HitPoints , number ] [ ] = [
[ InvalidHitPoints , 10 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 0 ] ,
]
invalidValues . forEach ( ( [ hp , newMax ] ) = > {
test ( ` to ${ newMax } on ${ hitPointsToString ( hp ) } throws ` , ( ) = > {
expect ( ( ) = > setMaxHealth ( hp , newMax ) ) . toThrow ( )
} )
} )
const values : [ HitPoints , number , Partial < HitPoints > ] [ ] = [
[ { currentHealth : 8 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 10 , { } ] ,
[ { currentHealth : 8 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 5 , { maxHealth : 5 , currentHealth : 5 } ] ,
[ { currentHealth : 8 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 4 } , 20 , { maxHealth : 20 } ] ,
[ { currentHealth : 8 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 4 } , 19 , { maxHealth : 19 } ] ,
[ { currentHealth : 8 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 4 } , 21 , { maxHealth : 21 , currentConfidence : 3 } ] ,
]
values . forEach ( ( [ hp , newMax , expectedChanges ] ) = > {
const expected = { . . . hp , . . . expectedChanges }
test ( ` to ${ newMax } on ${ hitPointsToString ( hp ) } results in ${ hitPointsToString ( expected ) } ` , ( ) = > {
expect ( setMaxHealth ( hp , newMax ) ) . toStrictEqual ( expected )
} )
} )
} )
describe ( "setCurrentHealth" , ( ) = > {
const invalidValues : [ HitPoints , number ] [ ] = [
[ InvalidHitPoints , 10 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , - 11 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 11 ] ,
]
invalidValues . forEach ( ( [ hp , newValue ] ) = > {
test ( ` to ${ newValue } on ${ hitPointsToString ( hp ) } throws ` , ( ) = > {
expect ( ( ) = > setCurrentHealth ( hp , newValue ) ) . toThrow ( )
} )
} )
const values : [ HitPoints , number , Partial < HitPoints > ] [ ] = [
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 0 , { currentHealth : 0 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 5 , { currentHealth : 5 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 , { currentHealth : 10 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , - 5 , { currentHealth : - 5 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , - 10 , { currentHealth : - 10 } ] ,
[ { currentHealth : 4 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 0 , { currentHealth : 0 , currentConfidence : 0 } ] ,
[ { currentHealth : 4 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 8 , { currentHealth : 8 } ] ,
]
values . forEach ( ( [ hp , newValue , expectedChanges ] ) = > {
const expected = { . . . hp , . . . expectedChanges }
test ( ` to ${ newValue } on ${ hitPointsToString ( hp ) } results in ${ hitPointsToString ( expected ) } ` , ( ) = > {
expect ( setCurrentHealth ( hp , newValue ) ) . toStrictEqual ( expected )
} )
} )
} )
describe ( "damageConfidence" , ( ) = > {
const invalidValues : [ HitPoints , number ] [ ] = [
[ InvalidHitPoints , 10 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , - 1 ] ,
]
invalidValues . forEach ( ( [ hp , damage ] ) = > {
test ( ` for ${ damage } on ${ hitPointsToString ( hp ) } throws ` , ( ) = > {
expect ( ( ) = > damageConfidence ( hp , damage ) ) . toThrow ( )
} )
} )
const values : [ HitPoints , number , Partial < HitPoints > ] [ ] = [
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 0 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 6 , { currentConfidence : 0 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 5 , { currentConfidence : 0 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 4 , { currentConfidence : 1 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 10 } , 9 , { currentConfidence : 1 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 10 } , 11 , { currentConfidence : 0 } ] ,
]
values . forEach ( ( [ hp , damage , expectedChanges ] ) = > {
const expected = { . . . hp , . . . expectedChanges }
test ( ` for ${ damage } on ${ hitPointsToString ( hp ) } results in ${ hitPointsToString ( expected ) } ` , ( ) = > {
expect ( damageConfidence ( hp , damage ) ) . toStrictEqual ( expected )
} )
} )
} )
describe ( "recoverConfidence" , ( ) = > {
const invalidValues : [ HitPoints , number ] [ ] = [
[ InvalidHitPoints , 10 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , - 1 ] ,
]
invalidValues . forEach ( ( [ hp , healing ] ) = > {
test ( ` for ${ healing } on ${ hitPointsToString ( hp ) } throws ` , ( ) = > {
expect ( ( ) = > recoverConfidence ( hp , healing ) ) . toThrow ( )
} )
} )
const values : [ HitPoints , number , Partial < HitPoints > ] [ ] = [
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 0 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 10 } , 10 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 6 , { currentConfidence : 10 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 5 , { currentConfidence : 10 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 4 , { currentConfidence : 9 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 9 , { currentConfidence : 9 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 11 , { currentConfidence : 10 } ] ,
[ { currentHealth : 5 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 6 , { currentConfidence : 10 } ] ,
[ { currentHealth : 5 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 5 , { currentConfidence : 10 } ] ,
[ { currentHealth : 5 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 4 , { currentConfidence : 9 } ] ,
]
values . forEach ( ( [ hp , healing , expectedChanges ] ) = > {
const expected = { . . . hp , . . . expectedChanges }
test ( ` for ${ healing } on ${ hitPointsToString ( hp ) } results in ${ hitPointsToString ( expected ) } ` , ( ) = > {
expect ( recoverConfidence ( hp , healing ) ) . toStrictEqual ( expected )
} )
} )
} )
describe ( "setMaxConfidence" , ( ) = > {
const invalidValues : [ HitPoints , number ] [ ] = [
[ InvalidHitPoints , 10 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 0 ] ,
]
invalidValues . forEach ( ( [ hp , newMax ] ) = > {
test ( ` to ${ newMax } on ${ hitPointsToString ( hp ) } throws ` , ( ) = > {
expect ( ( ) = > setMaxConfidence ( hp , newMax ) ) . toThrow ( )
} )
} )
const values : [ HitPoints , number , Partial < HitPoints > ] [ ] = [
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 10 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 10 } , 10 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 6 , { maxConfidence : 6 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 5 , { maxConfidence : 5 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 4 , { maxConfidence : 4 , currentConfidence : 4 } ] ,
[ { currentHealth : 4 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 30 , { maxConfidence : 30 } ] ,
[ { currentHealth : 4 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 5 , { maxConfidence : 5 , currentConfidence : 2 } ] ,
]
values . forEach ( ( [ hp , newMax , expectedChanges ] ) = > {
const expected = { . . . hp , . . . expectedChanges }
test ( ` to ${ newMax } on ${ hitPointsToString ( hp ) } results in ${ hitPointsToString ( expected ) } ` , ( ) = > {
expect ( setMaxConfidence ( hp , newMax ) ) . toStrictEqual ( expected )
} )
} )
} )
describe ( "setCurrentConfidence" , ( ) = > {
const invalidValues : [ HitPoints , number ] [ ] = [
[ InvalidHitPoints , 10 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , - 1 ] ,
[ { currentHealth : 5 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 6 ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 5 } , 11 ] ,
]
invalidValues . forEach ( ( [ hp , newValue ] ) = > {
test ( ` to ${ newValue } on ${ hitPointsToString ( hp ) } throws ` , ( ) = > {
expect ( ( ) = > setCurrentConfidence ( hp , newValue ) ) . toThrow ( )
} )
} )
const values : [ HitPoints , number , Partial < HitPoints > ] [ ] = [
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 0 , { } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 5 , { currentConfidence : 5 } ] ,
[ { currentHealth : 10 , maxHealth : 10 , maxConfidence : 10 , currentConfidence : 0 } , 10 , { currentConfidence : 10 } ] ,
[ { currentHealth : 4 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 0 , { currentConfidence : 0 } ] ,
[ { currentHealth : 4 , maxHealth : 10 , maxConfidence : 20 , currentConfidence : 5 } , 8 , { currentConfidence : 8 } ] ,
]
values . forEach ( ( [ hp , newValue , expectedChanges ] ) = > {
const expected = { . . . hp , . . . expectedChanges }
test ( ` to ${ newValue } on ${ hitPointsToString ( hp ) } results in ${ hitPointsToString ( expected ) } ` , ( ) = > {
expect ( setCurrentConfidence ( hp , newValue ) ) . toStrictEqual ( expected )
} )
} )
} )