package server import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "os" "os/exec" "strings" "testing" "time" ) // Exercise the actual CLI executable boundary, full production router and // browser-session authentication. All mail, credentials and storage are synthetic. func TestNeoMuttMailViewerFlow(t *testing.T) { if _, err := exec.LookPath("python3"); err != nil { t.Skip("python3 not installed") } fixture, token, domain := configFixture(t) fixture.store.BackgroundJobsDisabled = true s := New(fixture.store) p, err := s.store.PrincipalForSession(domain, token) if err != nil { t.Fatal(err) } browser, err := s.store.CreateBrowserAuthSession(domain, p.UUID, 0) if err != nil { t.Fatal(err) } handler := s.handler() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Host = "localhost" if r.URL.Path == "/fixture-browser-login" { s.setAuthCookie(w, r, browser) http.Redirect(w, r, r.URL.Query().Get("return"), http.StatusSeeOther) return } handler.ServeHTTP(w, r) })) defer server.Close() ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second) defer cancel() input, _ := json.Marshal(map[string]any{"origin": server.URL, "browser": browser, "root": t.TempDir(), "chromium": os.Getenv("PAGESWIKI_BROWSER_TEST") == "1"}) cmd := exec.CommandContext(ctx, "python3", "pages-cli/test_neomutt_viewer.py") cmd.Stdin = bytes.NewReader(input) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("NeoMutt flow: %v\n%s", err, out) } if !strings.Contains(string(out), "MAIL_VIEWER_OK") { t.Fatalf("missing verification: %s", out) } t.Log(string(out)) // Capture creates only the leaf document: prefix and date/list directories // remain virtual. Exercise the parent URLs under both documented settings. request := func(path string) *httptest.ResponseRecorder { r := httptest.NewRequest(http.MethodGet, "http://localhost"+strings.ReplaceAll(path, " ", "%20"), nil) r.AddCookie(&http.Cookie{Name: authCookieName, Value: browser}) w := httptest.NewRecorder() handler.ServeHTTP(w, r) return w } for _, enabled := range []bool{false, true} { value, expected := "false", http.StatusNotFound if enabled { value, expected = "true", http.StatusOK } if err := s.store.SetConfigInDomain(domain, "virtual_folder_pages", value); err != nil { t.Fatal(err) } if w := request("/tasks/"); w.Code != http.StatusMovedPermanently || w.Header().Get("Location") != "/tasks" { t.Fatalf("tasks trailing slash: %d %s", w.Code, w.Header().Get("Location")) } for _, path := range []string{"/tasks", "/tasks/example.list", "/tasks/2026/01", "/mail", "/mail/example.list", "/private/mail", "/private/mail/2026", "/private/mail/2026/01", "/受信 メール", "/docs/mail", "/api/mail"} { w := request(path) if w.Code != expected || !strings.Contains(strings.ToLower(w.Body.String()), "") { t.Fatalf("parent %s virtual=%t: %d %s", path, enabled, w.Code, w.Body.String()) } if strings.Contains(w.Body.String(), `{"detail":"Not Found"}`) { t.Fatal("unexpected JSON error") } } t.Logf("parent prefix/list/year/month: virtual_folder_pages=%t status=%d HTML", enabled, expected) } if err := s.store.SetConfigInDomain(domain, "virtual_folder_pages", "false"); err != nil { t.Fatal(err) } id, err := s.store.CreatePageAtPathInDomain(domain, "/private/mail") if err != nil { t.Fatal(err) } if _, err = s.store.SaveRevisionContent(id, []byte("