Skip to content

Commit

Permalink
Merge pull request #16 from FriendlyCaptcha/agreenberg/safely-patch-t…
Browse files Browse the repository at this point in the history
…ostring

Fix problematic patching behavior for browser objects
  • Loading branch information
greenberga authored Oct 30, 2024
2 parents c398cce + 023db40 commit 3a7ba34
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
12 changes: 10 additions & 2 deletions sdktest/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# sdktest

**sdktest** is a tool for testing our SDK in all kinds of scenario's, end-to-end, in different browsers.
**sdktest** is a tool for testing our SDK in all kinds of scenarios, end-to-end, in different browsers.

**sdktest** is not designed to be beautiful, fast or elegant, it only has to do its job well.


## Getting Started

* Ensure you've built the SDK (in the parent folder, `npm run build`)
* Install Go for your device (`brew install go`)
* Create a config file in the root of this folder called `sdktest.yaml`, [`sdktest.example.yaml`](./sdktest.example.yaml) should provide a good starting point.
* Run `go run main.go server` and point your browser at [`localhost:8912`](http://localhost:8912).
Expand All @@ -19,3 +20,10 @@ go run main.go autotest
# Or with serve, to keep the server alive too for headful debugging.
go run main.go autotest --serve
```

## A note on widget interactivity

By default, widgets require the web user to click the checkbox in order to complete. This means
that the test cases that expect a widget to complete will---under the default conditions---fail with a `TimeoutError` unless you manually click the checkbox. This is expected behavior, and you can make the tests pass by clicking the checkboxes.

There is a way to make the tests pass without requiring manual clicking, and it involves using a sitekey for an application whose widget mode is set to `noninteractive`. Widgets with that mode can complete without any additional interaction from the web user.
5 changes: 5 additions & 0 deletions sdktest/test/safe_patching/body.tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script defer>
ResizeObserver_toString = ResizeObserver.toString();
</script>
<script defer src="{{ .SiteJSPath }}"></script>
<script defer src="main.tmpl.ts"></script>
14 changes: 14 additions & 0 deletions sdktest/test/safe_patching/main.tmpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*!
* Copyright (c) Friendly Captcha GmbH 2023.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { sdktest } from "../../sdktestlib/sdk.js";

// This gets set in a <script> tag inlined in the HTML file.
declare var ResizeObserver_toString: string;

sdktest.test({ name: "loading the SDK doesn't break the `toString()` method for native browser objects" }, t => {
t.assert.equal(ResizeObserver_toString, ResizeObserver.toString());
});
5 changes: 1 addition & 4 deletions src/signals/collectStacktrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ export const takeRecords = (function () {

const originalFuncToString = Function.prototype.toString;
const newFuncToString = function toString(this: unknown, ...args: unknown[]) {
if (isFunc(this)) {
return;
}
const patchedRef = isFunc(this) ? origPatchMap.get(this) : false;
const ref = this === newFuncToString ? originalFuncToString : patchedRef ? patchedRef : this;
return originalFuncToString.apply(ref, args as []);
Expand Down Expand Up @@ -97,7 +94,7 @@ export const takeRecords = (function () {
// Values holding functions
["eval", w, "eval"],
["Object.is", w.Object, "is"],
["Array." + p + "e.slice", w.Array[p], "slice"],
["Array." + p + ".slice", w.Array[p], "slice"],
["Document." + p + ".querySelectorAll", w.Document[p], "querySelectorAll"],
["Document." + p + ".createElement", w.Document[p], "createElement"],
["EventTarget." + p + ".dispatchEvent", dispatchEvent, "dispatchEvent"],
Expand Down

0 comments on commit 3a7ba34

Please sign in to comment.