:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(surveys): send notification tests with the real project url (#119840)
Alex Lidera67346a7039a
diff --git a/frontend/src/scenes/surveys/surveyNotificationModalLogic.test.ts b/frontend/src/scenes/surveys/surveyNotificationModalLogic.test.tsindex 2409c897d0c3..4bd652540c7c 100644--- a/frontend/src/scenes/surveys/surveyNotificationModalLogic.test.ts+++ b/frontend/src/scenes/surveys/surveyNotificationModalLogic.test.ts@@ -11,11 +11,13 @@ import {SurveyEventProperties,SurveyQuestionType,SurveyType,+ TeamType,} from '~/types'import {alignGlobalsWithNotificationFilter,buildLastSurveyResponseQuery,+ buildTemplateGlobals,getDefaultSurveyMessage,mergeResponseFiltersIntoExistingFilters,remapSurveyResponseProperties,@@ -318,4 +320,11 @@ describe('surveyNotificationModalLogic', () => {expect(merged?.events?.filter((event) => event.id === SurveyEventName.SENT)).toHaveLength(0)expect(merged?.events?.filter((event) => event.id === SurveyEventName.DISMISSED)).toHaveLength(1)})++ it('builds the sample test globals for the current project', () => {+ const globals = buildTemplateGlobals({ ...NEW_SURVEY, id: 'survey-abc' }, { id: 42, name: 'Acme' } as TeamType)++ expect(globals.project).toEqual({ id: 42, name: 'Acme', url: `${window.location.origin}/project/42` })+ expect(globals.event.properties.$survey_id).toEqual('survey-abc')+ })})diff --git a/frontend/src/scenes/surveys/surveyNotificationModalLogic.ts b/frontend/src/scenes/surveys/surveyNotificationModalLogic.tsindex 293b25981f39..1a5c3f65b13f 100644--- a/frontend/src/scenes/surveys/surveyNotificationModalLogic.ts+++ b/frontend/src/scenes/surveys/surveyNotificationModalLogic.ts@@ -28,6 +28,7 @@ import {getSurveyIdBasedResponseKey,surveyEmitsPartialSentEvents,} from 'scenes/surveys/utils'+import { teamLogic } from 'scenes/teamLogic'import { urls } from 'scenes/urls'import { performQuery } from '~/queries/query'@@ -54,6 +55,8 @@ import {SurveyEventName,SurveyEventProperties,SurveyQuestionType,+ TeamPublicType,+ TeamType,} from '~/types'import type {@@ -265,12 +268,17 @@ function buildSurveyNotificationForm(survey: SurveyNotificationContext): SurveyN}}-function buildTemplateGlobals(survey: SurveyNotificationContext): CyclotronJobInvocationGlobals {+export function buildTemplateGlobals(+ survey: SurveyNotificationContext,+ team: TeamType | TeamPublicType | null+): CyclotronJobInvocationGlobals {+ // The test send posts a real message, and its "View survey" link is built from project.url,+ // so a placeholder project here ends up as a dead link in the customer's Slack channel.return buildSurveyExampleInvocationGlobals({survey,- projectId: 1,- projectName: 'Project',- projectUrl: 'https://app.posthog.com/project/1',+ projectId: team?.id ?? 0,+ projectName: team?.name ?? 'Default project',+ projectUrl: `${window.location.origin}/project/${team?.id}`,})}@@ -998,6 +1006,7 @@ export interface surveyNotificationModalLogicValues {surveyLoading: boolean // surveyLogicsurveyNotifications: HogFunctionType[] // surveyLogicsurveyNotificationsLoading: boolean // surveyLogic+ currentTeam: TeamPublicType | TeamType | null // teamLogiccopiedNotification: HogFunctionType | nulleditingNotification: HogFunctionType | nullhasSlackIntegration: boolean@@ -1257,7 +1266,10 @@ export interface surveyNotificationModalLogicMeta {integrations: IntegrationType[] | null,notificationForm: SurveyNotificationForm) => IntegrationType | null- templateGlobals: (survey: NewSurvey | Survey) => CyclotronJobInvocationGlobals+ templateGlobals: (+ survey: NewSurvey | Survey,+ currentTeam: TeamPublicType | TeamType | null+ ) => CyclotronJobInvocationGlobalssubmitDisabledReason: (survey: NewSurvey | Survey,notificationFormErrors: DeepPartialMap<SurveyNotificationForm, ValidationErrorType>,@@ -1285,6 +1297,8 @@ export const surveyNotificationModalLogic = kea<surveyNotificationModalLogicType['integrations'],surveyLogic({ id: props.surveyId }),['survey', 'surveyLoading', 'surveyNotifications', 'surveyNotificationsLoading'],+ teamLogic,+ ['currentTeam'],],actions: [surveyLogic({ id: props.surveyId }),@@ -1450,7 +1464,11 @@ export const surveyNotificationModalLogic = kea<surveyNotificationModalLogicTypeintegrations?.find((integration: IntegrationType) => integration.id === form.slackIntegrationId) ??null,],- templateGlobals: [(s) => [s.survey], (survey: SurveyNotificationContext) => buildTemplateGlobals(survey)],+ templateGlobals: [+ (s) => [s.survey, s.currentTeam],+ (survey: SurveyNotificationContext, currentTeam: TeamType | TeamPublicType | null) =>+ buildTemplateGlobals(survey, currentTeam),+ ],submitDisabledReason: [(s) => [s.survey,diff --git a/nodejs/src/cdp/cdp-api.serial.test.ts b/nodejs/src/cdp/cdp-api.serial.test.tsindex a3156c0f3e87..165b29fa49a7 100644--- a/nodejs/src/cdp/cdp-api.serial.test.ts+++ b/nodejs/src/cdp/cdp-api.serial.test.ts@@ -266,6 +266,33 @@ describe('CDP API', () => {`)})+ it('keeps the server project when the request globals carry a different one', async () => {+ const res = await supertest(app)+ .post(`/api/projects/${hogFunction.team_id}/hog_functions/${hogFunction.id}/invocations`)+ .send({+ globals: {+ ...globals,+ project: { id: 999, name: 'Placeholder', url: 'https://example.com/project/999' },+ },+ mock_async_functions: true,+ configuration: {+ inputs: {+ ...hogFunction.inputs,+ body: {+ value: { project_url: '{project.url}' },+ bytecode: { project_url: ['_H', 1, 32, 'url', 32, 'project', 1, 2] },+ },+ },+ },+ })++ expect(res.status).toEqual(200)+ expect(res.body.errors).toEqual([])+ const fetchLog = res.body.logs.map((log: any) => log.message).join('\n')+ expect(fetchLog).toContain(`"project_url": "http://localhost:8000/project/${team.id}"`)+ expect(fetchLog).not.toContain('/project/999')+ })+it('can invoke a function via the API with real fetch', async () => {mockFetch.mockImplementationOnce(() =>Promise.resolve({diff --git a/nodejs/src/cdp/cdp-api.ts b/nodejs/src/cdp/cdp-api.tsindex ed3459b1cd62..5bce94f57786 100644--- a/nodejs/src/cdp/cdp-api.ts+++ b/nodejs/src/cdp/cdp-api.ts@@ -598,11 +598,13 @@ export class CdpApi {const triggerGlobals: HogFunctionInvocationGlobals = {...globals,+ // Sample globals from the app can carry a placeholder project, and a test send with+ // mock_async_functions off delivers for real, so the server's project always wins.project: {+ ...globals.project,id: team.id,name: team.name,url: `${this.config.SITE_URL}/project/${team.id}`,- ...globals.project,},}