Render Mermaid diagrams and worktree images in agent Markdown
This commit is contained in:
@@ -6,8 +6,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -116,22 +118,25 @@ func TestServesEmbeddedSPA(t *testing.T) {
|
||||
t.Cleanup(hs.Close)
|
||||
|
||||
for path, contentType := range map[string]string{
|
||||
"/": "text/html",
|
||||
"/style.css": "text/css",
|
||||
"/dist/app.js": "text/javascript",
|
||||
"/dist/app.js.map": "",
|
||||
"/src/app.ts": "",
|
||||
"/dist/inbox.js": "text/javascript",
|
||||
"/dist/api.gen.js": "text/javascript",
|
||||
"/dist/tasks.js": "text/javascript",
|
||||
"/dist/navbar.js": "text/javascript",
|
||||
"/dist/panes.js": "text/javascript",
|
||||
"/dist/task.js": "text/javascript",
|
||||
"/dist/state.js": "text/javascript",
|
||||
"/dist/dom.js": "text/javascript",
|
||||
"/dist/api.js": "text/javascript",
|
||||
"/dist/settings.js": "text/javascript",
|
||||
"/sw.js": "text/javascript",
|
||||
"/": "text/html",
|
||||
"/style.css": "text/css",
|
||||
"/dist/app.js": "text/javascript",
|
||||
"/dist/app.js.map": "",
|
||||
"/src/app.ts": "",
|
||||
"/dist/inbox.js": "text/javascript",
|
||||
"/dist/api.gen.js": "text/javascript",
|
||||
"/dist/tasks.js": "text/javascript",
|
||||
"/dist/navbar.js": "text/javascript",
|
||||
"/dist/panes.js": "text/javascript",
|
||||
"/dist/task.js": "text/javascript",
|
||||
"/dist/state.js": "text/javascript",
|
||||
"/dist/dom.js": "text/javascript",
|
||||
"/dist/api.js": "text/javascript",
|
||||
"/dist/settings.js": "text/javascript",
|
||||
"/sw.js": "text/javascript",
|
||||
"/dist/mermaid.js": "text/javascript",
|
||||
"/vendor/mermaid.min.js": "text/javascript",
|
||||
"/vendor/mermaid.LICENSE": "",
|
||||
"/fonts/atkinson-hyperlegible-mono-latin-wght-normal.woff2": "font/woff2",
|
||||
"/fonts/LICENSE": "",
|
||||
} {
|
||||
@@ -313,6 +318,64 @@ func TestMergeTask(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskImage(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
p := h.createProject("demo")
|
||||
|
||||
task := api.Task{}
|
||||
h.do("POST", "/api/projects/"+p.Id+"/tasks", api.TaskCreate{Title: "images"}, &task)
|
||||
|
||||
err := os.MkdirAll(filepath.Join(task.WorktreePath, "shots"), 0o755)
|
||||
if err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
writeFile(t, filepath.Join(task.WorktreePath, "shots", "a.png"), "\x89PNG\r\n\x1a\nfake")
|
||||
writeFile(t, filepath.Join(task.WorktreePath, "notes.txt"), "text")
|
||||
outside := filepath.Join(t.TempDir(), "secret.png")
|
||||
writeFile(t, outside, "secret")
|
||||
err = os.Symlink(outside, filepath.Join(task.WorktreePath, "link.png"))
|
||||
if err != nil {
|
||||
t.Fatalf("symlink: %v", err)
|
||||
}
|
||||
sibling := task.WorktreePath + "x"
|
||||
err = os.MkdirAll(sibling, 0o755)
|
||||
if err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
writeFile(t, filepath.Join(sibling, "a.png"), "sibling")
|
||||
|
||||
for path, want := range map[string]int{
|
||||
"shots/a.png": http.StatusOK,
|
||||
filepath.Join(task.WorktreePath, "shots", "a.png"): http.StatusOK,
|
||||
"shots/../shots/a.png": http.StatusOK,
|
||||
"missing.png": http.StatusNotFound,
|
||||
"notes.txt": http.StatusBadRequest,
|
||||
"shots": http.StatusBadRequest,
|
||||
"link.png": http.StatusForbidden,
|
||||
outside: http.StatusForbidden,
|
||||
"../" + filepath.Base(sibling) + "/a.png": http.StatusForbidden,
|
||||
} {
|
||||
req, err := http.NewRequest("GET", h.url+"/api/tasks/"+task.Id+"/image?path="+url.QueryEscape(path), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("new request: %v", err)
|
||||
}
|
||||
req.Header.Set("Tailscale-User-Login", login)
|
||||
req.Header.Set("Tailscale-User-Name", "Alice")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("get %s: %v", path, err)
|
||||
}
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != want {
|
||||
t.Fatalf("get %s: %d %s, want %d", path, resp.StatusCode, body, want)
|
||||
}
|
||||
if want == http.StatusOK && (resp.Header.Get("Content-Type") != "image/png" || !bytes.HasPrefix(body, []byte("\x89PNG"))) {
|
||||
t.Fatalf("get %s: %q %q", path, resp.Header.Get("Content-Type"), body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, path, content string) {
|
||||
t.Helper()
|
||||
err := os.WriteFile(path, []byte(content), 0o644)
|
||||
|
||||
Reference in New Issue
Block a user