-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructure files for tests; test getLatestAvailableReports()
- Loading branch information
Maxime Chaillet
committed
Jan 31, 2025
1 parent
6ae7b69
commit ddd4a08
Showing
7 changed files
with
236 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { run } from './aa-storm-alert/worker'; | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
jest.mock('node-fetch'); | ||
import nodeFetch from 'node-fetch'; | ||
import { getLatestAvailableReports } from './alert'; | ||
|
||
describe('alert mechanism', () => { | ||
describe('getLatestAvailableReports()', () => { | ||
const mockedFetch = nodeFetch as unknown as jest.Mock; | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const tests = [ | ||
{ | ||
data: { | ||
'2025-01-31': { | ||
elvis: [ | ||
{ | ||
ref_time: '2025-01-31T06:00:00Z', | ||
state: 'monitoring', | ||
path: 'elvis/2025-01-31T06:00:00Z.json', | ||
}, | ||
], | ||
}, | ||
}, | ||
expected: [ | ||
{ | ||
ref_time: '2025-01-31T06:00:00Z', | ||
state: 'monitoring', | ||
path: 'elvis/2025-01-31T06:00:00Z.json', | ||
}, | ||
], | ||
}, | ||
{ | ||
data: { | ||
'2025-01-30': { | ||
'07-20242025': [ | ||
{ | ||
ref_time: '2025-01-30T06:00:00Z', | ||
state: 'monitoring', | ||
path: '07-20242025/2025-01-30T06:00:00Z.json', | ||
}, | ||
{ | ||
ref_time: '2025-01-30T12:00:00Z', | ||
state: 'monitoring', | ||
path: '07-20242025/2025-01-30T12:00:00Z.json', | ||
}, | ||
], | ||
elvis: [ | ||
{ | ||
ref_time: '2025-01-30T06:00:00Z', | ||
state: 'monitoring', | ||
path: 'elvis/2025-01-30T06:00:00Z.json', | ||
}, | ||
{ | ||
ref_time: '2025-01-30T18:00:00Z', | ||
state: 'monitoring', | ||
path: 'elvis/2025-01-30T18:00:00Z.json', | ||
}, | ||
], | ||
}, | ||
}, | ||
expected: [ | ||
{ | ||
ref_time: '2025-01-30T12:00:00Z', | ||
state: 'monitoring', | ||
path: '07-20242025/2025-01-30T12:00:00Z.json', | ||
}, | ||
{ | ||
ref_time: '2025-01-30T18:00:00Z', | ||
state: 'monitoring', | ||
path: 'elvis/2025-01-30T18:00:00Z.json', | ||
}, | ||
], | ||
}, | ||
]; | ||
it.each(tests)('get latest reports', async ({ data, expected }) => { | ||
mockedFetch.mockResolvedValue({ | ||
json: () => data, | ||
}); | ||
|
||
const result = await getLatestAvailableReports(); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('it returns an empty array when request fails', async () => { | ||
mockedFetch.mockRejectedValue(null); | ||
const result = await getLatestAvailableReports(); | ||
expect(result).toEqual([]); | ||
}); | ||
}); | ||
|
||
// describe(''); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { createConnection, Repository } from 'typeorm'; | ||
import { StormDataResponseBody, WindState } from '../types/rawStormDataTypes'; | ||
import { LatestAAStormReports } from '../entities/latestAAStormReports.entity'; | ||
import { StormAlertData } from '../types/email'; | ||
import { | ||
buildEmailPayloads, | ||
filterAlreadyProcessedReports, | ||
getLatestAvailableReports, | ||
} from './alert'; | ||
|
||
// Replace with real function when available | ||
function sendStormAlertEmail(data: StormAlertData) { | ||
//nothing yet | ||
} | ||
|
||
export async function run() { | ||
// create a connection to the remote db | ||
const connection = await createConnection(); | ||
const latestStormReportsRepository = | ||
connection.getRepository(LatestAAStormReports); | ||
|
||
const latestAvailableReports = await getLatestAvailableReports(); | ||
|
||
// filter reports which have been already processed | ||
|
||
const filteredAvailableReports = await filterAlreadyProcessedReports( | ||
latestAvailableReports, | ||
latestStormReportsRepository, | ||
); | ||
|
||
// check whether an email should be sent | ||
const emailPayloads = await buildEmailPayloads(filteredAvailableReports); | ||
|
||
console.log('emailPayload', emailPayloads); | ||
|
||
// create templates | ||
sendStormAlertEmail(emailPayloads); | ||
// send emails | ||
|
||
// drop all latest storm reports stored to prevent accumulation of useless data in this table by time | ||
await latestStormReportsRepository.clear(); | ||
await latestStormReportsRepository.save( | ||
latestAvailableReports.map((report) => ({ reportIdentifier: report.path })), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface EmailPayload {} | ||
|
||
export interface ShortReport { | ||
ref_time: string; | ||
state: string; | ||
path: string; | ||
} | ||
|
||
export interface ShortReportsResponseBody { | ||
[date: string]: { | ||
[stormName: string]: ShortReport[]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface StormAlertData { | ||
email: string; | ||
cycloneName: string; | ||
cycloneTime: string; | ||
activatedTriggers?: { | ||
districts48kt: string[]; | ||
districts64kt: string[]; | ||
windspeed: string; | ||
}; | ||
redirectUrl: string; | ||
base64Image: string; | ||
readiness: boolean; | ||
} |