auth-device-tracking.test.ts38 lines · main
1import { describe, expect, test } from 'bun:test';
2
3import { deviceFingerprint, deviceHint } from './auth-device-tracking.js';
4
5describe('deviceFingerprint', () => {
6 test('is stable for the same user-agent', () => {
7 const ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/120.0.0.0 Safari/537.36';
8 expect(deviceFingerprint(ua)).toBe(deviceFingerprint(ua));
9 });
10
11 test('differs across user-agents', () => {
12 expect(deviceFingerprint('Chrome')).not.toBe(deviceFingerprint('Firefox'));
13 });
14
15 test('null / empty → same "unknown" bucket', () => {
16 expect(deviceFingerprint(null)).toBe(deviceFingerprint(undefined));
17 expect(deviceFingerprint(null)).toBe(deviceFingerprint(''));
18 });
19});
20
21describe('deviceHint', () => {
22 test('detects Chrome on macOS', () => {
23 const ua =
24 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
25 expect(deviceHint(ua)).toBe('Chrome on macOS');
26 });
27
28 test('detects Firefox on Windows', () => {
29 const ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0';
30 expect(deviceHint(ua)).toBe('Firefox on Windows');
31 });
32
33 test('detects Safari on iOS', () => {
34 const ua =
35 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1';
36 expect(deviceHint(ua)).toBe('Safari on iOS');
37 });
38});