: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(workflows): exclude mocked tests from usage records (#118783)
Alex Mays03e29e329cc9
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.tsindex 67e4c7aec197..302950f1f378 100644--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts@@ -659,13 +659,24 @@ describe('HogFunctionHandler', () => {// The billing kind is the whole point of the per-channel handlers: push bills at its own rate// (roughly half of email), so a completed invocation must emit exactly one billable_invocation// carrying the handler's billing type — never fall back to another channel's kind.- it.each([- ['fetch', 'workflow_billable_invocations'],- ['email', 'workflow_emails_sent'],- ['sms', 'workflow_sms_sent'],- ] as const)(- 'emits a single billable_invocation with %s kind matching the handler billing type',- async (billingType, usageKey) => {+ it.each(+ (+ [+ ['fetch', 'workflow_billable_invocations'],+ ['email', 'workflow_emails_sent'],+ ['push', 'workflow_push_sent'],+ ['sms', 'workflow_sms_sent'],+ ] as const+ ).flatMap(([billingType, usageKey]) =>+ [+ { mode: 'production', isTest: false, mockFunctions: false },+ { mode: 'unmocked test', isTest: true, mockFunctions: false },+ { mode: 'mocked test', isTest: true, mockFunctions: true },+ ].map((options) => ({ billingType, usageKey, ...options }))+ )+ )(+ 'emits $billingType metrics and only excludes mocked usage in $mode mode',+ async ({ billingType, usageKey, isTest, mockFunctions }) => {const usageReporter: Pick<CdpUsageReporterService, 'reportBillableInvocation'> = {reportBillableInvocation: jest.fn(),}@@ -683,7 +694,17 @@ describe('HogFunctionHandler', () => {queuePriority: 0,})- await handler.execute({ invocation, action, result: invocationResult })+ await handler.execute({+ invocation,+ action,+ result: invocationResult,+ hogExecutorOptions: {+ isTest,+ ...(mockFunctions+ ? { asyncFunctionsNames: [], functions: { fetch: () => ({ status: 200, body: {} }) } }+ : {}),+ },+ })const billableMetrics = invocationResult.metrics.filter((metric) => metric.metric_name === 'billable_invocation'@@ -699,9 +720,14 @@ describe('HogFunctionHandler', () => {metric_name: 'billable_invocation',count: 1,})- expect(usageReporter.reportBillableInvocation).toHaveBeenCalledWith(- expect.objectContaining({ teamId: team.id, usageKey })- )+ if (isTest && mockFunctions) {+ expect(usageReporter.reportBillableInvocation).not.toHaveBeenCalled()+ } else {+ expect(usageReporter.reportBillableInvocation).toHaveBeenCalledTimes(1)+ expect(usageReporter.reportBillableInvocation).toHaveBeenCalledWith(+ expect.objectContaining({ teamId: team.id, usageKey })+ )+ }})diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.tsindex 991ffc996203..f984540a2d85 100644--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts@@ -258,12 +258,14 @@ export class HogFunctionHandler implements ActionHandler {billingMetricType: this.hogFlowActionBillingType,})- // actionStepCount holds across a retry of this step but changes on a loop revisit.- this.usageReporter?.reportBillableInvocation({- teamId: invocation.teamId,- usageKey: WORKFLOW_USAGE_KEYS[this.hogFlowActionBillingType],- recordId: `flow:${invocation.id}:${invocation.state.actionStepCount}:${this.hogFlowActionBillingType}`,- })+ if (!hogExecutorOptions?.isTest || !hogExecutorOptions.functions) {+ // actionStepCount holds across a retry of this step but changes on a loop revisit.+ this.usageReporter?.reportBillableInvocation({+ teamId: invocation.teamId,+ usageKey: WORKFLOW_USAGE_KEYS[this.hogFlowActionBillingType],+ recordId: `flow:${invocation.id}:${invocation.state.actionStepCount}:${this.hogFlowActionBillingType}`,+ })+ }}// Re-pin the attribution version to the one that actually sent. Live edits reach runs