package main import ( "encoding/json" "testing" ) // toolForge.Pull reads the seat git's answer of the verb pull (design 47 G ยง654), bare or in the wrappings the runtime puts // around an answer; pullEventOf reads the seat's pull-merged and pull-closed bodies. func TestToolForgePullReadsTheSeatGitAnswersAndEventsAreRead(t *testing.T) { var asked struct { key string body map[string]any } for _, tc := range []struct { answer string open, merged bool head string }{ {`{"pull":{"repository":"novox/mesh-catalog","number":435,"state":"merged","head":"7b5f7f84acb73aaf8189200548ff69fd9aac04b2"}}`, false, true, "7b5f7f84acb73aaf8189200548ff69fd9aac04b2"}, {`{"pull":{"repository":"novox/mesh-catalog","number":434,"state":"open","head":"c948f3a61c74257e6e3147effb396d9d6434f491"}}`, true, false, "c948f3a61c74257e6e3147effb396d9d6434f491"}, {`{"pull":{"state":"closed","head":"c948f3a61c74257e6e3147effb396d9d6434f491"}}`, false, false, "c948f3a61c74257e6e3147effb396d9d6434f491"}, } { raw := json.RawMessage(tc.answer) content, _ := json.Marshal(map[string]any{"content": []map[string]any{{"type": "text", "text": tc.answer}}}) ok, _ := json.Marshal(map[string]any{"ok": true, "output": "", "answer": raw}) for name, answer := range map[string]json.RawMessage{"bare": raw, "content list": content, "ok answer": ok} { f := toolForge{ask: func(key string, body any) (json.RawMessage, error) { asked.key, asked.body = key, body.(map[string]any) return answer, nil }} open, merged, head, err := f.Pull("novox", "mesh-catalog", 434) if err != nil || open != tc.open || merged != tc.merged || head != tc.head { t.Errorf("%s (%s): open %v merged %v head %q, err %v", tc.answer, name, open, merged, head, err) } if asked.key != "seat:git.pull" || asked.body["repository"] != "novox/mesh-catalog" || asked.body["number"] != 434 { t.Errorf("asked %+v", asked) } } } failed := toolForge{ask: func(string, any) (json.RawMessage, error) { return json.RawMessage(`{"error":"no such pull request","error-kind":"not-found"}`), nil }} if _, _, _, err := failed.Pull("novox", "mesh-catalog", 9); err == nil { t.Error("an error answer was read as a pull request") } p, ok := pullEventOf([]byte(`{"repository":"novox/mesh-catalog","number":435,"branch":"fix/a","head":"7b5f7f84acb73aaf8189200548ff69fd9aac04b2","base":"main"}`)) if !ok || p.Owner != "novox" || p.Repo != "mesh-catalog" || p.Number != 435 || p.HeadSHA != "7b5f7f84acb73aaf8189200548ff69fd9aac04b2" { t.Errorf("%+v %v", p, ok) } if _, ok := pullEventOf([]byte(`{"repository":"norepo","number":1}`)); ok { t.Error("a repository with no owner was read") } }