From 2517ac9cbe2b626c0f1f772c5e25fd12fcbba708 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sun, 6 Dec 2026 05:01:56 +0000 Subject: [PATCH 1/3] FEAT: Put the browser tests into the FlowData as JavaScript properties BrowserDerivedCloud is the cloud engine for browserderived. The browser tests are properties of browserderived that the accessible properties type as JavaScript. The cloud can return any number of them and no name is fixed. Through the engine the JSON bundler lists the tests, and the client script the server serves runs them and sends the answers back. The tests come only with a request that names HumanConfidence, so the engine logs nothing for a key without the product. The response cache stops looking for one name. As in the .NET package, a response is left out of the cache where a browserderived property that may be a browser test holds a value. A property may be one in three cases: - javascriptProperties lists it - the accessible properties give it the JavaScript type - the accessible properties give it no type at all The live 51Did tests find the browser tests the same way. Every browser test name in the tests is made up, so code that knows one name fails them. --- ci/check-environment-variable-names.ps1 | 4 +- .../readme.md | 8 + .../browserderived.py | 46 +++++ .../cloudengine.py | 7 +- .../cloudrequestengine.py | 63 +++++- .../constants.py | 7 +- .../tests/classes/browser_tests_harness.js | 129 ++++++++++++ .../tests/test_browserderived.py | 187 ++++++++++++++++++ .../tests/test_cloudrequestengine_cache.py | 102 +++++++--- .../tests/test_finished_writes.py | 8 +- .../tests/test_browser_tests_found.py | 56 ++++++ .../tests/test_did_client_live.py | 22 ++- 12 files changed, 599 insertions(+), 40 deletions(-) create mode 100644 pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/browserderived.py create mode 100644 pipeline/fiftyone_pipeline_cloudrequestengine/tests/classes/browser_tests_harness.js create mode 100644 pipeline/fiftyone_pipeline_cloudrequestengine/tests/test_browserderived.py create mode 100644 pipeline/fiftyone_pipeline_did/tests/test_browser_tests_found.py diff --git a/ci/check-environment-variable-names.ps1 b/ci/check-environment-variable-names.ps1 index 694469ce..9070888a 100644 --- a/ci/check-environment-variable-names.ps1 +++ b/ci/check-environment-variable-names.ps1 @@ -28,9 +28,9 @@ $ErrorActionPreference = 'Stop' $script:Prefix = '_51DEGREES_' # Names GitHub, the operating system, Python, pytest and Flask define. Any -# name starting GITHUB_ is GitHub's. +# name starting GITHUB_ is GitHub's, and GitHub sets CI on every runner. $script:PlatformNames = @( - 'FLASK_APP', 'PATH', 'PSModulePath', 'PYTEST_ADDOPTS', 'PYTHON_GIL', + 'CI', 'FLASK_APP', 'PATH', 'PSModulePath', 'PYTEST_ADDOPTS', 'PYTHON_GIL', 'PYTHONPATH', 'TEMP', 'TMP', 'TMPDIR') $script:PlatformPrefixes = @('GITHUB_') diff --git a/pipeline/fiftyone_pipeline_cloudrequestengine/readme.md b/pipeline/fiftyone_pipeline_cloudrequestengine/readme.md index fb88e8fd..08304ec7 100644 --- a/pipeline/fiftyone_pipeline_cloudrequestengine/readme.md +++ b/pipeline/fiftyone_pipeline_cloudrequestengine/readme.md @@ -168,6 +168,14 @@ client script's complete handlers run later than they otherwise would, by the time the tests take and one more request. A page can read the first response's properties sooner through its change handlers. +The `browserderived` element's JavaScript properties are the browser +tests. The client script your server serves runs them once a +`BrowserDerivedCloud` engine +(`fiftyone_pipeline_cloudrequestengine.browserderived`) follows the +`CloudRequestEngine` in the pipeline. The device detection cloud pipeline +builders add that engine for you. If you build the pipeline another way, +add it there yourself. + Where a page sets a Content Security Policy, it must allow these for the browser tests to run. Otherwise HumanConfidenceSource stays `Passive`. diff --git a/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/browserderived.py b/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/browserderived.py new file mode 100644 index 00000000..9c485af2 --- /dev/null +++ b/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/browserderived.py @@ -0,0 +1,46 @@ +# ********************************************************************* +# This Original Work is copyright of 51 Degrees Mobile Experts Limited. +# Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House, +# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. +# +# This Original Work is licensed under the European Union Public Licence +# (EUPL) v.1.2 and is subject to its terms as set out below. +# +# If a copy of the EUPL was not distributed with this file, You can obtain +# one at https://opensource.org/licenses/EUPL-1.2. +# +# The 'Compatible Licences' set out in the Appendix to the EUPL (as may be +# amended by the European Commission) shall be deemed incompatible for +# the purposes of the Work and the provisions of the compatibility +# clause in Article 5 of the EUPL shall not apply. +# +# If using the Work as, or as part of, a network application, by +# including the attribution notice(s) required under Article 5 of the EUPL +# in the end user terms of the application under an appropriate heading, +# such notice(s) shall fulfill the requirements of that article. +# ********************************************************************* + +from .cloudengine import CloudEngine + + +class BrowserDerivedCloud(CloudEngine): + + """! + Cloud engine for the "browserderived" section of the cloud response, + whose properties are available as flowdata.browserderived. + + The section is read as every cloud engine reads its own, and each + property takes its type from the Cloud Request Engine's accessible + properties. Properties whose metadata gives the JavaScript type are + the browser tests that come with HumanConfidence. The JSON bundler + lists those and the client script runs them. The cloud service can + return any number of them under any names, and none is written here. + + A key without the browserderived product logs no warning, because the + browser tests only come with a request that names HumanConfidence. + """ + + def __init__(self): + super(BrowserDerivedCloud, self).__init__() + self.datakey = "browserderived" + self.warns_on_missing_product = False diff --git a/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/cloudengine.py b/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/cloudengine.py index 7a4579a6..b95177da 100644 --- a/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/cloudengine.py +++ b/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/cloudengine.py @@ -91,6 +91,10 @@ def __init__(self): # Whether set_cache has logged that this engine ignores a cache. self._cache_ignored_logged = False + # Whether to log a key that carries no properties for this engine. + # Set it False in an engine whose product only comes with some + # requests. + self.warns_on_missing_product = True def get_evidence_key_filter(self): @@ -219,7 +223,8 @@ def _load_properties(self, log): # at all. That is not a configuration error, so the engine has # no properties and the missing product is logged here, once. if not self.datakey in carried: - log("warning", "The key in use does not include access to any properties under the engine with key " + self.datakey + " that was added to the pipeline. For more details on resource keys, see our explainer: https://51degrees.com/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=comment&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine-src-fiftyone_pipeline_cloudrequestengine-cloudengine.py&utm_term=engine-properties-not-accessible " + "Available engine data keys are: " + str([e for e in carried])) + if getattr(self, "warns_on_missing_product", True): + log("warning", "The key in use does not include access to any properties under the engine with key " + self.datakey + " that was added to the pipeline. For more details on resource keys, see our explainer: https://51degrees.com/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=comment&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine-src-fiftyone_pipeline_cloudrequestengine-cloudengine.py&utm_term=engine-properties-not-accessible " + "Available engine data keys are: " + str([e for e in carried])) self.properties = {} else: self.properties = carried[self.datakey] diff --git a/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/cloudrequestengine.py b/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/cloudrequestengine.py index fdb61810..c262b6ca 100644 --- a/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/cloudrequestengine.py +++ b/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/cloudrequestengine.py @@ -85,6 +85,60 @@ def _properties(answer, data_key, name): yield value +def _browser_tests(answer, accessible): + + """! + The value of every browserderived Property in a response that may be a + JavaScript Property, told the way the .NET package tells one. A + Property counts in any of three cases: + + - the response's javascriptProperties lists it as browserderived and + its name joined by a dot, compared in any case + - the accessible Properties give it the JavaScript type, also compared + in any case + - the accessible Properties give it no type at all + + A Property is left out only where they type it as something else and + the list does not have it, so the rule can only refuse to cache. The + members that give null reasons are not Properties. + + @type answer: dict + @param answer: the response's JSON, read as an object + @type accessible: dict + @param accessible: the accessible Properties by product data key, or + None while they are not known + """ + + listed = None + for key, value in answer.items(): + if str(key).lower() == Constants.JAVASCRIPT_PROPERTIES_MEMBER: + if isinstance(value, list): + listed = {str(entry).lower() for entry in value + if isinstance(entry, str)} + types = {} + for key, product in (accessible or {}).items(): + if str(key).lower() == Constants.BROWSER_TESTS_DATA_KEY \ + and isinstance(product, dict): + for name, meta in product.items(): + value_type = meta.get("type") if isinstance(meta, dict) \ + else None + if value_type: + types[str(name).lower()] = str(value_type).lower() + for key, section in answer.items(): + if str(key).lower() != Constants.BROWSER_TESTS_DATA_KEY \ + or not isinstance(section, dict): + continue + for member, value in section.items(): + name = str(member).lower() + if name.endswith("nullreason"): + continue + value_type = types.get(name) + if (listed is not None and + Constants.BROWSER_TESTS_DATA_KEY + "." + name in listed) \ + or value_type in (None, "javascript"): + yield value + + def _takes_timeout(client): """! @@ -1039,8 +1093,8 @@ def is_cacheable(self, data): - A sealed Human Confidence result, being a HumanConfidence under derived that humanconfidence.is_sealed_value finds sealed. HumanConfidenceSource plays no part. - - The browser tests' script, being a BrowserTestsJavaScript under - browserderived that is not blank. + - A browser test, being a JavaScript Property of browserderived + that is not blank, told as _browser_tests tells one. A request made for one visitor never reaches the cache, as process and bypasses_cache say. A response that cannot be read as a JSON @@ -1069,9 +1123,8 @@ def is_cacheable(self, data): Constants.HUMAN_CONFIDENCE_PROPERTY)): return False return all(humanconfidence.is_blank(value) - for value in _properties( - answer, Constants.BROWSER_TESTS_DATA_KEY, - Constants.BROWSER_TESTS_PROPERTY)) + for value in _browser_tests( + answer, self.known_flow_element_properties())) def report_properties_not_covered(self, result): diff --git a/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/constants.py b/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/constants.py index 01d2e1a0..4e408b68 100644 --- a/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/constants.py +++ b/pipeline/fiftyone_pipeline_cloudrequestengine/src/fiftyone_pipeline_cloudrequestengine/constants.py @@ -162,13 +162,14 @@ class Constants: # The members of a response that hold a value made for one visitor, # compared in any case. See CloudRequestEngine.is_cacheable. A 51Did is - # a top-level member. HumanConfidence and the browser tests' script are - # properties under the data key of the element that gives them. + # a top-level member. HumanConfidence is a property under derived. The + # browser tests are the JavaScript Properties of browserderived, and + # the response's javascriptProperties member lists them. DID_MEMBER = "fodid" HUMAN_CONFIDENCE_DATA_KEY = "derived" HUMAN_CONFIDENCE_PROPERTY = "humanconfidence" BROWSER_TESTS_DATA_KEY = "browserderived" - BROWSER_TESTS_PROPERTY = "browsertestsjavascript" + JAVASCRIPT_PROPERTIES_MEMBER = "javascriptproperties" # Takes the place of the licence key in every message the engine # builds, and of the sealed result and the transaction in a message made diff --git a/pipeline/fiftyone_pipeline_cloudrequestengine/tests/classes/browser_tests_harness.js b/pipeline/fiftyone_pipeline_cloudrequestengine/tests/classes/browser_tests_harness.js new file mode 100644 index 00000000..eabcb04f --- /dev/null +++ b/pipeline/fiftyone_pipeline_cloudrequestengine/tests/classes/browser_tests_harness.js @@ -0,0 +1,129 @@ +// Runs a rendered client script in Node against a minimal stand in for a +// browser, and prints every request the script sends back to the server. +// +// Usage: node browser_tests_harness.js