mirror of
https://github.com/motajs/template.git
synced 2026-11-20 11:24:30 +08:00
test(06): fix lint and type errors in data-layer test files
This commit is contained in:
1 parent
693b337e24
commit
136a9845d6
8 files changed
+284
-189
No files matched your search
@@ -103,13 +103,17 @@ function createEnemy(attrs: Partial<IEnemyAttr> = {}): FakeEnemy {
|
||||
addSpecial: () => {},
|
||||
deleteSpecial: () => {},
|
||||
setAttribute: (key: string, value: unknown) => {
|
||||
(values as never)[key] = value;
|
||||
// 假怪物按动态字符串键读写属性,固定形状的 IEnemyAttr 无法表达字符串索引
|
||||
(values as unknown as Record<string, unknown>)[key] = value;
|
||||
},
|
||||
addAttribute: (key: string, value: number) => {
|
||||
(values as never)[key] += value;
|
||||
(values as unknown as Record<string, number>)[key] += value;
|
||||
},
|
||||
copyFrom: () => {},
|
||||
saveState: () => ({ attrs: structuredClone(values), specials: new Map() }),
|
||||
saveState: () => ({
|
||||
attrs: structuredClone(values),
|
||||
specials: new Map()
|
||||
}),
|
||||
loadState: () => {}
|
||||
} as never;
|
||||
return { enemy, attrs: values };
|
||||
@@ -129,7 +133,9 @@ function createSpecial<T>(code: number, value: T): ISpecial<T> {
|
||||
* @param resolve 根据计算后怪物对象查找视图的回调
|
||||
*/
|
||||
function createContext(
|
||||
resolve?: (enemy: IReadonlyEnemy<IEnemyAttr>) => IEnemyView<IEnemyAttr> | null
|
||||
resolve?: (
|
||||
enemy: IReadonlyEnemy<IEnemyAttr>
|
||||
) => IEnemyView<IEnemyAttr> | null
|
||||
): IEnemyContext<IEnemyAttr, IHeroAttr> {
|
||||
return {
|
||||
width: 8,
|
||||
@@ -192,8 +198,8 @@ describe('CommonAuraConverter', () => {
|
||||
const halo = createSpecial(25, createHalo());
|
||||
const guard = createSpecial(26, undefined);
|
||||
|
||||
expect(converter.shouldConvert(halo, {} as never)).toBe(true);
|
||||
expect(converter.shouldConvert(guard, {} as never)).toBe(false);
|
||||
expect(converter.shouldConvert(halo)).toBe(true);
|
||||
expect(converter.shouldConvert(guard)).toBe(false);
|
||||
});
|
||||
|
||||
// 验证转换结果为携带怪物、特殊属性与定位符的光环视图
|
||||
@@ -234,10 +240,7 @@ describe('CommonAura range', () => {
|
||||
const { enemy } = createEnemy();
|
||||
const aura = new modules.CommonAura(
|
||||
enemy,
|
||||
createSpecial(
|
||||
25,
|
||||
createHalo({ haloRange: 2, haloSquare: true })
|
||||
),
|
||||
createSpecial(25, createHalo({ haloRange: 2, haloSquare: true })),
|
||||
{ x: 3, y: 4 }
|
||||
);
|
||||
|
||||
@@ -250,10 +253,7 @@ describe('CommonAura range', () => {
|
||||
const { enemy } = createEnemy();
|
||||
const aura = new modules.CommonAura(
|
||||
enemy,
|
||||
createSpecial(
|
||||
25,
|
||||
createHalo({ haloRange: 2, haloSquare: false })
|
||||
),
|
||||
createSpecial(25, createHalo({ haloRange: 2, haloSquare: false })),
|
||||
{ x: 3, y: 4 }
|
||||
);
|
||||
|
||||
@@ -276,7 +276,10 @@ describe('CommonAura apply', () => {
|
||||
{ x: 0, y: 0 }
|
||||
);
|
||||
|
||||
aura.apply(createWritableHandler(target.enemy, { x: 1, y: 0 }), base.enemy);
|
||||
aura.apply(
|
||||
createWritableHandler(target.enemy, { x: 1, y: 0 }),
|
||||
base.enemy
|
||||
);
|
||||
|
||||
expect(target.attrs.hp).toBe(30);
|
||||
expect(target.attrs.atk).toBe(33);
|
||||
@@ -293,7 +296,10 @@ describe('CommonAura apply', () => {
|
||||
{ x: 0, y: 0 }
|
||||
);
|
||||
|
||||
aura.apply(createWritableHandler(target.enemy, { x: 1, y: 0 }), base.enemy);
|
||||
aura.apply(
|
||||
createWritableHandler(target.enemy, { x: 1, y: 0 }),
|
||||
base.enemy
|
||||
);
|
||||
|
||||
expect(target.attrs).toMatchObject({ hp: 20, atk: 8, def: 5 });
|
||||
});
|
||||
@@ -319,12 +325,12 @@ describe('GuardAuraConverter', () => {
|
||||
it('converts only special code 26', () => {
|
||||
const converter = new modules.GuardAuraConverter();
|
||||
|
||||
expect(
|
||||
converter.shouldConvert(createSpecial(26, undefined), {} as never)
|
||||
).toBe(true);
|
||||
expect(
|
||||
converter.shouldConvert(createSpecial(25, undefined), {} as never)
|
||||
).toBe(false);
|
||||
expect(converter.shouldConvert(createSpecial(26, undefined))).toBe(
|
||||
true
|
||||
);
|
||||
expect(converter.shouldConvert(createSpecial(25, undefined))).toBe(
|
||||
false
|
||||
);
|
||||
});
|
||||
|
||||
// 验证转换结果为携带上下文、怪物、特殊属性与定位符的支援光环视图
|
||||
@@ -376,9 +382,9 @@ describe('GuardAura', () => {
|
||||
|
||||
aura.apply(createWritableHandler(target.enemy, { x: 2, y: 1 }));
|
||||
|
||||
expect((target.attrs.guard as Set<ITileLocator>).has(sourceLocator)).toBe(
|
||||
true
|
||||
);
|
||||
expect(
|
||||
(target.attrs.guard as Set<ITileLocator>).has(sourceLocator)
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
// 验证支援光环不会把来源定位符添加到自身
|
||||
|
||||
@@ -97,7 +97,7 @@ function createEnemy(options: FakeEnemyOptions = {}): FakeEnemy {
|
||||
code,
|
||||
value: specials.get(code),
|
||||
deepEqualsTo: () => false
|
||||
} as ISpecial<any>)
|
||||
} as unknown as ISpecial<any>)
|
||||
: null,
|
||||
hasSpecial: (code: number) => specials.has(code),
|
||||
iterateSpecials: () => [],
|
||||
@@ -107,10 +107,11 @@ function createEnemy(options: FakeEnemyOptions = {}): FakeEnemy {
|
||||
addSpecial: () => {},
|
||||
deleteSpecial: () => {},
|
||||
setAttribute: (key: string, value: unknown) => {
|
||||
(attrs as never)[key] = value;
|
||||
// 假怪物按动态字符串键读写属性,固定形状的 IEnemyAttr 无法表达字符串索引
|
||||
(attrs as unknown as Record<string, unknown>)[key] = value;
|
||||
},
|
||||
addAttribute: (key: string, value: number) => {
|
||||
(attrs as never)[key] += value;
|
||||
(attrs as unknown as Record<string, number>)[key] += value;
|
||||
},
|
||||
copyFrom: () => {},
|
||||
saveState: () => ({
|
||||
@@ -145,7 +146,7 @@ function createHero(
|
||||
return {
|
||||
getBaseAttribute: (name: string) => (values as never)[name],
|
||||
getFinalAttribute: (name: string) => (values as never)[name]
|
||||
} as IReadonlyHeroAttribute<IHeroAttr>;
|
||||
} as unknown as IReadonlyHeroAttribute<IHeroAttr>;
|
||||
}
|
||||
|
||||
interface FakeStateOptions {
|
||||
@@ -196,12 +197,10 @@ function createContext(
|
||||
* 将一个计算后怪物对象包装为只提供该对象读取的假视图
|
||||
* @param computed 计算后怪物对象
|
||||
*/
|
||||
function createView(
|
||||
computed: IEnemy<IEnemyAttr>
|
||||
): IEnemyView<IEnemyAttr> {
|
||||
function createView(computed: IEnemy<IEnemyAttr>): IEnemyView<IEnemyAttr> {
|
||||
return {
|
||||
getComputedEnemy: () => computed
|
||||
} as IEnemyView<IEnemyAttr>;
|
||||
} as unknown as IEnemyView<IEnemyAttr>;
|
||||
}
|
||||
|
||||
interface HandlerOptions {
|
||||
@@ -244,9 +243,7 @@ function specialsOf(entries: Array<[number, unknown]>): Map<number, unknown> {
|
||||
/**
|
||||
* 创建一个计算器实例
|
||||
*/
|
||||
function createCalculator(): InstanceType<
|
||||
TestModules['MainDamageCalculator']
|
||||
> {
|
||||
function createCalculator(): InstanceType<TestModules['MainDamageCalculator']> {
|
||||
return new modules.MainDamageCalculator();
|
||||
}
|
||||
|
||||
@@ -273,7 +270,9 @@ describe('MainDamageCalculator base and defeat branches', () => {
|
||||
|
||||
// 验证无敌怪物在没有十字架时不可战胜,持有十字架后恢复正常计算
|
||||
it('blocks 无敌 without a cross and allows it with one', () => {
|
||||
const { enemy } = createEnemy({ specials: specialsOf([[20, undefined]]) });
|
||||
const { enemy } = createEnemy({
|
||||
specials: specialsOf([[20, undefined]])
|
||||
});
|
||||
const calculator = createCalculator();
|
||||
|
||||
const blocked = calculator.calculate(createHandler({ enemy }));
|
||||
@@ -287,7 +286,9 @@ describe('MainDamageCalculator base and defeat branches', () => {
|
||||
|
||||
// 验证魔攻怪物的每轮伤害不减免勇士防御
|
||||
it('ignores hero defense for 魔攻', () => {
|
||||
const { enemy } = createEnemy({ specials: specialsOf([[2, undefined]]) });
|
||||
const { enemy } = createEnemy({
|
||||
specials: specialsOf([[2, undefined]])
|
||||
});
|
||||
const calculator = createCalculator();
|
||||
|
||||
const info = calculator.calculate(createHandler({ enemy }));
|
||||
@@ -301,12 +302,12 @@ describe('MainDamageCalculator base and defeat branches', () => {
|
||||
const double = createEnemy({ specials: specialsOf([[4, undefined]]) });
|
||||
const triple = createEnemy({ specials: specialsOf([[5, undefined]]) });
|
||||
|
||||
expect(calculator.calculate(createHandler({ enemy: double.enemy }))).toEqual(
|
||||
{ damage: 6, turn: 2 }
|
||||
);
|
||||
expect(calculator.calculate(createHandler({ enemy: triple.enemy }))).toEqual(
|
||||
{ damage: 9, turn: 2 }
|
||||
);
|
||||
expect(
|
||||
calculator.calculate(createHandler({ enemy: double.enemy }))
|
||||
).toEqual({ damage: 6, turn: 2 });
|
||||
expect(
|
||||
calculator.calculate(createHandler({ enemy: triple.enemy }))
|
||||
).toEqual({ damage: 9, turn: 2 });
|
||||
});
|
||||
|
||||
// 验证多段伤害按特殊属性数值倍乘每轮伤害
|
||||
@@ -334,9 +335,7 @@ describe('MainDamageCalculator support and additive branches', () => {
|
||||
});
|
||||
const calculator = createCalculator();
|
||||
|
||||
const info = calculator.calculate(
|
||||
createHandler({ enemy, context })
|
||||
);
|
||||
const info = calculator.calculate(createHandler({ enemy, context }));
|
||||
|
||||
expect(info).toEqual({ damage: 12, turn: 4 });
|
||||
expect(calls).toEqual(['1,0']);
|
||||
@@ -378,7 +377,9 @@ describe('MainDamageCalculator support and additive branches', () => {
|
||||
|
||||
// 验证先攻会额外附加一次每轮伤害
|
||||
it('adds one enemy hit for 先攻', () => {
|
||||
const { enemy } = createEnemy({ specials: specialsOf([[1, undefined]]) });
|
||||
const { enemy } = createEnemy({
|
||||
specials: specialsOf([[1, undefined]])
|
||||
});
|
||||
const calculator = createCalculator();
|
||||
|
||||
const info = calculator.calculate(createHandler({ enemy }));
|
||||
@@ -500,7 +501,9 @@ describe('MainDamageCalculator vampire and bypass branches', () => {
|
||||
|
||||
// 验证仇恨按 flag 字段值附加伤害且不受魔防影响
|
||||
it('adds the hatred flag value for 仇恨', () => {
|
||||
const { enemy } = createEnemy({ specials: specialsOf([[17, undefined]]) });
|
||||
const { enemy } = createEnemy({
|
||||
specials: specialsOf([[17, undefined]])
|
||||
});
|
||||
const calculator = createCalculator();
|
||||
|
||||
const info = calculator.calculate(
|
||||
|
||||
@@ -2,10 +2,7 @@
|
||||
import { beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
import { type IEnemyAttr, type IHeroAttr } from '@user/data-common';
|
||||
import { type IEnemyHandler } from '@user/data-system';
|
||||
import {
|
||||
type IEnemy,
|
||||
type IReadonlyHeroAttribute
|
||||
} from '@user/data-base';
|
||||
import { type IEnemy, type IReadonlyHeroAttribute } from '@user/data-base';
|
||||
|
||||
vi.hoisted(() => {
|
||||
vi.stubGlobal('main', { replayChecking: true });
|
||||
@@ -86,13 +83,17 @@ function createEnemy(
|
||||
addSpecial: () => {},
|
||||
deleteSpecial: () => {},
|
||||
setAttribute: (key: string, value: unknown) => {
|
||||
(values as never)[key] = value;
|
||||
// 假怪物按动态字符串键读写属性,固定形状的 IEnemyAttr 无法表达字符串索引
|
||||
(values as unknown as Record<string, unknown>)[key] = value;
|
||||
},
|
||||
addAttribute: (key: string, value: number) => {
|
||||
(values as never)[key] += value;
|
||||
(values as unknown as Record<string, number>)[key] += value;
|
||||
},
|
||||
copyFrom: () => {},
|
||||
saveState: () => ({ attrs: structuredClone(values), specials: new Map() }),
|
||||
saveState: () => ({
|
||||
attrs: structuredClone(values),
|
||||
specials: new Map()
|
||||
}),
|
||||
loadState: () => {}
|
||||
} as never;
|
||||
return { enemy, attrs: values };
|
||||
|
||||
@@ -6,11 +6,9 @@ import {
|
||||
type IDamageSystem,
|
||||
type IEnemyContext,
|
||||
type IEnemyView,
|
||||
type IMapDamageInfo,
|
||||
type IReadonlyEnemyHandler
|
||||
type IMapDamageInfo
|
||||
} from '@user/data-system';
|
||||
import {
|
||||
type IEnemy,
|
||||
type IReadonlyEnemy,
|
||||
type IReadonlyHeroAttribute,
|
||||
type ISpecial
|
||||
@@ -77,10 +75,19 @@ beforeAll(async () => {
|
||||
AmbushDamageView: mapDamageModule.AmbushDamageView,
|
||||
MainMapDamageConverter: mapDamageModule.MainMapDamageConverter,
|
||||
MainMapDamageReducer: mapDamageModule.MainMapDamageReducer,
|
||||
MapDamageType: typesModule.MapDamageType,
|
||||
MapDamageType: {
|
||||
Unknown: typesModule.MapDamageType.Unknown,
|
||||
Zone: typesModule.MapDamageType.Zone,
|
||||
Layer: typesModule.MapDamageType.Layer,
|
||||
Repulse: typesModule.MapDamageType.Repulse,
|
||||
Between: typesModule.MapDamageType.Between
|
||||
},
|
||||
ManhattanRange: motaModule.ManhattanRange,
|
||||
RectRange: motaModule.RectRange,
|
||||
FaceGroup: commonModule.FaceGroup
|
||||
FaceGroup: {
|
||||
Dir4: commonModule.FaceGroup.Dir4,
|
||||
Dir8: commonModule.FaceGroup.Dir8
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
@@ -144,7 +151,8 @@ function createContext(
|
||||
): IEnemyContext<IEnemyAttr, IHeroAttr> {
|
||||
const width = options.width ?? 6;
|
||||
const height = options.height ?? 6;
|
||||
const enemies = options.enemies ?? new Map<string, IEnemyView<IEnemyAttr>>();
|
||||
const enemies =
|
||||
options.enemies ?? new Map<string, IEnemyView<IEnemyAttr>>();
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
@@ -177,9 +185,9 @@ function createView(
|
||||
* 创建一个按固定方向表返回移动描述的假朝向处理器
|
||||
* @param dirs 朝向与坐标增量对
|
||||
*/
|
||||
function createFace(
|
||||
dirs: Array<[number, { x: number; y: number }]>
|
||||
): { mapMovement: () => Array<[number, { x: number; y: number }]> } {
|
||||
function createFace(dirs: Array<[number, { x: number; y: number }]>): {
|
||||
mapMovement: () => Array<[number, { x: number; y: number }]>;
|
||||
} {
|
||||
return { mapMovement: () => dirs };
|
||||
}
|
||||
|
||||
@@ -212,11 +220,15 @@ describe('ZoneDamageView', () => {
|
||||
zoneSquare: false,
|
||||
range: 1
|
||||
});
|
||||
const view = new modules.ZoneDamageView(createContext(), { x: 2, y: 3 }, special);
|
||||
const view = new modules.ZoneDamageView(
|
||||
createContext(),
|
||||
{ x: 2, y: 3 },
|
||||
special
|
||||
);
|
||||
|
||||
expect(view.getRange()).toBeInstanceOf(modules.ManhattanRange);
|
||||
expect(view.getRangeParam()).toEqual({ cx: 2, cy: 3, radius: 1 });
|
||||
expect(view.getDamageWithoutCheck({ x: 0, y: 0 })).toEqual(
|
||||
expect(view.getDamageWithoutCheck()).toEqual(
|
||||
createInfo(7, modules.MapDamageType.Zone)
|
||||
);
|
||||
});
|
||||
@@ -228,11 +240,15 @@ describe('ZoneDamageView', () => {
|
||||
zoneSquare: true,
|
||||
range: 2
|
||||
});
|
||||
const view = new modules.ZoneDamageView(createContext(), { x: 2, y: 3 }, special);
|
||||
const view = new modules.ZoneDamageView(
|
||||
createContext(),
|
||||
{ x: 2, y: 3 },
|
||||
special
|
||||
);
|
||||
|
||||
expect(view.getRange()).toBeInstanceOf(modules.RectRange);
|
||||
expect(view.getRangeParam()).toEqual({ x: 0, y: 1, w: 5, h: 5 });
|
||||
expect(view.getDamageWithoutCheck({ x: 0, y: 0 })).toEqual(
|
||||
expect(view.getDamageWithoutCheck()).toEqual(
|
||||
createInfo(4, modules.MapDamageType.Zone)
|
||||
);
|
||||
});
|
||||
@@ -244,7 +260,11 @@ describe('ZoneDamageView', () => {
|
||||
zoneSquare: false,
|
||||
range: 1
|
||||
});
|
||||
const view = new modules.ZoneDamageView(createContext(), { x: 2, y: 3 }, special);
|
||||
const view = new modules.ZoneDamageView(
|
||||
createContext(),
|
||||
{ x: 2, y: 3 },
|
||||
special
|
||||
);
|
||||
|
||||
expect(view.getDamageAt({ x: 2, y: 4 })).toEqual(
|
||||
createInfo(7, modules.MapDamageType.Zone)
|
||||
@@ -258,7 +278,11 @@ describe('RepulseDamageView', () => {
|
||||
it('uses a radius one range and returns null at the source', () => {
|
||||
const special = createSpecial(18, 4);
|
||||
const source: ITileLocator = { x: 2, y: 3 };
|
||||
const view = new modules.RepulseDamageView(createContext(), source, special);
|
||||
const view = new modules.RepulseDamageView(
|
||||
createContext(),
|
||||
source,
|
||||
special
|
||||
);
|
||||
|
||||
expect(view.getRangeParam()).toEqual({ cx: 2, cy: 3, radius: 1 });
|
||||
expect(view.getDamageWithoutCheck(source)).toBeNull();
|
||||
@@ -268,7 +292,11 @@ describe('RepulseDamageView', () => {
|
||||
it('returns repulse damage carrying the source locator', () => {
|
||||
const special = createSpecial(18, 4);
|
||||
const source: ITileLocator = { x: 2, y: 3 };
|
||||
const view = new modules.RepulseDamageView(createContext(), source, special);
|
||||
const view = new modules.RepulseDamageView(
|
||||
createContext(),
|
||||
source,
|
||||
special
|
||||
);
|
||||
|
||||
const info = view.getDamageWithoutCheck({ x: 3, y: 3 });
|
||||
|
||||
@@ -308,9 +336,12 @@ describe('LaserDamageView', () => {
|
||||
expect(view.getRangeParam()).toEqual({
|
||||
cx: 2,
|
||||
cy: 3,
|
||||
dir: [{ x: 0, y: 0 }, { x: 1, y: 0 }]
|
||||
dir: [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 }
|
||||
]
|
||||
});
|
||||
expect(view.getDamageWithoutCheck({ x: 5, y: 3 })).toEqual(
|
||||
expect(view.getDamageWithoutCheck()).toEqual(
|
||||
createInfo(9, modules.MapDamageType.Layer)
|
||||
);
|
||||
});
|
||||
@@ -462,7 +493,7 @@ describe('AmbushDamageView', () => {
|
||||
const locator: ITileLocator = { x: 2, y: 3 };
|
||||
const view = new modules.AmbushDamageView(createContext(), locator);
|
||||
|
||||
const info = view.getDamageWithoutCheck(locator);
|
||||
const info = view.getDamageWithoutCheck();
|
||||
|
||||
expect(info?.damage).toBe(0);
|
||||
expect(info?.type).toBe(modules.MapDamageType.Unknown);
|
||||
@@ -531,18 +562,15 @@ describe('MainMapDamageReducer', () => {
|
||||
const caught: ITileLocator = { x: 0, y: 1 };
|
||||
const reducer = new modules.MainMapDamageReducer();
|
||||
|
||||
const info = reducer.reduce(
|
||||
[
|
||||
createInfo(3, modules.MapDamageType.Zone),
|
||||
createInfo(5, modules.MapDamageType.Repulse, {
|
||||
repulse: new Set([repulse])
|
||||
}),
|
||||
createInfo(5, modules.MapDamageType.Between, {
|
||||
catch: new Set([caught])
|
||||
})
|
||||
],
|
||||
{ x: 0, y: 0 }
|
||||
);
|
||||
const info = reducer.reduce([
|
||||
createInfo(3, modules.MapDamageType.Zone),
|
||||
createInfo(5, modules.MapDamageType.Repulse, {
|
||||
repulse: new Set([repulse])
|
||||
}),
|
||||
createInfo(5, modules.MapDamageType.Between, {
|
||||
catch: new Set([caught])
|
||||
})
|
||||
]);
|
||||
|
||||
expect(info.damage).toBe(13);
|
||||
expect(info.type).toBe(modules.MapDamageType.Repulse);
|
||||
@@ -554,7 +582,7 @@ describe('MainMapDamageReducer', () => {
|
||||
it('returns zero unknown damage for an empty input', () => {
|
||||
const reducer = new modules.MainMapDamageReducer();
|
||||
|
||||
const info = reducer.reduce([], { x: 0, y: 0 });
|
||||
const info = reducer.reduce([]);
|
||||
|
||||
expect(info.damage).toBe(0);
|
||||
expect(info.type).toBe(modules.MapDamageType.Unknown);
|
||||
|
||||
Reference in new issue
Block a user