Skip to content

Commit

Permalink
Fixed vscode settings for prettier
Browse files Browse the repository at this point in the history
Added a test for start/stop monitored vessels fetching
  • Loading branch information
JonasGLund99 committed Nov 6, 2024
1 parent 122ff0c commit 87a9236
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 80 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
50 changes: 25 additions & 25 deletions src/__tests__/ClientHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import GRPCClientHandler from '../implementations/GRPCClientHandler';
import { AISServiceClientImpl } from '../../proto/AIS-protobuf/ais';
import GRPCClientHandler from '../implementations/GRPCClientHandler'
import { AISServiceClientImpl, VesselInfoResponse } from '../../proto/AIS-protobuf/ais'

// Create a mock implementation of Rpc
const mockClient: jest.Mocked<AISServiceClientImpl> = {
GetVesselInfo: jest.fn(),
} as unknown as jest.Mocked<AISServiceClientImpl>;
GetVesselInfo: jest.fn(),
} as unknown as jest.Mocked<AISServiceClientImpl>

jest.mock('../../proto/AIS-protobuf/ais');
jest.mock('../../proto/AIS-protobuf/ais')

describe('GRPCClientHandler', () => {
let clientHandler: GRPCClientHandler;
let clientHandler: GRPCClientHandler

beforeEach(() => {
clientHandler = new GRPCClientHandler(mockClient);
});
beforeEach(() => {
clientHandler = new GRPCClientHandler(mockClient)
})

it('should return detailed vessel information', async () => {
const mockResponse = { mmsi: 123456789, name: 'Test Vessel', shipType: 'Cargo' } as any; // Mock the actual response
mockClient.GetVesselInfo.mockResolvedValue(mockResponse);
it('should return detailed vessel information', async () => {
const mockResponse = { mmsi: 123456789, name: 'Test Vessel', shipType: 'Cargo' } as VesselInfoResponse // Mock the actual response
mockClient.GetVesselInfo.mockResolvedValue(mockResponse)

const result = await clientHandler.getVesselInfo({ mmsi: 123456789, timestamp: 1609459200 });
expect(result).toEqual({
mmsi: 123456789,
name: 'Test Vessel',
shipType: 'Cargo',
imo: undefined,
callSign: undefined,
width: undefined,
length: undefined,
positionFixingDevice: undefined,
});
});
});
const result = await clientHandler.getVesselInfo({ mmsi: 123456789, timestamp: 1609459200 })
expect(result).toEqual({
mmsi: 123456789,
name: 'Test Vessel',
shipType: 'Cargo',
imo: undefined,
callSign: undefined,
width: undefined,
length: undefined,
positionFixingDevice: undefined,
})
})
})
101 changes: 58 additions & 43 deletions src/__tests__/StreamManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
import StreamManager, { SIMPLE_FETCH_INTERVAL, MONITORED_FETCH_INTERVAL } from '../implementations/StreamManager';
import { IClientHandler } from '../interfaces/IClientHandler';
import { ISimpleVessel } from '../models/simpleVessel';
import { IMonitoredVessel } from '../models/monitoredVessel';
import { RefObject } from 'react';
import StreamManager from '../implementations/StreamManager'
import { IClientHandler } from '../interfaces/IClientHandler'
import { ISimpleVessel } from '../models/simpleVessel'
import { IMonitoredVessel } from '../models/monitoredVessel'
import { RefObject } from 'react'

jest.mock('../interfaces/IClientHandler');
jest.mock('../interfaces/IClientHandler')

describe('StreamManager', () => {
let mockClientHandler: jest.Mocked<IClientHandler>;
let setAllVessels: jest.Mock<React.Dispatch<React.SetStateAction<ISimpleVessel[]>>>;
let setMonitoredVessels: jest.Mock<React.Dispatch<React.SetStateAction<IMonitoredVessel[]>>>;
let dateTimeRef: RefObject<Date>;
let streamManager: StreamManager;

beforeEach(() => {
mockClientHandler = {
getSimpleVessles: jest.fn().mockResolvedValue([{ mmsi: 123456, location: { point: { lon: 1, lat: 2 }, timestamp: new Date(), heading: 45 } }]),
getMonitoredVessels: jest.fn().mockResolvedValue([{ mmsi: 123456, trustworthiness: 0.9, reason: 'Test' }]),
} as unknown as jest.Mocked<IClientHandler>;

setAllVessels = jest.fn();
setMonitoredVessels = jest.fn();
dateTimeRef = { current: new Date() } as RefObject<Date>;

streamManager = new StreamManager(mockClientHandler, setAllVessels, setMonitoredVessels, dateTimeRef);
});

it('should start and stop simple vessel fetching', async () => {
jest.useFakeTimers();
jest.spyOn(global, 'setTimeout')
streamManager.startSimpleVesselFetching();
expect(mockClientHandler.getSimpleVessles).toHaveBeenCalledTimes(1); // Called once

// jest.advanceTimersByTime(SIMPLE_FETCH_INTERVAL); // Simulate 5 seconds for the next call
// expect(mockClientHandler.getSimpleVessles).toHaveBeenCalledTimes(2); // Called in loop

streamManager.stopSimpleVesselFetching(); // Stop the loop
// jest.clearAllTimers();
});

it('should handle monitoring zone change and stop fetching if zone is invalid', () => {
streamManager.onMonitoringZoneChange(undefined);
expect(setMonitoredVessels).toHaveBeenCalledWith([]);
});
});
let mockClientHandler: jest.Mocked<IClientHandler>
let setAllVessels: jest.Mock<React.Dispatch<React.SetStateAction<ISimpleVessel[]>>>
let setMonitoredVessels: jest.Mock<React.Dispatch<React.SetStateAction<IMonitoredVessel[]>>>
let dateTimeRef: RefObject<Date>
let streamManager: StreamManager

