|
| 1 | +import { platform, release } from 'node:os' |
| 2 | +import { hostOsResourceAttributes } from '../host-os.node' |
| 3 | + |
| 4 | +jest.mock('node:os', () => ({ platform: jest.fn(), release: jest.fn() })) |
| 5 | + |
| 6 | +const mockPlatform = platform as jest.Mock |
| 7 | +const mockRelease = release as jest.Mock |
| 8 | + |
| 9 | +describe('hostOsResourceAttributes', () => { |
| 10 | + it('reports the host OS', () => { |
| 11 | + mockPlatform.mockReturnValue('linux') |
| 12 | + mockRelease.mockReturnValue('6.1.0-27-amd64') |
| 13 | + |
| 14 | + expect(hostOsResourceAttributes()).toEqual({ 'os.name': 'Linux', 'os.version': '6.1.0-27-amd64' }) |
| 15 | + }) |
| 16 | + |
| 17 | + it.each([ |
| 18 | + ['darwin', 'macOS'], |
| 19 | + ['win32', 'Windows'], |
| 20 | + ['linux', 'Linux'], |
| 21 | + ['freebsd', 'FreeBSD'], |
| 22 | + ])('reports %s under the name the other PostHog SDKs use, %s', (identifier, expected) => { |
| 23 | + // posthog-ios and posthog-android already send these names; `platform()` |
| 24 | + // returns os.type identifiers, which would be a second spelling. |
| 25 | + mockPlatform.mockReturnValue(identifier) |
| 26 | + mockRelease.mockReturnValue('1.0.0') |
| 27 | + |
| 28 | + expect(hostOsResourceAttributes()['os.name']).toBe(expected) |
| 29 | + }) |
| 30 | + |
| 31 | + it('passes an unmapped platform through rather than dropping it', () => { |
| 32 | + mockPlatform.mockReturnValue('haiku') |
| 33 | + mockRelease.mockReturnValue('1.0.0') |
| 34 | + |
| 35 | + expect(hostOsResourceAttributes()['os.name']).toBe('haiku') |
| 36 | + }) |
| 37 | + |
| 38 | + it('omits a key node:os cannot supply rather than emitting it empty', () => { |
| 39 | + mockPlatform.mockReturnValue('linux') |
| 40 | + mockRelease.mockReturnValue('') |
| 41 | + |
| 42 | + expect(hostOsResourceAttributes()).toEqual({ 'os.name': 'Linux' }) |
| 43 | + }) |
| 44 | + |
| 45 | + it('returns no attributes when node:os throws', () => { |
| 46 | + mockPlatform.mockImplementation(() => { |
| 47 | + throw new Error('unsupported') |
| 48 | + }) |
| 49 | + mockRelease.mockImplementation(() => { |
| 50 | + throw new Error('unsupported') |
| 51 | + }) |
| 52 | + |
| 53 | + expect(hostOsResourceAttributes()).toEqual({}) |
| 54 | + }) |
| 55 | +}) |
0 commit comments