|
1 | 1 | import { beforeEach, describe, expect, it, test, vi } from 'vitest'; |
2 | 2 | import { DEBUG_BUILD } from '../../../src/debug-build'; |
3 | 3 | import { debug } from '../../../src/utils/debug-logger'; |
4 | | -import { dsnToString, extractOrgIdFromDsnHost, makeDsn } from '../../../src/utils/dsn'; |
| 4 | +import { deriveOrgIdFromClient, dsnToString, extractOrgIdFromDsnHost, makeDsn } from '../../../src/utils/dsn'; |
| 5 | +import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; |
5 | 6 |
|
6 | 7 | function testIf(condition: boolean) { |
7 | 8 | return condition ? test : test.skip; |
@@ -247,3 +248,53 @@ describe('extractOrgIdFromDsnHost', () => { |
247 | 248 | expect(extractOrgIdFromDsnHost('')).toBeUndefined(); |
248 | 249 | }); |
249 | 250 | }); |
| 251 | + |
| 252 | +describe('deriveOrgIdFromClient', () => { |
| 253 | + let client: TestClient; |
| 254 | + |
| 255 | + beforeEach(() => { |
| 256 | + vi.clearAllMocks(); |
| 257 | + }); |
| 258 | + |
| 259 | + test('returns orgId from client options when available', () => { |
| 260 | + client = new TestClient( |
| 261 | + getDefaultTestClientOptions({ |
| 262 | + orgId: '00222111', |
| 263 | + dsn: 'https://[email protected]/1', |
| 264 | + }), |
| 265 | + ); |
| 266 | + |
| 267 | + const result = deriveOrgIdFromClient(client); |
| 268 | + expect(result).toBe('00222111'); |
| 269 | + }); |
| 270 | + |
| 271 | + test('converts non-string orgId to string', () => { |
| 272 | + client = new TestClient( |
| 273 | + getDefaultTestClientOptions({ |
| 274 | + orgId: 12345, |
| 275 | + dsn: 'https://[email protected]/1', |
| 276 | + }), |
| 277 | + ); |
| 278 | + |
| 279 | + const result = deriveOrgIdFromClient(client); |
| 280 | + expect(result).toBe('12345'); |
| 281 | + }); |
| 282 | + |
| 283 | + test('extracts orgId from DSN host when options.orgId is not available', () => { |
| 284 | + client = new TestClient( |
| 285 | + getDefaultTestClientOptions({ |
| 286 | + dsn: 'https://[email protected]/1', |
| 287 | + }), |
| 288 | + ); |
| 289 | + |
| 290 | + const result = deriveOrgIdFromClient(client); |
| 291 | + expect(result).toBe('012300'); |
| 292 | + }); |
| 293 | + |
| 294 | + test('returns undefined when neither options.orgId nor DSN host are available', () => { |
| 295 | + client = new TestClient(getDefaultTestClientOptions({})); |
| 296 | + |
| 297 | + const result = deriveOrgIdFromClient(client); |
| 298 | + expect(result).toBeUndefined(); |
| 299 | + }); |
| 300 | +}); |
0 commit comments