MCPcopy Create free account
hub / github.com/basecamp/hey-cli / journalServerWithReadBehavior

Function journalServerWithReadBehavior

internal/cmd/journal_test.go:26–62  ·  view source on GitHub ↗

journalServerWithReadBehavior creates a journal test server. readBehavior controls GET /calendar/days/{date}/journal_entry: "200" — returns a Recording with content "204" — returns 204 No Content (SDK returns nil), no legacy fallback "204-with-fallback" — returns 204

(t *testing.T, readBehavior string)

Source from the content-addressed store, hash-verified

24// "204" — returns 204 No Content (SDK returns nil), no legacy fallback
25// "204-with-fallback" — returns 204 on SDK path, serves HTML on legacy /edit path
26func journalServerWithReadBehavior(t *testing.T, readBehavior string) *httptest.Server {
27 t.Helper()
28 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
29 switch {
30 case r.Method == "GET" && strings.Contains(r.URL.Path, "/calendar/days/") && strings.HasSuffix(r.URL.Path, "/journal_entry/edit"):
31 // Legacy HTML-scrape path
32 w.Header().Set("Content-Type", "text/html")
33 if readBehavior == "204-with-fallback" {
34 fmt.Fprint(w, `<html><body><input id="journal_trix_input" value="&lt;div&gt;Fallback content&lt;/div&gt;"></body></html>`)
35 } else {
36 fmt.Fprint(w, `<html><body></body></html>`)
37 }
38 case r.Method == "GET" && strings.Contains(r.URL.Path, "/calendar/days/") && (strings.HasSuffix(r.URL.Path, "/journal_entry") || strings.HasSuffix(r.URL.Path, "/journal_entry.json")):
39 path := r.URL.Path
40 path = strings.TrimPrefix(path, "/calendar/days/")
41 date := strings.TrimSuffix(strings.TrimSuffix(path, ".json"), "/journal_entry")
42 switch readBehavior {
43 case "204", "204-with-fallback":
44 w.WriteHeader(204)
45 default:
46 resp := map[string]any{
47 "id": 1,
48 "content": "<div>Entry for " + date + "</div>",
49 "type": "Calendar::JournalEntry",
50 "title": "Journal Entry",
51 "starts_at": "2024-01-15T00:00:00Z",
52 }
53 w.Header().Set("Content-Type", "application/json")
54 json.NewEncoder(w).Encode(resp)
55 }
56 case r.Method == "PATCH" && strings.Contains(r.URL.Path, "/calendar/days/") && (strings.HasSuffix(r.URL.Path, "/journal_entry") || strings.HasSuffix(r.URL.Path, "/journal_entry.json")):
57 w.WriteHeader(204)
58 default:
59 w.WriteHeader(200)
60 }
61 }))
62}
63
64func runJournalWrite(t *testing.T, server *httptest.Server, args ...string) (output.Response, error) {
65 t.Helper()

Calls

no outgoing calls

Tested by

no test coverage detected