|
1 | | -import { PostHog } from '@/entrypoints/index.node' |
| 1 | +import { PostHog, PostHogOptions } from '@/entrypoints/index.node' |
2 | 2 | import { anyFlagsCall, anyLocalEvalCall, apiImplementation, isPending, wait, waitForPromises } from './utils' |
3 | 3 | import { randomUUID } from 'crypto' |
4 | 4 |
|
5 | 5 | jest.mock('../version', () => ({ version: '1.2.3' })) |
6 | 6 |
|
7 | 7 | const mockedFetch = jest.spyOn(globalThis, 'fetch').mockImplementation() |
8 | 8 |
|
| 9 | +const posthogImmediateResolveOptions: PostHogOptions = { |
| 10 | + fetchRetryCount: 0, |
| 11 | +} |
| 12 | + |
9 | 13 | const waitForFlushTimer = async (): Promise<void> => { |
10 | 14 | await waitForPromises() |
11 | 15 | // To trigger the flush via the timer |
@@ -2469,6 +2473,92 @@ describe('PostHog Node.js', () => { |
2469 | 2473 | }) |
2470 | 2474 | }) |
2471 | 2475 |
|
| 2476 | + describe('evaluation environments', () => { |
| 2477 | + beforeEach(() => { |
| 2478 | + mockedFetch.mockClear() |
| 2479 | + }) |
| 2480 | + |
| 2481 | + it('should send evaluation environments when configured', async () => { |
| 2482 | + mockedFetch.mockImplementation( |
| 2483 | + apiImplementation({ |
| 2484 | + decideFlags: { 'test-flag': true }, |
| 2485 | + flagsPayloads: {}, |
| 2486 | + }) |
| 2487 | + ) |
| 2488 | + |
| 2489 | + const posthogWithEnvs = new PostHog('TEST_API_KEY', { |
| 2490 | + host: 'http://example.com', |
| 2491 | + evaluationEnvironments: ['production', 'backend'], |
| 2492 | + ...posthogImmediateResolveOptions, |
| 2493 | + }) |
| 2494 | + |
| 2495 | + await posthogWithEnvs.getAllFlags('some-distinct-id') |
| 2496 | + |
| 2497 | + expect(mockedFetch).toHaveBeenCalledWith( |
| 2498 | + 'http://example.com/flags/?v=2&config=true', |
| 2499 | + expect.objectContaining({ |
| 2500 | + method: 'POST', |
| 2501 | + body: expect.stringContaining('"evaluation_environments":["production","backend"]'), |
| 2502 | + }) |
| 2503 | + ) |
| 2504 | + |
| 2505 | + await posthogWithEnvs.shutdown() |
| 2506 | + }) |
| 2507 | + |
| 2508 | + it('should not send evaluation environments when not configured', async () => { |
| 2509 | + mockedFetch.mockImplementation( |
| 2510 | + apiImplementation({ |
| 2511 | + decideFlags: { 'test-flag': true }, |
| 2512 | + flagsPayloads: {}, |
| 2513 | + }) |
| 2514 | + ) |
| 2515 | + |
| 2516 | + const posthogWithoutEnvs = new PostHog('TEST_API_KEY', { |
| 2517 | + host: 'http://example.com', |
| 2518 | + ...posthogImmediateResolveOptions, |
| 2519 | + }) |
| 2520 | + |
| 2521 | + await posthogWithoutEnvs.getAllFlags('some-distinct-id') |
| 2522 | + |
| 2523 | + expect(mockedFetch).toHaveBeenCalledWith( |
| 2524 | + 'http://example.com/flags/?v=2&config=true', |
| 2525 | + expect.objectContaining({ |
| 2526 | + method: 'POST', |
| 2527 | + body: expect.not.stringContaining('evaluation_environments'), |
| 2528 | + }) |
| 2529 | + ) |
| 2530 | + |
| 2531 | + await posthogWithoutEnvs.shutdown() |
| 2532 | + }) |
| 2533 | + |
| 2534 | + it('should not send evaluation environments when configured as empty array', async () => { |
| 2535 | + mockedFetch.mockImplementation( |
| 2536 | + apiImplementation({ |
| 2537 | + decideFlags: { 'test-flag': true }, |
| 2538 | + flagsPayloads: {}, |
| 2539 | + }) |
| 2540 | + ) |
| 2541 | + |
| 2542 | + const posthogWithEmptyEnvs = new PostHog('TEST_API_KEY', { |
| 2543 | + host: 'http://example.com', |
| 2544 | + evaluationEnvironments: [], |
| 2545 | + ...posthogImmediateResolveOptions, |
| 2546 | + }) |
| 2547 | + |
| 2548 | + await posthogWithEmptyEnvs.getAllFlags('some-distinct-id') |
| 2549 | + |
| 2550 | + expect(mockedFetch).toHaveBeenCalledWith( |
| 2551 | + 'http://example.com/flags/?v=2&config=true', |
| 2552 | + expect.objectContaining({ |
| 2553 | + method: 'POST', |
| 2554 | + body: expect.not.stringContaining('evaluation_environments'), |
| 2555 | + }) |
| 2556 | + ) |
| 2557 | + |
| 2558 | + await posthogWithEmptyEnvs.shutdown() |
| 2559 | + }) |
| 2560 | + }) |
| 2561 | + |
2472 | 2562 | describe('getRemoteConfigPayload', () => { |
2473 | 2563 | let requestRemoteConfigPayloadSpy: jest.SpyInstance |
2474 | 2564 |
|
|
0 commit comments