Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-mcp

Model Context Protocol server for Pudu language documentation and compiler tools

0.1.1Apache-2.01

InstallClose

ReplyTest.pudu

Pudu61 lines3.6 KB

GitHub ↗
1/** @Test.Domain.Rpc.ReplyTest.Suite — reply encoding and result fields */2module PuduLangMcp.Domain.Rpc.ReplyTest34import Std.Io as Io5import Std.Json as Json6import Std.Result as Result7import Std.Test as Test8import PuduLangMcp.Constants.Protocol as Protocol9import PuduLangMcp.Constants.Server as Server10import PuduLangMcp.Domain.Rpc.Message as Message11import PuduLangMcp.Domain.Rpc.Reply as Reply12import PuduLangMcp.Errors.RpcError as RpcError13import PuduLangMcp.Utils.JsonAccess as Access1415/// A reply line decoded back to JSON.16fn decoded(line: Str) -> Json.Json {17  Result.unwrapOr(Json.decode(line), Json.Null)18}1920/// The first version an unsupported-version error lists.21fn firstSupported(reply: &Json.Json) -> Option[Json.Json] {22  let supported = Json.path(reply, &["error", "data", "supported"]) ?23  Json.at(&supported, 0)24}2526/// Runs the suite.27fn main() -> Int {28  let plain = Reply.complete(&[("value", Json.Number(1))], &Reply.NoCache)29  let cached = Reply.complete(&[], &Reply.Cached(5000, Protocol.CACHE_PUBLIC))30  let clamped = Reply.complete(&[], &Reply.Cached(-10, Protocol.CACHE_PRIVATE))31  let line = Reply.result(&Message.NumberId(3), &Json.object(&[("text", Json.Text("a\nb"))]))32  let failed = decoded(Reply.failure(&None, &RpcError.ParseError("bad")))33  let versioned = decoded(Reply.failure(&Some(Message.TextId("x")), &RpcError.UnsupportedVersion("1900-01-01")))34  let missing = decoded(Reply.failure(&Some(Message.NumberId(1)), &RpcError.ResourceNotFound("pudu://nope")))35  let checks = Test.suite("Domain.Rpc.Reply", &[36      Test.equals("a complete result carries its result type", &Access.text(&plain, "resultType"), &Some("complete")),37      Test.equals("a complete result keeps its fields", &Access.integer(&plain, "value"), &Some(1)),38      Test.equals("a complete result names the server", &Access.pathText(&plain, &["_meta", Protocol.META_SERVER_INFO, "name"]), &Some(Server.SERVER_NAME)),39      Test.equals("an uncached result has no ttl", &Access.member(&plain, "ttlMs"), &None),40      Test.equals("a cached result carries its ttl", &Access.integer(&cached, "ttlMs"), &Some(5000)),41      Test.equals("a cached result carries its scope", &Access.text(&cached, "cacheScope"), &Some("public")),42      Test.equals("a negative ttl is written as zero", &Access.integer(&clamped, "ttlMs"), &Some(0)),43      Test.that("a reply is one line even when its text has line breaks", !line.contains("\n")),44      Test.equals("a result reply keeps its id", &Access.integer(&decoded(line), "id"), &Some(3)),45      Test.equals("a result reply is JSON-RPC 2.0", &Access.text(&decoded(line), "jsonrpc"), &Some("2.0")),46      Test.equals("an unknown id is written as null", &Access.member(&failed, "id"), &Some(Json.Null)),47      Test.equals("an error carries its code", &Access.pathInt(&failed, &["error", "code"]), &Some(Protocol.PARSE_ERROR)),48      Test.equals("an error without data has no data member", &Json.path(&failed, &["error", "data"]), &None),49      Test.equals("a failure never carries a result", &Access.member(&failed, "result"), &None),50      Test.equals("a version error names what was requested", &Access.pathText(&versioned, &["error", "data", "requested"]), &Some("1900-01-01")),51      Test.equals("a version error lists the modern version first", &firstSupported(&versioned), &Some(Json.Text(Protocol.MODERN_VERSION))),52      Test.equals("a missing resource is invalid params", &Access.pathInt(&missing, &["error", "code"]), &Some(Protocol.INVALID_PARAMS)),53      Test.equals("a missing resource names its uri", &Access.pathText(&missing, &["error", "data", "uri"]), &Some("pudu://nope"))54    ])55  let ran = Test.run(&checks)56  for failure in Test.failuresOf(&ran) {57    let _reported = Io.writeErrorLine(failure)58  }59  Test.report(&ran)60}61