fingerprintjs

Форк
0
/
audio.test.ts 
69 строк · 2.7 Кб
1
import { getBrowserMajorVersion, isChromium, isGecko, isMobile, isSafari, isWebKit } from '../../tests/utils'
2
import getAudioFingerprint, { getMiddle, SpecialFingerprint, stabilize } from './audio'
3

4
describe('Sources', () => {
5
  describe('audio', () => {
6
    it('returns expected value', async () => {
7
      const result = (await getAudioFingerprint())()
8

9
      if (doesBrowserSuspendAudioContext()) {
10
        expect(result).toBe(SpecialFingerprint.KnownForSuspending)
11
      } else if (isGecko()) {
12
        expect(result).toBeGreaterThan(0.000169)
13
        expect(result).toBeLessThan(0.00017)
14
      } else if (isWebKit()) {
15
        if ('OfflineAudioContext' in window) {
16
          expect(result).toBeGreaterThan(0.000058)
17
          expect(result).toBeLessThan(0.000066)
18
        } else {
19
          expect(result).toBeGreaterThan(0.000113)
20
          expect(result).toBeLessThan(0.000114)
21
        }
22
      } else if (isChromium()) {
23
        expect(result).toBeGreaterThan(0.00006)
24
        expect(result).toBeLessThan(0.00009)
25
      } else {
26
        throw new Error('Unexpected browser')
27
      }
28
    })
29

30
    it('returns a stable value', async () => {
31
      const finishFirst = await getAudioFingerprint()
32
      const first = finishFirst()
33
      const finishSecond = await getAudioFingerprint()
34
      const second = finishSecond()
35
      expect(first).toBe(second)
36
      expect(finishFirst()).toBe(first)
37
    })
38
  })
39

40
  describe('getMiddle helper', () => {
41
    it('calculates properly and ignores zeros', () => {
42
      const signal = [
43
        0, -5.92384345, -3.39578492, -4.39564854, 0, -3.89384529, -8.92384234, -5.01429423, -4.12045834, -3.93298453, 0,
44
      ]
45
      expect(getMiddle(signal)).toEqual(-6.15981363)
46
    })
47
  })
48

49
  describe('stabilize helper', () => {
50
    it('calculates properly', () => {
51
      expect(stabilize(0.000123456789, 2)).toBe(0.00012)
52
      expect(stabilize(0.000000012345, 2)).toBe(0.000000012)
53
      expect(stabilize(-0.000123456789, 2)).toBe(-0.00012)
54
      expect(stabilize(0.000123456789, 7)).toBe(0.0001234568)
55
      expect(stabilize(0.000123456789, 2.2)).toBe(0.000125)
56
      expect(stabilize(0.000123456789, 2.5)).toBe(0.000124)
57
      expect(stabilize(0.000123456789, 6.2)).toBe(0.000123457)
58
      expect(stabilize(0.000123456789, 15.5)).toBe(0.000123456789)
59
      expect(stabilize(0.000123456789, 5.2)).toBe(0.000123455)
60
      expect(stabilize(0.000123456789, 5.5)).toBe(0.000123456)
61
    })
62
  })
63
})
64

65
function doesBrowserSuspendAudioContext() {
66
  // WebKit has stopped telling its real version in the user-agent string since version 605.1.15,
67
  // therefore the browser version has to be checked instead of the engine version.
68
  return isSafari() && isMobile() && (getBrowserMajorVersion() ?? 0) < 12
69
}
70

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.