: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(autoresearch): skip validate call when a day field is invalid (#109553)
posthog[bot]bd0499724f3f
diff --git a/products/autoresearch/frontend/AutoresearchNewScene.tsx b/products/autoresearch/frontend/AutoresearchNewScene.tsxindex 0d48f567de33..3f7d864e92a4 100644--- a/products/autoresearch/frontend/AutoresearchNewScene.tsx+++ b/products/autoresearch/frontend/AutoresearchNewScene.tsx@@ -39,8 +39,12 @@ function formatNumber(value: number | null | undefined): string {}function ValidationPanel(): JSX.Element {- const { validation, validationLoading, validationFailed, newPipeline } = useValues(autoresearchNewLogic)+ const { validation, validationLoading, validationFailed, newPipeline, newPipelineValidationErrors } =+ useValues(autoresearchNewLogic)const { runValidate } = useActions(autoresearchNewLogic)+ // The form hides field errors until the first submit, and the validate loader skips invalid day values.+ // So this panel is the only place that says why no estimate appears.+ const dayError = newPipelineValidationErrors.training_lookback_days ?? newPipelineValidationErrors.horizon_daysif (validationFailed && !validationLoading) {return (@@ -57,6 +61,14 @@ function ValidationPanel(): JSX.Element {)}+ if (dayError) {+ return (+ <div className="border rounded p-4 bg-bg-light text-muted text-sm">+ {dayError}. Fix it to see live training estimates.+ </div>+ )+ }+if (!validation && !validationLoading && !hasTarget(newPipeline)) {return (<div className="border rounded p-4 bg-bg-light text-muted text-sm">diff --git a/products/autoresearch/frontend/autoresearchNewLogic.test.ts b/products/autoresearch/frontend/autoresearchNewLogic.test.tsnew file mode 100644index 000000000000..4b58e2b9b420--- /dev/null+++ b/products/autoresearch/frontend/autoresearchNewLogic.test.ts@@ -0,0 +1,45 @@+import { expectLogic } from 'kea-test-utils'++import { initKeaTests } from '~/test/init'++import { autoresearchNewLogic } from './autoresearchNewLogic'+import { autoresearchValidateCreate } from './generated/api'++jest.mock('./generated/api', () => ({+ autoresearchCreate: jest.fn(),+ autoresearchValidateCreate: jest.fn(),+}))++const mockValidate = autoresearchValidateCreate as jest.Mock++describe('autoresearchNewLogic', () => {+ beforeEach(() => {+ jest.clearAllMocks()+ jest.useFakeTimers()+ initKeaTests()+ mockValidate.mockResolvedValue({ can_proceed: true, warnings: [] })+ })++ afterEach(() => {+ jest.useRealTimers()+ })++ it.each([+ ['a valid form', {}, 1],+ ['a cleared horizon', { horizon_days: NaN }, 0],+ ['a cleared training lookback', { training_lookback_days: NaN }, 0],+ ['a horizon out of range', { horizon_days: 0 }, 0],+ ['a fractional horizon', { horizon_days: 1.5 }, 0],+ ['a fractional training lookback', { training_lookback_days: 30.5 }, 0],+ ])('sends %s to the validate endpoint only when the day fields are valid', async (_, days, expectedCalls) => {+ const logic = autoresearchNewLogic()+ logic.mount()++ logic.actions.setNewPipelineValues({ target_event: '$pageview', ...days })+ await jest.advanceTimersByTimeAsync(1000)+ await expectLogic(logic).toFinishAllListeners()++ expect(mockValidate).toHaveBeenCalledTimes(expectedCalls)+ expect(logic.values.validationFailed).toBe(false)+ })+})diff --git a/products/autoresearch/frontend/autoresearchNewLogic.ts b/products/autoresearch/frontend/autoresearchNewLogic.tsindex 7a4bbade37ab..bbd9320c9044 100644--- a/products/autoresearch/frontend/autoresearchNewLogic.ts+++ b/products/autoresearch/frontend/autoresearchNewLogic.ts@@ -167,7 +167,10 @@ export const autoresearchNewLogic = kea<autoresearchNewLogicType>([const teamId = values.currentTeamIdconst { horizon_days, training_lookback_days, training_population, inference_population } =values.newPipeline- if (!teamId || !hasTarget(values.newPipeline)) {+ const { horizon_days: horizonError, training_lookback_days: lookbackError } =+ values.newPipelineValidationErrors+ // A cleared number input holds NaN, which serializes to null and the API rejects.+ if (!teamId || !hasTarget(values.newPipeline) || horizonError || lookbackError) {return null}const { target_event, target_definition } = targetRequestFields(values.newPipeline)@@ -222,13 +225,17 @@ export const autoresearchNewLogic = kea<autoresearchNewLogicType>([? 'Prediction horizon must be at least 1 day': formValues.horizon_days > 365? 'Prediction horizon must be 365 days or fewer'- : undefined,+ : !Number.isInteger(formValues.horizon_days)+ ? 'Prediction horizon must be a whole number of days'+ : undefined,training_lookback_days:!formValues.training_lookback_days || formValues.training_lookback_days < 7? 'Training lookback must be at least 7 days': formValues.training_lookback_days > 730? 'Training lookback must be 730 days or fewer'- : undefined,+ : !Number.isInteger(formValues.training_lookback_days)+ ? 'Training lookback must be a whole number of days'+ : undefined,}),submit: async (payload: NewPipelineFormValues) => {if (!values.currentTeamId) {