From ef2e552e9318ba9c4a0ae17bdfe8789986871578 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 12 Nov 2024 12:15:29 +0100 Subject: [PATCH] add test --- posthog/src/main/java/com/posthog/PostHog.kt | 7 ++-- .../src/test/java/com/posthog/PostHogTest.kt | 33 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/posthog/src/main/java/com/posthog/PostHog.kt b/posthog/src/main/java/com/posthog/PostHog.kt index aed23dec..a554d65c 100644 --- a/posthog/src/main/java/com/posthog/PostHog.kt +++ b/posthog/src/main/java/com/posthog/PostHog.kt @@ -556,7 +556,8 @@ public class PostHog private constructor( config?.logger?.log("identify called with invalid anonymousId: $anonymousId.") } - if (previousDistinctId != distinctId && !isIdentified) { + val hasDifferentDistinctId = previousDistinctId != distinctId + if (hasDifferentDistinctId && !isIdentified) { // this has to be set before capture since this flag will be read during the event // capture synchronized(identifiedLock) { @@ -583,7 +584,9 @@ public class PostHog private constructor( if (reloadFeatureFlags) { reloadFeatureFlags() } - } else if (userProperties?.isNotEmpty() == true || userPropertiesSetOnce?.isNotEmpty() == true) { + // we need to make sure the user props update is for the same user + // otherwise they have to reset and identify again + } else if (!hasDifferentDistinctId && (userProperties?.isNotEmpty() == true || userPropertiesSetOnce?.isNotEmpty() == true)) { capture( "\$set", distinctId = distinctId, diff --git a/posthog/src/test/java/com/posthog/PostHogTest.kt b/posthog/src/test/java/com/posthog/PostHogTest.kt index 5db8741d..9fc0fe6a 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -574,13 +574,44 @@ internal class PostHogTest { sut.identify( "anotherDistinctId", + ) + + queueExecutor.shutdownAndAwaitTermination() + + assertEquals(1, http.requestCount) + + sut.close() + } + + @Test + fun `captures a set event if identified`() { + val http = mockHttp() + val url = http.url("/") + + val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false, flushAt = 2) + + sut.identify( + DISTINCT_ID, + userProperties = userProps, + userPropertiesSetOnce = userPropsOnce, + ) + + sut.identify( + DISTINCT_ID, userProperties = userProps, userPropertiesSetOnce = userPropsOnce, ) queueExecutor.shutdownAndAwaitTermination() - assertEquals(1, http.requestCount) + val request = http.takeRequest() + + val content = request.body.unGzip() + val batch = serializer.deserialize(content.reader()) + + val theEvent = batch.batch.last() + + assertEquals("\$set", theEvent.event) sut.close() }