|
| 1 | +import { Page, Route } from '@playwright/test'; |
| 2 | + |
| 3 | +import { FeatureFlagValue, PlaywrightArgs } from '../types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Represents a single flag in the OFREP response |
| 7 | + */ |
| 8 | +interface OFREPFlag { |
| 9 | + key: string; |
| 10 | + value: FeatureFlagValue; |
| 11 | + reason: string; |
| 12 | + variant: string; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Represents the OFREP bulk evaluation response body |
| 17 | + */ |
| 18 | +interface OFREPBulkResponse { |
| 19 | + flags: OFREPFlag[]; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Delays execution for the specified number of milliseconds |
| 24 | + */ |
| 25 | +function delay(ms: number): Promise<void> { |
| 26 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Handles the OFREP bulk evaluation endpoint by merging flags into the response |
| 31 | + */ |
| 32 | +async function handleBulkEvaluationRoute( |
| 33 | + route: Route, |
| 34 | + flags: Record<string, FeatureFlagValue>, |
| 35 | + latency: number |
| 36 | +): Promise<void> { |
| 37 | + let response: Awaited<ReturnType<Route['fetch']>> | undefined; |
| 38 | + |
| 39 | + try { |
| 40 | + response = await route.fetch(); |
| 41 | + |
| 42 | + if (!response.ok()) { |
| 43 | + await route.fulfill({ response }); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const body: OFREPBulkResponse = await response.json(); |
| 48 | + |
| 49 | + // override existing flags with provided values |
| 50 | + for (const flag of body.flags) { |
| 51 | + if (flag.key in flags) { |
| 52 | + flag.value = flags[flag.key]; |
| 53 | + flag.reason = 'STATIC'; |
| 54 | + flag.variant = 'playwright-override'; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // add any flags not present in the original response |
| 59 | + for (const [key, value] of Object.entries(flags)) { |
| 60 | + const exists = body.flags.some((f) => f.key === key); |
| 61 | + if (!exists) { |
| 62 | + body.flags.push({ |
| 63 | + key, |
| 64 | + value, |
| 65 | + reason: 'STATIC', |
| 66 | + variant: 'playwright-override', |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Apply artificial latency if specified |
| 72 | + if (latency > 0) { |
| 73 | + await delay(latency); |
| 74 | + } |
| 75 | + |
| 76 | + await route.fulfill({ |
| 77 | + response, |
| 78 | + body: JSON.stringify(body), |
| 79 | + headers: { 'content-type': 'application/json' }, |
| 80 | + }); |
| 81 | + } catch (error) { |
| 82 | + console.error('@grafana/plugin-e2e: Failed to intercept OFREP bulk evaluation', error); |
| 83 | + // fulfill with original response if available, otherwise return error response |
| 84 | + if (response) { |
| 85 | + await route.fulfill({ response }); |
| 86 | + } else { |
| 87 | + await route.fulfill({ |
| 88 | + status: 500, |
| 89 | + body: JSON.stringify({ error: 'Failed to intercept OFREP request' }), |
| 90 | + headers: { 'content-type': 'application/json' }, |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Handles the OFREP single flag evaluation endpoint by returning the override if defined |
| 98 | + */ |
| 99 | +async function handleSingleFlagRoute( |
| 100 | + route: Route, |
| 101 | + flags: Record<string, FeatureFlagValue>, |
| 102 | + latency: number |
| 103 | +): Promise<void> { |
| 104 | + try { |
| 105 | + const url = new URL(route.request().url()); |
| 106 | + const flagKey = url.pathname.split('/').pop(); |
| 107 | + |
| 108 | + if (flagKey && flagKey in flags) { |
| 109 | + // apply artificial latency if specified |
| 110 | + if (latency > 0) { |
| 111 | + await delay(latency); |
| 112 | + } |
| 113 | + |
| 114 | + await route.fulfill({ |
| 115 | + status: 200, |
| 116 | + body: JSON.stringify({ |
| 117 | + key: flagKey, |
| 118 | + value: flags[flagKey], |
| 119 | + reason: 'STATIC', |
| 120 | + variant: 'playwright-override', |
| 121 | + }), |
| 122 | + headers: { 'content-type': 'application/json' }, |
| 123 | + }); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + // fetch the original response if we don't have an override |
| 128 | + const response = await route.fetch(); |
| 129 | + await route.fulfill({ response }); |
| 130 | + } catch (error) { |
| 131 | + console.error('@grafana/plugin-e2e: Failed to intercept OFREP single flag evaluation', error); |
| 132 | + // return error response since we can't continue after route.fetch() |
| 133 | + await route.fulfill({ |
| 134 | + status: 500, |
| 135 | + body: JSON.stringify({ error: 'Failed to intercept OFREP request' }), |
| 136 | + headers: { 'content-type': 'application/json' }, |
| 137 | + }); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Sets up route interception for OpenFeature OFREP endpoints |
| 143 | + */ |
| 144 | +export async function setupOpenFeatureRoutes( |
| 145 | + page: Page, |
| 146 | + openFeature: Record<string, FeatureFlagValue>, |
| 147 | + latency: number, |
| 148 | + selectors: PlaywrightArgs['selectors'] |
| 149 | +): Promise<void> { |
| 150 | + console.log('@grafana/plugin-e2e: setting up OpenFeature OFREP interception', { |
| 151 | + openFeature, |
| 152 | + latency, |
| 153 | + }); |
| 154 | + |
| 155 | + // intercept bulk evaluation endpoint |
| 156 | + await page.route(selectors.apis.OpenFeature.ofrepBulkPattern, async (route) => { |
| 157 | + await handleBulkEvaluationRoute(route, openFeature, latency); |
| 158 | + }); |
| 159 | + |
| 160 | + // intercept single flag evaluation endpoint |
| 161 | + await page.route(selectors.apis.OpenFeature.ofrepSinglePattern, async (route) => { |
| 162 | + await handleSingleFlagRoute(route, openFeature, latency); |
| 163 | + }); |
| 164 | +} |
0 commit comments