types_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package api
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. )
  7. func TestOwnerCapabilitiesAndErrors(t *testing.T) {
  8. if (Owner{}).Valid() {
  9. t.Fatal("empty owner accepted")
  10. }
  11. if !(Owner{EnvironmentID: "e", ThreadID: "t", RunID: "r"}).Valid() {
  12. t.Fatal("valid owner rejected")
  13. }
  14. c := Capabilities{Features: map[string]Capability{"ok": {Status: "supported"}, "partial": {Status: "degraded"}}}
  15. if !c.Supports("ok") || c.Supports("partial") || c.Supports("missing") {
  16. t.Fatal("capability status ignored")
  17. }
  18. if Err("CODE", "message").Error() != "CODE: message" {
  19. t.Fatal("error code missing")
  20. }
  21. if !errors.Is(&Error{Code: "BROWSER_TIMEOUT", Message: "expired", Cause: context.DeadlineExceeded}, context.DeadlineExceeded) {
  22. t.Fatal("transport cause lost")
  23. }
  24. }
  25. func TestActionValidation(t *testing.T) {
  26. for _, action := range []string{"click", "hover", "fill", "type", "select", "check", "uncheck", "press", "scroll"} {
  27. r := ActionRequest{SnapshotID: "s", Ref: "e1", Action: action}
  28. if action == "press" {
  29. r.Value = "Enter"
  30. }
  31. if action == "scroll" {
  32. r.Value = "down"
  33. }
  34. if e := ValidateAction(r); e != nil {
  35. t.Errorf("%s: %v", action, e)
  36. }
  37. }
  38. for _, r := range []ActionRequest{{}, {SnapshotID: "s", Ref: "e", Action: "execute"}, {SnapshotID: "s", Ref: "e", Action: "press", Value: "RunShell"}, {SnapshotID: "s", Ref: "e", Action: "scroll", Value: "sideways"}, {SnapshotID: "s", Ref: "e", Action: "fill", Value: string(make([]byte, 16385))}} {
  39. if ValidateAction(r) == nil {
  40. t.Errorf("invalid action accepted: %q", r.Action)
  41. }
  42. }
  43. }