Files
Docwell/Server/internal/unfurl/rich_test.go
T
tajniak81andClaude Opus 5.5 f1963f1391 Add Confluence import, a Slack app per workspace, and GitHub, Linear and Figma link previews
Link previews: link cards for GitHub issues, pull requests and
repositories, Linear issues and Figma files are read through the
services' APIs and show a status pill (Open, Merged, In Progress…) and a
line of details. Public items need nothing; a workspace's tokens, set by
its admins under Workspace settings → Integrations and checked with each
service before being sealed, reach private ones and Linear.
GET /link-preview takes the workspace; the link card stores kind,
status, state and meta.

Slack: each workspace connects its own Slack app, made from a manifest
the settings page fills in. Docwell posts chosen page and comment events
to a channel (edits at most every 30 minutes per page), previews links
to Docwell pages posted in Slack, and answers /docwell with a search as
the member whose email matches the Slack user, shown only to them. Only
pages every member can open are posted or previewed. Requests from Slack
are signature-checked; the webhook dispatcher gained listeners so Slack
follows the same events.

Confluence: a space's HTML export imports with its page tree (from
index.html, else breadcrumbs), links between pages, pictures and
attachments. Panels become callouts, code macros code blocks with their
language, expand macros toggles, task lists to-dos.

New collection workspace_integrations (schema snapshot regenerated),
OpenAPI and generated Go, TypeScript and Dart types, strings in all five
languages, the user guide, and a rebuilt mobile editor bundle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 20:42:11 +01:00

125 lines
5.3 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package unfurl
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// onlyTo lets requests reach srv and fails every other one, so a test never
// goes to the real services.
type onlyTo struct{ host string }
func (o onlyTo) RoundTrip(r *http.Request) (*http.Response, error) {
if r.URL.Host != o.host {
return nil, errors.New("blocked in tests: " + r.URL.String())
}
return http.DefaultTransport.RoundTrip(r)
}
func fakeServices(t *testing.T) *Fetcher {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("GET /gh/repos/acme/app/issues/7", func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `{"title":"Fix the login","body":"## Steps\n**Click** [here](https://x)","state":"closed","comments":3,
"user":{"login":"alice","avatar_url":"https://avatars.githubusercontent.com/u/1"},"pull_request":{"merged_at":"2026-11-01T10:00:00Z"}}`)
})
mux.HandleFunc("GET /gh/repos/acme/app/issues/8", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer ghtok" {
http.NotFound(w, r)
return
}
_, _ = io.WriteString(w, `{"title":"Private bug","state":"open","user":{"login":"bob","avatar_url":"javascript:x"}}`)
})
mux.HandleFunc("GET /gh/repos/acme/app", func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `{"full_name":"acme/app","description":"The app","stargazers_count":1234,"language":"Go","owner":{"avatar_url":"https://avatars.githubusercontent.com/u/2"}}`)
})
mux.HandleFunc("POST /linear/graphql", func(w http.ResponseWriter, r *http.Request) {
var in struct {
Variables map[string]string `json:"variables"`
}
_ = json.NewDecoder(r.Body).Decode(&in)
if r.Header.Get("Authorization") != "lin_key" || in.Variables["id"] != "ENG-42" {
_, _ = io.WriteString(w, `{"data":{"issue":null}}`)
return
}
_, _ = io.WriteString(w, `{"data":{"issue":{"identifier":"ENG-42","title":"Ship search","description":"Do it","priorityLabel":"High",
"state":{"name":"In Progress","type":"started"},"assignee":{"name":"Carol"},"team":{"name":"Engineering"}}}}`)
})
mux.HandleFunc("GET /figma/v1/files/AbCdEfGhIjKl", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Figma-Token") != "figtok" {
w.WriteHeader(http.StatusForbidden)
return
}
_, _ = io.WriteString(w, `{"name":"Checkout flow","thumbnailUrl":"https://s3.figma.com/t.png","lastModified":"2026-11-02T08:00:00Z","editorType":"figma"}`)
})
mux.HandleFunc("GET /figweb/api/oembed", func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `{"title":"Public board","thumbnail_url":"https://s3.figma.com/p.png"}`)
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
host, _ := url.Parse(srv.URL)
f := NewWithClient(&http.Client{Transport: onlyTo{host: host.Host}})
return f.WithAPIs(APIs{GitHub: srv.URL + "/gh", Linear: srv.URL + "/linear", Figma: srv.URL + "/figma", FigmaWeb: srv.URL + "/figweb"})
}
func TestRichPreviews(t *testing.T) {
f := fakeServices(t)
ctx := context.Background()
tokens := Tokens{GitHub: "ghtok", Linear: "lin_key", Figma: "figtok"}
cases := []struct {
url string
tokens Tokens
want Preview
}{
{"https://github.com/acme/app/pull/7/files", Tokens{}, Preview{Kind: KindGitHubPull, Title: "Fix the login", Status: "Merged", State: "merged",
Meta: "acme/app #7 · alice · 💬 3", Description: "Steps Click here", Image: "https://avatars.githubusercontent.com/u/1", Site: "GitHub"}},
{"https://github.com/acme/app/issues/8", tokens, Preview{Kind: KindGitHubIssue, Title: "Private bug", Status: "Open", State: "open",
Meta: "acme/app #8 · bob", Site: "GitHub"}},
{"https://www.github.com/acme/app", Tokens{}, Preview{Kind: KindGitHubRepo, Title: "acme/app", Description: "The app", Meta: "★ 1.2k · Go",
Image: "https://avatars.githubusercontent.com/u/2", Site: "GitHub"}},
{"https://linear.app/acme/issue/eng-42/ship-search", tokens, Preview{Kind: KindLinearIssue, Title: "Ship search", Description: "Do it",
Status: "In Progress", State: "started", Meta: "ENG-42 · Engineering · Carol · High", Site: "Linear"}},
{"https://www.figma.com/design/AbCdEfGhIjKl/Checkout", tokens, Preview{Kind: KindFigmaFile, Title: "Checkout flow",
Image: "https://s3.figma.com/t.png", Meta: "Figma design · 2026-11-02", Site: "Figma"}},
{"https://www.figma.com/board/ZyXwVuTsRqPo/Board", Tokens{}, Preview{Kind: KindFigmaFile, Title: "Public board",
Image: "https://s3.figma.com/p.png", Meta: "Figma design", Site: "Figma"}},
}
for _, c := range cases {
got, err := f.PreviewWith(ctx, c.url, c.tokens)
if err != nil {
t.Fatalf("%s: %v", c.url, err)
}
c.want.URL = c.url
if got != c.want {
t.Errorf("%s:\n got %+v\nwant %+v", c.url, got, c.want)
}
}
}
func TestRichPreviewFallsBack(t *testing.T) {
f := fakeServices(t)
ctx := context.Background()
for _, raw := range []string{
"https://github.com/acme/app/issues/8", // private without a token
"https://linear.app/acme/issue/ENG-42/ship-search", // Linear needs a key
"https://github.com/settings/profile", // a page of the site, not a repository
} {
got, err := f.PreviewWith(ctx, raw, Tokens{})
if err != nil {
t.Fatal(err)
}
if got.Kind != "" || !strings.Contains(got.Site, ".") {
t.Errorf("%s: want the plain preview, got %+v", raw, got)
}
}
}