-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simulator test, refactor some selectors
- Loading branch information
1 parent
532332d
commit ceb4363
Showing
12 changed files
with
440 additions
and
62 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
111 changes: 111 additions & 0 deletions
111
...olution/public/resolver/data_access_layer/mocks/one_node_with_paginated_related_events.ts
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,111 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { DataAccessLayer } from '../../types'; | ||
import { mockTreeWithOneNodeAndTwoPagesOfRelatedEvents } from '../../mocks/resolver_tree'; | ||
import { relatedEventsPaginationSize } from '../factory'; | ||
import { | ||
ResolverRelatedEvents, | ||
ResolverTree, | ||
ResolverEntityIndex, | ||
SafeResolverEvent, | ||
} from '../../../../common/endpoint/types'; | ||
import * as eventModel from '../../../../common/endpoint/models/event'; | ||
|
||
interface Metadata { | ||
/** | ||
* The `_id` of the document being analyzed. | ||
*/ | ||
databaseDocumentID: string; | ||
/** | ||
* A record of entityIDs to be used in tests assertions. | ||
*/ | ||
entityIDs: { | ||
/** | ||
* The entityID of the node related to the document being analyzed. | ||
*/ | ||
origin: 'origin'; | ||
}; | ||
} | ||
export function oneNodeWithPaginatedEvents(): { | ||
dataAccessLayer: DataAccessLayer; | ||
metadata: Metadata; | ||
} { | ||
const metadata: Metadata = { | ||
databaseDocumentID: '_id', | ||
entityIDs: { origin: 'origin' }, | ||
}; | ||
const tree = mockTreeWithOneNodeAndTwoPagesOfRelatedEvents({ | ||
originID: metadata.entityIDs.origin, | ||
}); | ||
|
||
return { | ||
metadata, | ||
dataAccessLayer: { | ||
/** | ||
* Fetch related events for an entity ID | ||
*/ | ||
async relatedEvents(entityID: string): Promise<ResolverRelatedEvents> { | ||
/** | ||
* Respond with the mocked related events when the origin's related events are fetched. | ||
**/ | ||
const events = entityID === metadata.entityIDs.origin ? tree.relatedEvents.events : []; | ||
|
||
return { | ||
entityID, | ||
events, | ||
nextEvent: null, | ||
}; | ||
}, | ||
|
||
/** | ||
* If called with an "after" cursor, return the 2nd page, else return the first. | ||
*/ | ||
async eventsWithEntityIDAndCategory( | ||
entityID: string, | ||
category: string, | ||
after?: string | ||
): Promise<{ events: SafeResolverEvent[]; nextEvent: string | null }> { | ||
let events: SafeResolverEvent[] = []; | ||
const eventsOfCategory = tree.relatedEvents.events.filter( | ||
(event) => event.event?.category === category | ||
); | ||
if (after === undefined) { | ||
events = eventsOfCategory.slice(0, relatedEventsPaginationSize); | ||
} else { | ||
events = eventsOfCategory.slice(relatedEventsPaginationSize - 1); | ||
} | ||
return { | ||
events, | ||
nextEvent: typeof after === 'undefined' ? 'firstEventPage2' : null, | ||
}; | ||
}, | ||
|
||
/** | ||
* Any of the origin's related events by event.id | ||
*/ | ||
async event(eventID: string): Promise<SafeResolverEvent | null> { | ||
return ( | ||
tree.relatedEvents.events.find((event) => eventModel.eventID(event) === eventID) ?? null | ||
); | ||
}, | ||
|
||
/** | ||
* Fetch a ResolverTree for a entityID | ||
*/ | ||
async resolverTree(): Promise<ResolverTree> { | ||
return tree; | ||
}, | ||
|
||
/** | ||
* Get entities matching a document. | ||
*/ | ||
async entities(): Promise<ResolverEntityIndex> { | ||
return [{ entity_id: metadata.entityIDs.origin }]; | ||
}, | ||
}, | ||
}; | ||
} |
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
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
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
Oops, something went wrong.