Show Equipo's merge and publish cards in Ask Base
For the website demo, Equipo (branch base-publish) raises a merge card with an optional preview_url and a publish card with its site. Base now shows both, in the panel and on /ask, as it shows questions: the question (or title), "Open the preview" / "Open the site" links (new tab, rel=noopener, only for http(s) URLs) and the card's own options as buttons in Equipo's wording; once answered, the choice or the site's state. An answer that gets 409 re-reads the conversation (the panel) or redraws /ask, and is never resent. The relay and snapshot pass question, merge and publish cards with only their own fields (equipo.Card.Projected); every other kind stays out. Equipo's chat lines may carry Markdown links: Base links only http(s) addresses and Equipo's /tasks/ pages (on Equipo's origin) and escapes everything else, raw HTML included (richText; ask.js builds DOM nodes). Base's one new string is under Review 6.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
16 files changed,
+655
−45
docs/copy-pending.md+20 −0internal/browsertest/cards_test.go+68 −0internal/equipo/chat.go+76 −11internal/equipo/equipo_test.go+30 −0internal/equipo/equipotest/chat.go+92 −0internal/look/static/app.css+4 −0internal/look/static/ask.js+117 −24internal/web/app.go+2 −1internal/web/ask.go+2 −2internal/web/ask_cards.go+116 −0internal/web/ask_relay.go+7 −3internal/web/relay_internal_test.go+21 −0internal/web/rich_internal_test.go+25 −0internal/web/templates/ask.html+1 −1internal/web/templates/partials/chat.html+9 −3internal/webtest/ask_test.go+65 −0
diff --git a/docs/copy-pending.md b/docs/copy-pending.md| index 5798393..c6eed30 100644 |
| --- a/docs/copy-pending.md |
| +++ b/docs/copy-pending.md |
| @@ -284,3 +284,23 @@ Review 5 (pending): "Ask Base anything", the chat with Equipo's coordinator. |
| | internal/web/settings.go | Appearance, the three buttons (System follows the device's light or dark setting; the chosen one is pressed) | System / Light / Dark | |
| Activity lines in the status ("Adding a project…" and the like) are Equipo's own labels, shown as Equipo sends them. |
| + |
| +Review 6 (pending): merge and publish cards in Ask Base, for the website demo. |
| + |
| +Base's own words: |
| + |
| +| File | Context | Text | |
| +|---|---|---| |
| +| internal/web/ask_cards.go, ask.js | A publish card whose site failed to publish (Equipo's site state "failed"; Equipo has no short line for it) | Publishing failed. | |
| + |
| +Equipo's words, reused as Equipo has them (internal/equipo/copy.go on its base-publish branch; Equipo reviews them): |
| + |
| +| File | Context | Equipo key | Text | |
| +|---|---|---|---| |
| +| partials/chat.html, ask.js | Merge card, preview link (new tab) | chat.card.preview_link | Open the preview | |
| +| partials/chat.html, ask.js | Publish card, site link (new tab) | chat.card.site_link | Open the site | |
| +| ask_cards.go, ask.js | Option buttons | chat.card.merge / wait / wait_mac / not_now / publish | Merge / Wait for checks / Wait for a Mac / Not now / Publish | |
| +| ask_cards.go, ask.js | Merge card once answered | chat.card.merge_chose_merge / _wait / _wait_mac / _not_now, merge_resolved | Merging without checks. / Waiting for checks. / Waiting for a Mac. / Not now. / Nothing to decide any more. | |
| +| ask_cards.go, ask.js | Publish card's site state | chat.card.publish_chose_publish, work.tl.published, work.tl.publish_unconfirmed, chat.card.publish_chose_not_now | Publishing. / Published / Publish could not be confirmed / Not published. | |
| + |
| +The cards' questions ("Merge {title} without checks?", "Publish {title} on the web as {app}?") and Equipo's chat lines come from Equipo as it sends them; Base turns their Markdown links into links and shows everything else as text. |
diff --git a/internal/browsertest/cards_test.go b/internal/browsertest/cards_test.go| new file mode 100644 |
| index 0000000..fdbaabb |
| --- /dev/null |
| +++ b/internal/browsertest/cards_test.go |
| @@ -0,0 +1,68 @@ |
| +//go:build browser |
| + |
| +package browsertest |
| + |
| +import ( |
| + "net/http" |
| + "strconv" |
| + "testing" |
| + "time" |
| + |
| + "github.com/chromedp/chromedp" |
| + |
| + "amadan.net/rastrillo/rastrillo/harness" |
| +) |
| + |
| +// The website demo in the panel: a merge card whose preview arrives, an |
| +// answer that turns out stale (re-read, not resent), and a publish card |
| +// through to published with its site. |
| +func TestAskWebsiteCards(t *testing.T) { |
| + rig, f := askBase(t) |
| + openPanel(t, rig) |
| + wait := func(js string) { |
| + t.Helper() |
| + drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second))) |
| + } |
| + merge := f.AskMerge("Quick website") |
| + card := `#ask-panel [data-card="` + merge + `"]` |
| + wait(`(() => { const c = document.querySelector(` + strconv.Quote(card) + `); |
| + return c && c.textContent.includes("Merge Quick website without checks?") && |
| + [...c.querySelectorAll("button")].map(b => b.textContent).join("|") === "Merge|Wait for checks" })()`) |
| + f.SetPreview(merge, "https://eq-tk0001.devbox.test/", true) |
| + wait(`!!document.querySelector(` + strconv.Quote(card+` a.card-link[href="https://eq-tk0001.devbox.test/"][target="_blank"][rel="noopener"]`) + `)`) |
| + mobileShot(t, rig, "1-merge-preview") |
| + |
| + // The preview moved on without the panel seeing it: Merge gets a 409, |
| + // and the panel re-reads the conversation instead of resending. |
| + f.SetPreview(merge, "https://eq-tk0001.devbox.test/v2/", false) |
| + rig.Allow(http.MethodPost, "/ask/cards/"+merge+"/answer", http.StatusConflict) |
| + drive(t, rig, chromedp.Click(chromedp.CSS(card+` button[value="merge"]`))) |
| + wait(`!!document.querySelector(` + strconv.Quote(card+` input[name="revision"][value="3"]`) + `)`) |
| + if calls := f.Calls("/api/v1/cards/" + merge + "/answer"); len(calls) != 1 { |
| + t.Fatalf("a stale answer was sent %d times", len(calls)) |
| + } |
| + drive(t, rig, chromedp.Click(chromedp.CSS(card+` button[value="merge"]`))) |
| + wait(`document.querySelector(` + strconv.Quote(card) + `).textContent.includes("Merging without checks.")`) |
| + if calls := f.Calls("/api/v1/cards/" + merge + "/answer"); len(calls) != 2 { |
| + t.Fatalf("answers: %d", len(calls)) |
| + } |
| + |
| + publish := f.AskPublish("Quick website", "site-sample") |
| + pcard := `#ask-panel [data-card="` + publish + `"]` |
| + wait(`(() => { const c = document.querySelector(` + strconv.Quote(pcard) + `); |
| + return c && [...c.querySelectorAll("button")].map(b => b.textContent).join("|") === "Publish|Not now" })()`) |
| + mobileShot(t, rig, "2-publish-asked") |
| + drive(t, rig, chromedp.Click(chromedp.CSS(pcard+` button[value="publish"]`))) |
| + wait(`(() => { const c = document.querySelector(` + strconv.Quote(pcard) + `); |
| + return c && c.querySelector(".card-state")?.textContent === "Published" && |
| + !!c.querySelector('a.card-link[href="https://site-sample.example.test/"][rel="noopener"]') })()`) |
| + // Equipo's line about it links the task (on Equipo) and the site. |
| + wait(`!!document.querySelector('#ask-panel .msg.coordinator a[href="` + f.URL + `/tasks/tk0001"]')`) |
| + f.Say("Try [this](javascript:alert(1)) or <img src=x onerror=alert(1)>.") |
| + wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent.includes("<img src=x"))`) |
| + if n := harness.Evaluate[int](rig, `document.querySelectorAll('#ask-panel a[href^="javascript"], #ask-panel .msg img').length`); n != 0 { |
| + t.Fatalf("unsafe markup rendered: %d", n) |
| + } |
| + rig.Screen("#ask-panel", "the panel after publishing") |
| + mobileShot(t, rig, "3-published") |
| +} |
diff --git a/internal/equipo/chat.go b/internal/equipo/chat.go| index 7d958fe..4939d74 100644 |
| --- a/internal/equipo/chat.go |
| +++ b/internal/equipo/chat.go |
| @@ -40,20 +40,85 @@ type Turn struct { |
| Reason string `json:"reason"` |
| } |
| -// Card is something in the conversation to act on. Base shows only open |
| -// question cards (Kind "question", State "open"), each Option a button. |
| +// Card is something in the conversation to act on. Base shows three |
| +// kinds (see Projected): a question, a merge card (Equipo asks whether to |
| +// merge a task without checks, with its preview once it is up) and a |
| +// publish card (whether to publish a merged website; Site says how that |
| +// went). Each Option is a button. |
| type Card struct { |
| - ID string `json:"id"` |
| - Revision int64 `json:"revision"` |
| - Kind string `json:"kind"` |
| - State string `json:"state"` |
| - Question string `json:"question"` |
| - Options []string `json:"options"` |
| - Choice string `json:"choice"` |
| + ID string `json:"id"` |
| + Revision int64 `json:"revision"` |
| + Kind string `json:"kind"` |
| + State string `json:"state"` |
| + Name string `json:"name,omitempty"` |
| + TaskID string `json:"task_id,omitempty"` |
| + Question string `json:"question"` |
| + Options []string `json:"options"` |
| + Choice string `json:"choice"` |
| + PreviewURL string `json:"preview_url,omitempty"` |
| + Site *CardSite `json:"site,omitempty"` |
| } |
| -// Open reports whether the card is a question waiting for an answer. |
| -func (c Card) Open() bool { return c.Kind == "question" && c.State == "open" } |
| +// CardSite is a publish card's website: its app, the merged commit, how |
| +// publishing it stands (asked, publishing, published, unconfirmed, failed |
| +// or declined) and, once known, its public URL. |
| +type CardSite struct { |
| + App string `json:"app"` |
| + SHA string `json:"sha"` |
| + State string `json:"state"` |
| + URL string `json:"url,omitempty"` |
| +} |
| + |
| +// Card kinds Base shows. |
| +const ( |
| + CardQuestion = "question" |
| + CardMerge = "merge" |
| + CardPublish = "publish" |
| +) |
| + |
| +// Projected is the card as Base passes it on: only the fields its kind |
| +// uses, and a link only if it is an http(s) URL. ok is false for every |
| +// other kind of card (projects, tasks and the rest stay in Equipo). |
| +func (c Card) Projected() (Card, bool) { |
| + out := Card{ID: c.ID, Revision: c.Revision, Kind: c.Kind, State: c.State, Question: c.Question, |
| + Options: c.Options, Choice: c.Choice} |
| + switch c.Kind { |
| + case CardQuestion: |
| + case CardMerge: |
| + out.Name, out.TaskID, out.PreviewURL = c.Name, c.TaskID, WebURL(c.PreviewURL) |
| + case CardPublish: |
| + out.Name, out.TaskID = c.Name, c.TaskID |
| + if c.Site != nil { |
| + out.Site = &CardSite{App: c.Site.App, SHA: c.Site.SHA, State: c.Site.State, URL: WebURL(c.Site.URL)} |
| + } |
| + default: |
| + return Card{}, false |
| + } |
| + return out, true |
| +} |
| + |
| +// Open reports whether the card waits for an answer. |
| +func (c Card) Open() bool { return c.State == "open" } |
| + |
| +// Shown reports whether a conversation read afresh shows the card: a |
| +// question or merge card while it is open, a publish card always (it goes |
| +// on to say whether the site was published). |
| +func (c Card) Shown() bool { |
| + if c.Kind == CardPublish { |
| + return true |
| + } |
| + return (c.Kind == CardQuestion || c.Kind == CardMerge) && c.Open() |
| +} |
| + |
| +// WebURL is raw if it is an absolute http or https URL with a host and no |
| +// user info, else "". |
| +func WebURL(raw string) string { |
| + u, err := url.Parse(raw) |
| + if raw == "" || err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil { |
| + return "" |
| + } |
| + return u.String() |
| +} |
| // Snapshot is the part of GET /api/v1/snapshot Base shows: the |
| // conversation's tail, its turns, its cards and the event cursor they |
diff --git a/internal/equipo/equipo_test.go b/internal/equipo/equipo_test.go| index da24c63..69d31ce 100644 |
| --- a/internal/equipo/equipo_test.go |
| +++ b/internal/equipo/equipo_test.go |
| @@ -143,3 +143,33 @@ func TestEventsHeaderTimeout(t *testing.T) { |
| t.Fatalf("Events: %v after %s", err, time.Since(start)) |
| } |
| } |
| + |
| +// Base shows question, merge and publish cards, each with only its own |
| +// fields, and only web links. |
| +func TestCardProjected(t *testing.T) { |
| + site := &equipo.CardSite{App: "site-sample", SHA: "eee", State: "published", URL: "https://site-sample.example.test/"} |
| + for name, c := range map[string]struct { |
| + in equipo.Card |
| + want string |
| + }{ |
| + "question": {equipo.Card{ID: "c1", Revision: 1, Kind: "question", State: "open", Name: "n", TaskID: "tk1", Question: "Which?", Options: []string{"a"}, PreviewURL: "https://x.test/", Site: site}, |
| + `{"id":"c1","revision":1,"kind":"question","state":"open","question":"Which?","options":["a"],"choice":""}`}, |
| + "merge": {equipo.Card{ID: "c2", Revision: 2, Kind: "merge", State: "open", Name: "Quick website", TaskID: "tk1", Question: "Merge Quick website without checks?", Options: []string{"merge", "wait"}, PreviewURL: "https://eq-tk1.devbox.test/", Site: site}, |
| + `{"id":"c2","revision":2,"kind":"merge","state":"open","name":"Quick website","task_id":"tk1","question":"Merge Quick website without checks?","options":["merge","wait"],"choice":"","preview_url":"https://eq-tk1.devbox.test/"}`}, |
| + "merge, bad preview": {equipo.Card{ID: "c3", Kind: "merge", State: "open", PreviewURL: "javascript:alert(1)"}, |
| + `{"id":"c3","revision":0,"kind":"merge","state":"open","question":"","options":null,"choice":""}`}, |
| + "publish": {equipo.Card{ID: "c4", Revision: 3, Kind: "publish", State: "answered", Name: "Quick website", TaskID: "tk1", Question: "Publish?", Options: []string{"publish", "not_now"}, Choice: "publish", PreviewURL: "https://x.test/", Site: site}, |
| + `{"id":"c4","revision":3,"kind":"publish","state":"answered","name":"Quick website","task_id":"tk1","question":"Publish?","options":["publish","not_now"],"choice":"publish","site":{"app":"site-sample","sha":"eee","state":"published","url":"https://site-sample.example.test/"}}`}, |
| + "publish, bad site url": {equipo.Card{ID: "c5", Kind: "publish", State: "open", Site: &equipo.CardSite{App: "a", State: "asked", URL: "data:text/html,hi"}}, |
| + `{"id":"c5","revision":0,"kind":"publish","state":"open","question":"","options":null,"choice":"","site":{"app":"a","sha":"","state":"asked"}}`}, |
| + } { |
| + got, ok := c.in.Projected() |
| + raw, _ := json.Marshal(got) |
| + if !ok || string(raw) != c.want { |
| + t.Errorf("%s: %v\n got %s\nwant %s", name, ok, raw, c.want) |
| + } |
| + } |
| + if _, ok := (equipo.Card{ID: "c6", Kind: "project", State: "added"}).Projected(); ok { |
| + t.Error("a project card was shown") |
| + } |
| +} |
diff --git a/internal/equipo/equipotest/chat.go b/internal/equipo/equipotest/chat.go| index 3785b66..ca92e40 100644 |
| --- a/internal/equipo/equipotest/chat.go |
| +++ b/internal/equipo/equipotest/chat.go |
| @@ -52,6 +52,18 @@ type card struct { |
| TaskID string `json:"task_id"` |
| CreatedAt time.Time `json:"created_at"` |
| UpdatedAt time.Time `json:"updated_at"` |
| + // PreviewURL is a merge card's preview once it is up; Site a publish |
| + // card's site (Equipo's base-publish branch). |
| + PreviewURL string `json:"preview_url,omitempty"` |
| + Site *site `json:"site,omitempty"` |
| +} |
| + |
| +type site struct { |
| + App string `json:"app"` |
| + SHA string `json:"sha"` |
| + State string `json:"state"` |
| + URL string `json:"url,omitempty"` |
| + At *time.Time `json:"at,omitempty"` |
| } |
| type event struct { |
| @@ -111,6 +123,79 @@ func (f *Fake) AskQuestion(question string, options ...string) string { |
| return c.ID |
| } |
| +// AskMerge puts an open merge card in the conversation, as Equipo does |
| +// for a task whose checks could not run: Merge or Wait for checks. |
| +func (f *Fake) AskMerge(title string) string { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + now := time.Now().UTC() |
| + c := &card{ID: f.next("c"), Revision: 1, Kind: "merge", State: "open", ProjectID: "p_0001", Name: title, |
| + Question: "Merge " + title + " without checks?", Options: []string{"merge", "wait"}, TaskID: "tk0001", |
| + CreatedAt: now, UpdatedAt: now} |
| + f.cards = append(f.cards, c) |
| + f.emit("card", "card", c.ID, c.Revision, "", *c) |
| + return c.ID |
| +} |
| + |
| +// SetPreview puts a preview link on merge card id, a new revision. With |
| +// announce false no event says so, as if it were missed: an answer at the |
| +// old revision then gets 409. |
| +func (f *Fake) SetPreview(id, url string, announce bool) { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + for _, c := range f.cards { |
| + if c.ID == id { |
| + c.PreviewURL, c.Revision, c.UpdatedAt = url, c.Revision+1, time.Now().UTC() |
| + if announce { |
| + f.emit("card", "card", c.ID, c.Revision, "", *c) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// AskPublish puts an open publish card in the conversation: Publish or Not |
| +// now. Publish runs to published, with the site's URL and a line saying so. |
| +func (f *Fake) AskPublish(title, app string) string { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + now := time.Now().UTC() |
| + c := &card{ID: f.next("c"), Revision: 1, Kind: "publish", State: "open", ProjectID: "p_0001", Name: title, |
| + Question: "Publish " + title + " on the web as " + app + "?", Options: []string{"publish", "not_now"}, |
| + TaskID: "tk0001", CreatedAt: now, UpdatedAt: now, |
| + Site: &site{App: app, SHA: strings.Repeat("e", 40), State: "asked"}} |
| + f.cards = append(f.cards, c) |
| + f.emit("card", "card", c.ID, c.Revision, "", *c) |
| + return c.ID |
| +} |
| + |
| +// publish runs a publish card answered Publish: publishing, then published. |
| +func (f *Fake) publish(c *card) { |
| + select { |
| + case <-f.closing: |
| + return |
| + case <-time.After(40 * time.Millisecond): |
| + } |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + now := time.Now().UTC() |
| + url := "https://" + c.Site.App + ".example.test/" |
| + c.Site.State, c.Site.URL, c.Site.At, c.Revision, c.UpdatedAt = "published", url, &now, c.Revision+1, now |
| + f.emit("card", "card", c.ID, c.Revision, "", *c) |
| + m := message{ID: f.next("m"), Revision: 1, Role: "coordinator", CreatedAt: now, |
| + Text: "Published [" + c.Name + "](/tasks/" + c.TaskID + ") at " + url + "."} |
| + f.messages = append(f.messages, m) |
| + f.emit("message", "message", m.ID, 1, "", m) |
| +} |
| + |
| +// Say adds a coordinator line to the conversation. |
| +func (f *Fake) Say(text string) { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + m := message{ID: f.next("m"), Revision: 1, Role: "coordinator", Text: text, CreatedAt: time.Now().UTC()} |
| + f.messages = append(f.messages, m) |
| + f.emit("message", "message", m.ID, 1, "", m) |
| +} |
| + |
| // Turn is the state of the turn with id ("" when there is none). |
| func (f *Fake) Turn(id string) string { |
| f.cmu.Lock() |
| @@ -326,6 +411,13 @@ func (f *Fake) answer(w http.ResponseWriter, r *http.Request, id string) { |
| return |
| } |
| c.State, c.Choice, c.Revision, c.UpdatedAt = "answered", in.Choice, c.Revision+1, time.Now().UTC() |
| + if c.Site != nil { |
| + c.Site.State = "declined" |
| + if in.Choice == "publish" { |
| + c.Site.State = "publishing" |
| + go f.publish(c) |
| + } |
| + } |
| f.emit("card", "card", c.ID, c.Revision, c.TurnID, *c) |
| m, t := f.ask(in.Choice) |
| f.cmu.Unlock() |
diff --git a/internal/look/static/app.css b/internal/look/static/app.css| index e3878da..e2c6666 100644 |
| --- a/internal/look/static/app.css |
| +++ b/internal/look/static/app.css |
| @@ -478,6 +478,10 @@ dialog.ask-panel::backdrop { background: transparent; } |
| .msg.card { align-self: stretch; max-width: none; background: var(--card); box-shadow: inset 0 0 0 1px var(--card-edge); } |
| .msg.card p { margin: 0 0 10px; } |
| .choices { display: flex; flex-wrap: wrap; gap: 8px; } |
| +.msg.card p + p { margin-top: -4px; } |
| +.card-link, .msg.coordinator a { color: var(--focus); text-decoration: underline; text-underline-offset: 3px; overflow-wrap: anywhere; } |
| +.card-link { display: inline-flex; align-items: center; min-height: 44px; font-weight: 500; } |
| +.msg.card .card-state { margin: 0; color: var(--haze); font-size: 14px; } |
| .ask-empty, .ask-problem { margin: 0; padding: 0 20px 12px; color: var(--haze); font-size: 14px; } |
| .ask-foot { display: flex; align-items: center; gap: 10px; padding: 0 16px; } |
| .ask-status { flex: 1; margin: 0; min-height: 1.5em; color: var(--haze); font-size: 14px; } |
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js| index 6b279f7..669f131 100644 |
| --- a/internal/look/static/ask.js |
| +++ b/internal/look/static/ask.js |
| @@ -19,6 +19,8 @@ function start(root) { |
| const stop = root.querySelector("form.stop"); |
| const composer = root.querySelector("form.composer"); |
| const field = composer.querySelector("textarea"); |
| + // Equipo's origin, which its /tasks/ links point at. |
| + const equipo = (root.dataset.equipo || "").replace(/\/+$/, ""); |
| let source = null; |
| let turn = null; |
| let retry = 0; |
| @@ -52,7 +54,8 @@ function start(root) { |
| const li = document.createElement("li"); |
| li.className = "msg " + (m.role === "person" ? "person" : "coordinator"); |
| li.dataset.id = m.id; |
| - li.textContent = m.text; |
| + if (m.role === "person") li.textContent = m.text; |
| + else rich(li, m.text); |
| const draft = m.role !== "person" && item("data-draft", m.turn_id); |
| if (draft) draft.replaceWith(li); |
| else thread.append(li); |
| @@ -72,39 +75,119 @@ function start(root) { |
| scroll(); |
| } |
| + // Equipo's lines may carry Markdown links. Only http(s) addresses and |
| + // Equipo's own /tasks/ pages become links (opening in a new tab); the |
| + // rest, raw HTML included, is text. Base's server does the same |
| + // (richText in ask_cards.go). |
| + const linkRE = /\[([^\[\]\n]{1,300})\]\(([^()\s]{1,1000})\)|https?:\/\/[^\s<>()\[\]"']+/g; |
| + function safeHref(target) { |
| + if (target.startsWith("/tasks/")) { |
| + return /^[A-Za-z0-9_-]{1,64}$/.test(target.slice(7)) && equipo ? equipo + target : ""; |
| + } |
| + try { |
| + const u = new URL(target); |
| + return (u.protocol === "http:" || u.protocol === "https:") && u.host && !u.username && !u.password ? u.href : ""; |
| + } catch { |
| + return ""; |
| + } |
| + } |
| + function link(href, text) { |
| + const a = document.createElement("a"); |
| + a.href = href; |
| + a.target = "_blank"; |
| + a.rel = "noopener"; |
| + a.textContent = text; |
| + return a; |
| + } |
| + function rich(el, text) { |
| + el.replaceChildren(); |
| + let last = 0; |
| + for (const m of text.matchAll(linkRE)) { |
| + let end = m.index + m[0].length, label = m[1], target = m[2]; |
| + if (label === undefined) { |
| + target = m[0].replace(/[.,;:!?]+$/, ""); |
| + end = m.index + target.length; |
| + label = target; |
| + } |
| + const href = safeHref(target); |
| + if (!href) continue; |
| + el.append(text.slice(last, m.index), link(href, label)); |
| + last = end; |
| + } |
| + el.append(text.slice(last)); |
| + } |
| + |
| + // The words for a card's options and, once it is answered, its state: |
| + // Equipo's own wording (its chat.card copy). Base's server has the same |
| + // (cardOption and cardStatus in ask_cards.go). |
| + const options = { merge: "Merge", wait: "Wait for checks", wait_mac: "Wait for a Mac", not_now: "Not now", publish: "Publish" }; |
| + const sites = { publishing: "Publishing.", published: "Published", unconfirmed: "Publish could not be confirmed", |
| + failed: "Publishing failed.", declined: "Not published." }; |
| + const chosen = { merge: "Merging without checks.", wait: "Waiting for checks.", wait_mac: "Waiting for a Mac.", |
| + not_now: "Not now.", publish: "Publishing." }; |
| + function cardStatus(c) { |
| + if (c.kind === "publish" && c.site && sites[c.site.state]) return sites[c.site.state]; |
| + if (c.state === "open") return ""; |
| + return chosen[c.choice] || "Nothing to decide any more."; |
| + } |
| + |
| + // A card: a question, a merge card (with its preview) or a publish card |
| + // (with its site), drawn as the server draws it (the ask-card template). |
| + // A newer revision redraws it; an answered question goes. |
| function addCard(c) { |
| + if (!c || !c.id) return; |
| const old = item("data-card", c.id); |
| - if (!(c.kind === "question" && c.state === "open")) { |
| + const known = c.kind === "question" || c.kind === "merge" || c.kind === "publish"; |
| + if (!known || (c.kind === "question" && c.state !== "open")) { |
| if (old) old.remove(); |
| scroll(); |
| return; |
| } |
| - if (old) return; |
| + if (old && Number(old.dataset.revision) > c.revision) return; |
| const li = document.createElement("li"); |
| - li.className = "msg card"; |
| + li.className = "msg card card-" + c.kind; |
| li.dataset.card = c.id; |
| + li.dataset.revision = String(c.revision); |
| const q = document.createElement("p"); |
| - q.textContent = c.question; |
| - const form = document.createElement("form"); |
| - form.className = "choices"; |
| - form.method = "post"; |
| - form.action = `/ask/cards/${encodeURIComponent(c.id)}/answer`; |
| - const rev = document.createElement("input"); |
| - rev.type = "hidden"; |
| - rev.name = "revision"; |
| - rev.value = String(c.revision); |
| - form.append(rev); |
| - for (const o of c.options || []) { |
| - const b = document.createElement("button"); |
| - b.className = "btn ghost"; |
| - b.type = "submit"; |
| - b.name = "choice"; |
| - b.value = o; |
| - b.textContent = o; |
| - form.append(b); |
| + q.textContent = c.question || c.name || ""; |
| + li.append(q); |
| + for (const [href, text] of [[c.preview_url, "Open the preview"], [c.site && c.site.url, "Open the site"]]) { |
| + const safe = href && safeHref(href); |
| + if (!safe || safe.startsWith(equipo + "/tasks/")) continue; |
| + const p = document.createElement("p"); |
| + const a = link(safe, text); |
| + a.className = "card-link"; |
| + p.append(a); |
| + li.append(p); |
| + } |
| + if (c.state === "open") { |
| + const form = document.createElement("form"); |
| + form.className = "choices"; |
| + form.method = "post"; |
| + form.action = `/ask/cards/${encodeURIComponent(c.id)}/answer`; |
| + const rev = document.createElement("input"); |
| + rev.type = "hidden"; |
| + rev.name = "revision"; |
| + rev.value = String(c.revision); |
| + form.append(rev); |
| + for (const o of c.options || []) { |
| + const b = document.createElement("button"); |
| + b.className = "btn ghost"; |
| + b.type = "submit"; |
| + b.name = "choice"; |
| + b.value = o; |
| + b.textContent = options[o] || o; |
| + form.append(b); |
| + } |
| + li.append(form); |
| + } else { |
| + const p = document.createElement("p"); |
| + p.className = "card-state"; |
| + p.textContent = cardStatus(c); |
| + li.append(p); |
| } |
| - li.append(q, form); |
| - thread.append(li); |
| + if (old) old.replaceWith(li); |
| + else thread.append(li); |
| scroll(); |
| } |
| @@ -194,7 +277,10 @@ function start(root) { |
| // post sends a form by fetch and returns its JSON, or null when it was |
| // already on its way or failed (and the panel says so). |
| + // lastStatus is the HTTP status of the latest post that got an answer. |
| + let lastStatus = 0; |
| async function post(form, extra) { |
| + lastStatus = 0; |
| const body = new URLSearchParams(new FormData(form)); |
| body.delete("key"); |
| for (const [k, v] of Object.entries(extra || {})) body.set(k, v); |
| @@ -209,6 +295,7 @@ function start(root) { |
| try { |
| const res = await fetch(form.action, { method: "POST", headers: json, body }); |
| const out = await res.json(); |
| + lastStatus = res.status; |
| known = res.status !== 502; |
| if (res.ok) return out; |
| // Lost the connection: the panel says what to do. Otherwise the |
| @@ -251,6 +338,12 @@ function start(root) { |
| if (!choice) return; |
| for (const b of form.querySelectorAll("button")) b.disabled = true; |
| const out = await post(form, { choice }); |
| + if (!out && lastStatus === 409) { |
| + // The card moved on (a new revision, or answered elsewhere): read the |
| + // conversation afresh and let the person choose again. Never resend. |
| + await load(); |
| + return; |
| + } |
| if (!out) { |
| for (const b of form.querySelectorAll("button")) b.disabled = false; |
| return; |
diff --git a/internal/web/app.go b/internal/web/app.go| index cff2fd8..2b322ea 100644 |
| --- a/internal/web/app.go |
| +++ b/internal/web/app.go |
| @@ -81,7 +81,8 @@ func App(d *db.DB, cfg config.Config, mailer mail.Sender, inst Installer, logger |
| return nil, err |
| } |
| // newKey gives each chat form its own request key when the page is drawn. |
| - pages, err := look.NewRenderer(sub, template.FuncMap{"newKey": equipo.NewKey}) |
| + pages, err := look.NewRenderer(sub, template.FuncMap{"newKey": equipo.NewKey, |
| + "cardOption": cardOption, "cardStatus": cardStatus}) |
| if err != nil { |
| return nil, err |
| } |
diff --git a/internal/web/ask.go b/internal/web/ask.go| index 875d8ef..b29aad7 100644 |
| --- a/internal/web/ask.go |
| +++ b/internal/web/ask.go |
| @@ -167,8 +167,8 @@ func turnStatus(t *equipo.Turn) string { |
| func (v *askView) fill(s equipo.Snapshot) { |
| v.Messages = s.Messages |
| for _, c := range s.Cards { |
| - if c.Open() { |
| - v.Cards = append(v.Cards, c) |
| + if p, ok := c.Projected(); ok && p.Shown() { |
| + v.Cards = append(v.Cards, p) |
| } |
| } |
| for i := len(s.Turns) - 1; i >= 0; i-- { |
diff --git a/internal/web/ask_cards.go b/internal/web/ask_cards.go| new file mode 100644 |
| index 0000000..eba1f88 |
| --- /dev/null |
| +++ b/internal/web/ask_cards.go |
| @@ -0,0 +1,116 @@ |
| +package web |
| + |
| +import ( |
| + "html" |
| + "html/template" |
| + "regexp" |
| + "strings" |
| + |
| + "amadan.net/carlos/base/internal/equipo" |
| +) |
| + |
| +// cardOptions are the buttons' words for the options Equipo's cards offer, |
| +// in Equipo's own wording (its chat.card.* copy). An option Base does not |
| +// know shows as Equipo sent it. |
| +var cardOptions = map[string]string{ |
| + "merge": "Merge", |
| + "wait": "Wait for checks", |
| + "wait_mac": "Wait for a Mac", |
| + "not_now": "Not now", |
| + "publish": "Publish", |
| +} |
| + |
| +// cardOption is the label of a card's option. |
| +func cardOption(opt string) string { |
| + if l, ok := cardOptions[opt]; ok { |
| + return l |
| + } |
| + return opt |
| +} |
| + |
| +// cardStatus is the line a card shows once it no longer waits for an |
| +// answer: for a publish card, where publishing its site stands; for a |
| +// merge card, what was chosen. Equipo's words where it has them. |
| +func cardStatus(c equipo.Card) string { |
| + if c.Kind == equipo.CardPublish && c.Site != nil { |
| + switch c.Site.State { |
| + case "publishing": |
| + return "Publishing." |
| + case "published": |
| + return "Published" |
| + case "unconfirmed": |
| + return "Publish could not be confirmed" |
| + case "failed": |
| + return "Publishing failed." |
| + case "declined": |
| + return "Not published." |
| + } |
| + } |
| + if c.Open() { |
| + return "" |
| + } |
| + switch c.Choice { |
| + case "merge": |
| + return "Merging without checks." |
| + case "wait": |
| + return "Waiting for checks." |
| + case "wait_mac": |
| + return "Waiting for a Mac." |
| + case "not_now": |
| + return "Not now." |
| + case "publish": |
| + return "Publishing." |
| + } |
| + return "Nothing to decide any more." |
| +} |
| + |
| +// linkRE finds a Markdown link [label](target), or a bare http(s) URL. |
| +var ( |
| + linkRE = regexp.MustCompile(`\[([^\[\]\n]{1,300})\]\(([^()\s]{1,1000})\)|https?://[^\s<>()\[\]"']+`) |
| + taskRE = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`) |
| +) |
| + |
| +// richText is a coordinator's line as HTML: every character escaped, and |
| +// a Markdown link or bare URL made a link only when it goes to an http(s) |
| +// address, or to one of Equipo's own /tasks/ pages (on Equipo's origin). |
| +// Anything else, raw HTML included, stays as text. The panel's script |
| +// does the same (ask.js, rich). |
| +func richText(text, origin string) template.HTML { |
| + var b strings.Builder |
| + last := 0 |
| + for _, m := range linkRE.FindAllStringSubmatchIndex(text, -1) { |
| + if m[0] < last { |
| + continue |
| + } |
| + end, label, target := m[1], "", "" |
| + if m[2] >= 0 { |
| + label, target = text[m[2]:m[3]], text[m[4]:m[5]] |
| + } else { |
| + target = strings.TrimRight(text[m[0]:m[1]], ".,;:!?") |
| + end, label = m[0]+len(target), target |
| + } |
| + href := safeHref(target, origin) |
| + if href == "" { |
| + continue |
| + } |
| + b.WriteString(html.EscapeString(text[last:m[0]])) |
| + b.WriteString(`<a href="` + html.EscapeString(href) + `" target="_blank" rel="noopener">` + html.EscapeString(label) + `</a>`) |
| + last = end |
| + } |
| + b.WriteString(html.EscapeString(text[last:])) |
| + return template.HTML(b.String()) |
| +} |
| + |
| +// safeHref is where a link in Equipo's text may go, or "". |
| +func safeHref(target, origin string) string { |
| + if task, ok := strings.CutPrefix(target, "/tasks/"); ok { |
| + if !taskRE.MatchString(task) { |
| + return "" |
| + } |
| + return strings.TrimRight(origin, "/") + "/tasks/" + task |
| + } |
| + return equipo.WebURL(target) |
| +} |
| + |
| +// Rich is a coordinator's line with its links (richText), for templates. |
| +func (v askView) Rich(text string) template.HTML { return richText(text, v.Equipo) } |
diff --git a/internal/web/ask_relay.go b/internal/web/ask_relay.go| index 74b4642..4498c2f 100644 |
| --- a/internal/web/ask_relay.go |
| +++ b/internal/web/ask_relay.go |
| @@ -139,6 +139,7 @@ func (a *app) askAnswer(w http.ResponseWriter, r *http.Request) { |
| } |
| rev, _ := strconv.ParseInt(r.PostFormValue("revision"), 10, 64) |
| out, err := a.ask.client.Answer(r.Context(), c.token, chi.URLParam(r, "id"), r.PostFormValue("choice"), rev, askKey(r.PostFormValue("key"))) |
| + out.Card, _ = out.Card.Projected() |
| a.askDone(w, r, c, err, out) |
| } |
| @@ -342,8 +343,8 @@ func (e sseEvent) frame() string { |
| // project reduces an event's data to the conversation's shape, the one |
| // /ask/state uses; ok is false for every other kind of event, and for |
| -// cards that are not questions (projects, tasks and the rest stay in |
| -// Equipo). |
| +// cards Base does not show (equipo.Card.Projected: projects, tasks and |
| +// the rest stay in Equipo). |
| func project(typ string, raw json.RawMessage) (any, bool) { |
| switch typ { |
| case "message": |
| @@ -354,7 +355,10 @@ func project(typ string, raw json.RawMessage) (any, bool) { |
| return t, json.Unmarshal(raw, &t) == nil && t.ID != "" |
| case "card": |
| var c equipo.Card |
| - return c, json.Unmarshal(raw, &c) == nil && c.ID != "" && c.Kind == "question" |
| + if json.Unmarshal(raw, &c) != nil || c.ID == "" { |
| + return nil, false |
| + } |
| + return c.Projected() |
| case "delta": |
| var d struct { |
| TurnID string `json:"turn_id"` |
diff --git a/internal/web/relay_internal_test.go b/internal/web/relay_internal_test.go| index 247189f..bea935e 100644 |
| --- a/internal/web/relay_internal_test.go |
| +++ b/internal/web/relay_internal_test.go |
| @@ -98,3 +98,24 @@ func TestRelayWritesItsOwnFrames(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Merge and publish cards pass with only their own fields; anything else |
| +// Equipo puts on them stays behind. |
| +func TestRelayMergeAndPublishCards(t *testing.T) { |
| + merge := `{"cursor":47,"type":"card","data":{"id":"c_3","revision":2,"kind":"merge","state":"open","turn_id":"t_9","project_id":"p_9","name":"Quick website","question":"Merge Quick website without checks?","options":["merge","wait"],"choice":"","task_id":"tk0001","created_at":"2026-11-12T10:00:00Z","specialist":{"x":1},"secret_field":"s3cr3t","preview_url":"https://eq-tk0001.devbox.test/"}}` |
| + publish := `{"cursor":48,"type":"card","data":{"id":"c_4","revision":1,"kind":"publish","state":"open","project_id":"p_9","name":"Quick website","question":"Publish Quick website on the web as site-sample?","options":["publish","not_now"],"choice":"","task_id":"tk0001","site":{"app":"site-sample","sha":"eee","state":"asked","at":"2026-11-12T10:00:00Z","token":"s3cr3t"}}}` |
| + out := relayTo(t, strings.NewReader(frame("47", "card", merge)+frame("48", "card", publish)), time.Second, func() {}) |
| + for _, banned := range []string{"p_9", "t_9", "specialist", "s3cr3t", "created_at", "\"at\""} { |
| + if strings.Contains(out, banned) { |
| + t.Errorf("relayed %q:\n%s", banned, out) |
| + } |
| + } |
| + for _, want := range []string{ |
| + `data: {"type":"card","data":{"id":"c_3","revision":2,"kind":"merge","state":"open","name":"Quick website","task_id":"tk0001","question":"Merge Quick website without checks?","options":["merge","wait"],"choice":"","preview_url":"https://eq-tk0001.devbox.test/"}}`, |
| + `data: {"type":"card","data":{"id":"c_4","revision":1,"kind":"publish","state":"open","name":"Quick website","task_id":"tk0001","question":"Publish Quick website on the web as site-sample?","options":["publish","not_now"],"choice":"","site":{"app":"site-sample","sha":"eee","state":"asked"}}}`, |
| + } { |
| + if !strings.Contains(out, want) { |
| + t.Errorf("relay lacks %s:\n%s", want, out) |
| + } |
| + } |
| +} |
diff --git a/internal/web/rich_internal_test.go b/internal/web/rich_internal_test.go| new file mode 100644 |
| index 0000000..501ac4b |
| --- /dev/null |
| +++ b/internal/web/rich_internal_test.go |
| @@ -0,0 +1,25 @@ |
| +package web |
| + |
| +import "testing" |
| + |
| +// Equipo's lines may carry Markdown links. Base links only web addresses |
| +// and Equipo's own /tasks/ pages (on Equipo's origin); everything else, |
| +// raw HTML included, stays text. |
| +func TestRichText(t *testing.T) { |
| + const origin = "https://equipo.test/" |
| + for in, want := range map[string]string{ |
| + "Published [Quick website](/tasks/tk0001) at https://site-sample.example.test/.": `Published <a href="https://equipo.test/tasks/tk0001" target="_blank" rel="noopener">Quick website</a> at <a href="https://site-sample.example.test/" target="_blank" rel="noopener">https://site-sample.example.test/</a>.`, |
| + "See [docs](https://docs.test/a?b=1&c=2)": `See <a href="https://docs.test/a?b=1&c=2" target="_blank" rel="noopener">docs</a>`, |
| + "[x](javascript:alert(1))": `[x](javascript:alert(1))`, |
| + "[x](data:text/html,<b>hi</b>)": `[x](data:text/html,<b>hi</b>)`, |
| + "<script>alert(1)</script> <b>bold</b>": `<script>alert(1)</script> <b>bold</b>`, |
| + "[<img src=x onerror=alert(1)>](https://a.test/)": `<a href="https://a.test/" target="_blank" rel="noopener"><img src=x onerror=alert(1)></a>`, |
| + "[x](/tasks/../admin)": `[x](/tasks/../admin)`, |
| + "[x](//evil.test/)": `[x](//evil.test/)`, |
| + "plain & simple": `plain & simple`, |
| + } { |
| + if got := string(richText(in, origin)); got != want { |
| + t.Errorf("richText(%q)\n got %s\nwant %s", in, got, want) |
| + } |
| + } |
| +} |
diff --git a/internal/web/templates/ask.html b/internal/web/templates/ask.html| index a2ce106..e48f1ee 100644 |
| --- a/internal/web/templates/ask.html |
| +++ b/internal/web/templates/ask.html |
| @@ -1,7 +1,7 @@ |
| {{define "title"}}Ask Base · {{.Name}}{{end}} |
| {{define "head"}}{{if .Ask.Turn}}<noscript><meta http-equiv="refresh" content="5"></noscript>{{end}}{{end}} |
| {{define "main"}}<main class="inner"> |
| -<section class="sheet glass ask-page" data-ask data-state="{{.Ask.State}}"> |
| +<section class="sheet glass ask-page" data-ask data-state="{{.Ask.State}}" data-equipo="{{.Ask.Equipo}}"> |
| <div class="crumb"><a class="wordmark" href="/">base</a><span>Ask Base</span></div> |
| {{template "ask-head" .Ask}} |
| {{template "ask-body" .Ask}} |
diff --git a/internal/web/templates/partials/chat.html b/internal/web/templates/partials/chat.html| index d535a15..c6d30e4 100644 |
| --- a/internal/web/templates/partials/chat.html |
| +++ b/internal/web/templates/partials/chat.html |
| @@ -2,9 +2,15 @@ |
| {{define "ask-head"}}<div class="ask-head"><h2 id="ask-title">Ask Base</h2>{{template "ask-more" .}}</div>{{end}} |
| {{/* Open Equipo, behind a ⋯ menu: a <details>, so it opens without scripts. */}} |
| {{define "ask-more"}}<details class="ask-more"><summary class="round" aria-label="More"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 12h.01M12 12h.01M18 12h.01"/></svg></summary><div class="ask-menu glass"><a href="{{.Equipo}}" target="_blank" rel="noopener">Open Equipo</a></div></details>{{end}} |
| -{{define "ask-panel"}}<dialog id="ask-panel" class="ask-panel glass" aria-labelledby="ask-title" data-ask data-state="{{.State}}"> |
| +{{define "ask-panel"}}<dialog id="ask-panel" class="ask-panel glass" aria-labelledby="ask-title" data-ask data-state="{{.State}}" data-equipo="{{.Equipo}}"> |
| <div class="ask-head"><h2 id="ask-title">Ask Base</h2>{{template "ask-more" .}}<button class="round close" type="button" data-ask-close aria-label="Close"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6L6 18"/></svg></button></div> |
| {{template "ask-body" .}}</dialog>{{end}} |
| +{{/* A card: a question, a merge card (with its preview) or a publish card (with its site). The panel's script draws the same (ask.js, cardItem). */}} |
| +{{define "ask-card"}}<li class="msg card card-{{.Kind}}" data-card="{{.ID}}" data-revision="{{.Revision}}"><p>{{with .Question}}{{.}}{{else}}{{.Name}}{{end}}</p> |
| +{{- with .PreviewURL}}<p><a class="card-link" href="{{.}}" target="_blank" rel="noopener">Open the preview</a></p>{{end}} |
| +{{- with .Site}}{{with .URL}}<p><a class="card-link" href="{{.}}" target="_blank" rel="noopener">Open the site</a></p>{{end}}{{end}} |
| +{{- if .Open}}<form class="choices" method="post" action="/ask/cards/{{.ID}}/answer"><input type="hidden" name="revision" value="{{.Revision}}"><input type="hidden" name="key" value="{{newKey}}">{{range .Options}}<button class="btn ghost" type="submit" name="choice" value="{{.}}">{{cardOption .}}</button>{{end}}</form> |
| +{{- else}}<p class="card-state">{{cardStatus .}}</p>{{end}}</li>{{end}} |
| {{define "ask-body"}}<div class="ask-note" data-when="connect"{{if not (.Shows "connect")}} hidden{{end}}> |
| <p>Connect Equipo, and its coordinator answers you here.</p> |
| <form method="post" action="/ask/connect"><input type="hidden" name="next" value="{{.Next}}"><button class="btn" type="submit">Connect Equipo</button></form> |
| @@ -22,8 +28,8 @@ |
| </div> |
| <div class="ask-chat" data-when="ready"{{if not (.Shows "ready")}} hidden{{end}}> |
| <ol class="thread" role="log" aria-label="Conversation"> |
| -{{range .Messages}}<li class="msg {{.Role}}" data-id="{{.ID}}">{{.Text}}</li> |
| -{{end}}{{range .Cards}}<li class="msg card" data-card="{{.ID}}"><p>{{.Question}}</p><form class="choices" method="post" action="/ask/cards/{{.ID}}/answer"><input type="hidden" name="revision" value="{{.Revision}}"><input type="hidden" name="key" value="{{newKey}}">{{range .Options}}<button class="btn ghost" type="submit" name="choice" value="{{.}}">{{.}}</button>{{end}}</form></li> |
| +{{range .Messages}}<li class="msg {{.Role}}" data-id="{{.ID}}">{{if eq .Role "coordinator"}}{{$.Rich .Text}}{{else}}{{.Text}}{{end}}</li> |
| +{{end}}{{range .Cards}}{{template "ask-card" .}} |
| {{end}}</ol> |
| <p class="ask-empty" data-empty{{if or .Messages .Cards}} hidden{{end}}>Ask about your work, or ask Base to get something done.</p> |
| {{with .Problem}}<p class="ask-problem" role="alert">{{.}}</p>{{end}} |
diff --git a/internal/webtest/ask_test.go b/internal/webtest/ask_test.go| index f7c3679..88a353b 100644 |
| --- a/internal/webtest/ask_test.go |
| +++ b/internal/webtest/ask_test.go |
| @@ -618,3 +618,68 @@ func TestAskMoreMenu(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Scripts off: a merge card with its preview, a publish card through to |
| +// published with its site, and Equipo's Markdown links, all on /ask. |
| +func TestAskMergeAndPublishCards(t *testing.T) { |
| + a, f := newAskApp(t) |
| + a.connectEquipo(f) |
| + merge := f.AskMerge("Quick website") |
| + f.SetPreview(merge, "https://eq-tk0001.devbox.test/", true) |
| + publish := f.AskPublish("Quick website", "site-sample") |
| + f.Say("Try [this](javascript:alert(1)) or <b>that</b>.") |
| + page := a.get("/ask").Body |
| + for _, want := range []string{ |
| + "Merge Quick website without checks?", |
| + `<a class="card-link" href="https://eq-tk0001.devbox.test/" target="_blank" rel="noopener">Open the preview</a>`, |
| + `name="choice" value="merge">Merge</button>`, `name="choice" value="wait">Wait for checks</button>`, |
| + "Publish Quick website on the web as site-sample?", |
| + `name="choice" value="publish">Publish</button>`, `name="choice" value="not_now">Not now</button>`, |
| + "Try [this](javascript:alert(1)) or <b>that</b>.", |
| + } { |
| + if !strings.Contains(page, want) { |
| + t.Errorf("/ask lacks %s:\n%s", want, page) |
| + } |
| + } |
| + if strings.Contains(page, `href="javascript`) { |
| + t.Fatal("a javascript: link was rendered") |
| + } |
| + |
| + // A stale merge answer (the preview arrived unseen) is not resent: Base |
| + // goes back to /ask, which shows the card at its new revision. |
| + f.SetPreview(merge, "https://eq-tk0001.devbox.test/v2/", false) |
| + stale := formFields(t, page, "/ask/cards/"+merge+"/answer") |
| + stale.Set("choice", "merge") |
| + if r := a.post("/ask/cards/"+merge+"/answer", stale); r.Code != 303 || r.Location() != "/ask" { |
| + t.Fatalf("stale answer: %d %q", r.Code, r.Location()) |
| + } |
| + if calls := f.Calls("/api/v1/cards/" + merge + "/answer"); len(calls) != 1 || !strings.Contains(calls[0].Body, `"revision":2`) { |
| + t.Fatalf("answers: %+v", calls) |
| + } |
| + if fresh := formFields(t, a.get("/ask").Body, "/ask/cards/"+merge+"/answer"); fresh.Get("revision") != "3" { |
| + t.Fatalf("after the 409 the card is at revision %q, want 3", fresh.Get("revision")) |
| + } |
| + |
| + answer := formFields(t, page, "/ask/cards/"+publish+"/answer") |
| + answer.Set("choice", "publish") |
| + a.post("/ask/cards/"+publish+"/answer", answer) |
| + if c := f.Calls("/api/v1/cards/" + publish + "/answer"); len(c) != 1 || strings.TrimSpace(c[0].Body) != `{"choice":"publish","revision":1}` { |
| + t.Fatalf("publish answer: %+v", c) |
| + } |
| + var body string |
| + for i := 0; i < 100; i++ { |
| + if body = a.get("/ask").Body; strings.Contains(body, "Open the site") { |
| + break |
| + } |
| + time.Sleep(20 * time.Millisecond) |
| + } |
| + for _, want := range []string{ |
| + `<a class="card-link" href="https://site-sample.example.test/" target="_blank" rel="noopener">Open the site</a>`, |
| + `<p class="card-state">Published</p>`, |
| + `Published <a href="` + f.URL + `/tasks/tk0001" target="_blank" rel="noopener">Quick website</a> at <a href="https://site-sample.example.test/"`, |
| + } { |
| + if !strings.Contains(body, want) { |
| + t.Errorf("after publishing /ask lacks %s:\n%s", want, body) |
| + } |
| + } |
| +} |