chore: non-combat tests update

This commit is contained in:
2026-04-17 12:58:29 +08:00
parent f775d51a58
commit 7601a97ec9
4 changed files with 102 additions and 83 deletions
@@ -1,33 +1,57 @@
import { describe, it, expect } from 'vitest';
import {
generateDeckFromInventory,
createStatusCard,
createCard,
createDeckRegions,
createPlayerDeck,
generateCardId,
} from '@/samples/slay-the-spire-like/deck/factory';
import { createGridInventory, placeItem } from '@/samples/slay-the-spire-like/grid-inventory';
import type { GridInventory, InventoryItem } from '@/samples/slay-the-spire-like/grid-inventory';
import type { GameItemMeta } from '@/samples/slay-the-spire-like/progress/types';
import { IDENTITY_TRANSFORM } from '@/samples/slay-the-spire-like/utils/shape-collision';
import { parseShapeString } from '@/samples/slay-the-spire-like/utils/parse-shape';
} from '@/samples/slay-the-spire-like/system/deck/factory';
import { createGridInventory, placeItem } from '@/samples/slay-the-spire-like/system/grid-inventory';
import type { GridInventory, InventoryItem } from '@/samples/slay-the-spire-like/system/grid-inventory';
import type { GameItemMeta } from '@/samples/slay-the-spire-like/system/progress/types';
import { IDENTITY_TRANSFORM } from '@/samples/slay-the-spire-like/system/utils/shape-collision';
import { parseShapeString } from '@/samples/slay-the-spire-like/system/utils/parse-shape';
import type { CardData, ItemData } from '@/samples/slay-the-spire-like/system/types';
/**
* Helper: create a minimal CardData for testing.
*/
function createTestCardData(id: string, name: string, desc: string): CardData {
return {
id,
name,
desc,
type: 'item',
costType: 'energy',
costCount: 1,
targetType: 'single',
effects: [],
};
}
/**
* Helper: create a minimal ItemData for testing.
*/
function createTestItemData(id: string, name: string, shapeStr: string, desc: string): ItemData {
return {
id,
type: 'weapon',
name,
shape: shapeStr,
card: createTestCardData(id, name, desc),
price: 10,
description: desc,
};
}
/**
* Helper: create a minimal GameItemMeta for testing.
*/
function createTestMeta(name: string, desc: string, shapeStr: string): GameItemMeta {
const shape = parseShapeString(shapeStr);
const itemData = createTestItemData(name.toLowerCase(), name, shapeStr, desc);
return {
itemData: {
type: 'weapon',
name,
shape: shapeStr,
costType: 'energy',
costCount: 1,
targetType: 'single',
price: 10,
desc,
},
itemData,
shape,
};
}
@@ -70,21 +94,14 @@ describe('deck/factory', () => {
});
});
describe('createStatusCard', () => {
it('should create a card with null sourceItemId and itemData', () => {
const card = createStatusCard('wound-1', '伤口', '无法被弃牌');
expect(card.id).toBe('wound-1');
expect(card.sourceItemId).toBeNull();
expect(card.itemData).toBeNull();
expect(card.cellKey).toBeNull();
expect(card.displayName).toBe('伤口');
expect(card.description).toBe('无法被弃牌');
});
it('should have empty region and position', () => {
const card = createStatusCard('stun-1', '眩晕', '跳过出牌阶段');
describe('createCard', () => {
it('should create a card with itemId and cardData', () => {
const cardData = createTestCardData('wound', '伤口', '无法被弃牌');
const card = createCard('wound-1', cardData, 0);
expect(card.id).toBe('card-wound-1-0');
expect(card.itemId).toBe('wound-1');
expect(card.cardData).toBe(cardData);
expect(card.regionId).toBe('');
expect(card.position).toEqual([]);
});
@@ -98,10 +115,10 @@ describe('deck/factory', () => {
const deck = generateDeckFromInventory(inv);
expect(Object.keys(deck.cards).length).toBe(6);
expect(deck.drawPile.length).toBe(6);
expect(deck.hand).toEqual([]);
expect(deck.discardPile).toEqual([]);
expect(deck.exhaustPile).toEqual([]);
expect(deck.regions.drawPile.childIds.length).toBe(6);
expect(deck.regions.hand.childIds).toEqual([]);
expect(deck.regions.discardPile.childIds).toEqual([]);
expect(deck.regions.exhaustPile.childIds).toEqual([]);
});
it('should link cards to their source items', () => {
@@ -109,18 +126,18 @@ describe('deck/factory', () => {
const deck = generateDeckFromInventory(inv);
const daggerCards = Object.values(deck.cards).filter(
c => c.sourceItemId === 'dagger-1'
c => c.itemId === 'dagger-1'
);
const shieldCards = Object.values(deck.cards).filter(
c => c.sourceItemId === 'shield-1'
c => c.itemId === 'shield-1'
);
expect(daggerCards.length).toBe(2);
expect(shieldCards.length).toBe(4);
// Verify item data
expect(daggerCards[0].itemData?.name).toBe('短刀');
expect(shieldCards[0].itemData?.name).toBe('盾');
// Verify card data
expect(daggerCards[0].cardData.name).toBe('短刀');
expect(shieldCards[0].cardData.name).toBe('盾');
});
it('should set displayName and description from item data', () => {
@@ -128,28 +145,28 @@ describe('deck/factory', () => {
const deck = generateDeckFromInventory(inv);
for (const card of Object.values(deck.cards)) {
expect(card.displayName).toBeTruthy();
expect(card.description).toBeTruthy();
expect(card.cardData.name).toBeTruthy();
expect(card.cardData.desc).toBeTruthy();
}
const daggerCard = Object.values(deck.cards).find(
c => c.itemData?.name === '短刀'
c => c.cardData.name === '短刀'
);
expect(daggerCard?.displayName).toBe('短刀');
expect(daggerCard?.description).toBe('【攻击3】【攻击3】');
expect(daggerCard?.cardData.name).toBe('短刀');
expect(daggerCard?.cardData.desc).toBe('【攻击3】【攻击3】');
});
it('should assign unique cell keys to each card from same item', () => {
it('should assign unique IDs to each card from same item', () => {
const inv = createTestInventory();
const deck = generateDeckFromInventory(inv);
const daggerCards = Object.values(deck.cards).filter(
c => c.sourceItemId === 'dagger-1'
c => c.itemId === 'dagger-1'
);
const cellKeys = daggerCards.map(c => c.cellKey);
const uniqueKeys = new Set(cellKeys);
expect(uniqueKeys.size).toBe(cellKeys.length);
const ids = daggerCards.map(c => c.id);
const uniqueIds = new Set(ids);
expect(uniqueIds.size).toBe(ids.length);
});
it('should handle empty inventory', () => {
@@ -157,19 +174,19 @@ describe('deck/factory', () => {
const deck = generateDeckFromInventory(inv);
expect(Object.keys(deck.cards).length).toBe(0);
expect(deck.drawPile).toEqual([]);
expect(deck.regions.drawPile.childIds).toEqual([]);
});
it('should place all cards in draw pile initially', () => {
const inv = createTestInventory();
const deck = generateDeckFromInventory(inv);
for (const cardId of deck.drawPile) {
for (const cardId of deck.regions.drawPile.childIds) {
expect(deck.cards[cardId]).toBeDefined();
}
// All cards are in draw pile
expect(new Set(deck.drawPile).size).toBe(Object.keys(deck.cards).length);
expect(new Set(deck.regions.drawPile.childIds).size).toBe(Object.keys(deck.cards).length);
});
});
@@ -198,10 +215,10 @@ describe('deck/factory', () => {
const deck = createPlayerDeck();
expect(deck.cards).toEqual({});
expect(deck.drawPile).toEqual([]);
expect(deck.hand).toEqual([]);
expect(deck.discardPile).toEqual([]);
expect(deck.exhaustPile).toEqual([]);
expect(deck.regions.drawPile.childIds).toEqual([]);
expect(deck.regions.hand.childIds).toEqual([]);
expect(deck.regions.discardPile.childIds).toEqual([]);
expect(deck.regions.exhaustPile.childIds).toEqual([]);
});
});
});