Skip to content

Commit

Permalink
Order independent testing (#593)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jmstevers authored Feb 25, 2025
1 parent 41ee5b7 commit 1e5c238
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
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

0 comments on commit 1e5c238

Please sign in to comment.