Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-6459 Replace passing error handling in tldraw #5053

Merged
merged 18 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { TextEncoder } from 'util';
import { INestApplication, NotAcceptableException } from '@nestjs/common';
import { MongoMemoryDatabaseModule } from '@infra/database';
import { createConfigModuleOptions } from '@src/config';
import { Logger } from '@src/core/logger';
import { of, throwError } from 'rxjs';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HttpService } from '@nestjs/axios';
import { AxiosError, AxiosHeaders, AxiosResponse } from 'axios';
import { axiosResponseFactory } from '@shared/testing';
import { CoreModule } from '@src/core';
import { TldrawRedisFactory, TldrawRedisService } from '../../redis';
import { TldrawDrawing } from '../../entities';
import { TldrawWsService } from '../../service';
Expand All @@ -22,6 +22,7 @@ import { TldrawWs } from '..';
import { WsCloseCode, WsCloseMessage } from '../../types';
import { TldrawConfig } from '../../config';

// This is a unit test, no api test...need to be refactored
describe('WebSocketController (WsAdapter)', () => {
let app: INestApplication;
let gateway: TldrawWs;
Expand All @@ -41,6 +42,7 @@ describe('WebSocketController (WsAdapter)', () => {
imports: [
MongoMemoryDatabaseModule.forRoot({ entities: [TldrawDrawing] }),
ConfigModule.forRoot(createConfigModuleOptions(tldrawTestConfig)),
CoreModule,
],
providers: [
TldrawWs,
Expand All @@ -54,10 +56,6 @@ describe('WebSocketController (WsAdapter)', () => {
provide: TldrawRepo,
useValue: createMock<TldrawRepo>(),
},
{
provide: Logger,
useValue: createMock<Logger>(),
},
{
provide: HttpService,
useValue: createMock<HttpService>(),
Expand All @@ -79,7 +77,7 @@ describe('WebSocketController (WsAdapter)', () => {
});

afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
CeEv marked this conversation as resolved.
Show resolved Hide resolved
});

describe('when tldraw connection is established', () => {
Expand Down
6 changes: 3 additions & 3 deletions apps/server/src/modules/tldraw/controller/tldraw.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
NotFoundException,
NotAcceptableException,
} from '@nestjs/common';
import { Logger } from '@src/core/logger';
import { isAxiosError } from 'axios';
import { firstValueFrom } from 'rxjs';
import { HttpService } from '@nestjs/axios';
import { DomainErrorHandler } from '@src/core';
import { WebsocketInitErrorLoggable } from '../loggable';
import { TldrawConfig, TLDRAW_SOCKET_PORT } from '../config';
import { WsCloseCode, WsCloseMessage } from '../types';
Expand All @@ -27,7 +27,7 @@ export class TldrawWs implements OnGatewayInit, OnGatewayConnection {
private readonly configService: ConfigService<TldrawConfig, true>,
private readonly tldrawWsService: TldrawWsService,
private readonly httpService: HttpService,
private readonly logger: Logger
private readonly domainErrorHandler: DomainErrorHandler
) {}

public async handleConnection(client: WebSocket, request: Request): Promise<void> {
Expand Down Expand Up @@ -106,7 +106,7 @@ export class TldrawWs implements OnGatewayInit, OnGatewayConnection {
err?: unknown
): void {
client.close(code, message);
this.logger.warning(new WebsocketInitErrorLoggable(code, message, docName, err));
this.domainErrorHandler.exec(new WebsocketInitErrorLoggable(code, message, docName, err));
}

private handleError(err: unknown, client: WebSocket, docName: string): void {
Expand Down
18 changes: 4 additions & 14 deletions apps/server/src/modules/tldraw/redis/tldraw-redis.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { INestApplication } from '@nestjs/common';
import { createMock } from '@golevelup/ts-jest';
import { Logger } from '@src/core/logger';
import { Test } from '@nestjs/testing';
Expand All @@ -7,8 +6,6 @@ import { createConfigModuleOptions } from '@src/config';
import * as Yjs from 'yjs';
import * as AwarenessProtocol from 'y-protocols/awareness';
import { HttpService } from '@nestjs/axios';
import { WsAdapter } from '@nestjs/platform-ws';
import { TldrawWs } from '../controller';
import { TldrawWsService } from '../service';
import { TldrawBoardRepo, TldrawRepo, YMongodb } from '../repo';
import { MetricsService } from '../metrics';
Expand Down Expand Up @@ -39,19 +36,20 @@ jest.mock('y-protocols/sync', () => {
});

describe('TldrawRedisService', () => {
let app: INestApplication;
let service: TldrawRedisService;

beforeAll(async () => {
const testingModule = await Test.createTestingModule({
imports: [ConfigModule.forRoot(createConfigModuleOptions(tldrawTestConfig))],
providers: [
TldrawWs,
TldrawWsService,
YMongodb,
MetricsService,
TldrawRedisFactory,
TldrawRedisService,
{
provide: TldrawWsService,
useValue: createMock<TldrawWsService>(),
},
{
provide: TldrawBoardRepo,
useValue: createMock<TldrawBoardRepo>(),
Expand All @@ -72,17 +70,9 @@ describe('TldrawRedisService', () => {
}).compile();

service = testingModule.get(TldrawRedisService);
app = testingModule.createNestApplication();
app.useWebSocketAdapter(new WsAdapter(app));
await app.init();
});

afterAll(async () => {
await app.close();
});

afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

Expand Down
32 changes: 3 additions & 29 deletions apps/server/src/modules/tldraw/repo/tldraw-board.repo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { WsAdapter } from '@nestjs/platform-ws';
import { Doc } from 'yjs';
import { createMock } from '@golevelup/ts-jest';
import { HttpService } from '@nestjs/axios';
import { Logger } from '@src/core/logger';
import { ConfigModule } from '@nestjs/config';
import { MongoMemoryDatabaseModule } from '@infra/database';
import { createConfigModuleOptions } from '@src/config';
import { TldrawBoardRepo } from './tldraw-board.repo';
import { WsSharedDocDo } from '../domain';
import { TldrawWsService } from '../service';
import { tldrawTestConfig } from '../testing';
import { TldrawDrawing } from '../entities';
import { TldrawWs } from '../controller';
import { MetricsService } from '../metrics';
import { TldrawRepo } from './tldraw.repo';
import { YMongodb } from './y-mongodb';
import { TldrawRedisFactory, TldrawRedisService } from '../redis';

describe('TldrawBoardRepo', () => {
let app: INestApplication;
let repo: TldrawBoardRepo;

beforeAll(async () => {
Expand All @@ -30,42 +21,25 @@ describe('TldrawBoardRepo', () => {
ConfigModule.forRoot(createConfigModuleOptions(tldrawTestConfig)),
],
providers: [
TldrawWs,
TldrawWsService,
TldrawBoardRepo,
YMongodb,
MetricsService,
TldrawRedisFactory,
TldrawRedisService,
{
provide: TldrawRepo,
useValue: createMock<TldrawRepo>(),
provide: YMongodb,
useValue: createMock<YMongodb>(),
},
{
provide: Logger,
useValue: createMock<Logger>(),
},
{
provide: HttpService,
useValue: createMock<HttpService>(),
},
],
}).compile();

repo = testingModule.get(TldrawBoardRepo);
app = testingModule.createNestApplication();
app.useWebSocketAdapter(new WsAdapter(app));
await app.init();

jest.useFakeTimers();
});

afterEach(() => {
jest.resetAllMocks();
});

afterAll(async () => {
await app.close();
jest.restoreAllMocks();
CeEv marked this conversation as resolved.
Show resolved Hide resolved
});

it('should check if repo and its properties are set correctly', () => {
Expand Down
9 changes: 4 additions & 5 deletions apps/server/src/modules/tldraw/repo/tldraw.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { EntityManager } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { cleanupCollections } from '@shared/testing';
import { MikroORM } from '@mikro-orm/core';
import { ConfigModule } from '@nestjs/config';
import { createConfigModuleOptions } from '@src/config';
import { tldrawEntityFactory, tldrawTestConfig } from '../testing';
import { MongoMemoryDatabaseModule } from '@src/infra/database';
import { tldrawEntityFactory } from '../testing';
import { TldrawDrawing } from '../entities';
import { TldrawRepo } from './tldraw.repo';
import { TldrawWsTestModule } from '../tldraw-ws-test.module';

describe('TldrawRepo', () => {
let testingModule: TestingModule;
Expand All @@ -17,7 +15,8 @@ describe('TldrawRepo', () => {

beforeAll(async () => {
testingModule = await Test.createTestingModule({
imports: [TldrawWsTestModule, ConfigModule.forRoot(createConfigModuleOptions(tldrawTestConfig))],
imports: [MongoMemoryDatabaseModule.forRoot({ entities: [TldrawDrawing] })],
providers: [TldrawRepo],
}).compile();

repo = testingModule.get(TldrawRepo);
Expand Down
33 changes: 11 additions & 22 deletions apps/server/src/modules/tldraw/repo/y-mongodb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ import { Logger } from '@src/core/logger';
import { createMock } from '@golevelup/ts-jest';
import * as Yjs from 'yjs';
import { createConfigModuleOptions } from '@src/config';
import { HttpService } from '@nestjs/axios';
import { TldrawRedisFactory, TldrawRedisService } from '../redis';
import { tldrawEntityFactory, tldrawTestConfig } from '../testing';
import { TldrawDrawing } from '../entities';
import { TldrawWs } from '../controller';
import { TldrawWsService } from '../service';
import { MetricsService } from '../metrics';
import { TldrawBoardRepo } from './tldraw-board.repo';
import { TldrawRepo } from './tldraw.repo';
import { YMongodb } from './y-mongodb';
import { Version } from './key.factory';

jest.mock('yjs', () => {
const moduleMock: unknown = {
Expand All @@ -39,22 +34,12 @@ describe('YMongoDb', () => {
ConfigModule.forRoot(createConfigModuleOptions(tldrawTestConfig)),
],
providers: [
TldrawWs,
TldrawWsService,
TldrawBoardRepo,
TldrawRepo,
YMongodb,
MetricsService,
TldrawRedisFactory,
TldrawRedisService,
TldrawRepo,
{
provide: Logger,
useValue: createMock<Logger>(),
},
{
provide: HttpService,
useValue: createMock<HttpService>(),
},
],
}).compile();

Expand Down Expand Up @@ -168,20 +153,24 @@ describe('YMongoDb', () => {

describe('getAllDocumentNames', () => {
const setup = async () => {
const drawing1 = tldrawEntityFactory.build({ docName: 'test-name1', version: 'v1_sv' });
const drawing2 = tldrawEntityFactory.build({ docName: 'test-name2', version: 'v1_sv' });
const drawing3 = tldrawEntityFactory.build({ docName: 'test-name3', version: 'v1_sv' });
const drawing1 = tldrawEntityFactory.build({ docName: 'test-name1', version: Version.V1_SV });
const drawing2 = tldrawEntityFactory.build({ docName: 'test-name2', version: Version.V1_SV });
const drawing3 = tldrawEntityFactory.build({ docName: 'test-name3', version: Version.V1_SV });

await em.persistAndFlush([drawing1, drawing2, drawing3]);
em.clear();

return {
expectedDocNames: [drawing1.docName, drawing2.docName, drawing3.docName],
};
};

it('should return all document names', async () => {
await setup();
const { expectedDocNames } = await setup();

const docNames = await mdb.getAllDocumentNames();

expect(docNames).toEqual(['test-name1', 'test-name2', 'test-name3']);
expect(docNames).toEqual(expectedDocNames);
});
});

Expand Down
Loading
Loading