//// Built-in declarations cannot exercise invalid extension metadata through //// the daemon. These checks cover the shared rejection boundary that keeps //// malformed forms and requests out of both clients. import albedo/harness/client_api as client import gleam/http import gleam/json import gleam/list import gleam/option.{None, Some} import gleam/result import gleam/string import gleeunit/should fn action( fields: List(client.Field), operation: client.Operation, ) -> client.Action { client.Action("edit", "edit", "", None, fields, operation) } fn operation() -> client.Operation { client.operation_defaults("edit", http.Post, "/items", 200) } pub fn inconsistent_defaults_are_rejected_before_a_page_can_be_built_test() -> Nil { let choice = client.choice_field("mode", True, ["on", "off"]) let invalid_fields = [ client.choice_field("mode", True, []), client.Field(..choice, default: json.string("unknown")), client.Field( ..client.integer_field("count", False), default: json.string("3"), ), client.Field(..choice, default_binding: Some(client.Form("/mode"))), client.Field(..choice, default_binding: Some(client.Row("/bad~2escape"))), ] list.each(invalid_fields, fn(field) { let assert Error(error) = client.actions([action([field], operation())]) string.contains(error, "action edit: field ") |> should.be_true }) client.actions([ action([client.Field(..choice, default: json.string("on"))], operation()), ]) |> result.is_ok |> should.be_true } pub fn request_bindings_must_resolve_declared_inputs_and_paths_test() -> Nil { let field = client.text_field("a/b~c", True) let valid = client.Operation( ..operation(), path_template: "/items/{id}", path: [#("id", client.Row("/resource/id"))], body: [#("/value", client.Form("/a~1b~0c"))], ) client.actions([action([field], valid)]) |> result.is_ok |> should.be_true list.each( [ client.Operation(..valid, path: []), client.Operation(..valid, path: [#("other", client.Row("/id"))]), client.Operation(..valid, body: client.form_body(["missing"])), client.Operation(..valid, body: [ #("value", client.Literal(json.string("x"))), ]), client.Operation(..valid, query: [#("key", client.Row("id"))]), ], fn(invalid) { client.actions([action([field], invalid)]) |> result.is_error |> should.be_true }, ) } pub fn duplicate_fields_and_actions_are_rejected_test() -> Nil { let field = client.text_field("title", True) let edit = action([field], operation()) client.actions([edit, edit]) |> result.is_error |> should.be_true client.actions([action([field, field], operation())]) |> result.is_error |> should.be_true } pub fn a_command_can_have_distinct_read_and_mutation_declarations_test() -> Nil { let read = client.Command("/mode", client.Read, [], operation()) let mutation = client.Command(..read, delivery: client.Mutation) client.validate_commands([read, mutation]) |> should.equal(Ok(Nil)) client.validate_commands([read, read]) |> result.is_error |> should.be_true }