Files
erqiwang_youle/client/tests/test_cardorder.js
T
joywayerandClaude Opus 5 39a920bed5 二七王:前端 CardOrder,手牌主牌序排序
直接委托 shared/cards.js 的 order_cards(与服务端同一份文件),
前端只负责 concat 防改入参、把未选主表达为 mainflower=0。
单测拿服务端 class.arith 的输出做逐一对照,五种主花色全覆盖。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-26 20:01:51 +08:00

50 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 主牌序排序:必须与服务端 order_cards 同序(清单 T-5)
const { load } = require('./_load');
const t = require('./_assert')();
load('client/js/01_SubGame/codes/shared/cards.js');
load('client/js/01_SubGame/codes/core/CardOrder.js');
// 服务端权威实现,用来做对照
const SRV = require('../../server/games/erqiwang/class.arith.js');
// 牌id构造(与服务端测试同款):deck d(1-2), flower f(1-4), number n(1-13)
const id = (d, f, n) => (d - 1) * 54 + (f - 1) * 13 + (n - 1);
const big = d => (d - 1) * 54 + 53; // 大王
const small = d => (d - 1) * 54 + 52; // 小王
// 一手混牌:两副牌、四花色、含王与 2/7
const hand = [
id(1, 1, 5), id(1, 4, 13), big(1), id(2, 2, 7), id(1, 3, 2),
id(1, 1, 1), small(2), id(2, 4, 7), id(1, 2, 2), id(2, 3, 10)
];
// ---- 与服务端逐一对照:选主前(0) 与四种主花色 ----
[0, 1, 2, 3, 4].forEach(mf => {
const expected = SRV.order_cards(mf, hand.concat());
t.eq('与服务端同序 mainflower=' + mf, EQW_CardOrder.sort(hand, mf), expected);
});
// ---- 不修改入参数组 ----
const orig = hand.concat();
EQW_CardOrder.sort(hand, 1);
t.eq('入参未被修改', hand, orig);
// ---- 返回的是新数组,不是同一引用 ----
t.eq('返回新数组', EQW_CardOrder.sort(hand, 1) === hand, false);
// ---- 选主前后顺序确实不同(正2/正7 升格、主花色并入主牌段)----
const beforeChoose = EQW_CardOrder.sort(hand, 0);
const afterChoose = EQW_CardOrder.sort(hand, 1);
t.eq('选主后顺序变化', JSON.stringify(beforeChoose) === JSON.stringify(afterChoose), false);
// ---- 主牌在左:排序从大到小,最大的一张恒为大王 ----
t.eq('首张是大王', EQW_CardOrder.sort(hand, 1)[0], big(1));
// ---- 边界 ----
t.eq('空数组', EQW_CardOrder.sort([], 1), []);
t.eq('null 入参', EQW_CardOrder.sort(null, 1), []);
t.eq('单张', EQW_CardOrder.sort([id(1, 1, 5)], 1), [id(1, 1, 5)]);
process.exit(t.done('cardorder') ? 0 : 1);