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

Order independent testing #593

Merged
merged 9 commits into from
Feb 25, 2025
Merged

Conversation

jmstevers
Copy link
Contributor

Splits events by \n\n\n, sorts the lines inside each event, and then checks the diff between normalized files.

I'd like @Superpat to look at this before I mark it ready for review.

@Superpat
Copy link
Contributor

Superpat commented Feb 4, 2025

Will try to look at this tomorrow ! Thank you for fixing this.

@Superpat
Copy link
Contributor

Superpat commented Feb 4, 2025

@jmstevers I did read it over quickly and am right in believing that this removes all ordering of the datalines ? For instance fragment lines should always be at the end. And sdk's should keep the ordering of the fragment lines they are sent.

@jmstevers
Copy link
Contributor Author

jmstevers commented Feb 5, 2025

Nope, data lines can be anywhere in the event (sse spec). Data lines are just appended to a buffer whenever they are read. I am concerned about the html being messed up when they are sorted, but I thought that you would have to be going out of your way to slice up the fragment string and reorder it.

Yep, you are right. I forgot that there are data lines other than fragments. I'll have to break up the data lines to ensure fragments are always at the end and that they are in the correct order.

@Superpat
Copy link
Contributor

Superpat commented Feb 5, 2025

Hmm I dont know anymore, I'm looking at the sse parsing code and the event is parsed before the specific event handlers for mergeSignals and etc even run, so theoretically you where right. I just want to ask @delaneyj just to be sure.

In any case, we would still need to make sure the order of fragments is not lost.

@jmstevers
Copy link
Contributor Author

jmstevers commented Feb 5, 2025

Okay after reading the source code, each key in the data line (like fragments) is added to a map if it's not already in the map. If it's already in the map then its value is appended to the value in the map. So if im correct then this is valid.

data: fragments <div>
event: datastar-merge-fragments
data: fragments hello
data: mergeMode append
data: fragments </div>

But I'll do some further testing

@jmstevers
Copy link
Contributor Author

Yep, this works. So for the tests I will just ensure fragments are in the correct order by appending them to a buffer whenever they appear and leaving them unsorted in the normalized text file.

…pace after colon if its after a key at the beginning of the line
@jmstevers
Copy link
Contributor Author

@delaneyj Can you confirm if this is intended behavior?

@bencroker
Copy link
Collaborator

@jmstevers please clarify the question so I can answer.

@jmstevers
Copy link
Contributor Author

jmstevers commented Feb 5, 2025

Is it valid for sdks to send down this?

data: fragments <div>
event: datastar-merge-fragments
data: fragments hello
data: mergeMode append
data: fragments </div>

D* reads it correctly but it's kind of a messed up format. I worded my original question poorly...

@bencroker
Copy link
Collaborator

No. The order is defined. The SDK spec states that:

When called the function must write to the response buffer the following in specified order.

See https://github.com/starfederation/datastar/blob/develop/sdk/README.md#logic

@jmstevers
Copy link
Contributor Author

jmstevers commented Feb 6, 2025

So strict ordering is being enforced? I thought this was decided against in the discord, hence this PR.

IMO it's an unnecessary restriction and conflicts with how some frameworks order their events.

@bencroker
Copy link
Collaborator

bencroker commented Feb 6, 2025

Reading directly from the SDK spec, the order must follow this format. What about this is unclear?

event:
id:
retry:
data:

@jmstevers
Copy link
Contributor Author

It's unclear why the spec requires it.

@Superpat
Copy link
Contributor

Superpat commented Feb 6, 2025

@bencroker yeah the actual library code accepts any order for those lines. @delaneyj said he could make the code a bit simpler if sdk's did abide by the strict ordering of the test suite (that is even more strict then what the spec says), but I dont know it it's worth it.

@bencroker
Copy link
Collaborator

It's unclear why the spec requires it.

Purely for consistency’s sake. It’s similar to how we require that functions follow a naming convention. We want all SDK APIs and SSE event output to look alike. Note that the data lines can be in any order.

Unless you can provide a convincing argument to loosen the ordering, I don’t see any reason to change it.

@bencroker yeah the actual library code accepts any order for those lines. @delaneyj said he could make the code a bit simpler if sdk's did abide by the strict ordering of the test suite (that is even more strict then what the spec says), but I dont know it it's worth it.

