From 76ff57bf21a08ff3dff236abb4b5ee68813549a0 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 13 Nov 2026 19:48:22 -0700 Subject: [PATCH] Pin how the two environment variables combine, and what disabled() says Either variable opting out turns reporting off whatever the other says; blank and unlisted values leave it on; DO_NOT_TRACK is trimmed; and TraceClient.disabled() gives the config reason unless the environment already opted out. Tests only. Co-Authored-By: Claude Opus 5.5 --- .../stephenson/trace/TraceClientTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/test/java/software/stephenson/trace/TraceClientTest.java b/src/test/java/software/stephenson/trace/TraceClientTest.java index 99f6b58..9f0a961 100644 --- a/src/test/java/software/stephenson/trace/TraceClientTest.java +++ b/src/test/java/software/stephenson/trace/TraceClientTest.java @@ -1165,6 +1165,86 @@ void environment_disablesAndWinsOverTheServerWideFile(@TempDir Path plugins) thr assertNull(TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k").serverWideConfig(plugins.toFile()).build().disabledReason()); } + @Test + void environment_eitherVariableAloneTurnsReportingOffWhateverTheOtherSays() { + // Arrange: one variable opts out, the other says something that is + // not an opt-out. Neither can cancel the other. + String[][] pairs = { + {"on", "1"}, + {"true", " Yes "}, + {"off", "0"}, + {"No", "false"}, + }; + + for (String[] pair : pairs) { + environment.clear(); + environment.put("TRACE_USAGE_REPORTING", pair[0]); + environment.put("DO_NOT_TRACK", pair[1]); + + // Act + TraceClient client = TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k").build(); + + // Assert + assertEquals(TraceClient.REASON_ENVIRONMENT, client.disabledReason(), + "TRACE_USAGE_REPORTING=" + pair[0] + ", DO_NOT_TRACK=" + pair[1]); + assertFalse(client.isEnabled()); + } + } + + @Test + void environment_valuesThatAreNotAnOptOutLeaveReportingOn() { + // Only the listed spellings opt out: a blank value, the opposite + // word, or anything else is not read as a wish to stop reporting. + for (String value : new String[] {"", " ", "on", "true", "yes", "1", "disabled", "offline"}) { + // Arrange + environment.clear(); + environment.put("TRACE_USAGE_REPORTING", value); + + // Act + TraceClient client = TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k").build(); + + // Assert + assertNull(client.disabledReason(), "TRACE_USAGE_REPORTING=\"" + value + "\""); + client.close(); + } + for (String value : new String[] {"", " ", "0", "false", "no", "off", "2", "y"}) { + // Arrange + environment.clear(); + environment.put("DO_NOT_TRACK", value); + + // Act + TraceClient client = TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k").build(); + + // Assert + assertNull(client.disabledReason(), "DO_NOT_TRACK=\"" + value + "\""); + client.close(); + } + } + + @Test + void disabled_isOffForTheConfigReasonUnlessTheEnvironmentSaysSoFirst() { + // Act + TraceClient byDefault = TraceClient.disabled(); + + // Assert + assertFalse(byDefault.isEnabled()); + assertEquals(TraceClient.REASON_CONFIG, byDefault.disabledReason()); + assertNull(byDefault.installId(), "a disabled client has no install ID"); + + // Arrange: the environment is checked before the client's own switch, + // so it is the reason even here. + environment.put("DO_NOT_TRACK", "1"); + + // Act + TraceClient byEnvironment = TraceClient.disabled(); + + // Assert + assertFalse(byEnvironment.isEnabled()); + assertEquals(TraceClient.REASON_ENVIRONMENT, byEnvironment.disabledReason()); + byDefault.close(); + byEnvironment.close(); + } + @Test void disabledReason_followsThePrecedenceEnvironmentThenServerWideThenConfigThenKey(@TempDir Path plugins) throws Exception { // Arrange: everything says off at once.