diff --git a/test/integration_test.exs b/test/integration_test.exs index ef8c887..9c000c0 100644 --- a/test/integration_test.exs +++ b/test/integration_test.exs @@ -33,7 +33,8 @@ defmodule PostHog.IntegrationTest do end end - :logger.add_handler(:posthog, PostHog.Handler, %{config: config}) + :ok = :logger.add_handler(:posthog, PostHog.Handler, %{config: config}) + on_exit(fn -> :logger.remove_handler(:posthog) end) %{wait_fun: wait} end @@ -98,18 +99,25 @@ defmodule PostHog.IntegrationTest do end test "supervisor report", %{wait_fun: wait} do - Application.stop(:logger) - Application.put_env(:logger, :handle_sasl_reports, true) - Application.put_env(:logger, :level, :info) - Application.start(:logger) + original = + Map.new([:handle_sasl_reports, :level], &{&1, Application.fetch_env(:logger, &1)}) on_exit(fn -> Application.stop(:logger) - Application.put_env(:logger, :handle_sasl_reports, false) - Application.delete_env(:logger, :level) + + Enum.each(original, fn + {key, {:ok, value}} -> Application.put_env(:logger, key, value) + {key, :error} -> Application.delete_env(:logger, key) + end) + Application.start(:logger) end) + Application.stop(:logger) + Application.put_env(:logger, :handle_sasl_reports, true) + Application.put_env(:logger, :level, :info) + Application.start(:logger) + LoggerHandlerKit.Act.supervisor_progress_report(:failed_to_start_child) wait.() end @@ -117,7 +125,7 @@ defmodule PostHog.IntegrationTest do describe "event capture" do test "captures event", %{test: test, wait_fun: wait} do - PostHog.capture("case tested", test, %{number: 1}) + assert :ok = PostHog.capture("case tested", %{distinct_id: test, number: 1}) wait.() end end diff --git a/test/mix/tasks/posthog_package_source_code_test.exs b/test/mix/tasks/posthog_package_source_code_test.exs new file mode 100644 index 0000000..17a6f0b --- /dev/null +++ b/test/mix/tasks/posthog_package_source_code_test.exs @@ -0,0 +1,69 @@ +defmodule Mix.Tasks.Posthog.PackageSourceCodeTest do + use ExUnit.Case, async: false + + alias Mix.Tasks.Posthog.PackageSourceCode + alias PostHog.ErrorTracking.Sources + + @moduletag :tmp_dir + + setup %{tmp_dir: dir} do + on_exit(fn -> File.rm_rf!(dir) end) + keys = [:root_source_code_paths, :source_code_path_pattern, :source_code_exclude_patterns] + original = Map.new(keys, &{&1, Application.fetch_env(:posthog, &1)}) + Enum.each(keys, &Application.delete_env(:posthog, &1)) + + on_exit(fn -> + Enum.each(original, fn + {key, {:ok, value}} -> Application.put_env(:posthog, key, value) + {key, :error} -> Application.delete_env(:posthog, key) + end) + end) + end + + test "CLI roots override config and package multiple roots with default exclusions", %{ + tmp_dir: dir + } do + first = Path.join(dir, "first") + second = Path.join(dir, "second") + output = Path.join([dir, "output", "source.map"]) + File.mkdir_p!(Path.join(first, "lib")) + File.mkdir_p!(second) + File.write!(Path.join(first, "lib/one.ex"), "first line\nsecond line\n") + File.write!(Path.join(second, "two.ex"), "other root\n") + File.write!(Path.join(second, "ignored.exs"), "not production source") + + for excluded <- ["_build", "priv", "test"] do + File.mkdir_p!(Path.join(first, excluded)) + File.write!(Path.join([first, excluded, "ignored.ex"]), "excluded source") + end + + Application.put_env(:posthog, :root_source_code_paths, [Path.join(dir, "unused")]) + + ExUnit.CaptureIO.capture_io(fn -> + PackageSourceCode.run(["--root-path", first, "--root-path", second, "--output", output]) + end) + + assert {:ok, + %{ + "lib/one.ex" => %{1 => "first line", 2 => "second line"}, + "two.ex" => %{1 => "other root"} + }} == Sources.decode_source_map(File.read!(output)) + end + + test "uses configured roots, pattern and exclusions when no CLI root is supplied", %{ + tmp_dir: dir + } do + File.write!(Path.join(dir, "keep.exs"), "included\n") + File.write!(Path.join(dir, "skip.exs"), "excluded\n") + File.write!(Path.join(dir, "other.ex"), "wrong extension\n") + Application.put_env(:posthog, :root_source_code_paths, [dir]) + Application.put_env(:posthog, :source_code_path_pattern, "*.exs") + Application.put_env(:posthog, :source_code_exclude_patterns, [~r/^skip/]) + output = Path.join(dir, "source.map") + + ExUnit.CaptureIO.capture_io(fn -> PackageSourceCode.run(["-o", output]) end) + + assert {:ok, %{"keep.exs" => %{1 => "included"}}} == + Sources.decode_source_map(File.read!(output)) + end +end diff --git a/test/mix/tasks/posthog_public_api_test.exs b/test/mix/tasks/posthog_public_api_test.exs new file mode 100644 index 0000000..a420289 --- /dev/null +++ b/test/mix/tasks/posthog_public_api_test.exs @@ -0,0 +1,66 @@ +defmodule Mix.Tasks.Posthog.PublicApiTest do + use ExUnit.Case, async: false + + alias Mix.Tasks.Posthog.PublicApi + + @moduletag :tmp_dir + + setup %{tmp_dir: dir} do + on_exit(fn -> File.rm_rf!(dir) end) + end + + test "committed snapshot matches the public API" do + assert ExUnit.CaptureIO.capture_io(fn -> PublicApi.run(["--check"]) end) =~ + "Public API snapshot is up to date" + end + + test "update creates parent directories and writes the committed contract", %{tmp_dir: dir} do + path = Path.join([dir, "nested", "api.snapshot"]) + + ExUnit.CaptureIO.capture_io(fn -> PublicApi.run(["--update", "--snapshot", path]) end) + + assert File.read!(path) == File.read!("public_api.snapshot") + + assert ExUnit.CaptureIO.capture_io(fn -> PublicApi.run(["--snapshot", path]) end) =~ + "Public API snapshot is up to date" + end + + test "check rejects a stale snapshot without overwriting it", %{tmp_dir: dir} do + path = Path.join(dir, "api.snapshot") + File.write!(path, "obsolete API\n") + + error = + assert_raise Mix.Error, fn -> PublicApi.run(["--check", "--snapshot", path]) end + + assert error.message =~ "Public API snapshot is out of date" + assert error.message =~ "-obsolete API" + assert error.message =~ "+PostHog" + assert File.read!(path) == "obsolete API\n" + end + + test "check reports a missing snapshot without creating it", %{tmp_dir: dir} do + path = Path.join(dir, "missing.snapshot") + + assert_raise Mix.Error, ~r/Public API snapshot does not exist/, fn -> + PublicApi.run(["--snapshot", path]) + end + + refute File.exists?(path) + end + + test "check reports unreadable snapshot paths", %{tmp_dir: dir} do + assert_raise Mix.Error, ~r/Could not read .*: :eisdir/, fn -> + PublicApi.run(["--snapshot", dir]) + end + end + + test "check and update cannot be combined", %{tmp_dir: dir} do + path = Path.join(dir, "api.snapshot") + + assert_raise Mix.Error, "Pass only one of --check or --update", fn -> + PublicApi.run(["--check", "--update", "--snapshot", path]) + end + + refute File.exists?(path) + end +end diff --git a/test/posthog/api/client_test.exs b/test/posthog/api/client_test.exs index 4fc2158..0afe154 100644 --- a/test/posthog/api/client_test.exs +++ b/test/posthog/api/client_test.exs @@ -6,7 +6,10 @@ defmodule PostHog.API.ClientTest do test "client/2 sets the posthog-elixir User-Agent" do %Client{client: req} = Client.client("phc_test", "https://us.i.posthog.com") - assert req.headers["user-agent"] == [Client.user_agent()] + version = Application.spec(:posthog, :vsn) |> to_string() + expected = "posthog-elixir/#{version}" + assert Client.user_agent() == expected + assert req.headers["user-agent"] == [expected] end for {case_name, response_or_exception, expected} <- [ @@ -45,7 +48,7 @@ defmodule PostHog.API.ClientTest do send( parent, {:request, Req.Request.fetch_option(req, :compress_body), - Req.Request.get_header(req, "content-encoding")} + Req.Request.get_header(req, "content-encoding"), req.body} ) {req, Req.Response.new(status: 200, body: %{})} @@ -53,7 +56,7 @@ defmodule PostHog.API.ClientTest do ) assert {:ok, %{status: 200}} = Client.request(req, :post, "/", []) - assert_received {:request, {:ok, false}, []} + assert_received {:request, {:ok, false}, [], [123_456]} end test "request retries keep the compressed body reusable" do @@ -78,6 +81,13 @@ defmodule PostHog.API.ClientTest do assert_received {:request, ["gzip"], first_body} assert_received {:request, ["gzip"], second_body} assert_received {:request, ["gzip"], third_body} + + assert first_body |> :zlib.gunzip() |> Jason.decode!() == %{ + "event" => "test", + "api_key" => nil + } + + assert Agent.get(calls, & &1) == 3 assert :zlib.gunzip(first_body) == :zlib.gunzip(second_body) assert :zlib.gunzip(second_body) == :zlib.gunzip(third_body) end diff --git a/test/posthog/context_test.exs b/test/posthog/context_test.exs index af5f313..12b7e6f 100644 --- a/test/posthog/context_test.exs +++ b/test/posthog/context_test.exs @@ -44,10 +44,10 @@ defmodule PostHog.ContextTest do Context.set(:all, :all, %{hello: "world"}) Logger.metadata(foo: "baz") - assert %{foo: "bar", hello: "world"} = Context.get(PostHog, "$exception") - assert %{foo: "baz", hello: "world"} = Context.get(MyPostHog, "$exception") - assert %{hello: "world"} = Context.get(MyPostHog, "$exception_list") - assert %{hello: "world"} = Context.get(FooBar, "$exception") + assert Context.get(PostHog, "$exception") == %{foo: "bar", hello: "world"} + assert Context.get(MyPostHog, "$exception") == %{foo: "baz", hello: "world"} + assert Context.get(MyPostHog, "$exception_list") == %{hello: "world"} + assert Context.get(FooBar, "$exception") == %{hello: "world"} end test "in case of overlapping keys prefer more specific scope" do diff --git a/test/posthog/error_tracking/sources_test.exs b/test/posthog/error_tracking/sources_test.exs index eff441c..d3f41cd 100644 --- a/test/posthog/error_tracking/sources_test.exs +++ b/test/posthog/error_tracking/sources_test.exs @@ -181,28 +181,35 @@ defmodule PostHog.ErrorTracking.SourcesTest do assert source_lines[line_number] == source_line end - test "excludes directories matching the default patterns" do - # Default patterns use ~r"/dir/" — match mid-path occurrences. - # Explicitly verify the custom-pattern path works correctly. - result = - Sources.load_files( - root_source_code_paths: [File.cwd!()], - source_code_exclude_patterns: [~r"/posthog/", ~r"/mix/"] - ) - - refute Enum.any?(result, fn {path, _} -> - String.contains?(path, "/posthog/") or String.contains?(path, "/mix/") - end) - end - - test "respects custom exclude patterns" do - result = - Sources.load_files( - root_source_code_paths: [File.cwd!()], - source_code_exclude_patterns: [~r"/posthog/"] - ) - - refute Enum.any?(result, fn {path, _} -> String.contains?(path, "/posthog/") end) + @tag :tmp_dir + test "excludes directories matching the default patterns", %{tmp_dir: root} do + on_exit(fn -> File.rm_rf!(root) end) + + for path <- ["lib/keep.ex", "_build/drop.ex", "priv/drop.ex", "test/drop.ex"] do + absolute = Path.join(root, path) + File.mkdir_p!(Path.dirname(absolute)) + File.write!(absolute, "sentinel\n") + end + + assert %{"lib/keep.ex" => %{1 => "sentinel"}} == + Sources.load_files(root_source_code_paths: [root]) + end + + @tag :tmp_dir + test "respects custom exclude patterns", %{tmp_dir: root} do + on_exit(fn -> File.rm_rf!(root) end) + + for path <- ["lib/keep.ex", "lib/posthog/drop.ex"] do + absolute = Path.join(root, path) + File.mkdir_p!(Path.dirname(absolute)) + File.write!(absolute, "sentinel\n") + end + + assert %{"lib/keep.ex" => %{1 => "sentinel"}} == + Sources.load_files( + root_source_code_paths: [root], + source_code_exclude_patterns: [~r"/posthog/"] + ) end end end diff --git a/test/posthog/feature_flags/definition_loader_test.exs b/test/posthog/feature_flags/definition_loader_test.exs index b77a04b..2acc897 100644 --- a/test/posthog/feature_flags/definition_loader_test.exs +++ b/test/posthog/feature_flags/definition_loader_test.exs @@ -260,19 +260,26 @@ defmodule PostHog.FeatureFlags.DefinitionLoaderTest do for status <- statuses do name = Module.concat(__MODULE__, "Status#{status}") - start_supervised!({PostHog.Supervisor, config(name, feature_flags_poll_interval_ms: 20)}) + + start_supervised!( + {PostHog.Supervisor, config(name, feature_flags_poll_interval_ms: 60_000)} + ) + assert DefinitionLoader.ready?(name) capture_log(fn -> DefinitionLoader.refresh(name) end) assert_receive {:status_request, ^status} if status == 429 do assert DefinitionLoader.ready?(name) - refute_receive {:status_request, 429}, 100 + loader = GenServer.whereis(PostHog.Registry.via(name, DefinitionLoader)) + state = :sys.get_state(loader) + assert state.quota_backoff_ms == 120_000 + assert Process.read_timer(state.timer_ref) > 60_000 else refute DefinitionLoader.ready?(name) end - stop_supervised(name) + assert :ok = stop_supervised(name) end end @@ -340,12 +347,12 @@ defmodule PostHog.FeatureFlags.DefinitionLoaderTest do assert Agent.get(activity, & &1.max) == 1 end - test "blocked definition requests are canceled before bounded shutdown and provider cleanup" do + test "timed-out definition requests are killed before bounded shutdown and provider cleanup" do owner = self() stub_with(PostHog.API.Mock, PostHog.API.Stub) expect(PostHog.API.Mock, :request, fn :stub_client, :get, "/flags/definitions", _opts -> - send(owner, :definition_request_started) + send(owner, {:definition_request_started, self()}) receive do: (:never -> :ok) end) @@ -357,7 +364,16 @@ defmodule PostHog.FeatureFlags.DefinitionLoaderTest do ) start_supervised!({PostHog.Supervisor, cfg}) - assert_receive :definition_request_started + assert_receive {:definition_request_started, request_worker} + request_monitor = Process.monitor(request_worker) + assert_receive {:DOWN, ^request_monitor, :process, ^request_worker, _reason} + refute Process.alive?(request_worker) + + loader = GenServer.whereis(PostHog.Registry.via(__MODULE__.Blocked, DefinitionLoader)) + assert :sys.get_state(loader).initial_load_complete? + refute DefinitionLoader.ready?(__MODULE__.Blocked) + refute_received :provider_shutdown + started = System.monotonic_time(:millisecond) assert :ok = stop_supervised(__MODULE__.Blocked) assert System.monotonic_time(:millisecond) - started < 500 @@ -449,7 +465,7 @@ defmodule PostHog.FeatureFlags.DefinitionLoaderTest do send(loader, {:boundary_result, make_ref(), self(), {:ok, :late}}) send(loader, {:DOWN, make_ref(), :process, self(), :normal}) send(loader, {:unexpected, :message}) - Process.sleep(10) + assert :sys.get_state(loader).definition_generation == state.definition_generation assert Process.alive?(loader) assert DefinitionLoader.ready?(__MODULE__.LateMessages) end diff --git a/test/posthog/feature_flags/flag_definition_cache_provider_test.exs b/test/posthog/feature_flags/flag_definition_cache_provider_test.exs index 93e1e8b..6455144 100644 --- a/test/posthog/feature_flags/flag_definition_cache_provider_test.exs +++ b/test/posthog/feature_flags/flag_definition_cache_provider_test.exs @@ -183,6 +183,11 @@ defmodule PostHog.FeatureFlags.FlagDefinitionCacheProviderTest do owner = self() + stub(PostHog.API.Mock, :request, fn _client, _method, _path, _opts -> + send(owner, :unexpected_api_fetch) + {:error, :unexpected_api_fetch} + end) + {:ok, provider} = Agent.start_link(fn -> %{owner: owner, decision: false, read: envelope("cached"), store: :ok, shutdown: :ok} @@ -196,6 +201,7 @@ defmodule PostHog.FeatureFlags.FlagDefinitionCacheProviderTest do assert definitions.minimal_flag_called_events assert :ok = stop_supervised(__MODULE__.Cached) assert_receive :shutdown + refute_receive :unexpected_api_fetch end test "positive decision fetches, publishes, and stores the complete envelope" do diff --git a/test/posthog/feature_flags/local_evaluation_integration_test.exs b/test/posthog/feature_flags/local_evaluation_integration_test.exs index 4bd62b1..9edd92d 100644 --- a/test/posthog/feature_flags/local_evaluation_integration_test.exs +++ b/test/posthog/feature_flags/local_evaluation_integration_test.exs @@ -602,6 +602,7 @@ defmodule PostHog.FeatureFlags.LocalEvaluationIntegrationTest do start_instance(__MODULE__.Dependencies) assert {:ok, true} = FeatureFlags.check(__MODULE__.Dependencies, "dependent", "user") + PostHog.set_context(__MODULE__.Dependencies, %{"$feature/dependent" => "sentinel"}) before_context = PostHog.get_context(__MODULE__.Dependencies) captured_count = length(PostHog.Test.all_captured(__MODULE__.Dependencies)) @@ -609,7 +610,7 @@ defmodule PostHog.FeatureFlags.LocalEvaluationIntegrationTest do FeatureFlags.get_feature_flag_result( __MODULE__.Dependencies, "dependent", - "user", + "silent-user", send_event: false ) diff --git a/test/posthog/feature_flags/local_evaluator_test.exs b/test/posthog/feature_flags/local_evaluator_test.exs index 00bd064..500c02c 100644 --- a/test/posthog/feature_flags/local_evaluator_test.exs +++ b/test/posthog/feature_flags/local_evaluator_test.exs @@ -568,15 +568,23 @@ defmodule PostHog.FeatureFlags.LocalEvaluatorTest do }) |> put_in(["filters", "payloads"], %{"control" => ~s({"color":"blue"}), "test" => "false"}) - result = evaluate(flag).results["variant"] - assert result.variant in ["control", "test"] - assert result.payload in [%{"color" => "blue"}, false] + context = %{distinct_id: "user-1"} + + assert %Result{variant: "control", payload: %{"color" => "blue"}} = + evaluate(flag, context).results["variant"] + + assert %Result{variant: "test", payload: false} = + evaluate(flag, %{distinct_id: "user"}).results["variant"] override = put_in(flag, ["filters", "groups", Access.at(0), "variant"], "test") - assert %Result{variant: "test", payload: false} = evaluate(override).results["variant"] + + assert %Result{variant: "test", payload: false} = + evaluate(override, context).results["variant"] invalid = put_in(flag, ["filters", "groups", Access.at(0), "variant"], "missing") - assert evaluate(invalid).results["variant"].variant == result.variant + + assert %Result{variant: "control", payload: %{"color" => "blue"}} = + evaluate(invalid, context).results["variant"] boolean = put_in(flag("boolean-payload"), ["filters", "payloads"], %{"true" => "true"}) assert %Result{payload: true} = evaluate(boolean).results["boolean-payload"] @@ -744,14 +752,15 @@ defmodule PostHog.FeatureFlags.LocalEvaluatorTest do }).results["timezone-less"].enabled end - for property <- [ - %{"key" => "x", "operator" => "unknown", "value" => 1}, - %{"key" => "x", "operator" => "regex", "value" => "["}, - %{"key" => "x", "operator" => "is_date_before", "value" => "never"}, - %{"key" => "x", "operator" => "semver_eq", "value" => "01.2.3"} + for {property, actual} <- [ + {%{"key" => "x", "operator" => "unknown", "value" => 1}, 1}, + {%{"key" => "x", "operator" => "regex", "value" => "["}, "hello123"}, + {%{"key" => "x", "operator" => "is_date_before", "value" => "never"}, "2025-01-01"}, + {%{"key" => "x", "operator" => "semver_eq", "value" => "01.2.3"}, "1.2.3"} ] do - result = evaluate(flag("bad", [property]), %{person_properties: %{x: "bad"}}) - assert MapSet.member?(result.unresolved, "bad") + result = evaluate(flag("bad", [property]), %{person_properties: %{x: actual}, now: now}) + assert result.results == %{} + assert result.unresolved == MapSet.new(["bad"]) end end diff --git a/test/posthog/handler_test.exs b/test/posthog/handler_test.exs index 4b0600f..86b2ef3 100644 --- a/test/posthog/handler_test.exs +++ b/test/posthog/handler_test.exs @@ -1577,6 +1577,13 @@ defmodule PostHog.HandlerTest do String.ends_with?(type_end, "caused shutdown") ]) end + + types = + Enum.map(events, fn %{properties: %{"$exception_list": [%{type: type}]}} -> type end) + + for suffix <- ["started", "terminated", "caused shutdown"] do + assert Enum.count(types, &String.ends_with?(&1, suffix)) == 1 + end end @tag config: [metadata: [:extra]] @@ -1602,6 +1609,8 @@ defmodule PostHog.HandlerTest do ] } } = event + + refute Map.has_key?(event.properties, :hello) end @tag config: [metadata: :all] diff --git a/test/posthog/integrations/llm_analytics/req_test.exs b/test/posthog/integrations/llm_analytics/req_test.exs index 1624004..5fcb5e5 100644 --- a/test/posthog/integrations/llm_analytics/req_test.exs +++ b/test/posthog/integrations/llm_analytics/req_test.exs @@ -94,7 +94,12 @@ defmodule PostHog.Integrations.LLMAnalytics.ReqTest do "$ai_provider": "openai", "$ai_request_url": "https://api.openai.com/v1/responses", "$ai_is_error": true, - "$ai_error": %{} + "$ai_error": %{ + "code" => "insufficient_quota", + "type" => "insufficient_quota", + "message" => + "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors." + } } } ] = all_captured(@supervisor_name) diff --git a/test/posthog/llm_analytics_test.exs b/test/posthog/llm_analytics_test.exs index 95f575a..e625555 100644 --- a/test/posthog/llm_analytics_test.exs +++ b/test/posthog/llm_analytics_test.exs @@ -406,31 +406,31 @@ defmodule Posthog.LLMAnalyticsTest do end test "no properties" do - assert {:ok, _span_id} = LLMAnalytics.capture_current_span("$ai_generation") + assert {:ok, span_id} = LLMAnalytics.capture_current_span("$ai_generation") assert [event] = all_captured() assert %{ event: "$ai_generation", - properties: %{} + properties: %{"$ai_span_id": ^span_id} } = event end @tag config: [supervisor_name: MyPostHog] test "custom PostHog instance" do PostHog.set_context(MyPostHog, %{distinct_id: "foo"}) - assert {:ok, _span_id} = LLMAnalytics.capture_current_span(MyPostHog, "$ai_generation") + assert {:ok, first_id} = LLMAnalytics.capture_current_span(MyPostHog, "$ai_generation") - assert {:ok, _span_id} = + assert {:ok, second_id} = LLMAnalytics.capture_current_span(MyPostHog, "$ai_generation", %{foo: "bar"}) assert [ %{ event: "$ai_generation", - properties: %{foo: "bar"} + properties: %{foo: "bar", "$ai_span_id": ^second_id} }, %{ event: "$ai_generation", - properties: %{} + properties: %{"$ai_span_id": ^first_id} } ] = all_captured(MyPostHog) end @@ -480,6 +480,8 @@ defmodule Posthog.LLMAnalyticsTest do event: "$ai_generation", properties: %{bar: "baz", "$ai_parent_id": ^current_span_id, "$ai_span_id": ^id} } = event + + assert %{"$ai_span_id": ^current_span_id, foo: "bar"} = LLMAnalytics.pop_span() end test "no properties" do diff --git a/test/posthog/sender_test.exs b/test/posthog/sender_test.exs index 27bbdb0..94c7b18 100644 --- a/test/posthog/sender_test.exs +++ b/test/posthog/sender_test.exs @@ -253,6 +253,7 @@ defmodule PostHog.SenderTest do [{^pid, :available}] = Registry.lookup(registry, {PostHog.Sender, 1}) send(pid, {:timeout, ref, :batch_time_reached}) + assert %{events: [], num_events: 0, timer_ref: nil} = :sys.get_state(pid) refute_receive :ready end end diff --git a/test/posthog_test.exs b/test/posthog_test.exs index 986d9bf..299a63d 100644 --- a/test/posthog_test.exs +++ b/test/posthog_test.exs @@ -293,8 +293,8 @@ defmodule PostHogTest do end test "includes relevant event context" do - PostHog.set_context(%{hello: "world", distinct_id: "distinct_id"}) - PostHog.set_event_context("case tested", %{foo: "bar"}) + PostHog.set_context(%{hello: "world", distinct_id: "distinct_id", final: "global"}) + PostHog.set_event_context("case tested", %{foo: "bar", final: "event"}) PostHog.set_context(MyPostHog, %{spam: "eggs"}) PostHog.capture("case tested", %{final: "override"}) @@ -310,6 +310,9 @@ defmodule PostHogTest do }, timestamp: _ } = event + + refute Map.has_key?(event.properties, :spam) + refute Map.has_key?(event.properties, :distinct_id) end end