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

test: move Fastify server tests to node:test #661

Merged
merged 1 commit into from
May 20, 2024
Merged
Changes from all 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
69 changes: 35 additions & 34 deletions test-e2e/manager-fastify-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
import { test } from 'brittle'
import test from 'node:test'
import assert from 'node:assert/strict'
import { randomBytes } from 'crypto'
import { join } from 'path'
import { fileURLToPath } from 'url'
Expand Down Expand Up @@ -33,7 +34,7 @@ const projectMigrationsFolder = new URL('../drizzle/project', import.meta.url)
const clientMigrationsFolder = new URL('../drizzle/client', import.meta.url)
.pathname

test('start/stop lifecycle', async (t) => {
test('start/stop lifecycle', async () => {
const fastify = Fastify()
const fastifyController = new FastifyController({ fastify })

Expand All @@ -58,23 +59,23 @@ test('start/stop lifecycle', async (t) => {
variant: 'original',
})
const response1 = await fetch(blobUrl1)
t.is(response1.status, 404, 'server started and listening')
assert.equal(response1.status, 404, 'server started and listening')

const blobUrl2 = await project.$blobs.getUrl({
driveId: randomBytes(32).toString('hex'),
name: randomBytes(8).toString('hex'),
type: 'video',
variant: 'original',
})
t.is(
assert.equal(
new URL(blobUrl1).port,
new URL(blobUrl2).port,
'server port is the same'
)

await fastifyController.stop()

await t.exception.all(async () => {
await assert.rejects(async () => {
await fetch(blobUrl2)
}, 'failed to fetch due to connection error')

Expand All @@ -88,18 +89,18 @@ test('start/stop lifecycle', async (t) => {
variant: 'original',
})
const response3 = await fetch(blobUrl3)
t.is(response3.status, 404, 'server started and listening')
assert.equal(response3.status, 404, 'server started and listening')

await fastifyController.stop()

await t.exception.all(async () => {
await assert.rejects(async () => {
await fetch(blobUrl3)
}, 'failed to fetch due to connection error')
})

test('retrieving blobs using url', async (t) => {
const clock = FakeTimers.install({ shouldAdvanceTime: true })
t.teardown(() => clock.uninstall())
t.after(() => clock.uninstall())

const fastify = Fastify()
const fastifyController = new FastifyController({ fastify })
Expand All @@ -114,7 +115,7 @@ test('retrieving blobs using url', async (t) => {

const project = await manager.getProject(await manager.createProject())

const exceptionPromise1 = t.exception(async () => {
const exceptionPromise1 = assert.rejects(async () => {
await project.$blobs.getUrl({
driveId: randomBytes(32).toString('hex'),
name: 'foo',
Expand All @@ -129,25 +130,25 @@ test('retrieving blobs using url', async (t) => {
// Manager should await for the server to start internally
fastifyController.start()

await t.test('blob does not exist', async (st) => {
await t.test('blob does not exist', async () => {
const blobUrl = await project.$blobs.getUrl({
driveId: randomBytes(32).toString('hex'),
name: 'foo',
type: 'photo',
variant: 'original',
})

st.ok(
assert(
new URL(blobUrl),
'retrieving url based on HTTP server resolves after starting it'
)

const response = await fetch(blobUrl)

st.is(response.status, 404, 'response is 404')
assert.equal(response.status, 404, 'response is 404')
})

await t.test('blob exists', async (st) => {
await t.test('blob exists', async () => {
const blobId = await project.$blobs.create(
{ original: join(BLOB_FIXTURES_DIR, 'original.png') },
{ mimeType: 'image/png' }
Expand All @@ -158,15 +159,15 @@ test('retrieving blobs using url', async (t) => {
variant: 'original',
})

st.ok(
assert(
new URL(blobUrl),
'retrieving url based on HTTP server resolves after starting it'
)

const response = await fetch(blobUrl)

st.is(response.status, 200, 'response status ok')
st.is(
assert.equal(response.status, 200, 'response status ok')
assert.equal(
response.headers.get('content-type'),
'image/png',
'matching content type header'
Expand All @@ -175,12 +176,12 @@ test('retrieving blobs using url', async (t) => {
const expected = await fs.readFile(join(BLOB_FIXTURES_DIR, 'original.png'))
const body = Buffer.from(await response.arrayBuffer())

st.alike(body, expected, 'matching reponse body')
assert.deepEqual(body, expected, 'matching reponse body')
})

await fastifyController.stop()

const exceptionPromise2 = t.exception(async () => {
const exceptionPromise2 = assert.rejects(async () => {
await project.$blobs.getUrl({
driveId: randomBytes(32).toString('hex'),
name: 'foo',
Expand All @@ -194,7 +195,7 @@ test('retrieving blobs using url', async (t) => {

test('retrieving icons using url', async (t) => {
const clock = FakeTimers.install({ shouldAdvanceTime: true })
t.teardown(() => clock.uninstall())
t.after(() => clock.uninstall())

const fastify = Fastify()
const fastifyController = new FastifyController({ fastify })
Expand All @@ -209,7 +210,7 @@ test('retrieving icons using url', async (t) => {

const project = await manager.getProject(await manager.createProject())

const exceptionPromise1 = t.exception(async () => {
const exceptionPromise1 = assert.rejects(async () => {
await project.$icons.getIconUrl(randomBytes(32).toString('hex'), {
mimeType: 'image/png',
pixelDensity: 1,
Expand All @@ -222,7 +223,7 @@ test('retrieving icons using url', async (t) => {

await fastifyController.start()

await t.test('icon does not exist', async (st) => {
await t.test('icon does not exist', async () => {
const nonExistentIconId = randomBytes(32).toString('hex')

const iconUrl = await project.$icons.getIconUrl(nonExistentIconId, {
Expand All @@ -231,17 +232,17 @@ test('retrieving icons using url', async (t) => {
pixelDensity: 1,
})

st.ok(
assert(
new URL(iconUrl),
'retrieving url based on HTTP server resolves after starting it'
)

const response = await fetch(iconUrl)

st.is(response.status, 404, 'response is 404')
assert.equal(response.status, 404, 'response is 404')
})

await t.test('icon exists', async (st) => {
await t.test('icon exists', async () => {
const iconBuffer = randomBytes(128)

const iconId = await project.$icons.create({
Expand All @@ -262,26 +263,26 @@ test('retrieving icons using url', async (t) => {
pixelDensity: 1,
})

st.ok(
assert(
new URL(iconUrl),
'retrieving url based on HTTP server resolves after starting it'
)

const response = await fetch(iconUrl)

st.is(response.status, 200, 'response status ok')
st.is(
assert.equal(response.status, 200, 'response status ok')
assert.equal(
response.headers.get('content-type'),
'image/png',
'matching content type header'
)
const body = Buffer.from(await response.arrayBuffer())
st.alike(body, iconBuffer, 'matching response body')
assert.deepEqual(body, iconBuffer, 'matching response body')
})

await fastifyController.stop()

const exceptionPromise2 = t.exception(async () => {
const exceptionPromise2 = assert.rejects(async () => {
await project.$icons.getIconUrl(randomBytes(32).toString('hex'), {
mimeType: 'image/png',
pixelDensity: 1,
Expand All @@ -294,7 +295,7 @@ test('retrieving icons using url', async (t) => {

test('retrieving style.json using stable url', async (t) => {
const clock = FakeTimers.install({ shouldAdvanceTime: true })
t.teardown(() => clock.uninstall())
t.after(() => clock.uninstall())

const fastify = Fastify()

Expand Down Expand Up @@ -322,7 +323,7 @@ test('retrieving style.json using stable url', async (t) => {
fastify,
})

const exceptionPromise1 = t.exception(async () => {
const exceptionPromise1 = assert.rejects(async () => {
await manager.getMapStyleJsonUrl()
}, 'cannot retrieve style json url before HTTP server starts')

Expand All @@ -333,15 +334,15 @@ test('retrieving style.json using stable url', async (t) => {

const styleJsonUrl = await manager.getMapStyleJsonUrl()

t.ok(new URL(styleJsonUrl))
assert(new URL(styleJsonUrl))

const response = await fetch(styleJsonUrl)

t.is(response.status, 200, 'response status ok')
assert.equal(response.status, 200, 'response status ok')

await fastifyController.stop()

const exceptionPromise2 = t.exception(async () => {
const exceptionPromise2 = assert.rejects(async () => {
await manager.getMapStyleJsonUrl()
}, 'cannot retrieve style json url after HTTP server closes')

Expand Down
Loading