Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map k6 browser API to Goja #673

Merged
merged 16 commits into from
Dec 15, 2022
Merged

Conversation

inancgumus
Copy link
Member

@inancgumus inancgumus commented Dec 13, 2022

As discussed in issue #661, we're now "manually" mapping the browser module methods to Goja. The global Goja field name mappers are removed from the code. This can be automated in the future as the mapping can be error-prone.

The current mappers are just forwarders to the inner methods, and we add $ and $$ for the Query and QueryAll methods.

Let me know if I missed something while mapping the methods; and whether the way we solved it is good enough.

Closes #661.

@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch from b142322 to 4fa0f6b Compare December 13, 2022 12:38
@inancgumus inancgumus marked this pull request as ready for review December 13, 2022 12:44
@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch from 4fa0f6b to f46a236 Compare December 13, 2022 13:18
@inancgumus inancgumus changed the base branch from main to fix/ci-linter-check-dependencies December 13, 2022 13:18
@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch from f46a236 to 995b4ed Compare December 13, 2022 13:21
@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch 2 times, most recently from 21ad06b to 2e2e9ba Compare December 13, 2022 14:13
@inancgumus inancgumus requested a review from mstoykov December 13, 2022 14:51
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left some comments, but looks good in general.

I would not be able to guess if that is enough to not break anything, but I would argue (again) that you should have more JS code tests that use the API as users will do.

This will also help with making certain that this mapping are what you want - as it will exercise them ;)

