Skip to content

Commit

Permalink
Unit tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cesardeazevedo committed Oct 31, 2024
1 parent 13cf3c6 commit d6df90a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 53 deletions.
8 changes: 5 additions & 3 deletions src/__tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ const extensions = [
}),
]

type TEST_FILE_NAMES = 'test_upload.png' | 'test_upload2.png' | 'test_upload_error.png'

type Fixtures = {
editor: Editor
editorMarkdown: Editor
editorUserAbout: Editor
getFile: (filaneme: string) => Promise<File>
getFile: (filaneme: TEST_FILE_NAMES) => Promise<File>
fileUploadExtension: (editor: Editor) => typeof FileUploadExtension
fileUploadSpies: (editor: Editor) => {
spySign: Mock
Expand Down Expand Up @@ -70,8 +72,8 @@ export const test = base.extend<Fixtures>({
)
},
getFile: ({}, use) => {
return use(async (filename: string) => {
const buffer = await readFile(join(__dirname, filename))
return use(async (filename: TEST_FILE_NAMES) => {
const buffer = await readFile(join(__dirname, 'test_files', filename))
return new File([buffer], filename, { type: 'image/png' })
})
},
Expand Down
52 changes: 52 additions & 0 deletions src/__tests__/mockBlossom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { bufferToHex } from '../extensions/FileUploadExtension'

const url = 'https://localhost:3000'

export const hash1 = '6c36995913e97b73d5365f93a7b524a9e45edc68e4f11b78060154987c53602c'
export const hash2 = '008a2224c4d2a513ab2a4add09a2ac20c2d9cec1144b5111bc1317edb2366eac'
export const hash3 = '94f4e40be68952422f78f5bf5ff63cddd2490bfdb7fa92351c3a38317043426c'

export const responses = {
[hash1]: {
sha256: hash1,
url: `${url}/${hash1}`,
type: 'image/png',
size: 21792,
nip94: {
url: `${url}/${hash1}`,
size: 21792,
x: hash1,
m: 'image/png',
dim: '500x500',
},
},
[hash2]: {
sha256: hash2,
url: `${url}/${hash2}`,
type: 'image/png',
size: 16630,
nip94: {
url: `${url}/${hash2}`,
size: 16630,
x: hash2,
m: 'image/png',
dim: '500x500',
},
},
// error hash
[hash3]: {
message: 'Invalid file',
},
}

export const mockBlossomServer = setupServer(
http.put(`${url}/upload`, async (info) => {
const buffer = (await info.request.body?.getReader().read())?.value
if (buffer) {
const sha256 = bufferToHex(await crypto.subtle.digest('SHA-256', buffer.buffer)) as keyof typeof responses
return HttpResponse.json(responses[sha256] || {}, { status: sha256 === hash3 ? 401 : 200 })
}
}),
)
File renamed without changes
File renamed without changes
File renamed without changes
75 changes: 25 additions & 50 deletions src/__tests__/upload.test.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,37 @@
import crypto from 'crypto'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import { bufferToHex } from '../extensions/FileUploadExtension'
import { hash1, hash2, responses, mockBlossomServer } from './mockBlossom'
import { test } from './fixtures'

const hash1 = '6c36995913e97b73d5365f93a7b524a9e45edc68e4f11b78060154987c53602c'
const hash2 = '008a2224c4d2a513ab2a4add09a2ac20c2d9cec1144b5111bc1317edb2366eac'
// error hash
const hash3 = '94f4e40be68952422f78f5bf5ff63cddd2490bfdb7fa92351c3a38317043426c'

const res1 = {
sha256: hash1,
url: `https://localhost:3000/${hash1}`,
type: 'image/png',
size: 21792,
tags: [],
}
const res2 = {
sha256: hash2,
url: `https://localhost:3000/${hash2}`,
type: 'image/png',
size: 16630,
tags: [],
function getReponse(hash: typeof hash1 | typeof hash2) {
const res = responses[hash]
return {
sha256: res.sha256,
type: res.type,
size: res.size,
url: res.url + '.png',
tags: [
['url', res.url + '.png'],
['size', res.size],
['x', res.sha256],
['m', res.type],
['dim', '500x500'],
],
}
}

const server = setupServer(
http.put('https://localhost:3000/upload', async (info) => {
const contentType = info.request.headers.get('content-type')
const buffer = (await info.request.body?.getReader().read())?.value
if (buffer) {
const sha256 = bufferToHex(await crypto.subtle.digest('SHA-256', buffer.buffer))
// Test error file
if (sha256 === hash3) {
return HttpResponse.json({ message: 'Invalid file' }, { status: 401 })
}
return HttpResponse.json({
sha256,
url: `https://localhost:3000/${sha256}`,
type: contentType,
size: buffer?.buffer.byteLength,
})
}
}),
)
const res1 = getReponse(hash1)
const res2 = getReponse(hash2)

describe('FileUpload', () => {
beforeAll(() => {
server.listen()
mockBlossomServer.listen()

Check failure on line 26 in src/__tests__/upload.test.ts

View workflow job for this annotation

GitHub Actions / checks (18)

src/__tests__/upload.test.ts > FileUpload

TypeError: Cannot create proxy with a non-object as target or handler ❯ _WebSocketInterceptor.setup node_modules/.pnpm/@mswjs[email protected]/node_modules/@mswjs/interceptors/src/interceptors/WebSocket/index.ts:70:28 ❯ _WebSocketInterceptor.apply node_modules/.pnpm/@mswjs[email protected]/node_modules/@mswjs/interceptors/src/Interceptor.ts:130:10 ❯ SetupServerApi.listen node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/msw/src/node/SetupServerCommonApi.ts:129:26 ❯ src/__tests__/upload.test.ts:26:23
})

afterAll(() => {
server.close()
mockBlossomServer.close()
})

afterEach(() => {
server.resetHandlers()
mockBlossomServer.resetHandlers()
})

test('assert 2 successfully file uploads', async ({ editor, getFile, fileUploadSpies }) => {
Expand All @@ -64,10 +41,9 @@ describe('FileUpload', () => {
const file2 = await getFile('test_upload2.png')

editor.commands.setContent('GM!')
editor.commands.addFile(file2, editor.$doc.size - 2)
editor.commands.addFile(file, editor.$doc.size - 2)
editor.storage.fileUpload.uploader.addFile(file, editor.$doc.size - 2)
editor.storage.fileUpload.uploader.addFile(file2, editor.$doc.size - 2)

await new Promise<void>((resolve) => setTimeout(() => resolve()))
const schema = editor.getJSON()

expect(schema.content).toHaveLength(3)
Expand Down Expand Up @@ -110,10 +86,9 @@ describe('FileUpload', () => {
const file2 = await getFile('test_upload_error.png')

editor.commands.setContent('GM!')
editor.commands.addFile(file2, editor.$doc.size - 2)
editor.commands.addFile(file, editor.$doc.size - 2)
editor.storage.fileUpload.uploader.addFile(file, editor.$doc.size - 2)
editor.storage.fileUpload.uploader.addFile(file2, editor.$doc.size - 2)

await new Promise<void>((resolve) => setTimeout(() => resolve()))
await expect(editor.storage.fileUpload.uploader.start()).rejects.toStrictEqual(new Error('Error: Invalid file'))

const schema2 = editor.getJSON()
Expand Down

0 comments on commit d6df90a

Please sign in to comment.