package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
const session = `{"id":"aaaaaaaa-0000-0000-0000-000000000001","p":"/home/u/proj","b":"main",
"t":"Fix the build","ai":true,"n":false,"e":false,"i":false,"f":"2025-08-14T10:00:00.000Z",
"l":"2025-08-14T10:05:00.000Z","d":5,"u":1,"a":1,"k":1.5,"v":"1.0.0",
"c":[{"r":"u","x":"why does the build fail?"},{"r":"t","x":"Bash: make"},
{"r":"a","x":"A missing tag."}]}`
func payload(sessions ...string) string {
if len(sessions) == 0 {
sessions = []string{session}
}
return `{"s":[` + strings.Join(sessions, ",") + `],"m":[]}`
}
type harness struct {
t *testing.T
srv *Server
h http.Handler
}
func newHarness(t *testing.T, mod func(*Config)) *harness {
t.Helper()
cfg := Config{Addr: ":0", DataDir: t.TempDir(), PublicURL: "https://x.test",
MaxBytes: 1 << 20, MaxSessions: 1, TrustedProxy: "10.0.0.1",
CreatesPerH: 100, WritesPerH: 100}
if mod != nil {
mod(&cfg)
}
srv, err := NewServer(cfg)
if err != nil {
t.Fatal(err)
}
return &harness{t: t, srv: srv, h: srv.Routes()}
}
func (h *harness) do(method, path, body, secret string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, strings.NewReader(body))
req.RemoteAddr = "192.0.2.1:1234"
if secret != "" {
req.Header.Set("Authorization", "Bearer "+secret)
}
rec := httptest.NewRecorder()
h.h.ServeHTTP(rec, req)
return rec
}
func (h *harness) create(body string) shareResp {
h.t.Helper()
rec := h.do("POST", "/api/shares", body, "")
if rec.Code != http.StatusCreated {
h.t.Fatalf("create: %d %s", rec.Code, rec.Body)
}
var r shareResp
if err := json.Unmarshal(rec.Body.Bytes(), &r); err != nil {
h.t.Fatal(err)
}
return r
}
func TestCreateAndRead(t *testing.T) {
h := newHarness(t, nil)
r := h.create(`{"payload":` + payload() + `,"expire":"7d"}`)
if !ValidID(r.ID) || r.Secret == "" || r.URL != "https://x.test/share/"+r.ID {
t.Fatalf("bad response %+v", r)
}
if d := time.Until(r.Expires); d < 6*24*time.Hour || d > 8*24*time.Hour {
t.Fatalf("expiry %v", r.Expires)
}
page := h.do("GET", "/share/"+r.ID, "", "")
if page.Code != 200 || !strings.Contains(page.Header().Get("Content-Type"), "text/html") {
t.Fatalf("html: %d", page.Code)
}
csp := page.Header().Get("Content-Security-Policy")
if !strings.Contains(csp, "script-src 'sha256-") || strings.Contains(csp, "unsafe-inline' ;") {
t.Fatalf("csp: %s", csp)
}
if page.Header().Get("X-Robots-Tag") == "" {
t.Fatal("no X-Robots-Tag")
}
body := page.Body.String()
// The data block must hold exactly one : its own end.
start := strings.Index(body, `")
if strings.Contains(body[start:start+end], " tag." {
t.Fatalf("json roundtrip: %v %s", err, js.Body)
}
if rec := h.do("GET", "/share/"+r.ID+".pdf", "", ""); rec.Code != 404 {
t.Fatalf("unknown extension: %d", rec.Code)
}
}
func TestCompactionBlock(t *testing.T) {
h := newHarness(t, nil)
withC := strings.Replace(session, `"c":[`, `"c":[{"r":"c","x":"earlier summary"},`, 1)
r := h.create(`{"payload":` + payload(withC) + `}`)
txt := h.do("GET", "/share/"+r.ID+".txt", "", "").Body.String()
if !strings.Contains(txt, "--- context compacted here ---") || strings.Contains(txt, "earlier summary") {
t.Fatalf("txt: %s", txt)
}
}
func TestBranchAndDirectoryChange(t *testing.T) {
h := newHarness(t, nil)
branch := strings.Replace(session, `"c":[`, `"h":1,"o":"bbbbbbbb-0000","ot":"The original",`+
`"c":[{"r":"u","x":"old question"},{"r":"d","x":"/home/u/proj/sub"},`, 1)
r := h.create(`{"payload":` + payload(branch) + `}`)
txt := h.do("GET", "/share/"+r.ID+".txt", "", "").Body.String()
for _, want := range []string{"--- inherited history, from The original ---\n\n## User\n\nold question",
"--- the branch starts here ---\n\n> cd: /home/u/proj/sub\n"} {
if !strings.Contains(txt, want) {
t.Fatalf("missing %q in txt: %s", want, txt)
}
}
js := h.do("GET", "/share/"+r.ID+".json", "", "").Body.String()
if !strings.Contains(js, `"h":1,"o":"bbbbbbbb-0000","ot":"The original"`) {
t.Fatalf("json: %s", js)
}
}
func TestImages(t *testing.T) {
h := newHarness(t, nil)
png := "data:image/png;base64," + base64.StdEncoding.EncodeToString([]byte("\x89PNG\r\n\x1a\nrest"))
withImg := strings.Replace(session, `"c":[`, `"c":[{"r":"i","x":"[image]","src":"`+png+`"},{"r":"v","x":"[image]"},`, 1)
r := h.create(`{"payload":` + payload(withImg) + `}`)
if js := h.do("GET", "/share/"+r.ID+".json", "", "").Body.String(); !strings.Contains(js, png) {
t.Fatalf("image lost: %s", js)
}
txt := h.do("GET", "/share/"+r.ID+".txt", "", "").Body.String()
if !strings.Contains(txt, "> image attached\n> image from the tool\n") {
t.Fatalf("txt: %s", txt)
}
svg := "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte("