test: consider Buffers equal to Uint8Arrays
This commit is contained in:
parent
132c90ed94
commit
4ab7f4baa8
3 changed files with 19 additions and 4 deletions
|
@ -54,6 +54,8 @@ export function setupChai(chai: any, vitestExpect: any) {
|
||||||
writable: true,
|
writable: true,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
chai.expect.addEqualityTesters = customTesters => vitestExpect.addCustomEqualityTesters(customTesters)
|
||||||
}
|
}
|
||||||
|
|
||||||
const stubbedGlobal = new Map()
|
const stubbedGlobal = new Map()
|
||||||
|
|
|
@ -1,8 +1,19 @@
|
||||||
|
import { expect } from 'vitest'
|
||||||
import { setPlatform } from '../../packages/core/src/platform.js'
|
import { setPlatform } from '../../packages/core/src/platform.js'
|
||||||
|
import { buffersEqual } from '../../packages/core/src/utils/buffer-utils.js'
|
||||||
|
|
||||||
// @ts-expect-error no .env here
|
// @ts-expect-error no .env here
|
||||||
if (import.meta.env.TEST_ENV === 'browser' || import.meta.env.TEST_ENV === 'deno') {
|
if (import.meta.env.TEST_ENV === 'browser' || import.meta.env.TEST_ENV === 'deno') {
|
||||||
setPlatform(new (await import('../../packages/web/src/platform.js')).WebPlatform())
|
setPlatform(new (await import('../../packages/web/src/platform.js')).WebPlatform())
|
||||||
} else {
|
} else {
|
||||||
setPlatform(new (await import('../../packages/node/src/common-internals-node/platform.js')).NodePlatform())
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
])
|
|
@ -1,5 +1,5 @@
|
||||||
import { gzipSync, inflateSync } from 'node:zlib'
|
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 { getPlatform } from '@mtcute/core/platform.js'
|
||||||
import { dataViewFromBuffer, ICryptoProvider } from '@mtcute/core/utils.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 sourceBytes = getPlatform().hexDecode(source)
|
||||||
const dv = dataViewFromBuffer(sourceBytes)
|
const dv = dataViewFromBuffer(sourceBytes)
|
||||||
|
|
||||||
|
let spy: MockInstance<[], number>
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
let offset = 0
|
let offset = 0
|
||||||
|
|
||||||
vi.spyOn(globalThis.Math, 'random').mockImplementation(() => {
|
spy = vi.spyOn(globalThis.Math, 'random').mockImplementation(() => {
|
||||||
const ret = dv.getUint32(offset, true) / 0xffffffff
|
const ret = dv.getUint32(offset, true) / 0xffffffff
|
||||||
offset += 4
|
offset += 4
|
||||||
|
|
||||||
|
@ -64,7 +66,7 @@ export function useFakeMathRandom(source = DEFAULT_ENTROPY): void {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.spyOn(globalThis.Math, 'random').mockRestore()
|
spy.mockRestore()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue