File size: 20,573 Bytes
1ecc382 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
/**
* Tests for Multi-Piclet Battle Engine
* Covers battles with up to 4 Piclets on the field at once
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { MultiBattleEngine } from './MultiBattleEngine';
import { MultiBattleConfig, TurnActions } from './multi-piclet-types';
import { PicletType, AttackType } from './types';
import {
STELLAR_WOLF,
TOXIC_CRAWLER,
BERSERKER_BEAST,
AQUA_GUARDIAN
} from './test-data';
describe('MultiBattleEngine', () => {
let config: MultiBattleConfig;
let engine: MultiBattleEngine;
beforeEach(() => {
config = {
playerParty: [STELLAR_WOLF, BERSERKER_BEAST],
opponentParty: [TOXIC_CRAWLER, AQUA_GUARDIAN],
playerActiveCount: 1,
opponentActiveCount: 1,
battleType: 'single'
};
engine = new MultiBattleEngine(config);
});
describe('Battle Initialization', () => {
it('should initialize single battle correctly', () => {
const state = engine.getState();
expect(state.turn).toBe(1);
expect(state.phase).toBe('selection');
expect(state.activePiclets.player).toHaveLength(2);
expect(state.activePiclets.opponent).toHaveLength(2);
// First position should be active, second should be null
expect(state.activePiclets.player[0]).not.toBeNull();
expect(state.activePiclets.player[1]).toBeNull();
expect(state.activePiclets.opponent[0]).not.toBeNull();
expect(state.activePiclets.opponent[1]).toBeNull();
});
it('should initialize double battle correctly', () => {
const doubleConfig: MultiBattleConfig = {
playerParty: [STELLAR_WOLF, BERSERKER_BEAST],
opponentParty: [TOXIC_CRAWLER, AQUA_GUARDIAN],
playerActiveCount: 2,
opponentActiveCount: 2,
battleType: 'double'
};
const doubleEngine = new MultiBattleEngine(doubleConfig);
const state = doubleEngine.getState();
// Both positions should be active
expect(state.activePiclets.player[0]).not.toBeNull();
expect(state.activePiclets.player[1]).not.toBeNull();
expect(state.activePiclets.opponent[0]).not.toBeNull();
expect(state.activePiclets.opponent[1]).not.toBeNull();
});
it('should handle parties correctly', () => {
const state = engine.getState();
expect(state.parties.player).toHaveLength(2);
expect(state.parties.opponent).toHaveLength(2);
expect(state.parties.player[0].name).toBe('Stellar Wolf');
expect(state.parties.opponent[0].name).toBe('Toxic Crawler');
});
});
describe('Action Generation', () => {
it('should generate valid move actions for active Piclets', () => {
const actions = engine.getValidActions('player');
// Should have move actions for the active Piclet
const moveActions = actions.filter(a => a.type === 'move');
expect(moveActions.length).toBeGreaterThan(0);
// All move actions should be for position 0 (the active Piclet)
moveActions.forEach(action => {
expect((action as any).position).toBe(0);
});
});
it('should generate switch actions for party members', () => {
const actions = engine.getValidActions('player');
const switchActions = actions.filter(a => a.type === 'switch');
expect(switchActions.length).toBeGreaterThan(0);
// Should be able to switch the inactive party member into position 0
const switchToPosition0 = switchActions.find(a =>
(a as any).position === 0 && (a as any).partyIndex === 1
);
expect(switchToPosition0).toBeDefined();
});
});
describe('Single Battle Execution', () => {
it('should execute a single battle turn correctly', () => {
const turnActions: TurnActions = {
player: [{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0 // Tackle
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0 // Tackle
}]
};
const initialOpponentHp = engine.getState().activePiclets.opponent[0]!.currentHp;
engine.executeTurn(turnActions);
const finalOpponentHp = engine.getState().activePiclets.opponent[0]!.currentHp;
expect(finalOpponentHp).toBeLessThan(initialOpponentHp);
const log = engine.getLog();
expect(log.some(msg => msg.includes('used Tackle'))).toBe(true);
});
it('should handle switch actions correctly', () => {
const turnActions: TurnActions = {
player: [{
type: 'switch',
side: 'player',
position: 0,
partyIndex: 1 // Switch to Berserker Beast
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
}]
};
const initialName = engine.getState().activePiclets.player[0]!.definition.name;
engine.executeTurn(turnActions);
const finalName = engine.getState().activePiclets.player[0]!.definition.name;
expect(initialName).toBe('Stellar Wolf');
expect(finalName).toBe('Berserker Beast');
const log = engine.getLog();
expect(log.some(msg => msg.includes('switched out'))).toBe(true);
expect(log.some(msg => msg.includes('switched in'))).toBe(true);
});
});
describe('Double Battle System', () => {
let doubleEngine: MultiBattleEngine;
beforeEach(() => {
const doubleConfig: MultiBattleConfig = {
playerParty: [STELLAR_WOLF, BERSERKER_BEAST],
opponentParty: [TOXIC_CRAWLER, AQUA_GUARDIAN],
playerActiveCount: 2,
opponentActiveCount: 2,
battleType: 'double'
};
doubleEngine = new MultiBattleEngine(doubleConfig);
});
it('should execute double battle turns correctly', () => {
const turnActions: TurnActions = {
player: [
{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0 // Stellar Wolf uses Tackle
},
{
type: 'move',
side: 'player',
position: 1,
moveIndex: 0 // Berserker Beast uses Tackle
}
],
opponent: [
{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0 // Toxic Crawler uses Tackle
},
{
type: 'move',
side: 'opponent',
position: 1,
moveIndex: 0 // Aqua Guardian uses Tackle
}
]
};
doubleEngine.executeTurn(turnActions);
const log = doubleEngine.getLog();
expect(log.some(msg => msg.includes('Stellar Wolf used'))).toBe(true);
expect(log.some(msg => msg.includes('Berserker Beast used'))).toBe(true);
expect(log.some(msg => msg.includes('Toxic Crawler used'))).toBe(true);
expect(log.some(msg => msg.includes('Aqua Guardian used'))).toBe(true);
});
it('should handle mixed actions in double battles', () => {
const turnActions: TurnActions = {
player: [
{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0 // Attack
},
{
type: 'switch',
side: 'player',
position: 1,
partyIndex: 0 // This would be switching to same Piclet, but tests the system
}
],
opponent: [
{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
},
{
type: 'move',
side: 'opponent',
position: 1,
moveIndex: 0
}
]
};
doubleEngine.executeTurn(turnActions);
const log = doubleEngine.getLog();
expect(log.some(msg => msg.includes('used'))).toBe(true);
});
});
describe('Action Priority System', () => {
it('should prioritize switch actions over moves', () => {
const doubleConfig: MultiBattleConfig = {
playerParty: [STELLAR_WOLF, BERSERKER_BEAST],
opponentParty: [TOXIC_CRAWLER, AQUA_GUARDIAN],
playerActiveCount: 2,
opponentActiveCount: 2,
battleType: 'double'
};
const doubleEngine = new MultiBattleEngine(doubleConfig);
const turnActions: TurnActions = {
player: [
{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0 // Regular move
}
],
opponent: [
{
type: 'switch',
side: 'opponent',
position: 0,
partyIndex: 1 // Switch action (should go first)
}
]
};
doubleEngine.executeTurn(turnActions);
const log = doubleEngine.getLog();
const switchIndex = log.findIndex(msg => msg.includes('switched'));
const moveIndex = log.findIndex(msg => msg.includes('used'));
// Switch should happen before move (if both occurred)
if (switchIndex !== -1 && moveIndex !== -1) {
expect(switchIndex).toBeLessThan(moveIndex);
}
});
it('should use speed for same priority actions', () => {
// Stellar Wolf (speed 70) vs Toxic Crawler (speed 55)
const turnActions: TurnActions = {
player: [{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0 // Tackle (priority 0)
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0 // Tackle (priority 0)
}]
};
engine.executeTurn(turnActions);
const log = engine.getLog();
const stellarIndex = log.findIndex(msg => msg.includes('Stellar Wolf used'));
const toxicIndex = log.findIndex(msg => msg.includes('Toxic Crawler used'));
// Stellar Wolf should go first due to higher speed
expect(stellarIndex).toBeLessThan(toxicIndex);
});
});
describe('Victory Conditions', () => {
it('should end battle when all opponent Piclets faint', () => {
// Create a battle with single-Piclet opponent party (no reserves)
const singleOpponentConfig: MultiBattleConfig = {
playerParty: [STELLAR_WOLF, BERSERKER_BEAST],
opponentParty: [TOXIC_CRAWLER], // Only one Piclet, no reserves
playerActiveCount: 1,
opponentActiveCount: 1,
battleType: 'single'
};
const singleEngine = new MultiBattleEngine(singleOpponentConfig);
// Set opponent to very low HP
(singleEngine as any).state.activePiclets.opponent[0]!.currentHp = 1;
const turnActions: TurnActions = {
player: [{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
}]
};
singleEngine.executeTurn(turnActions);
expect(singleEngine.isGameOver()).toBe(true);
expect(singleEngine.getWinner()).toBe('player');
});
it('should continue battle when reserves are available', () => {
// This test would require implementing automatic switching
// when a Piclet faints, which is more complex
expect(true).toBe(true); // Placeholder
});
});
describe('Targeting System', () => {
it('should target opponents correctly in single battles', () => {
const turnActions: TurnActions = {
player: [{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0 // Attack should hit opponent
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
}]
};
const initialHp = engine.getState().activePiclets.opponent[0]!.currentHp;
engine.executeTurn(turnActions);
const finalHp = engine.getState().activePiclets.opponent[0]!.currentHp;
expect(finalHp).toBeLessThan(initialHp);
});
it('should target all opponents in double battles', () => {
const doubleConfig: MultiBattleConfig = {
playerParty: [STELLAR_WOLF, BERSERKER_BEAST],
opponentParty: [TOXIC_CRAWLER, AQUA_GUARDIAN],
playerActiveCount: 2,
opponentActiveCount: 2,
battleType: 'double'
};
const doubleEngine = new MultiBattleEngine(doubleConfig);
// Create a multi-target move for testing
const multiTargetMove = {
name: 'Mass Strike',
type: 'normal' as any,
power: 30,
accuracy: 100,
pp: 10,
priority: 0,
flags: [] as any,
effects: [{
type: 'damage' as any,
target: 'allOpponents' as any,
amount: 'normal' as any
}]
};
// Add the move to the attacker
(doubleEngine as any).state.activePiclets.player[0].moves[0] = {
move: multiTargetMove,
currentPP: 10
};
const initialHp1 = doubleEngine.getState().activePiclets.opponent[0]!.currentHp;
const initialHp2 = doubleEngine.getState().activePiclets.opponent[1]!.currentHp;
const turnActions: TurnActions = {
player: [{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0 // Multi-target move
}],
opponent: [
{ type: 'move', side: 'opponent', position: 0, moveIndex: 0 },
{ type: 'move', side: 'opponent', position: 1, moveIndex: 0 }
]
};
doubleEngine.executeTurn(turnActions);
const finalHp1 = doubleEngine.getState().activePiclets.opponent[0]!.currentHp;
const finalHp2 = doubleEngine.getState().activePiclets.opponent[1]!.currentHp;
// Both opponents should take damage
expect(finalHp1).toBeLessThan(initialHp1);
expect(finalHp2).toBeLessThan(initialHp2);
});
it('should target self correctly', () => {
// Create a self-targeting move (like heal)
const selfTargetMove = {
name: 'Self Heal',
type: 'normal' as any,
power: 0,
accuracy: 100,
pp: 10,
priority: 0,
flags: [] as any,
effects: [{
type: 'heal' as any,
target: 'self' as any,
amount: 'medium' as any
}]
};
// Damage the Piclet first then heal
(engine as any).state.activePiclets.player[0].currentHp = 50;
// Add the heal move
(engine as any).state.activePiclets.player[0].moves[0] = {
move: selfTargetMove,
currentPP: 10
};
const initialHp = engine.getState().activePiclets.player[0]!.currentHp;
const turnActions: TurnActions = {
player: [{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0 // Self-heal move
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
}]
};
engine.executeTurn(turnActions);
const finalHp = engine.getState().activePiclets.player[0]!.currentHp;
// Player should have more HP after healing
expect(finalHp).toBeGreaterThan(initialHp);
});
});
describe('Status Effects in Multi-Battle', () => {
it('should process status effects for all active Piclets', () => {
const doubleConfig: MultiBattleConfig = {
playerParty: [STELLAR_WOLF, BERSERKER_BEAST],
opponentParty: [TOXIC_CRAWLER, AQUA_GUARDIAN],
playerActiveCount: 2,
opponentActiveCount: 2,
battleType: 'double'
};
const doubleEngine = new MultiBattleEngine(doubleConfig);
// Apply poison to both active player Piclets
(doubleEngine as any).state.activePiclets.player[0]!.statusEffects.push('poison');
(doubleEngine as any).state.activePiclets.player[1]!.statusEffects.push('poison');
const turnActions: TurnActions = {
player: [
{ type: 'move', side: 'player', position: 0, moveIndex: 0 },
{ type: 'move', side: 'player', position: 1, moveIndex: 0 }
],
opponent: [
{ type: 'move', side: 'opponent', position: 0, moveIndex: 0 },
{ type: 'move', side: 'opponent', position: 1, moveIndex: 0 }
]
};
doubleEngine.executeTurn(turnActions);
const log = doubleEngine.getLog();
const poisonMessages = log.filter(msg => msg.includes('hurt by poison'));
expect(poisonMessages.length).toBe(2); // Both Piclets should take poison damage
});
});
describe('Active Piclet Tracking', () => {
it('should correctly track active Piclets', () => {
const actives = engine.getActivePiclets();
expect(actives.player).toHaveLength(1);
expect(actives.opponent).toHaveLength(1);
expect(actives.player[0].definition.name).toBe('Stellar Wolf');
expect(actives.opponent[0].definition.name).toBe('Toxic Crawler');
});
it('should update active tracking after switches', () => {
const turnActions: TurnActions = {
player: [{
type: 'switch',
side: 'player',
position: 0,
partyIndex: 1 // Switch to Berserker Beast
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
}]
};
engine.executeTurn(turnActions);
const actives = engine.getActivePiclets();
expect(actives.player[0].definition.name).toBe('Berserker Beast');
});
});
describe('Party Management', () => {
it('should track available switches correctly', () => {
const availableSwitches = engine.getAvailableSwitches('player');
expect(availableSwitches).toHaveLength(1);
expect(availableSwitches[0].piclet.name).toBe('Berserker Beast');
expect(availableSwitches[0].partyIndex).toBe(1);
});
it('should update available switches after switching', () => {
const turnActions: TurnActions = {
player: [{
type: 'switch',
side: 'player',
position: 0,
partyIndex: 1 // Switch to Berserker Beast
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
}]
};
engine.executeTurn(turnActions);
const availableSwitches = engine.getAvailableSwitches('player');
expect(availableSwitches).toHaveLength(1);
expect(availableSwitches[0].piclet.name).toBe('Stellar Wolf');
expect(availableSwitches[0].partyIndex).toBe(0);
});
it('should handle fainted Piclets correctly', () => {
// Set player Piclet to very low HP
(engine as any).state.activePiclets.player[0]!.currentHp = 1;
const turnActions: TurnActions = {
player: [{
type: 'move',
side: 'player',
position: 0,
moveIndex: 0
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0 // This should cause player to faint
}]
};
engine.executeTurn(turnActions);
const log = engine.getLog();
expect(log.some(msg => msg.includes('fainted!'))).toBe(true);
// Active Piclet should be removed (null)
const state = engine.getState();
expect(state.activePiclets.player[0]).toBeNull();
});
});
describe('Edge Cases', () => {
it('should handle empty action arrays gracefully', () => {
const turnActions: TurnActions = {
player: [],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
}]
};
expect(() => {
engine.executeTurn(turnActions);
}).not.toThrow();
});
it('should handle invalid positions gracefully', () => {
const turnActions: TurnActions = {
player: [{
type: 'move',
side: 'player',
position: 1, // Invalid position (empty slot)
moveIndex: 0
}],
opponent: [{
type: 'move',
side: 'opponent',
position: 0,
moveIndex: 0
}]
};
expect(() => {
engine.executeTurn(turnActions);
}).not.toThrow();
});
});
}); |