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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ datastar-website
*/java/*/target/
*.pyc
__pycache__
sdk/test/**/**/testOutput.txt
__debug_bin*

# search index
Expand Down
2 changes: 2 additions & 0 deletions sdk/test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/**/norm*.txt
**/**/testOutput.txt
4 changes: 1 addition & 3 deletions sdk/test/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Datastar SDK testing suite

This test suite uses curl(1), cat(1) and sh(1) to test that a server respects the SDK spec found in `../README.md`.
This test suite uses curl(1), cat(1), sh(1), and awk(1) to test that a server respects the SDK spec found in `../README.md`.

It expects a server to expose a `/test` endpoint that accepts all HTTP methods. The server should then use ReadSignals
to extract the `events` array. It must then loop through the array of events and use `event.type` to decide which server sent event to use. If the output of the server differs from the expected output, then an error will be printed to the terminal.
Expand All @@ -18,8 +18,6 @@ If nothing else is output then all tests passed!

Results of the test can be found in `./get_cases/$case_name/testOutput.txt` (or `post_cases` depending on the test).

Take note that these tests are stricter about data line ordering then the SDK spec is, but unless that becomes an actual problem I dont intend to fix it, since it would add complexity.

## Adding new cases

To add a new test case, simply add a folder named after the test in either `./get-cases` or `./post-cases`.
Expand Down
32 changes: 32 additions & 0 deletions sdk/test/normalize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

awk '
BEGIN { RS = "\n\n\n"; FS = "\n"; }

function flush_data(data) {
if (data) {
print data | "sort"
close("sort", "to")
}
return ""
}

{
data = ""

for (i = 1; i <= NF; i++) {
if ($i ~ /^data: (fragments|script|path)/) {
data = flush_data(data)
print $i
} else if ($i ~ /^data:/) {
data = data $i "\n"
} else {
data = flush_data(data)
print $i
}
}

# flush remaining unordered data lines
flush_data(data)
}
' "$1"
7 changes: 6 additions & 1 deletion sdk/test/test-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ curl -sN --get -H "Accept: text/event-stream" -H "datastar-request: true" --data

[ ! -f "$1/testOutput.txt" ] && echo "case $1 failed: your server did not return anything" && return 1

diff -q "$1/testOutput.txt" "$1/output.txt" || { exit 1; }
./normalize.sh "$1/output.txt" >"$1/norm_output.txt"
./normalize.sh "$1/testOutput.txt" >"$1/norm_testOutput.txt"

diff -q "$1/norm_output.txt" "$1/norm_testOutput.txt" || { exit 1; }

rm "$1/norm_output.txt" "$1/norm_testOutput.txt"

exit 0
7 changes: 6 additions & 1 deletion sdk/test/test-post.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ curl -sN -H "Accept: text/event-stream" -H "datastar-request: true" --json "$inp

[ ! -f "$1/testOutput.txt" ] && echo "case $1 failed: your server did not return anything" && return 1

diff -q "$1/testOutput.txt" "$1/output.txt" || { exit 1; }
./normalize.sh "$1/output.txt" >"$1/norm_output.txt"
./normalize.sh "$1/testOutput.txt" >"$1/norm_testOutput.txt"

diff -q "$1/norm_output.txt" "$1/norm_testOutput.txt" || { exit 1; }

rm "$1/norm_output.txt" "$1/norm_testOutput.txt"

exit 0