Jev.Test (Jev v0.2.0)

Copy Markdown View Source

Test helpers: wire bodies from reply-shaped maps.

Most tests of a Jev.Server call handle_answer/3 with a literal reply map and never touch the transport. The ones that do stub Jev.HTTP with Req.Test, and this module writes the stub's response the same way you read a reply: question names to answers, confidence and probabilities beside them.

Req.Test.stub(Jev.HTTP, &Jev.Test.respond(&1, kind: :bug, security: 0.03))

respond/2 reads the questions from the request, so the stub only says what the answer is. body/2 is the pure half for other transports:

Jev.Test.body([kind: :bug, confidence: %{kind: 0.9}], kind: {"Kind?", %{bug: nil, other: nil}})
#=> %{"model" => "jev-test", "answers" => %{"kind" => %{"type" => "choice", ...}}, ...}

A choice is its label, a score its value, a yes/no its probability. Answers may be partial; a question with no answer is left out of the body, as the reply map tolerates. Reserved keys are the reply's own: confidence, probabilities, model (default "jev-test"), and usage (default zero).

Probabilities you do not give are the distribution implied by the confidence you do, the inverse of how the client computes confidence from probabilities: the answer gets c * (1 - 1/k) + 1/k and the rest share the remainder. With neither, the answer has probability one. A score's distribution centres on its nearest level, and its legend is the levels.

respond/2 and error/3 need :plug, which Req.Test needs as well.

Summary

Types

Answers by question name, plus any of the reserved reply keys.

Functions

The wire body a server would send for reply to questions.

Answers a Req.Test stub with a Jev.Error-shaped failure.

Reads and decodes the request body, assigning it to conn.assigns.jev_request.

Answers a Req.Test stub with reply, reading the questions from the request.

Types

reply()

@type reply() :: keyword() | map()

Answers by question name, plus any of the reserved reply keys.

Functions

body(reply, questions)

@spec body(reply(), keyword() | map()) :: map()

The wire body a server would send for reply to questions.

questions are in any shorthand Jev.questions/1 accepts, or the wire "questions" map of a request. Raises ArgumentError for an answer to an unknown question or a choice label outside the criteria.

error(conn, status, message)

@spec error(Plug.Conn.t(), pos_integer(), String.t()) :: Plug.Conn.t()

Answers a Req.Test stub with a Jev.Error-shaped failure.

Req.Test.stub(Jev.HTTP, &Jev.Test.error(&1, 429, "rate limited"))

request(conn)

@spec request(Plug.Conn.t()) :: {map(), Plug.Conn.t()}

Reads and decodes the request body, assigning it to conn.assigns.jev_request.

respond(conn, reply)

@spec respond(Plug.Conn.t(), reply()) :: Plug.Conn.t()

Answers a Req.Test stub with reply, reading the questions from the request.

Req.Test.stub(Jev.HTTP, &Jev.Test.respond(&1, kind: :bug, confidence: %{kind: 0.95}))

The request body is left decoded in conn.assigns.jev_request for assertions on the state or model.