test: update tests for WS perf and auth changes

This commit is contained in:
2026-03-29 19:32:01 +08:00
parent a13e8d1c35
commit c2a6783f26
2 changed files with 19 additions and 21 deletions

View File

@@ -22,6 +22,7 @@ describe('UsersController', () => {
const mockAuthUser: AuthenticatedUser = { const mockAuthUser: AuthenticatedUser = {
userId: 'uuid-123', userId: 'uuid-123',
email: 'test@example.com', email: 'test@example.com',
tokenType: 'access',
roles: ['user'], roles: ['user'],
}; };

View File

@@ -3,7 +3,6 @@ import { Test, TestingModule } from '@nestjs/testing';
import { StateGateway } from './state.gateway'; import { StateGateway } from './state.gateway';
import { AuthenticatedSocket } from '../../types/socket'; import { AuthenticatedSocket } from '../../types/socket';
import { JwtVerificationService } from '../../auth/services/jwt-verification.service'; import { JwtVerificationService } from '../../auth/services/jwt-verification.service';
import { UsersService } from '../../users/users.service';
import { PrismaService } from '../../database/prisma.service'; import { PrismaService } from '../../database/prisma.service';
import { UserSocketService } from './user-socket.service'; import { UserSocketService } from './user-socket.service';
import { WsNotificationService } from './ws-notification.service'; import { WsNotificationService } from './ws-notification.service';
@@ -23,6 +22,8 @@ type MockSocket = {
userId?: string; userId?: string;
activeDollId?: string | null; activeDollId?: string | null;
friends?: Set<string>; friends?: Set<string>;
senderName?: string;
senderNameCachedAt?: number;
}; };
handshake?: any; handshake?: any;
disconnect?: jest.Mock; disconnect?: jest.Mock;
@@ -39,7 +40,6 @@ describe('StateGateway', () => {
sockets: { sockets: { size: number; get: jest.Mock } }; sockets: { sockets: { size: number; get: jest.Mock } };
to: jest.Mock; to: jest.Mock;
}; };
let mockUsersService: Partial<UsersService>;
let mockJwtVerificationService: Partial<JwtVerificationService>; let mockJwtVerificationService: Partial<JwtVerificationService>;
let mockPrismaService: Partial<PrismaService>; let mockPrismaService: Partial<PrismaService>;
let mockUserSocketService: Partial<UserSocketService>; let mockUserSocketService: Partial<UserSocketService>;
@@ -67,12 +67,6 @@ describe('StateGateway', () => {
}), }),
}; };
mockUsersService = {
findOne: jest.fn().mockResolvedValue({
id: 'user-id',
}),
};
mockJwtVerificationService = { mockJwtVerificationService = {
extractToken: jest.fn((handshake) => handshake.auth?.token), extractToken: jest.fn((handshake) => handshake.auth?.token),
verifyToken: jest.fn().mockReturnValue({ verifyToken: jest.fn().mockReturnValue({
@@ -83,7 +77,12 @@ describe('StateGateway', () => {
mockPrismaService = { mockPrismaService = {
user: { user: {
findUnique: jest.fn().mockResolvedValue({ activeDollId: 'doll-123' }), findUnique: jest.fn().mockResolvedValue({
id: 'user-id',
name: 'Test User',
username: 'test-user',
activeDollId: 'doll-123',
}),
} as any, } as any,
friendship: { friendship: {
findMany: jest.fn().mockResolvedValue([]), findMany: jest.fn().mockResolvedValue([]),
@@ -119,7 +118,6 @@ describe('StateGateway', () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
providers: [ providers: [
StateGateway, StateGateway,
{ provide: UsersService, useValue: mockUsersService },
{ {
provide: JwtVerificationService, provide: JwtVerificationService,
useValue: mockJwtVerificationService, useValue: mockJwtVerificationService,
@@ -190,7 +188,6 @@ describe('StateGateway', () => {
); );
// Should NOT call these anymore in handleConnection // Should NOT call these anymore in handleConnection
expect(mockUsersService.findOne).not.toHaveBeenCalled();
expect(mockUserSocketService.setSocket).not.toHaveBeenCalled(); expect(mockUserSocketService.setSocket).not.toHaveBeenCalled();
// Should set data on client // Should set data on client
@@ -244,6 +241,9 @@ describe('StateGateway', () => {
// Mock Prisma responses // Mock Prisma responses
(mockPrismaService.user!.findUnique as jest.Mock).mockResolvedValue({ (mockPrismaService.user!.findUnique as jest.Mock).mockResolvedValue({
id: 'user-id',
name: 'Test User',
username: 'test-user',
activeDollId: 'doll-123', activeDollId: 'doll-123',
}); });
(mockPrismaService.friendship!.findMany as jest.Mock).mockResolvedValue([ (mockPrismaService.friendship!.findMany as jest.Mock).mockResolvedValue([
@@ -255,32 +255,29 @@ describe('StateGateway', () => {
mockClient as unknown as AuthenticatedSocket, mockClient as unknown as AuthenticatedSocket,
); );
// 1. Load User // 1. Set Socket
expect(mockUsersService.findOne).toHaveBeenCalledWith('test-sub');
// 2. Set Socket
expect(mockUserSocketService.setSocket).toHaveBeenCalledWith( expect(mockUserSocketService.setSocket).toHaveBeenCalledWith(
'user-id', 'user-id',
'client1', 'client1',
); );
// 3. Fetch State (DB) // 2. Fetch State (DB)
expect(mockPrismaService.user!.findUnique).toHaveBeenCalledWith({ expect(mockPrismaService.user!.findUnique).toHaveBeenCalledWith({
where: { id: 'user-id' }, where: { id: 'test-sub' },
select: { activeDollId: true }, select: { id: true, name: true, username: true, activeDollId: true },
}); });
expect(mockPrismaService.friendship!.findMany).toHaveBeenCalledWith({ expect(mockPrismaService.friendship!.findMany).toHaveBeenCalledWith({
where: { userId: 'user-id' }, where: { userId: 'test-sub' },
select: { friendId: true }, select: { friendId: true },
}); });
// 4. Update Client Data // 3. Update Client Data
expect(mockClient.data.userId).toBe('user-id'); expect(mockClient.data.userId).toBe('user-id');
expect(mockClient.data.activeDollId).toBe('doll-123'); expect(mockClient.data.activeDollId).toBe('doll-123');
expect(mockClient.data.friends).toContain('friend-1'); expect(mockClient.data.friends).toContain('friend-1');
expect(mockClient.data.friends).toContain('friend-2'); expect(mockClient.data.friends).toContain('friend-2');
// 5. Emit Initialized // 4. Emit Initialized
expect(mockClient.emit).toHaveBeenCalledWith('initialized', { expect(mockClient.emit).toHaveBeenCalledWith('initialized', {
userId: 'user-id', userId: 'user-id',
activeDollId: 'doll-123', activeDollId: 'doll-123',