games_app/backend/src/tests/shareCardService.test.ts

133 lines
4.1 KiB
TypeScript

import { describe, test } from 'node:test';
import assert from 'node:assert/strict';
import {
canSharePredictionResult,
canShareWalletOperation,
computeResultAmount,
createShareVerifyToken,
parseShareVerifyToken,
} from '../services/shareCardService';
import type { ShareCardSettingsRow } from '../repositories/shareCardRepository';
function settings(partial: Partial<ShareCardSettingsRow> = {}): ShareCardSettingsRow {
return {
id: 1,
enabled: true,
allow_main_wallet: true,
allow_bonus_wallet: true,
allow_test_wallet: false,
allow_won: true,
allow_lost: true,
allow_refunded: true,
wallet_ops_enabled: true,
allow_deposit: true,
allow_withdrawal: true,
allow_transfer: true,
updated_at: new Date(),
...partial,
};
}
describe('share card eligibility', () => {
test('blocks when globally disabled', () => {
assert.equal(
canSharePredictionResult(settings({ enabled: false }), {
stakeAccountKind: 'main',
status: 'won',
}),
false
);
});
test('blocks test wallet when disallowed', () => {
assert.equal(
canSharePredictionResult(settings(), { stakeAccountKind: 'test', status: 'won' }),
false
);
});
test('blocks lost when allow_lost is false', () => {
assert.equal(
canSharePredictionResult(settings({ allow_lost: false }), {
stakeAccountKind: 'main',
status: 'lost',
}),
false
);
});
test('allows main wallet won by default', () => {
assert.equal(
canSharePredictionResult(settings(), { stakeAccountKind: 'main', status: 'won' }),
true
);
});
test('blocks pending bets', () => {
assert.equal(
canSharePredictionResult(settings(), { stakeAccountKind: 'main', status: 'pending' }),
false
);
});
test('wallet ops respect global and per-type flags', () => {
assert.equal(canShareWalletOperation(settings(), { type: 'deposit' }), true);
assert.equal(canShareWalletOperation(settings(), { type: 'bonus' }), true);
assert.equal(canShareWalletOperation(settings(), { type: 'admin_adjustment' }), true);
assert.equal(
canShareWalletOperation(settings({ wallet_ops_enabled: false }), { type: 'deposit' }),
false
);
assert.equal(
canShareWalletOperation(settings({ allow_deposit: false }), { type: 'bonus' }),
false
);
assert.equal(
canShareWalletOperation(settings({ allow_transfer: false }), { type: 'transfer' }),
false
);
assert.equal(canShareWalletOperation(settings(), { type: 'game_stake' }), false);
});
});
describe('share card result amount', () => {
test('won uses full payout including stake', () => {
// stake 5 USDT, payout 9.5 USDT → +9.5 (not net +4.5)
const result = computeResultAmount('won', 5_000_000n, 9_500_000n);
assert.equal(result.resultSignedDisplay, '+9.5');
});
test('lost uses minus stake', () => {
const result = computeResultAmount('lost', 5_000_000n, 0n);
assert.equal(result.resultSignedDisplay, '-5');
});
});
describe('share card verify token', () => {
test('round-trips bet and user ids', () => {
const betId = '11111111-1111-1111-1111-111111111111';
const userId = '22222222-2222-2222-2222-222222222222';
const token = createShareVerifyToken('bet', betId, userId);
const parsed = parseShareVerifyToken(token);
assert.deepEqual(parsed, { kind: 'bet', id: betId, userId });
});
test('round-trips wallet transaction tokens', () => {
const txId = '33333333-3333-3333-3333-333333333333';
const userId = '22222222-2222-2222-2222-222222222222';
const token = createShareVerifyToken('tx', txId, userId);
const parsed = parseShareVerifyToken(token);
assert.deepEqual(parsed, { kind: 'tx', id: txId, userId });
});
test('rejects tampered token', () => {
const token = createShareVerifyToken(
'bet',
'11111111-1111-1111-1111-111111111111',
'22222222-2222-2222-2222-222222222222'
);
assert.equal(parseShareVerifyToken(token + 'x'), null);
assert.equal(parseShareVerifyToken('not-a-token'), null);
});
});