package stdio import ( "bufio" "encoding/json" "errors" "io" "testing" "time" ) // novox/hq ADR 0259 ยง3: a bundle takes a seat's work and answers proofs through the runtime; a piece of // work it fails is answered as an error, so the runtime offers it again, and a proof's error is said. func TestABundleTakesWorkAndAnswersProofsThroughTheRuntime(t *testing.T) { t.Setenv(ServedAs, "router") inR, inW := io.Pipe() outR, outW := io.Pipe() taken := make(chan string, 4) go func() { _ = ServeOn("", []Tool{{Name: "start", Run: func(map[string]any) (any, error) { if err := SeatTake("SEAT_OPERATOR_CHANNEL_worker", func(w Work) error { taken <- w.Subject if string(w.Body) == `{"fail":true}` { return errors.New("not recorded") } return nil }); err != nil { return nil, err } if err := SeatAnswer("mesh.seat.intake.proof.code.*", func(subject string, body json.RawMessage) (any, error) { if subject != "mesh.seat.intake.proof.code.telegram" { return nil, errors.New("no such kind") } return map[string]any{"accepted": true}, nil }); err != nil { return nil, err } return map[string]any{}, nil }}}, inR, outW) }() lines := bufio.NewScanner(outR) next := func() map[string]any { done := make(chan map[string]any, 1) go func() { if lines.Scan() { var m map[string]any _ = json.Unmarshal(lines.Bytes(), &m) done <- m } }() select { case m := <-done: return m case <-time.After(3 * time.Second): t.Fatal("the bundle said nothing") return nil } } send := func(m map[string]any) { b, _ := json.Marshal(m); _, _ = inW.Write(append(b, '\n')) } send(map[string]any{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": map[string]any{"name": "start"}}) asked := next() if asked["method"] != "mesh/seat.take" || asked["params"].(map[string]any)["worker"] != "SEAT_OPERATOR_CHANNEL_worker" { t.Fatalf("the take was not asked of the runtime: %v", asked) } send(map[string]any{"jsonrpc": "2.0", "id": asked["id"], "result": map[string]any{}}) asked = next() if asked["method"] != "mesh/seat.answer" { t.Fatalf("the answer was not asked of the runtime: %v", asked) } send(map[string]any{"jsonrpc": "2.0", "id": asked["id"], "result": map[string]any{}}) next() // the tool's answer send(map[string]any{"jsonrpc": "2.0", "id": 2, "method": "mesh/work", "params": map[string]any{"work": map[string]any{ "worker": "SEAT_OPERATOR_CHANNEL_worker", "subject": "mesh.seat.operator-channel.accept.ask.x", "body": map[string]any{"fail": true}}}}) if got := next(); got["error"] == nil { t.Errorf("work the handler failed was answered as taken: %v", got) } send(map[string]any{"jsonrpc": "2.0", "id": 3, "method": "mesh/work", "params": map[string]any{"work": map[string]any{ "worker": "SEAT_OPERATOR_CHANNEL_worker", "subject": "mesh.seat.operator-channel.accept.ask.x", "body": map[string]any{}}}}) if got := next(); got["error"] != nil { t.Errorf("work the handler took was answered as an error: %v", got) } send(map[string]any{"jsonrpc": "2.0", "id": 4, "method": "mesh/proof", "params": map[string]any{ "subject": "mesh.seat.intake.proof.code.telegram", "body": map[string]any{"code": "1"}}}) if got := next(); got["result"].(map[string]any)["accepted"] != true { t.Errorf("the proof was answered %v", got) } send(map[string]any{"jsonrpc": "2.0", "id": 5, "method": "mesh/proof", "params": map[string]any{ "subject": "mesh.seat.intake.proof.code.desktop", "body": map[string]any{}}}) if got := next(); got["error"] == nil { t.Errorf("a proof the handler refused was answered as accepted: %v", got) } if len(taken) != 2 { t.Errorf("the handler was handed %d pieces of work, want 2", len(taken)) } _ = inW.Close() }