Skip to content

Commit

Permalink
Help with Events Mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-akamai committed Sep 20, 2024
1 parent bbb42b6 commit 16bd8ba
Showing 1 changed file with 31 additions and 51 deletions.
82 changes: 31 additions & 51 deletions packages/manager/src/mocks/serverHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ import { accountLoginFactory } from 'src/factories/accountLogin';
import { accountUserFactory } from 'src/factories/accountUsers';
import { grantFactory, grantsFactory } from 'src/factories/grants';
import { LinodeKernelFactory } from 'src/factories/linodeKernel';
import { eventMessages } from 'src/features/Events/factory';
import { pickRandom } from 'src/utilities/random';
import { getStorage } from 'src/utilities/storage';

import type { Event } from '@linode/api-v4';

const getRandomWholeNumber = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1) + min);

Expand Down Expand Up @@ -1579,68 +1582,45 @@ export const handlers = [
http.get(
'*/events',
() => {
const events = eventFactory.buildList(1, {
action: 'lke_node_create',
entity: { id: 999, label: 'linode-1', type: 'linode' },
message:
'Rebooting this thing and showing an extremely long event message for no discernible reason other than the fairly obvious reason that we want to do some testing of whether or not these messages wrap.',
percent_complete: 15,
});
const dbEvents = eventFactory.buildList(1, {
action: 'database_low_disk_space',
entity: { id: 999, label: 'database-1', type: 'database' },
message: 'Low disk space.',
});
const oldEvents = eventFactory.buildList(20, {
action: 'account_update',
percent_complete: 100,
seen: true,
});
const eventWithSpecialCharacters = eventFactory.build({
action: 'ticket_update',
const baseEvent: Event = eventFactory.build({
action: 'linode_boot',
entity: {
id: 10,
label: 'Ticket name with special characters... (?)',
type: 'ticket',
id: 1,
label: '{entity}',
type: 'linode',
url: 'https://google.com',
},
message: 'Ticket name with special characters... (?)',
percent_complete: 100,
status: 'notification',
});
const placementGroupCreateEvent = eventFactory.buildList(1, {
action: 'placement_group_create',
entity: { id: 999, label: 'PG-1', type: 'placement_group' },
message: 'Placement Group successfully created.',
percent_complete: 100,
status: 'notification',
});
const placementGroupAssignedEvent = eventFactory.buildList(1, {
action: 'placement_group_assign',
entity: { id: 990, label: 'PG-2', type: 'placement_group' },
message: 'Placement Group successfully assigned.',
percent_complete: 100,
message: 'message with a `ticked` word - please contact Support',
secondary_entity: {
id: 1,
label: 'My Config',
label: '{secondary entity}',
type: 'linode',
url: '/v4/linode/instances/1/configs/1',
url: 'https://google.com',
},
status: 'notification',
status: '{status}' as Event['status'],
username: 'Mock User',
});

return HttpResponse.json(
makeResourcePage([
...events,
...dbEvents,
...oldEvents,
...placementGroupAssignedEvent,
...placementGroupCreateEvent,
eventWithSpecialCharacters,
])
const events = Object.entries(eventMessages).flatMap(
([eventKey, statuses], eventIndex) =>
Object.entries(statuses).map(
([status, messageFunc], statusIndex) => ({
...baseEvent,
action: eventKey as Event['action'],
id: eventIndex * 1000 + statusIndex,
message: messageFunc ? messageFunc(baseEvent) : '',
status: status as Event['status'],
})
)
);

// Randomly select 25 items
const randomEvents = events.sort(() => 0.5 - Math.random()).slice(0, 25);

return HttpResponse.json(makeResourcePage(randomEvents));
},
{
once: false,
once: true,
}
),

Expand Down

0 comments on commit 16bd8ba

Please sign in to comment.