Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
114 lines
4.2 KiB
Go
114 lines
4.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Exercise the real pinned Wasm binary, font metrics and pure-Vimscript plugin
|
|
// against an isolated fake workspace; never writes to a production session.
|
|
func TestVimWasmChromiumSmoke(t *testing.T) {
|
|
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
|
t.Skip("opt-in Chromium")
|
|
}
|
|
t.Chdir("../..")
|
|
if _, err := os.Stat("assets/vimwasm/node_modules/vim-wasm/vim.wasm"); err != nil {
|
|
t.Skip("npm ci in assets/vimwasm required")
|
|
}
|
|
report := make(chan string, 2)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
|
|
w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")
|
|
w.Header().Set("Cross-Origin-Resource-Policy", "same-origin")
|
|
switch r.URL.Path {
|
|
case "/api/session":
|
|
writeEventJSON(w, 200, map[string]any{"initial": "one.txt", "vim_settings": vimSettings{Enabled: true, FontSize: 19, Vimrc: "colorscheme uploaded\nset tabstop=2\n", ColorName: "uploaded.vim", Colorscheme: "let g:colors_name='test_upload'\nhi Normal guifg=#abcdef guibg=#171d2b\n"}, "files": []map[string]string{{"path": "one.txt", "content": "Hello Vim"}, {"path": "nested/two.txt", "content": "Second buffer"}}})
|
|
case "/api/save":
|
|
var body struct {
|
|
Content string `json:"content_base64"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
data, err := base64.StdEncoding.DecodeString(body.Content)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
select {
|
|
case report <- string(data):
|
|
default:
|
|
}
|
|
writeEventJSON(w, 200, map[string]bool{"ok": true})
|
|
case "/app.js":
|
|
data, err := os.ReadFile("assets/vimwasm/app/app.js")
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/javascript")
|
|
script := strings.Replace(string(data), "vim.focus()", `vim.focus(); void vim.cmdline("call writefile([&guifont,exists(':Files'),argc(),v:errmsg,&tabstop,get(g:,'colors_name','')],'/work/check.txt')").then(() => vim.cmdline("export /work/check.txt")).then(() => vim.cmdline("call setline(1, 'Edited by regression') | write"))`, 1)
|
|
script = strings.Replace(script, "if (!save.ok)", "document.body.dataset.vimSaves = String(Number(document.body.dataset.vimSaves || 0) + 1); if (document.body.dataset.vimSaves === '2') document.body.dataset.vimReady = 'true'; if (!save.ok)", 1)
|
|
_, _ = w.Write([]byte(script))
|
|
case "/HackNerdFont-Regular.ttf":
|
|
http.ServeFile(w, r, "static/fonts/hack/HackNerdFont-Regular.ttf")
|
|
default:
|
|
if strings.HasPrefix(r.URL.Path, "/vendor/") {
|
|
http.ServeFile(w, r, filepath.Join("assets/vimwasm/node_modules/vim-wasm", filepath.Base(r.URL.Path)))
|
|
} else {
|
|
http.FileServer(http.Dir("assets/vimwasm/app")).ServeHTTP(w, r)
|
|
}
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
for _, safe := range []bool{false, true} {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
|
defer cancel()
|
|
target := server.URL
|
|
if safe {
|
|
target += "/?safe=1"
|
|
}
|
|
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", target, t.TempDir())
|
|
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cmd.Args = append(cmd.Args, filepath.Join(dir, "vim.png"))
|
|
}
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil || strings.Contains(string(out), "Uncaught ") {
|
|
t.Fatalf("vim browser: %v %s", err, out)
|
|
}
|
|
select {
|
|
case result := <-report:
|
|
expected := "PagesWikiHack:h19\n2\n2\n\n2\ntest_upload\n"
|
|
if safe {
|
|
expected = "PagesWikiHack:h16\n2\n2\n\n4\n"
|
|
}
|
|
if (!safe && result != expected) || (safe && !strings.HasPrefix(result, expected)) {
|
|
t.Fatalf("vim startup report: %q\n%s", result, out)
|
|
}
|
|
default:
|
|
t.Fatalf("Vim never exported startup report: %s", out)
|
|
}
|
|
select {
|
|
case result := <-report:
|
|
if !strings.HasPrefix(result, "Edited by regression") {
|
|
t.Fatalf("write-back: %q", result)
|
|
}
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("BufWritePost did not export the edited file")
|
|
}
|
|
}
|
|
}
|