Skip to content

Commit f669db4

Browse files
authored
fix(frontend): prevent using mock feature flags (#10792)
## Changes 🏗️ Make sure `NEXT_PUBLIC_PW_TEST` is set only when running Playwright. This forces the app to use "mock" feature flags, so the tests run stable and predictable despite changes on LaunchDarkly. ## Checklist 📋 ### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: - [x] should not have `PW_TEST=true` ... ### For configuration changes: None
1 parent 0e755a5 commit f669db4

File tree

6 files changed

+19
-9
lines changed

6 files changed

+19
-9
lines changed

.github/workflows/platform-frontend-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ jobs:
160160
161161
- name: Run docker compose
162162
run: |
163-
docker compose -f ../docker-compose.yml up -d
163+
NEXT_PUBLIC_PW_TEST=true docker compose -f ../docker-compose.yml up -d
164164
env:
165165
DOCKER_BUILDKIT: 1
166166
BUILDX_CACHE_FROM: type=local,src=/tmp/.buildx-cache

autogpt_platform/docker-compose.platform.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ services:
5050
- app-network
5151
restart: on-failure
5252
healthcheck:
53-
test: ["CMD-SHELL", "poetry run prisma migrate status | grep -q 'No pending migrations' || exit 1"]
53+
test:
54+
[
55+
"CMD-SHELL",
56+
"poetry run prisma migrate status | grep -q 'No pending migrations' || exit 1",
57+
]
5458
interval: 30s
5559
timeout: 10s
5660
retries: 3
@@ -284,6 +288,8 @@ services:
284288
context: ../
285289
dockerfile: autogpt_platform/frontend/Dockerfile
286290
target: prod
291+
args:
292+
NEXT_PUBLIC_PW_TEST: ${NEXT_PUBLIC_PW_TEST:-false}
287293
depends_on:
288294
db:
289295
condition: service_healthy

autogpt_platform/frontend/.env.default

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
NEXT_PUBLIC_REACT_QUERY_DEVTOOL=true
1717

1818
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-FH2XK2W4GN
19-
NEXT_PUBLIC_PW_TEST=true
2019

autogpt_platform/frontend/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ RUN --mount=type=cache,target=/root/.local/share/pnpm pnpm install --frozen-lock
99
FROM base AS build
1010

1111
COPY autogpt_platform/frontend/ .
12+
# Allow CI to opt-in to Playwright test build-time flags
13+
ARG NEXT_PUBLIC_PW_TEST="false"
14+
ENV NEXT_PUBLIC_PW_TEST=$NEXT_PUBLIC_PW_TEST
1215
RUN if [ -f .env ]; then \
1316
cat .env.default .env > .env.merged && mv .env.merged .env; \
1417
else \
1518
cp .env.default .env; \
1619
fi
1720
RUN pnpm run generate:api
18-
RUN pnpm build
21+
# In CI, we want NEXT_PUBLIC_PW_TEST=true during build so Next.js inlines it
22+
RUN if [ "$NEXT_PUBLIC_PW_TEST" = "true" ]; then NEXT_PUBLIC_PW_TEST=true pnpm build; else pnpm build; fi
1923

2024
# Prod stage - based on NextJS reference Dockerfile https://github.com/vercel/next.js/blob/64271354533ed16da51be5dce85f0dbd15f17517/examples/with-docker/Dockerfile
2125
FROM node:21-alpine AS prod

autogpt_platform/frontend/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"lint": "next lint && prettier --check .",
1111
"format": "next lint --fix; prettier --write .",
1212
"types": "tsc --noEmit",
13-
"test": "next build --turbo && playwright test",
14-
"test-ui": "next build --turbo && playwright test --ui",
13+
"test": "NEXT_PUBLIC_PW_TEST=true next build --turbo && playwright test",
14+
"test-ui": "NEXT_PUBLIC_PW_TEST=true next build --turbo && playwright test --ui",
1515
"test:no-build": "playwright test",
1616
"gentests": "playwright codegen http://localhost:3000",
1717
"storybook": "storybook dev -p 6006",

autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { BehaveAs, getBehaveAs } from "@/lib/utils";
34
import { useFlags } from "launchdarkly-react-client-sdk";
45

56
export enum Flag {
@@ -18,7 +19,7 @@ export type FlagValues = {
1819
[Flag.GRAPH_SEARCH]: boolean;
1920
};
2021

21-
const isTest = process.env.NEXT_PUBLIC_PW_TEST === "true";
22+
const isPwMockEnabled = process.env.NEXT_PUBLIC_PW_TEST === "true";
2223

2324
const mockFlags = {
2425
[Flag.BETA_BLOCKS]: [],
@@ -31,9 +32,9 @@ const mockFlags = {
3132
export function useGetFlag<T extends Flag>(flag: T): FlagValues[T] | null {
3233
const currentFlags = useFlags<FlagValues>();
3334
const flagValue = currentFlags[flag];
35+
const isCloud = getBehaveAs() === BehaveAs.CLOUD;
3436

35-
if (isTest) return mockFlags[flag];
36-
if (!flagValue) return null;
37+
if (isPwMockEnabled && !isCloud) return mockFlags[flag];
3738

3839
return flagValue;
3940
}

0 commit comments

Comments
 (0)