beforeEach(() => {
mockClientHandler = {
getSimpleVessles: jest
.fn()
.mockResolvedValue([
{ mmsi: 123456, location: { point: { lon: 1, lat: 2 }, timestamp: new Date(), heading: 45 } },
]),
getMonitoredVessels: jest.fn().mockResolvedValue([{ mmsi: 123456, trustworthiness: 0.9, reason: 'Test' }]),
} as unknown as jest.Mocked<IClientHandler>

setAllVessels = jest.fn()
setMonitoredVessels = jest.fn()
dateTimeRef = { current: new Date() } as RefObject<Date>

streamManager = new StreamManager(mockClientHandler, setAllVessels, setMonitoredVessels, dateTimeRef)
})

it('should start and stop simple vessel fetching', async () => {
streamManager.startSimpleVesselFetching()
expect(mockClientHandler.getSimpleVessles).toHaveBeenCalledTimes(1) // Called once
streamManager.stopSimpleVesselFetching() // Stop the loop
})

it('should start and stop monitored vessel fetching', async () => {
// Start fetching
streamManager.onMonitoringZoneChange([
{ lon: 1, lat: 2 },
{ lon: 3, lat: 4 },
{ lon: 5, lat: 6 },
{ lon: 7, lat: 8 },
])
expect(mockClientHandler.getMonitoredVessels).toHaveBeenCalledTimes(1)

// Clear call count
mockClientHandler.getMonitoredVessels.mockClear()

// Stop fetching
streamManager.onMonitoringZoneChange(undefined)
expect(mockClientHandler.getMonitoredVessels).not.toHaveBeenCalled()
})

it('should handle monitoring zone change and stop fetching if zone is invalid', () => {
streamManager.onMonitoringZoneChange(undefined)
expect(setMonitoredVessels).toHaveBeenCalledWith([])
})
})
2 changes: 1 addition & 1 deletion src/implementations/GRPCClientHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ILocation } from '../models/location'
import { IVesselPath } from '../models/vesselPath'

export default class GRPCClientHandler implements IClientHandler {
constructor(private readonly client: AISServiceClientImpl) { }
constructor(private readonly client: AISServiceClientImpl) {}

async getVesselInfo(request: { mmsi: number; timestamp: number }): Promise<IDetailedVessel> {
const grpcReq: VesselInfoRequest = {
Expand Down
12 changes: 2 additions & 10 deletions src/implementations/StreamManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class StreamManager implements IStreamManager {
timestamp: Math.round(this.myDateTimeRef.current!.getTime() / 1000),
})

this.manageNewSimpleVessels(simpleVessels)
this.setAllVessels(simpleVessels)
}

private async simpleVesselLoop() {
Expand Down Expand Up @@ -62,7 +62,7 @@ export default class StreamManager implements IStreamManager {
timestamp: Math.round(this.myDateTimeRef.current!.getTime() / 1000),
selection: { points: this.zone },
})
this.manageNewMonitoredVessels(monitoredvessels)
this.setMonitoredVessels(monitoredvessels)
}

private async startMonitoredVesselFetching() {
Expand All @@ -77,12 +77,4 @@ export default class StreamManager implements IStreamManager {
private async stopMonitoredVesselFetching() {
clearTimeout(this.monitoredVesselTimeout)
}

private manageNewSimpleVessels(vessels: ISimpleVessel[]) {
this.setAllVessels(vessels)
}

private manageNewMonitoredVessels(vessels: IMonitoredVessel[]) {
this.setMonitoredVessels(vessels)
}
}

0 comments on commit 87a9236

Please sign in to comment.