|
| 1 | +import type { CatalogFlag, FlagCatalogRequest } from './flagCatalog' |
| 2 | +import { fetchFlagCatalog } from './flagCatalog' |
| 3 | + |
| 4 | +describe('flagCatalog', () => { |
| 5 | + describe('fetchFlagCatalog', () => { |
| 6 | + const baseRequest: FlagCatalogRequest = { page: 1, pageSize: 20, search: '', typeFilter: [], tagFilter: [] } |
| 7 | + |
| 8 | + function mockResponse(body: unknown) { |
| 9 | + return spyOn(globalThis, 'fetch').and.returnValue(Promise.resolve(new Response(JSON.stringify(body)))) |
| 10 | + } |
| 11 | + |
| 12 | + it('requests one page server-side (active-only, offset from page) and returns the server total', async () => { |
| 13 | + const sampleFlag: CatalogFlag = { |
| 14 | + key: 'flag-a', |
| 15 | + name: 'Flag A', |
| 16 | + type: 'BOOLEAN', |
| 17 | + variants: [{ name: 'on', value: true }], |
| 18 | + tags: ['x'], |
| 19 | + } |
| 20 | + const sampleTotal = 42 |
| 21 | + const spy = mockResponse({ |
| 22 | + data: [ |
| 23 | + { |
| 24 | + attributes: { |
| 25 | + key: sampleFlag.key, |
| 26 | + name: sampleFlag.name, |
| 27 | + value_type: sampleFlag.type, |
| 28 | + // The API returns variant values as strings; parseVariantValue turns them back. |
| 29 | + variants: sampleFlag.variants.map(({ name, value }) => ({ name, value: String(value) })), |
| 30 | + tags: sampleFlag.tags, |
| 31 | + }, |
| 32 | + }, |
| 33 | + ], |
| 34 | + meta: { page: { total: sampleTotal } }, |
| 35 | + }) |
| 36 | + |
| 37 | + const page = await fetchFlagCatalog('tok', 'datad0g.com', { ...baseRequest, page: 3, pageSize: 20 }) |
| 38 | + |
| 39 | + const [requestUrl, requestInit] = spy.calls.argsFor(0) as [string, RequestInit] |
| 40 | + const url = new URL(requestUrl) |
| 41 | + expect(url.pathname).toBe('/api/ui/ffe/feature-flags') |
| 42 | + expect(url.searchParams.get('page[limit]')).toBe('20') |
| 43 | + expect(url.searchParams.get('page[offset]')).toBe('40') // (3 - 1) * 20 |
| 44 | + expect(url.searchParams.get('is_archived')).toBe('false') |
| 45 | + expect((requestInit.headers as Record<string, string>).Authorization).toBe('Bearer tok') |
| 46 | + expect(page.total).toBe(sampleTotal) |
| 47 | + expect(page.flags).toEqual([sampleFlag]) |
| 48 | + }) |
| 49 | + |
| 50 | + it('sends search, value_type (repeated), and tags (repeated) as server-side filters', async () => { |
| 51 | + const spy = mockResponse({ data: [], meta: { page: { total: 0 } } }) |
| 52 | + |
| 53 | + await fetchFlagCatalog('tok', 'datad0g.com', { |
| 54 | + page: 1, |
| 55 | + pageSize: 20, |
| 56 | + search: 'checkout', |
| 57 | + typeFilter: ['BOOLEAN', 'STRING'], |
| 58 | + tagFilter: ['team:x', 'beta'], |
| 59 | + }) |
| 60 | + |
| 61 | + const [requestUrl] = spy.calls.argsFor(0) as [string, RequestInit] |
| 62 | + const url = new URL(requestUrl) |
| 63 | + expect(url.searchParams.get('search')).toBe('checkout') |
| 64 | + expect(url.searchParams.getAll('value_type')).toEqual(['BOOLEAN', 'STRING']) |
| 65 | + expect(url.searchParams.getAll('tags')).toEqual(['team:x', 'beta']) |
| 66 | + }) |
| 67 | + |
| 68 | + it('omits the search param when the term is empty', async () => { |
| 69 | + const spy = mockResponse({ data: [], meta: { page: { total: 0 } } }) |
| 70 | + await fetchFlagCatalog('tok', 'datad0g.com', baseRequest) |
| 71 | + const [requestUrl] = spy.calls.argsFor(0) as [string, RequestInit] |
| 72 | + expect(new URL(requestUrl).searchParams.has('search')).toBe(false) |
| 73 | + }) |
| 74 | + |
| 75 | + it('parses variant values by declared type, falling back to the raw string on bad input', async () => { |
| 76 | + mockResponse({ |
| 77 | + data: [ |
| 78 | + { |
| 79 | + attributes: { |
| 80 | + key: 'f', |
| 81 | + value_type: 'JSON', |
| 82 | + variants: [ |
| 83 | + { name: 'ok', value: '{"a":1}' }, |
| 84 | + { name: 'bad', value: 'not json' }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + attributes: { |
| 90 | + key: 'n', |
| 91 | + value_type: 'INTEGER', |
| 92 | + variants: [ |
| 93 | + { name: 'five', value: '5' }, |
| 94 | + { name: 'nan', value: 'abc' }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + }, |
| 98 | + ], |
| 99 | + meta: { page: { total: 2 } }, |
| 100 | + }) |
| 101 | + |
| 102 | + const { flags } = await fetchFlagCatalog('tok', 'datad0g.com', baseRequest) |
| 103 | + expect(flags[0].variants[0].value).toEqual({ a: 1 }) |
| 104 | + expect(flags[0].variants[1].value).toBe('not json') |
| 105 | + expect(flags[1].variants[0].value).toBe(5) |
| 106 | + expect(flags[1].variants[1].value).toBe('abc') |
| 107 | + }) |
| 108 | + |
| 109 | + it('keeps malformed or out-of-range variant values raw instead of coercing them', async () => { |
| 110 | + mockResponse({ |
| 111 | + data: [ |
| 112 | + { |
| 113 | + attributes: { |
| 114 | + key: 'b', |
| 115 | + value_type: 'BOOLEAN', |
| 116 | + variants: [ |
| 117 | + { name: 't', value: 'true' }, |
| 118 | + { name: 'f', value: 'false' }, |
| 119 | + { name: 'weird', value: 'True' }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + }, |
| 123 | + { |
| 124 | + attributes: { |
| 125 | + key: 'i', |
| 126 | + value_type: 'INTEGER', |
| 127 | + variants: [ |
| 128 | + { name: 'partial', value: '5abc' }, |
| 129 | + { name: 'float', value: '5.5' }, |
| 130 | + { name: 'unsafe', value: '9007199254740993' }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + }, |
| 134 | + { |
| 135 | + attributes: { |
| 136 | + key: 'd', |
| 137 | + value_type: 'NUMERIC', |
| 138 | + variants: [ |
| 139 | + { name: 'ok', value: '5.5' }, |
| 140 | + { name: 'partial', value: '5abc' }, |
| 141 | + { name: 'empty', value: '' }, |
| 142 | + ], |
| 143 | + }, |
| 144 | + }, |
| 145 | + ], |
| 146 | + meta: { page: { total: 3 } }, |
| 147 | + }) |
| 148 | + |
| 149 | + const { flags } = await fetchFlagCatalog('tok', 'datad0g.com', baseRequest) |
| 150 | + const [booleanFlag, integer, numeric] = flags |
| 151 | + expect(booleanFlag.variants.map((variant) => variant.value)).toEqual([true, false, 'True']) |
| 152 | + expect(integer.variants.map((variant) => variant.value)).toEqual(['5abc', '5.5', '9007199254740993']) |
| 153 | + expect(numeric.variants.map((variant) => variant.value)).toEqual([5.5, '5abc', '']) |
| 154 | + }) |
| 155 | + |
| 156 | + it('dedupes flags sharing a key within a page, keeping the first occurrence', async () => { |
| 157 | + mockResponse({ |
| 158 | + data: [ |
| 159 | + { attributes: { key: 'dup', name: 'First', value_type: 'STRING', variants: [], tags: [] } }, |
| 160 | + { attributes: { key: 'dup', name: 'Second', value_type: 'STRING', variants: [], tags: [] } }, |
| 161 | + ], |
| 162 | + meta: { page: { total: 2 } }, |
| 163 | + }) |
| 164 | + |
| 165 | + const { flags } = await fetchFlagCatalog('tok', 'datad0g.com', baseRequest) |
| 166 | + expect(flags.length).toBe(1) |
| 167 | + expect(flags[0].name).toBe('First') |
| 168 | + }) |
| 169 | + |
| 170 | + it('falls back to the key for a missing name and defaults tags/variants', async () => { |
| 171 | + mockResponse({ data: [{ attributes: { key: 'no-name', value_type: 'STRING' } }], meta: { page: { total: 1 } } }) |
| 172 | + |
| 173 | + const { flags } = await fetchFlagCatalog('tok', 'datad0g.com', baseRequest) |
| 174 | + expect(flags[0].name).toBe('no-name') |
| 175 | + expect(flags[0].tags).toEqual([]) |
| 176 | + expect(flags[0].variants).toEqual([]) |
| 177 | + }) |
| 178 | + |
| 179 | + it('tolerates a response that omits data/meta, falling total back to the page length', async () => { |
| 180 | + mockResponse({ errors: ['x'] }) |
| 181 | + const page = await fetchFlagCatalog('tok', 'datad0g.com', baseRequest) |
| 182 | + expect(page.flags).toEqual([]) |
| 183 | + expect(page.total).toBe(0) |
| 184 | + }) |
| 185 | + |
| 186 | + it('falls back total to the number of returned flags when meta.page.total is missing', async () => { |
| 187 | + mockResponse({ |
| 188 | + data: [{ attributes: { key: 'a', value_type: 'STRING' } }, { attributes: { key: 'b', value_type: 'STRING' } }], |
| 189 | + }) |
| 190 | + expect((await fetchFlagCatalog('tok', 'datad0g.com', baseRequest)).total).toBe(2) |
| 191 | + }) |
| 192 | + |
| 193 | + it('throws on a non-ok response', async () => { |
| 194 | + spyOn(globalThis, 'fetch').and.returnValue( |
| 195 | + Promise.resolve(new Response('err', { status: 500, statusText: 'Server Error' })) |
| 196 | + ) |
| 197 | + await expectAsync(fetchFlagCatalog('tok', 'datad0g.com', baseRequest)).toBeRejectedWithError( |
| 198 | + /Failed to fetch flag catalog/ |
| 199 | + ) |
| 200 | + }) |
| 201 | + }) |
| 202 | +}) |
0 commit comments