app.e2e-spec.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { INestApplication } from '@nestjs/common';
  2. import { Test, TestingModule } from '@nestjs/testing';
  3. import { io, Socket } from 'socket.io-client';
  4. import request from 'supertest';
  5. import { App } from 'supertest/types';
  6. import { AppModule } from './../src/app.module';
  7. // Mock chokidar to avoid ES module issues
  8. jest.mock('chokidar', () => ({
  9. watch: jest.fn(),
  10. }));
  11. describe('AppController (e2e)', () => {
  12. let app: INestApplication<App>;
  13. let clientSocket: Socket;
  14. beforeEach(async () => {
  15. const moduleFixture: TestingModule = await Test.createTestingModule({
  16. imports: [AppModule],
  17. }).compile();
  18. app = moduleFixture.createNestApplication();
  19. await app.listen(3001);
  20. });
  21. afterEach(async () => {
  22. if (clientSocket) {
  23. clientSocket.disconnect();
  24. }
  25. await app.close();
  26. });
  27. it('/ (GET)', () => {
  28. return request(app.getHttpServer())
  29. .get('/')
  30. .expect(200)
  31. .expect((res) => {
  32. expect(res.body).toHaveProperty('status', 'ok');
  33. });
  34. });
  35. describe('WebSocket Integration', () => {
  36. beforeEach((done) => {
  37. clientSocket = io('http://localhost:3001');
  38. clientSocket.on('connect', () => {
  39. done();
  40. });
  41. });
  42. it('should connect to WebSocket', () => {
  43. expect(clientSocket.connected).toBe(true);
  44. });
  45. it('should handle join message', (done) => {
  46. const room = 'testRoom';
  47. clientSocket.emit('join', { room });
  48. clientSocket.on('joined', (data) => {
  49. expect(data).toEqual({ room });
  50. done();
  51. });
  52. });
  53. it('should handle leave message', (done) => {
  54. const room = 'testRoom';
  55. clientSocket.emit('join', { room });
  56. clientSocket.on('joined', () => {
  57. clientSocket.emit('leave', { room });
  58. clientSocket.on('left', (data) => {
  59. expect(data).toEqual({ room });
  60. done();
  61. });
  62. });
  63. });
  64. it('should emit taskUpdate on settings change', (done) => {
  65. const settings = { testKey: 'testValue' };
  66. clientSocket.on('taskUpdate', (data) => {
  67. expect(data).toEqual({
  68. type: 'settings',
  69. task: 'update',
  70. settings,
  71. });
  72. done();
  73. });
  74. // Trigger settings update via HTTP
  75. request(app.getHttpServer())
  76. .post('/config/settings')
  77. .send(settings)
  78. .expect(201)
  79. .end((err) => {
  80. if (err) {
  81. done(err);
  82. }
  83. // The WebSocket event should have been emitted by now
  84. });
  85. });
  86. });
  87. });