refactor: initial moves
This commit is contained in:
181
shared/modules/trade/trade.service.test.ts
Normal file
181
shared/modules/trade/trade.service.test.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
import { describe, it, expect, mock, beforeEach, afterEach, spyOn } from "bun:test";
|
||||
import { tradeService } from "@shared/modules/trade/trade.service";
|
||||
import { itemTransactions } from "@db/schema";
|
||||
import { economyService } from "@shared/modules/economy/economy.service";
|
||||
import { inventoryService } from "@shared/modules/inventory/inventory.service";
|
||||
|
||||
// Mock dependencies
|
||||
const mockInsert = mock();
|
||||
const mockValues = mock();
|
||||
|
||||
mockInsert.mockReturnValue({ values: mockValues });
|
||||
|
||||
// Mock DrizzleClient
|
||||
mock.module("@shared/db/DrizzleClient", () => {
|
||||
return {
|
||||
DrizzleClient: {
|
||||
transaction: async (cb: any) => {
|
||||
const txMock = {
|
||||
insert: mockInsert, // For transaction logs
|
||||
};
|
||||
return cb(txMock);
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
describe("TradeService", () => {
|
||||
const userA = { id: "1", username: "UserA" };
|
||||
const userB = { id: "2", username: "UserB" };
|
||||
|
||||
let mockModifyUserBalance: any;
|
||||
let mockAddItem: any;
|
||||
let mockRemoveItem: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mockInsert.mockClear();
|
||||
mockValues.mockClear();
|
||||
|
||||
// Clear sessions
|
||||
(tradeService as any)._sessions.clear();
|
||||
|
||||
// Spies
|
||||
mockModifyUserBalance = spyOn(economyService, 'modifyUserBalance').mockResolvedValue({} as any);
|
||||
mockAddItem = spyOn(inventoryService, 'addItem').mockResolvedValue({} as any);
|
||||
mockRemoveItem = spyOn(inventoryService, 'removeItem').mockResolvedValue({} as any);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockModifyUserBalance.mockRestore();
|
||||
mockAddItem.mockRestore();
|
||||
mockRemoveItem.mockRestore();
|
||||
});
|
||||
|
||||
|
||||
describe("createSession", () => {
|
||||
it("should create a new session", () => {
|
||||
const session = tradeService.createSession("thread1", userA, userB);
|
||||
|
||||
expect(session.threadId).toBe("thread1");
|
||||
expect(session.state).toBe("NEGOTIATING");
|
||||
expect(session.userA.id).toBe("1");
|
||||
expect(session.userB.id).toBe("2");
|
||||
expect(tradeService.getSession("thread1")).toBe(session);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateMoney", () => {
|
||||
it("should update money offer", () => {
|
||||
tradeService.createSession("thread1", userA, userB);
|
||||
tradeService.updateMoney("thread1", "1", 100n);
|
||||
|
||||
const session = tradeService.getSession("thread1");
|
||||
expect(session?.userA.offer.money).toBe(100n);
|
||||
});
|
||||
|
||||
it("should unlock participants when offer changes", () => {
|
||||
const session = tradeService.createSession("thread1", userA, userB);
|
||||
session.userA.locked = true;
|
||||
session.userB.locked = true;
|
||||
|
||||
tradeService.updateMoney("thread1", "1", 100n);
|
||||
|
||||
expect(session.userA.locked).toBe(false);
|
||||
expect(session.userB.locked).toBe(false);
|
||||
});
|
||||
|
||||
it("should throw if not in trade", () => {
|
||||
tradeService.createSession("thread1", userA, userB);
|
||||
expect(() => tradeService.updateMoney("thread1", "3", 100n)).toThrow("User not in trade");
|
||||
});
|
||||
});
|
||||
|
||||
describe("addItem", () => {
|
||||
it("should add item to offer", () => {
|
||||
tradeService.createSession("thread1", userA, userB);
|
||||
tradeService.addItem("thread1", "1", { id: 10, name: "Sword" }, 1n);
|
||||
|
||||
const session = tradeService.getSession("thread1");
|
||||
expect(session?.userA.offer.items).toHaveLength(1);
|
||||
expect(session?.userA.offer.items[0]).toEqual({ id: 10, name: "Sword", quantity: 1n });
|
||||
});
|
||||
|
||||
it("should stack items if already offered", () => {
|
||||
tradeService.createSession("thread1", userA, userB);
|
||||
tradeService.addItem("thread1", "1", { id: 10, name: "Sword" }, 1n);
|
||||
tradeService.addItem("thread1", "1", { id: 10, name: "Sword" }, 2n);
|
||||
|
||||
const session = tradeService.getSession("thread1");
|
||||
expect(session?.userA.offer.items[0]!.quantity).toBe(3n);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removeItem", () => {
|
||||
it("should remove item from offer", () => {
|
||||
const session = tradeService.createSession("thread1", userA, userB);
|
||||
session.userA.offer.items.push({ id: 10, name: "Sword", quantity: 1n });
|
||||
|
||||
tradeService.removeItem("thread1", "1", 10);
|
||||
|
||||
expect(session.userA.offer.items).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("toggleLock", () => {
|
||||
it("should toggle lock status", () => {
|
||||
tradeService.createSession("thread1", userA, userB);
|
||||
|
||||
const locked1 = tradeService.toggleLock("thread1", "1");
|
||||
expect(locked1).toBe(true);
|
||||
|
||||
const locked2 = tradeService.toggleLock("thread1", "1");
|
||||
expect(locked2).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("executeTrade", () => {
|
||||
it("should execute trade successfully", async () => {
|
||||
const session = tradeService.createSession("thread1", userA, userB);
|
||||
|
||||
// Setup offers
|
||||
session.userA.offer.money = 100n;
|
||||
session.userA.offer.items = [{ id: 10, name: "Sword", quantity: 1n }];
|
||||
|
||||
session.userB.offer.money = 50n; // B paying 50 back? Or just swap.
|
||||
session.userB.offer.items = [];
|
||||
|
||||
// Lock both
|
||||
session.userA.locked = true;
|
||||
session.userB.locked = true;
|
||||
|
||||
await tradeService.executeTrade("thread1");
|
||||
|
||||
expect(session.state).toBe("COMPLETED");
|
||||
|
||||
// Verify Money Transfer A -> B (100)
|
||||
expect(mockModifyUserBalance).toHaveBeenCalledWith("1", -100n, 'TRADE_OUT', expect.any(String), "2", expect.anything());
|
||||
expect(mockModifyUserBalance).toHaveBeenCalledWith("2", 100n, 'TRADE_IN', expect.any(String), "1", expect.anything());
|
||||
|
||||
// Verify Money Transfer B -> A (50)
|
||||
expect(mockModifyUserBalance).toHaveBeenCalledWith("2", -50n, 'TRADE_OUT', expect.any(String), "1", expect.anything());
|
||||
expect(mockModifyUserBalance).toHaveBeenCalledWith("1", 50n, 'TRADE_IN', expect.any(String), "2", expect.anything());
|
||||
|
||||
// Verify Item Transfer A -> B (Sword)
|
||||
expect(mockRemoveItem).toHaveBeenCalledWith("1", 10, 1n, expect.anything());
|
||||
expect(mockAddItem).toHaveBeenCalledWith("2", 10, 1n, expect.anything());
|
||||
|
||||
// Verify DB Logs (Item Transaction)
|
||||
// 2 calls (sender log, receiver log) for 1 item
|
||||
expect(mockInsert).toHaveBeenCalledTimes(2);
|
||||
expect(mockInsert).toHaveBeenCalledWith(itemTransactions);
|
||||
});
|
||||
|
||||
it("should throw if not locked", async () => {
|
||||
const session = tradeService.createSession("thread1", userA, userB);
|
||||
session.userA.locked = true;
|
||||
// B not locked
|
||||
|
||||
expect(tradeService.executeTrade("thread1")).rejects.toThrow("Both players must accept");
|
||||
});
|
||||
});
|
||||
});
|
||||
200
shared/modules/trade/trade.service.ts
Normal file
200
shared/modules/trade/trade.service.ts
Normal file
@@ -0,0 +1,200 @@
|
||||
import type { TradeSession, TradeParticipant } from "./trade.types";
|
||||
import { economyService } from "@shared/modules/economy/economy.service";
|
||||
import { inventoryService } from "@shared/modules/inventory/inventory.service";
|
||||
import { itemTransactions } from "@db/schema";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import type { Transaction } from "@shared/lib/types";
|
||||
import { TransactionType, ItemTransactionType } from "@shared/lib/constants";
|
||||
|
||||
// Module-level session storage
|
||||
const sessions = new Map<string, TradeSession>();
|
||||
|
||||
/**
|
||||
* Unlocks both participants in a trade session
|
||||
*/
|
||||
const unlockAll = (session: TradeSession) => {
|
||||
session.userA.locked = false;
|
||||
session.userB.locked = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Processes a one-way transfer from one participant to another
|
||||
*/
|
||||
const processTransfer = async (tx: Transaction, from: TradeParticipant, to: TradeParticipant, threadId: string) => {
|
||||
// 1. Money
|
||||
if (from.offer.money > 0n) {
|
||||
await economyService.modifyUserBalance(
|
||||
from.id,
|
||||
-from.offer.money,
|
||||
TransactionType.TRADE_OUT,
|
||||
`Trade with ${to.username} (Thread: ${threadId})`,
|
||||
to.id,
|
||||
tx
|
||||
);
|
||||
await economyService.modifyUserBalance(
|
||||
to.id,
|
||||
from.offer.money,
|
||||
TransactionType.TRADE_IN,
|
||||
`Trade with ${from.username} (Thread: ${threadId})`,
|
||||
from.id,
|
||||
tx
|
||||
);
|
||||
}
|
||||
|
||||
// 2. Items
|
||||
for (const item of from.offer.items) {
|
||||
// Remove from sender
|
||||
await inventoryService.removeItem(from.id, item.id, item.quantity, tx);
|
||||
|
||||
// Add to receiver
|
||||
await inventoryService.addItem(to.id, item.id, item.quantity, tx);
|
||||
|
||||
// Log Item Transaction (Sender)
|
||||
await tx.insert(itemTransactions).values({
|
||||
userId: BigInt(from.id),
|
||||
relatedUserId: BigInt(to.id),
|
||||
itemId: item.id,
|
||||
quantity: -item.quantity,
|
||||
type: ItemTransactionType.TRADE_OUT,
|
||||
description: `Traded to ${to.username}`,
|
||||
});
|
||||
|
||||
// Log Item Transaction (Receiver)
|
||||
await tx.insert(itemTransactions).values({
|
||||
userId: BigInt(to.id),
|
||||
relatedUserId: BigInt(from.id),
|
||||
itemId: item.id,
|
||||
quantity: item.quantity,
|
||||
type: ItemTransactionType.TRADE_IN,
|
||||
description: `Received from ${from.username}`,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const tradeService = {
|
||||
// Expose for testing
|
||||
_sessions: sessions,
|
||||
/**
|
||||
* Creates a new trade session
|
||||
*/
|
||||
createSession: (threadId: string, userA: { id: string, username: string }, userB: { id: string, username: string }): TradeSession => {
|
||||
const session: TradeSession = {
|
||||
threadId,
|
||||
userA: {
|
||||
id: userA.id,
|
||||
username: userA.username,
|
||||
locked: false,
|
||||
offer: { money: 0n, items: [] }
|
||||
},
|
||||
userB: {
|
||||
id: userB.id,
|
||||
username: userB.username,
|
||||
locked: false,
|
||||
offer: { money: 0n, items: [] }
|
||||
},
|
||||
state: 'NEGOTIATING',
|
||||
lastInteraction: Date.now()
|
||||
};
|
||||
|
||||
sessions.set(threadId, session);
|
||||
return session;
|
||||
},
|
||||
|
||||
getSession: (threadId: string): TradeSession | undefined => {
|
||||
return sessions.get(threadId);
|
||||
},
|
||||
|
||||
endSession: (threadId: string) => {
|
||||
sessions.delete(threadId);
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates an offer. If allowed, validation checks should be done BEFORE calling this.
|
||||
* unlocking logic is handled here (if offer changes, unlock both).
|
||||
*/
|
||||
updateMoney: (threadId: string, userId: string, amount: bigint) => {
|
||||
const session = tradeService.getSession(threadId);
|
||||
if (!session) throw new Error("Session not found");
|
||||
if (session.state !== 'NEGOTIATING') throw new Error("Trade is not active");
|
||||
|
||||
const participant = session.userA.id === userId ? session.userA : session.userB.id === userId ? session.userB : null;
|
||||
if (!participant) throw new Error("User not in trade");
|
||||
|
||||
participant.offer.money = amount;
|
||||
unlockAll(session);
|
||||
session.lastInteraction = Date.now();
|
||||
},
|
||||
|
||||
addItem: (threadId: string, userId: string, item: { id: number, name: string }, quantity: bigint) => {
|
||||
const session = tradeService.getSession(threadId);
|
||||
if (!session) throw new Error("Session not found");
|
||||
if (session.state !== 'NEGOTIATING') throw new Error("Trade is not active");
|
||||
|
||||
const participant = session.userA.id === userId ? session.userA : session.userB.id === userId ? session.userB : null;
|
||||
if (!participant) throw new Error("User not in trade");
|
||||
|
||||
const existing = participant.offer.items.find(i => i.id === item.id);
|
||||
if (existing) {
|
||||
existing.quantity += quantity;
|
||||
} else {
|
||||
participant.offer.items.push({ id: item.id, name: item.name, quantity });
|
||||
}
|
||||
|
||||
unlockAll(session);
|
||||
session.lastInteraction = Date.now();
|
||||
},
|
||||
|
||||
removeItem: (threadId: string, userId: string, itemId: number) => {
|
||||
const session = tradeService.getSession(threadId);
|
||||
if (!session) throw new Error("Session not found");
|
||||
|
||||
const participant = session.userA.id === userId ? session.userA : session.userB.id === userId ? session.userB : null;
|
||||
if (!participant) throw new Error("User not in trade");
|
||||
|
||||
participant.offer.items = participant.offer.items.filter(i => i.id !== itemId);
|
||||
|
||||
unlockAll(session);
|
||||
session.lastInteraction = Date.now();
|
||||
},
|
||||
|
||||
toggleLock: (threadId: string, userId: string): boolean => {
|
||||
const session = tradeService.getSession(threadId);
|
||||
if (!session) throw new Error("Session not found");
|
||||
|
||||
const participant = session.userA.id === userId ? session.userA : session.userB.id === userId ? session.userB : null;
|
||||
if (!participant) throw new Error("User not in trade");
|
||||
|
||||
participant.locked = !participant.locked;
|
||||
session.lastInteraction = Date.now();
|
||||
|
||||
return participant.locked;
|
||||
},
|
||||
|
||||
/**
|
||||
* Executes the trade atomically.
|
||||
* 1. Validates balances/inventory for both users.
|
||||
* 2. Swaps money.
|
||||
* 3. Swaps items.
|
||||
* 4. Logs transactions.
|
||||
*/
|
||||
executeTrade: async (threadId: string): Promise<void> => {
|
||||
const session = tradeService.getSession(threadId);
|
||||
if (!session) throw new Error("Session not found");
|
||||
|
||||
if (!session.userA.locked || !session.userB.locked) {
|
||||
throw new Error("Both players must accept the trade first.");
|
||||
}
|
||||
|
||||
session.state = 'COMPLETED'; // Prevent double execution
|
||||
|
||||
await withTransaction(async (tx) => {
|
||||
// -- Validate & Execute User A -> User B --
|
||||
await processTransfer(tx, session.userA, session.userB, session.threadId);
|
||||
|
||||
// -- Validate & Execute User B -> User A --
|
||||
await processTransfer(tx, session.userB, session.userA, session.threadId);
|
||||
});
|
||||
|
||||
tradeService.endSession(threadId);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user