BlueGit

timfa questward

Files

questward / server / tests / npc-equipment.test.js

npc-equipment.test.js

npc-equipment.test.js 2.9 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { npcDefaultEquipment, npcClothDyeFromSeed, NPC_CLOTH_DYE_BYTE_MIN } from "../../shared/appearance/npcEquipment.js";
4import { layersFromSpawnerConfig } from "../../shared/appearance/layers.js";
5import { ItemId } from "../../shared/definitions/items.js";
6import { LeatherItemId } from "../../shared/definitions/leatherItems.js";
7import { clothTierForItemId } from "../../shared/definitions/clothItems.js";
8import { itemDefinition } from "../../shared/definitions/items.js";
9import { mobComponents } from "../src/simulation/spawners.js";
10import { MobId } from "../../shared/definitions/mobs.js";
11
12test("npcDefaultEquipment picks deterministic garments from display name", () =>
13{
14 const a = npcDefaultEquipment("Ada Lovelace", "female");
15 const b = npcDefaultEquipment("Ada Lovelace", "female");
16 assert.deepEqual(a, b);
17 assert.ok(a.chest?.itemId);
18 assert.ok(clothTierForItemId(a.legs?.itemId));
19 const legsKey = itemDefinition(a.legs.itemId)?.key ?? "";
20 assert.ok(legsKey.endsWith("-shorts") || legsKey.endsWith("-skirt") || legsKey.endsWith("-trousers"));
21 assert.equal(a.boots?.itemId, LeatherItemId.leatherShoes);
22 assert.ok(a.chest.dyeColor);
23 assert.ok(a.legs.dyeColor);
24});
25
26test("npc cloth dyes stay above minimum channel", () =>
27{
28 const dye = npcClothDyeFromSeed("Test NPC", 3);
29 const min = NPC_CLOTH_DYE_BYTE_MIN / 255;
30 assert.ok(dye.r >= min);
31 assert.ok(dye.g >= min);
32 assert.ok(dye.b >= min);
33});
34
35test("humanoid spawner without equipment gets default clothing", () =>
36{
37 const components = mobComponents({
38 mobTypeId: MobId.humanoid,
39 tileX: 8,
40 tileY: 8,
41 displayName: "Village Guard",
42 sex: "male",
43 equipment: {},
44 spawnerId: "test"
45 });
46 const slots = components.equipment?.slots ?? {};
47 assert.ok(slots.chest?.itemId);
48 assert.ok(slots.legs?.itemId);
49 assert.equal(slots.boots?.itemId, LeatherItemId.leatherShoes);
50 assert.equal(slots.chest?.instance?.kind, "clothing");
51 assert.equal(slots.legs?.instance?.kind, "clothing");
52});
53
54test("authored spawner chest overrides default clothing", () =>
55{
56 const components = mobComponents({
57 mobTypeId: MobId.humanoid,
58 tileX: 8,
59 tileY: 8,
60 displayName: "Tailor",
61 sex: "female",
62 equipment: { chest: ItemId.clothShirt },
63 spawnerId: "test"
64 });
65 assert.equal(components.equipment.slots.chest.itemId, ItemId.clothShirt);
66 assert.equal(components.equipment.slots.chest.instance, undefined);
67});
68
69test("layersFromSpawnerConfig previews body gear for humanoid NPCs only", () =>
70{
71 assert.equal(layersFromSpawnerConfig({ mobTypeId: MobId.deer }), null);
72 const layers = layersFromSpawnerConfig({
73 mobTypeId: MobId.humanoid,
74 displayName: "Preview Bob",
75 sex: "male",
76 equipment: { weapon: { itemId: ItemId.stick } }
77 });
78 assert.ok(layers?.length >= 2);
79 assert.match(layers[0].sprite, /player-body-male/);
80 assert.ok(layers.some((layer) => layer.itemId === ItemId.stick));
81 assert.ok(layers.some((layer) => layer.itemId === LeatherItemId.leatherShoes));
82});