updated test
This commit is contained in:
@@ -4,12 +4,16 @@ import { AuthenticatedSocket } from '../../types/socket';
|
|||||||
import { AuthService } from '../../auth/auth.service';
|
import { AuthService } from '../../auth/auth.service';
|
||||||
import { JwtVerificationService } from '../../auth/services/jwt-verification.service';
|
import { JwtVerificationService } from '../../auth/services/jwt-verification.service';
|
||||||
|
|
||||||
|
import { FriendsService } from '../../friends/friends.service';
|
||||||
|
|
||||||
interface MockSocket extends Partial<AuthenticatedSocket> {
|
interface MockSocket extends Partial<AuthenticatedSocket> {
|
||||||
id: string;
|
id: string;
|
||||||
data: {
|
data: {
|
||||||
user?: {
|
user?: {
|
||||||
keycloakSub: string;
|
keycloakSub: string;
|
||||||
};
|
};
|
||||||
|
userId?: string;
|
||||||
|
friends?: Set<string>;
|
||||||
};
|
};
|
||||||
handshake?: any;
|
handshake?: any;
|
||||||
disconnect?: jest.Mock;
|
disconnect?: jest.Mock;
|
||||||
@@ -20,9 +24,13 @@ describe('StateGateway', () => {
|
|||||||
let mockLoggerLog: jest.SpyInstance;
|
let mockLoggerLog: jest.SpyInstance;
|
||||||
let mockLoggerDebug: jest.SpyInstance;
|
let mockLoggerDebug: jest.SpyInstance;
|
||||||
let mockLoggerWarn: jest.SpyInstance;
|
let mockLoggerWarn: jest.SpyInstance;
|
||||||
let mockServer: { sockets: { sockets: { size: number } } };
|
let mockServer: {
|
||||||
|
sockets: { sockets: { size: number } };
|
||||||
|
to: jest.Mock;
|
||||||
|
};
|
||||||
let mockAuthService: Partial<AuthService>;
|
let mockAuthService: Partial<AuthService>;
|
||||||
let mockJwtVerificationService: Partial<JwtVerificationService>;
|
let mockJwtVerificationService: Partial<JwtVerificationService>;
|
||||||
|
let mockFriendsService: Partial<FriendsService>;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
mockServer = {
|
mockServer = {
|
||||||
@@ -31,7 +39,10 @@ describe('StateGateway', () => {
|
|||||||
size: 5,
|
size: 5,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
to: jest.fn().mockReturnValue({
|
||||||
|
emit: jest.fn(),
|
||||||
|
}),
|
||||||
|
} as any;
|
||||||
|
|
||||||
mockAuthService = {
|
mockAuthService = {
|
||||||
syncUserFromToken: jest.fn().mockResolvedValue({
|
syncUserFromToken: jest.fn().mockResolvedValue({
|
||||||
@@ -48,6 +59,10 @@ describe('StateGateway', () => {
|
|||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mockFriendsService = {
|
||||||
|
getFriends: jest.fn().mockResolvedValue([]),
|
||||||
|
};
|
||||||
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [
|
providers: [
|
||||||
StateGateway,
|
StateGateway,
|
||||||
@@ -56,6 +71,7 @@ describe('StateGateway', () => {
|
|||||||
provide: JwtVerificationService,
|
provide: JwtVerificationService,
|
||||||
useValue: mockJwtVerificationService,
|
useValue: mockJwtVerificationService,
|
||||||
},
|
},
|
||||||
|
{ provide: FriendsService, useValue: mockFriendsService },
|
||||||
],
|
],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
@@ -174,24 +190,82 @@ describe('StateGateway', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('handleCursorReportPosition', () => {
|
describe('handleCursorReportPosition', () => {
|
||||||
it('should log message received from authenticated client', () => {
|
it('should emit cursor position to connected friends', () => {
|
||||||
const mockClient: MockSocket = {
|
const mockClient: MockSocket = {
|
||||||
id: 'client1',
|
id: 'client1',
|
||||||
data: { user: { keycloakSub: 'test-sub' } },
|
data: {
|
||||||
|
user: { keycloakSub: 'test-sub' },
|
||||||
|
userId: 'user-1',
|
||||||
|
friends: new Set(['friend-1']),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
const data = { x: 100, y: 200 };
|
|
||||||
|
// Setup the userSocketMap to simulate a connected friend
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||||
|
(gateway as any).userSocketMap.set('friend-1', 'friend-socket-id');
|
||||||
|
|
||||||
|
const data = { x: 100, y: 200, isDrawing: false };
|
||||||
|
|
||||||
gateway.handleCursorReportPosition(
|
gateway.handleCursorReportPosition(
|
||||||
mockClient as unknown as AuthenticatedSocket,
|
mockClient as unknown as AuthenticatedSocket,
|
||||||
data,
|
data,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(mockLoggerLog).toHaveBeenCalledWith(
|
// Verify that the message was emitted to the friend
|
||||||
`Message received from client id: ${mockClient.id} (user: test-sub)`,
|
expect(mockServer.to).toHaveBeenCalledWith('friend-socket-id');
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||||
|
const emitMock = mockServer.to().emit as jest.Mock;
|
||||||
|
expect(emitMock).toHaveBeenCalledWith('friend-cursor-position', {
|
||||||
|
userId: 'user-1',
|
||||||
|
position: data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not emit when no friends are online', () => {
|
||||||
|
const mockClient: MockSocket = {
|
||||||
|
id: 'client1',
|
||||||
|
data: {
|
||||||
|
user: { keycloakSub: 'test-sub' },
|
||||||
|
userId: 'user-1',
|
||||||
|
friends: new Set(['friend-1']),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Don't set up userSocketMap - friend is not online
|
||||||
|
const data = { x: 100, y: 200, isDrawing: false };
|
||||||
|
|
||||||
|
gateway.handleCursorReportPosition(
|
||||||
|
mockClient as unknown as AuthenticatedSocket,
|
||||||
|
data,
|
||||||
);
|
);
|
||||||
expect(mockLoggerDebug).toHaveBeenCalledWith(
|
|
||||||
`Payload: ${JSON.stringify(data, null, 0)}`,
|
// Verify that no message was emitted
|
||||||
|
expect(mockServer.to).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should log warning when userId is missing', () => {
|
||||||
|
const mockClient: MockSocket = {
|
||||||
|
id: 'client1',
|
||||||
|
data: {
|
||||||
|
user: { keycloakSub: 'test-sub' },
|
||||||
|
// userId is missing
|
||||||
|
friends: new Set(['friend-1']),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const data = { x: 100, y: 200, isDrawing: false };
|
||||||
|
|
||||||
|
gateway.handleCursorReportPosition(
|
||||||
|
mockClient as unknown as AuthenticatedSocket,
|
||||||
|
data,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Verify that a warning was logged
|
||||||
|
expect(mockLoggerWarn).toHaveBeenCalledWith(
|
||||||
|
`Could not find user ID for client ${mockClient.id}`,
|
||||||
|
);
|
||||||
|
// Verify that no message was emitted
|
||||||
|
expect(mockServer.to).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw exception when client is not authenticated', () => {
|
it('should throw exception when client is not authenticated', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user