The client intentionally works with looser ordering because using an SDK is optional.

@jmstevers
Copy link
Contributor Author

jmstevers commented Feb 6, 2025

  1. Consistency with SSE spec
  2. Less work/mental load for sdk developers (however small)
  3. Simpler spec (however small)
  4. Python and Ruby (in some cases) currently fail the tests due to ordering of event, id, retry. This would "fix" those instantly (not that it's that hard to fix)

Why is SSE output looking alike wanted? If the library enforced strict ordering and the code was simpler because of it, I would understand. But it would only be a few lines shorter from what I've seen.

Also the data lines can not be in any order. According to the spec, fragment and script data lines must be written last (I am also questioning why this is).

Is it also required to have a space after the colon for fields?

I will modify the script to only sort non fragment/script/header fields for now so we can get this closed.

@bencroker
Copy link
Collaborator

bencroker commented Feb 6, 2025

Why is SSE output looking alike wanted?

So that regardless of whether I use the Go SDK or the PHP SDK, the SSE events produced will look alike. This is just standardisation for the sake of consistency when comparing SDK output, troubleshooting issues, etc.

Maybe @delaneyj can weigh in on anything I’m missing.

Also the data lines can not be in any order. According to the spec, fragment and script data lines must be written last (I am also questioning why this is).

That’s true, the spec does indeed say that options should precede the fragments data line.

Is it also required to have a space after the colon for fields?

According to the SDK spec, it is.

@delaneyj
Copy link
Collaborator

delaneyj commented Feb 6, 2025

SSE spec doesn't care about the order. Technically I don't either. I do think however there is value in being consistent. So every language and SDK looks the same and we aren't having to do crazy decodes each time. That's it, it's not a technical issue but a learning/documentation one IMO. And with that, I leave to y'all cause the client doesn't care.

@Superpat
Copy link
Contributor

Superpat commented Feb 6, 2025

So to recap:

event
id
retry

data: ...options (this is the only part where ordering does not matter)

data: data (which can be path, selector, script or fragment, depending on the event).

@bencroker
Copy link
Collaborator

Correct!

@jmstevers
Copy link
Contributor Author

  1. The function must include the selector in the event data in the format selector SELECTOR\n.

Selector is not required to be at the end of the data lines.

@bencroker
Copy link
Collaborator

If there is a mistake, PR please.

@jmstevers
Copy link
Contributor Author

jmstevers commented Feb 6, 2025

Is this a mistake? RemoveFragments does not require data: selector to be last according to the spec.

@Superpat
Copy link
Contributor

Superpat commented Feb 6, 2025

I just assumed it was treated like all the other mandatory datalines

@jmstevers jmstevers marked this pull request as ready for review February 6, 2025 19:05
@bencroker
Copy link
Collaborator

bencroker commented Feb 6, 2025

Looks good to me! Can you please review and approve, @Superpat?

@Superpat
Copy link
Contributor

Superpat commented Feb 7, 2025

My awk is not that great, but I think it looks good. I'll run it in the console to try it out.

@bencroker
Copy link
Collaborator

Where are we with this?

@Superpat
Copy link
Contributor

I will finish this review before the weekend is over. Still gotta finish setting up my work env after my ssd meltdown from last week..

@Superpat
Copy link
Contributor

@bencroker this works great. @jmstevers can you simply update the readme? since the warning about strict ordering is no longer necessary.

@Superpat
Copy link
Contributor

Superpat commented Feb 23, 2025

@jmstevers can you also add sdk/test/**/**/norm*.txt to the git ignore ?

@bencroker bencroker merged commit 1e5c238 into starfederation:develop Feb 25, 2025
2 checks passed
@bencroker
Copy link
Collaborator

Merged, thank you both!

@jmstevers jmstevers deleted the test-ordering branch February 25, 2025 03:23
bencroker added a commit that referenced this pull request Feb 25, 2025
* A typo in getting_started.md (#673)

Closing tag for the  data-computed example has an extra ">"

* Clojure sdk: Fixes, clojure snippets (#672)

* Clojure sdk: Fixes, new website snippets

Docs: corrected the readme's example
Fix: changed return value in the jetty implementation
Chore: Changed dependency coordinate of http-kit to a newer maven dependency
Fix: using setTimeout in the redirect sugar function
Fix: mistake in http-kit management in examples
Docs: redirect example
Feature: added another testing utility
Docs: added missing snippets for the Datastar website

* Fix: typo

* Fix code samples [deploy-site]

* Maintain whitespace (#678)

* Fix #677 [deploy-site]

* Better fix [deploy-site]

* feat(SDK): FastHTML SSE response and example (#576) (#624)

* feat(SDK): FastHTML SSE response and example (#576)

* feat(sdk): add simple fasthtml example

* remove comments

---------

Co-authored-by: Lucian Knock <[email protected]>

* Improve wording

* Fix text overflow on buttons (#681) [deploy-site]

* JavaScript

* Fix example link

* Add `develop` branch

* Add checkbox value behaviour based on signal type (#674)

* Modify checkbox and radio behaviour

* Add array and string types

* Move code into block

* Refactor and add tests

* Add release note

* DatastarNaN added to datastar elements without ids (#684)

* Default to element value using `data-bind` (#687)

* Default to element value using `data-bind`

* Fix tests

* Destructure result

* Destructure again

* Add release note

* Test command runs unit tests only (#689)

* Test command runs unit tests only

* Update paths in action

* Enforce branch policy

* Add passing check

* Add new docs for `data-bind`

* Update branch policy

* Fix and add tests

* Debounce applying plugins on page load (#690)

* Debounce applying plugins on page load

* Fix action

* Update Idiomorph to 0.7.2 (#688)

* Update to 0.7.2

* Fix indicator and remove auto ID generation

* Ensure plugins not reapplied (#691)

* Update to 0.7.2

* Fix indicator and remove auto ID generation

* Setup

* Data-* attributes hash their contents

* Update release notes

* Revert star.ts and remove test

---------

Co-authored-by: Delaney Gillilan <[email protected]>

* Fix `data-attr` not removing element attribute (#697)

* Fix `data-attr` not removing element attribute

* Typo

* Add `aliased` test

* Typescript sdk improvements (#698)

* improvements to node sdk

Add abort handling to node sdk, make onError simpler and close stream by default

 - closes #667
 - closes #602

* Improvements to typescript sdk readme

Ben Croker wanted a mergeSignals example

* Make typescript sdk pass tests

* add missing typescript snippets and use same name as sdk folder

* update datastar version in typescript sdk test servers

* Refactor SDK snippets and add title

* 699-Replace-legacy-call-to-`applyAttributePlugin` (#701)

* Replace legacy call to `applyAttributePlugin`
Fixes #699

* Ignore when alias prefix missing

* Remove merge fragment callbacks

* add bundler support

---------

Co-authored-by: Ben Croker <[email protected]>

* 695-Embrace-our-functional-overlords (#702)

* Replace legacy call to `applyAttributePlugin`
Fixes #699

* Ignore when alias prefix missing

* Remove merge fragment callbacks

* add bundler support

* Embrace our functional overlords
Fixes #695

* change to djb2 hash

* Replace reference page

* Add reference docs pages

---------

Co-authored-by: Ben Croker <[email protected]>

* No longer run callable on init (#703)

* No longer run callable on init

* Add release note

* Document alias and remove `prefix` (#704)

* Document alias and remove `prefix`

* Remove example

* Order independent testing (#593)

* normalize output and test output and then compare

* separate ordered data lines from unordered data lines + only remove space after colon if its after a key at the beginning of the line

* don't remove space after colon

* only sort unordered fields

* update readme

* add gitignore to sdk/.test folder

* Document SSE events

Fixes #581

* fix sdk npm package (#705)

* v1.0.0-beta.8

---------

Co-authored-by: Claes Ström <[email protected]>
Co-authored-by: Jérémy <[email protected]>
Co-authored-by: Lucian <[email protected]>
Co-authored-by: Lucian Knock <[email protected]>
Co-authored-by: Akash Gill <[email protected]>
Co-authored-by: Delaney <[email protected]>
Co-authored-by: Patrick Marchand <[email protected]>
Co-authored-by: Johnathan Stevers <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants