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

feat: ws1 1455 e2e test for get events #95

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 52 additions & 0 deletions suite/src/__tests__/fast/get-events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, test } from '@jest/globals'
import fetch from 'cross-fetch'
import { generateRandomEventId, generateRandomEvent } from '../../utils/rustCeramicHelpers'

const CeramicUrls = String(process.env.CERAMIC_URLS).split(',')

async function postEvent(url: string, event: any) {
let response = await fetch(url + '/ceramic/events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(event),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
}

async function getEventData(url: string, eventId: string) {
let response = await fetch(url + `/ceramic/events/${eventId}`)
console.log(response)
return response
}

describe('rust-ceramic e2e test', () => {
const ceramicUrl = CeramicUrls[0]
test('post and get event data, success', async () => {
const eventId = generateRandomEventId()
const event = generateRandomEvent(eventId)
// publishing the event to rust-ceramic
await postEvent(ceramicUrl, event)
// fetching the event from its event-id from rust-ceramic
const getResponse = await getEventData(ceramicUrl, eventId)
expect(getResponse.status).toEqual(200)
expect(await getResponse.json()).toEqual({
eventId: eventId,
dav1do marked this conversation as resolved.
Show resolved Hide resolved
eventData: event.eventData,
})
})

test('get event data for non-existing event', async () => {
const eventId = generateRandomEventId()
// fetching the event from its event-id from rust-ceramic
const getResponse = await getEventData(ceramicUrl, eventId)
const responseText = await getResponse.text()
expect(getResponse.status).toEqual(400)
expect(responseText).toContain('Event not found')
expect(responseText).toContain(eventId)
expect(getResponse.statusText).toContain('Bad Request')
dav1do marked this conversation as resolved.
Show resolved Hide resolved
})
})
26 changes: 26 additions & 0 deletions suite/src/utils/rustCeramicHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { randomCID, EventID, StreamID } from '@ceramicnetwork/streamid'
import { base16 } from 'multiformats/bases/base16'
import { CARFactory } from 'cartonne'
import { base64 } from 'multiformats/bases/base64'
import * as random from '@stablelib/random'

export function generateRandomEventId(): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe want to update the fast/events.ts random event generation to use these functions too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Don't think it should be in this PR though cause it does not have anything to do with the e2e test for fetching events

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would just be a minor refactor though? Shouldn't add too much to this PR's scope and the code comes out cleaner versus someone going in afterwards in a separate PR just for reducing code duplication. WDYT?

I don't have a strong opinion though, it's cool to open a ticket for cleanup, maybe tagged as debt.

Deciding whether to change adjacent code in a PR is more of an art than a science. In PRs with a lot of moving parts, unrelated changes can certainly be a distraction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a separate issue for the clean-up. I am planning to move all re-usable functions (not too many) from event.rs to the utils.
Can save us some time in the future :)
https://linear.app/3boxlabs/issue/WS1-1475/clear-some-technical-debt-for-rust-ceramic-and-cermic-tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, ticket looks good. I agree with Mohsin that there is a lot of nuance to refactors. In this case, we're still adding and changing these tests, so I don't think it's a problem to follow up.

My general thinking is this: as long as it doesn't blow up the testing scope, cause breaking changes, make review a lot harder, take way longer, or cause difficult merge conflicts, it's worth doing now. For the review piece, putting those refactoring changes (e.g. moving code across files or renaming) in their own commits can make it easier.

Basically, my preference is to do any refactoring I can when I'm in the code because who knows when I'll be back and have the chance, and there's the overhead of reloading the context into my brain and getting another review etc. It's nice to whittle a little tech debt every PR if possible. I think it's harder to sell "our cycle is tackling tech debt" than "we're doing these features and these tickets are a few extra points so we can take care of things" to leadership.

Copy link
Contributor Author

@samika98 samika98 Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, on addition it might be better to remove debt on the file being touched in the PR, this might be a more standardized way of removing debt. Since a developer has visibility of the file being worked on, introducing refactoring to another file that is not of interest to the PR might increase scope in review rather than having a predefined scope for the task

const modelID = new StreamID('model', randomCID())
const eventId = base16.encode(
EventID.createRandom('dev-unstable', 0, {
separatorKey: 'model',
separatorValue: modelID.toString(),
}).bytes,
)
return eventId
}

export function generateRandomEvent(eventId: string): any {
const carFactory = new CARFactory()
const car = carFactory.build().asV1()
car.put({ data: base64.encode(random.randomBytes(512)) }, { isRoot: true })
return {
eventId: eventId,
eventData: car.toString('base64'),
}
}
Loading