browser/module.go Outdated Show resolved Hide resolved
browser/module.go Outdated Show resolved Hide resolved
browser/module.go Outdated Show resolved Hide resolved
browser/module.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like there are lot of things that could go wrong (i'm not saying they have) i.e. the mappings could be incorrect. Is there anyway we can test the objects that are now manually being created and mapped?

browser/module.go Outdated Show resolved Hide resolved
browser/module.go Outdated Show resolved Hide resolved
browser/module.go Outdated Show resolved Hide resolved
@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch from 2e2e9ba to 812479f Compare December 14, 2022 07:08
Base automatically changed from fix/ci-linter-check-dependencies to main December 14, 2022 08:04
@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch from 0c388d5 to 29f50b2 Compare December 14, 2022 08:05
@inancgumus
Copy link
Member Author

inancgumus commented Dec 14, 2022

@ankur22, thanks for your review 🙇

It feels like there are lot of things that could go wrong (i'm not saying they have) i.e. the mappings could be incorrect. Is there anyway we can test the objects that are now manually being created and mapped?

Are we having to mapRequest and mapResponse because it can return a frame, which in turn can return pages? Seems like this is going to catch people out as we will forget to add new methods to the mappings. I preferred the approach where we looped through all the methods and just set them to the object, like above in mapBrowserToGoja.

mapBrowserToGoja is the root mapping, that's why it has a loop. We automatically add the other mappings thanks to the root mapping, so they don't need a loop. The main concern of Mihail was magically mapping the methods, so this PR is about manually doing it and locking in the behavior with tests.

I agree that the solution we have in place is not future-proof, but it's good enough for now. In the future, we should consider automating or generating this mapping code.

I've added some tests to verify the behavior, which you can find in the following commits:

On the bright side, users will receive an error message if something goes wrong and a method can't be found. With the tests in place, the likelihood of that happening should be low. Since the tests test whether all API methods (api/) is in the mappings or not. We should add more JS tests, though.

Additionally, these manual mappings will allow us to move the goja layer from our domain logic code, making it more type-safe and easier to work with. I'll create an issue about that soon.

Let me know if you have any questions or concerns.


Related:

The PR description:

As discussed in the issue #661, we're now "manually" mapping the browser module methods to Goja. The global Goja field name mappers are removed from the code. This can be automated in the future as the mapping can be error-prone.

Also here:

However, not sure the current solution will be future-proof. Since we have a relatively large API surface area; it can be error-prone to manually do this kind of mappings. I believe we might want to automate this in the future (maybe by writing a source-code generator). That way the resulting mapping can be in the code itself and people can easily track them without magic.

@inancgumus
Copy link
Member Author

inancgumus commented Dec 14, 2022

I have left some comments, but looks good in general.

@mstoykov, thank you for your review 🙇

I would not be able to guess if that is enough to not break anything, but I would argue (again) that you should have more JS code tests that use the API as users will do.
This will also help with making certain that this mapping are what you want - as it will exercise them ;)

We now have tests to check whether we map the entire API surface that have the wildcard methods. I agree we should have more JS tests, preferably in the examples/ folder. I believe we should work on this next.

I'm also thinking of changing the directory name to jstest or something like that. Since they won't be examples anymore and we already have examples in the k6-browser docs.

Copy link
Collaborator

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I've left some minor comments.

Having looked at the unit tests you've added in this, i'm not sure adding JS tests would add any more benefit to this, I could be wrong though.

browser/module.go Outdated Show resolved Hide resolved
browser/module.go Outdated Show resolved Hide resolved
browser/module_test.go Outdated Show resolved Hide resolved
@mstoykov
Copy link
Contributor

It feels like there are lot of things that could go wrong (i'm not saying they have) i.e. the mappings could be incorrect. Is there anyway we can test the objects that are now manually being created and mapped?

Arguably having them "manually" written (and hopefully tested manually soon as well) makes it less likely that something will just break because the "magic" translation layer changes or someone unknowingly renames a method which was used.

Additioanlly apart from $ and $$ I would argue that having fields such as page.request is way easier and more explicit with having a goja.Object created and defined "manually" then it being "magically" created by goja.

So that `$` and `$$` won't register in the global Goja runtime. This
means our code won't be able to automatically map `$` to `Query` and
`$$` to `QueryAll`.
This was borrowed from k6-core to register wildcards like `$` and `$$`
to the global Goja runtime. We won't support this mapping anymore.
This is the start of the mappings efforts from our browser types to
Goja.
`$` and `$$` are mapped to `Query` and `QueryAll`, respectively. Users
won't be able to use `Query` and `QueryAll`. Instead, they will use `$`
and `$$` to access those querying methods.
With this, the browser module exposes api types to goja mappings to
users.
inancgumus added a commit that referenced this pull request Dec 15, 2022
@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch from 4f23e29 to a636fc0 Compare December 15, 2022 09:45
inancgumus added a commit that referenced this pull request Dec 15, 2022
@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch from a636fc0 to 49f2f86 Compare December 15, 2022 09:52
@inancgumus inancgumus force-pushed the add/661-localize-query-method-mappings branch from 49f2f86 to 033adcf Compare December 15, 2022 10:02
@inancgumus
Copy link
Member Author

inancgumus commented Dec 15, 2022

LGTM. I've left some minor comments.

Thank you for the re-review @ankur22.

Having looked at the unit tests you've added in this, i'm not sure adding JS tests would add any more benefit to this, I could be wrong though.

JS tests can ensure that we are mapping the methods correctly to the ones we mapped. For example, what would happen if we map request.frame to something else? Since request.frame now returns a *goja.Object, we won't detect whether we correctly mapped to a Frame. We could add more Go tests, but it would be like re-implementing everything in tests.

Also, JStests are necessary for the rest of the extension too. So we can exercise them from the point of view of a user without setting up a testing suite from the Go side with all these goja translations.

These tests can also allow us to refactor the extension more freely instead of changing the Go code. So we can extract goja out of the extension and let the goja translation layer do the job (see #271).

@inancgumus inancgumus merged commit 9c86c19 into main Dec 15, 2022
@inancgumus inancgumus deleted the add/661-localize-query-method-mappings branch December 15, 2022 10:20
inancgumus added a commit that referenced this pull request Dec 15, 2022
…ethod-mappings"

This reverts commit 9c86c19, reversing
changes made to fbeb15f.

This is done because we're not fully sure that how the current mappings
will affect users. This will comeback when we add more JS tests and so
on.
@inancgumus inancgumus mentioned this pull request Dec 15, 2022
inancgumus added a commit that referenced this pull request Jan 4, 2023
…-query-method-mappings""

This reverts commit 3cee653.
@inancgumus inancgumus self-assigned this Jan 11, 2023
@inancgumus inancgumus changed the title Add mappings from the browser module to Goja Map k6 browser to Goja Jan 18, 2023
@inancgumus inancgumus changed the title Map k6 browser to Goja Map k6 browser API to Goja Jan 18, 2023
@inancgumus inancgumus added the productivity Issues and PRs that improve our productivity label Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
productivity Issues and PRs that improve our productivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Map k6 browser API to Goja
3 participants