|
| 1 | +/** |
| 2 | + * Copyright 2026, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { describe, it, afterEach, expect, vi } from 'vitest'; |
| 17 | +import * as utils from './helpers'; |
| 18 | + |
| 19 | +describe('getQualifiedSegments', () => { |
| 20 | + const odpIntegration = { |
| 21 | + key: 'odp', |
| 22 | + publicKey: 'test-api-key', |
| 23 | + host: 'https://odp.example.com', |
| 24 | + }; |
| 25 | + |
| 26 | + const makeDatafile = (overrides: Record<string, any> = {}) => ({ |
| 27 | + integrations: [odpIntegration], |
| 28 | + typedAudiences: [ |
| 29 | + { |
| 30 | + conditions: ['or', { match: 'qualified', value: 'seg1' }, { match: 'qualified', value: 'seg2' }], |
| 31 | + }, |
| 32 | + ], |
| 33 | + ...overrides, |
| 34 | + }); |
| 35 | + |
| 36 | + const mockFetchResponse = (body: any, ok = true) => { |
| 37 | + vi.stubGlobal( |
| 38 | + 'fetch', |
| 39 | + vi.fn().mockResolvedValue({ |
| 40 | + ok, |
| 41 | + json: () => Promise.resolve(body), |
| 42 | + }) |
| 43 | + ); |
| 44 | + }; |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + vi.restoreAllMocks(); |
| 48 | + vi.unstubAllGlobals(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns error when datafile is invalid or missing ODP integration', async () => { |
| 52 | + // undefined datafile |
| 53 | + // @ts-ignore |
| 54 | + let result = await utils.getQualifiedSegments('user-1'); |
| 55 | + expect(result.segments).toEqual([]); |
| 56 | + expect(result.error?.message).toBe('Invalid datafile: expected a JSON string or object'); |
| 57 | + |
| 58 | + // invalid JSON string |
| 59 | + result = await utils.getQualifiedSegments('user-1', '{bad json'); |
| 60 | + expect(result.segments).toEqual([]); |
| 61 | + expect(result.error?.message).toBe('Invalid datafile: failed to parse JSON string'); |
| 62 | + |
| 63 | + // no ODP integration |
| 64 | + result = await utils.getQualifiedSegments('user-1', { integrations: [] }); |
| 65 | + expect(result.segments).toEqual([]); |
| 66 | + expect(result.error?.message).toBe('ODP integration not found or missing publicKey/host'); |
| 67 | + |
| 68 | + // ODP integration missing publicKey |
| 69 | + result = await utils.getQualifiedSegments('user-1', { |
| 70 | + integrations: [{ key: 'odp', host: 'https://odp.example.com' }], |
| 71 | + }); |
| 72 | + expect(result.segments).toEqual([]); |
| 73 | + expect(result.error?.message).toBe('ODP integration not found or missing publicKey/host'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns empty array with no error when ODP is integrated but no segment conditions exist', async () => { |
| 77 | + const fetchSpy = vi.spyOn(global, 'fetch'); |
| 78 | + const datafile = makeDatafile({ typedAudiences: [], audiences: [] }); |
| 79 | + const result = await utils.getQualifiedSegments('user-1', datafile); |
| 80 | + |
| 81 | + expect(result.segments).toEqual([]); |
| 82 | + expect(result.error).toBeNull(); |
| 83 | + expect(fetchSpy).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('calls ODP GraphQL API and returns only qualified segments', async () => { |
| 87 | + mockFetchResponse({ |
| 88 | + data: { |
| 89 | + customer: { |
| 90 | + audiences: { |
| 91 | + edges: [{ node: { name: 'seg1', state: 'qualified' } }, { node: { name: 'seg2', state: 'not_qualified' } }], |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + const result = await utils.getQualifiedSegments('user-1', makeDatafile()); |
| 98 | + |
| 99 | + expect(result.segments).toEqual(['seg1']); |
| 100 | + expect(result.error).toBeNull(); |
| 101 | + expect(global.fetch).toHaveBeenCalledWith('https://odp.example.com/v3/graphql', { |
| 102 | + method: 'POST', |
| 103 | + headers: { |
| 104 | + 'Content-Type': 'application/json', |
| 105 | + 'x-api-key': 'test-api-key', |
| 106 | + }, |
| 107 | + body: expect.stringContaining('user-1'), |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns error when fetch fails or response is not ok', async () => { |
| 112 | + // network error |
| 113 | + vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('network error'))); |
| 114 | + let result = await utils.getQualifiedSegments('user-1', makeDatafile()); |
| 115 | + expect(result.segments).toEqual([]); |
| 116 | + expect(result.error?.message).toBe('network error'); |
| 117 | + |
| 118 | + // non-200 response |
| 119 | + mockFetchResponse({}, false); |
| 120 | + result = await utils.getQualifiedSegments('user-1', makeDatafile()); |
| 121 | + expect(result.segments).toEqual([]); |
| 122 | + expect(result.error?.message).toContain('ODP request failed with status'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('skips audiences with malformed conditions string without throwing', async () => { |
| 126 | + mockFetchResponse({ |
| 127 | + data: { |
| 128 | + customer: { |
| 129 | + audiences: { |
| 130 | + edges: [{ node: { name: 'seg1', state: 'qualified' } }], |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }); |
| 135 | + |
| 136 | + const datafile = makeDatafile({ |
| 137 | + typedAudiences: [{ conditions: '{bad json' }, { conditions: ['or', { match: 'qualified', value: 'seg1' }] }], |
| 138 | + }); |
| 139 | + |
| 140 | + const result = await utils.getQualifiedSegments('user-1', datafile); |
| 141 | + expect(result.segments).toEqual(['seg1']); |
| 142 | + expect(result.error).toBeNull(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns error when response contains GraphQL errors or missing edges', async () => { |
| 146 | + // GraphQL errors |
| 147 | + mockFetchResponse({ errors: [{ message: 'something went wrong' }] }); |
| 148 | + let result = await utils.getQualifiedSegments('user-1', makeDatafile()); |
| 149 | + expect(result.segments).toEqual([]); |
| 150 | + expect(result.error?.message).toBe('ODP GraphQL error: something went wrong'); |
| 151 | + |
| 152 | + // missing edges path |
| 153 | + mockFetchResponse({ data: {} }); |
| 154 | + result = await utils.getQualifiedSegments('user-1', makeDatafile()); |
| 155 | + expect(result.segments).toEqual([]); |
| 156 | + expect(result.error?.message).toBe('ODP response missing audience edges'); |
| 157 | + }); |
| 158 | +}); |
0 commit comments