test: consider Buffers equal to Uint8Arrays

This commit is contained in:
alina 🌸 2024-04-25 05:43:17 +03:00
parent 132c90ed94
commit 4ab7f4baa8
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
3 changed files with 19 additions and 4 deletions

View file

@ -54,6 +54,8 @@ export function setupChai(chai: any, vitestExpect: any) {
writable: true,
configurable: true,
})
chai.expect.addEqualityTesters = customTesters => vitestExpect.addCustomEqualityTesters(customTesters)
}
const stubbedGlobal = new Map()

View file

@ -1,8 +1,19 @@
import { expect } from 'vitest'
import { setPlatform } from '../../packages/core/src/platform.js'
import { buffersEqual } from '../../packages/core/src/utils/buffer-utils.js'
// @ts-expect-error no .env here
if (import.meta.env.TEST_ENV === 'browser' || import.meta.env.TEST_ENV === 'deno') {
setPlatform(new (await import('../../packages/web/src/platform.js')).WebPlatform())
} else {
setPlatform(new (await import('../../packages/node/src/common-internals-node/platform.js')).NodePlatform())
}
}
// consider Buffers equal to Uint8Arrays
expect.addEqualityTesters([
function (a, b) {
if (a instanceof Uint8Array && b instanceof Uint8Array) {
return buffersEqual(a, b)
}
}
])

View file

@ -1,5 +1,5 @@
import { gzipSync, inflateSync } from 'node:zlib'
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { afterEach, beforeAll, beforeEach, describe, expect, it, MockInstance, vi } from 'vitest'
import { getPlatform } from '@mtcute/core/platform.js'
import { dataViewFromBuffer, ICryptoProvider } from '@mtcute/core/utils.js'
@ -53,10 +53,12 @@ export function useFakeMathRandom(source = DEFAULT_ENTROPY): void {
const sourceBytes = getPlatform().hexDecode(source)
const dv = dataViewFromBuffer(sourceBytes)
let spy: MockInstance<[], number>
beforeEach(() => {
let offset = 0
vi.spyOn(globalThis.Math, 'random').mockImplementation(() => {
spy = vi.spyOn(globalThis.Math, 'random').mockImplementation(() => {
const ret = dv.getUint32(offset, true) / 0xffffffff
offset += 4
@ -64,7 +66,7 @@ export function useFakeMathRandom(source = DEFAULT_ENTROPY): void {
})
})
afterEach(() => {
vi.spyOn(globalThis.Math, 'random').mockRestore()
spy.mockRestore()
})
}