50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
type fakeAnswers map[string]string
|
|
|
|
func (f fakeAnswers) Get(key string) (*stdio.StateEntry, error) {
|
|
v, ok := f[key]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
return &stdio.StateEntry{Key: key, Value: json.RawMessage(v)}, nil
|
|
}
|
|
|
|
func (f fakeAnswers) Keys() ([]string, error) {
|
|
var out []string
|
|
for k := range f {
|
|
out = append(out, k)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// teller_answers reads the teller's answers: one by the ask's key, an ask with none said as not found, every answer when no key
|
|
// is named (ADR 0596 §3: the gate asks this tool, never the bucket).
|
|
func TestTellerAnswersReadsOneAnswerOrEveryAnswer(t *testing.T) {
|
|
tool := AnswerTools(fakeAnswers{"k1": `{"option":"approve"}`, "k2": `{"option":"decline"}`})[0]
|
|
if tool.Name != "teller_answers" {
|
|
t.Fatalf("the tool is %s", tool.Name)
|
|
}
|
|
out, err := tool.Run(map[string]any{"key": "k1"})
|
|
if err != nil || fmt.Sprint(out) != `map[answer:{"option":"approve"} found:true key:k1]` {
|
|
t.Errorf("one answer: %v, %v", out, err)
|
|
}
|
|
out, err = tool.Run(map[string]any{"key": "gone"})
|
|
if err != nil || fmt.Sprint(out) != `map[found:false key:gone]` {
|
|
t.Errorf("no answer: %v, %v", out, err)
|
|
}
|
|
out, err = tool.Run(map[string]any{})
|
|
raw, _ := json.Marshal(out)
|
|
if err != nil || string(raw) != `{"answers":[{"answer":{"option":"approve"},"key":"k1"},{"answer":{"option":"decline"},"key":"k2"}]}` {
|
|
t.Errorf("every answer: %s, %v", raw, err)
|
|
}
|
|
}
|