//go:build browser package mapatest import ( "context" "io" "log/slog" "net/http" "os" "path/filepath" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "gorm.io/gorm" "amadan.net/oficina/mapa/internal/mapa" "amadan.net/oficina/notion.git/notion" "amadan.net/oficina/notion.git/notiontest" ) // I15, scriptless (Notion import spec §8.4, §18): connect, start, watch // the meta refresh, stop, resume and finish in a real Chromium, with // JavaScript off and then on. The harness fails on any console error, // failed request or 4xx/5xx. With MAPA_IMPORT_SHOTS set to a directory // outside the repository, it also saves a picture of the run at each // stage: connect, mid-survey, mid-write, stopped and finished (§18: // "the run is looked at in a browser"). func TestI15TheImportWithScriptsOff(t *testing.T) { importWalk(t, false) } func TestI15TheImportWithScriptsOn(t *testing.T) { importWalk(t, true) } // importRig is a browser rig where Ana may import from a fake serving // the documented workspace. func importRig(t *testing.T) (*harness.Rig, *notiontest.Server, func() *gorm.DB) { server := notiontest.Start(notiontest.NewDocumented().Workspace, notion.APIVersion) t.Cleanup(server.Close) var database *db.DB rig := harness.New(t, func(origin string) http.Handler { logger := slog.New(slog.NewTextHandler(io.Discard, nil)) var err error database, err = db.Open(filepath.Join(t.TempDir(), "mapa.db"), logger) if err != nil { t.Fatal(err) } t.Cleanup(func() { database.Close() }) env := testEnv(Ana + "," + Bo) env["MAPA_ORIGIN"], env["MAPA_IMPORTERS"] = origin, Ana config, err := mapa.LoadConfig(func(key string) string { return env[key] }) if err != nil { t.Fatal(err) } built, err := mapa.AppForTest(database, config, logger, Ana) if err != nil { t.Fatal(err) } built.UseNotion(fakeNotion{server: server}) t.Cleanup(built.WaitImports) return built.Mux }) return rig, server, func() *gorm.DB { return database.G } } func importWalk(t *testing.T, live bool) { rig, server, database := importRig(t) rig.Run(scripts(live)) drive(t, rig, chromedp.Navigate(rig.Origin+"/import/notion")) rig.Screen(`input[name="token"]`, "connect") shot(t, rig, live, "1-connect") drive(t, rig, chromedp.SendKeys(`input[name="token"]`, "secret_test"), press(`form[action="/import/notion/connect"] button`), chromedp.WaitVisible(`form[action="/import/notion/start"]`)) server.Lock() drive(t, rig, press(`form[action="/import/notion/start"] button`), chromedp.WaitVisible(`form[action="/import/notion/stop"]`)) expectIn(t, rig, "Pages read so far: 0.") if !strings.Contains(read(t, rig, chromedp.OuterHTML("head")), `http-equiv="refresh"`) { t.Error("a running import's page does not refresh itself") } shot(t, rig, live, "2-mid-survey") drive(t, rig, press(`form[action="/import/notion/stop"] button`), chromedp.WaitVisible(`form[action="/import/notion/resume"]`)) server.Unlock() expectIn(t, rig, "You stopped the import") shot(t, rig, live, "3-stopped") midWrite(t, rig, database, live) drive(t, rig, press(`form[action="/import/notion/resume"] button`)) waitFor(t, rig, "Run complete") rig.Screen(`[mapa-import-report]`, "finished") expectIn(t, rig, "Created") shot(t, rig, live, "5-finished") } // midWrite shows the page as it reads at 2 of 5 written: the write // phase asks Notion nothing the fake could hold, so this stage is set // in the database, as the run would leave it, and read back. func midWrite(t *testing.T, rig *harness.Rig, database func() *gorm.DB, live bool) { now := time.Now().UTC() database().Exec("UPDATE notion_runs SET state = 'writing', total = 5,"+ " written = 2, heartbeat_at = ?, progress_at = ?", now, now) drive(t, rig, chromedp.Navigate(rig.Origin+"/import/notion")) expectIn(t, rig, "2 of 5 so far.") shot(t, rig, live, "4-mid-write") database().Exec("UPDATE notion_runs SET state = 'stopped', total = 0," + " written = 0") drive(t, rig, chromedp.Navigate(rig.Origin+"/import/notion"), chromedp.WaitVisible(`form[action="/import/notion/resume"]`)) } func expectIn(t *testing.T, rig *harness.Rig, words string) { t.Helper() if got := text(t, rig, "main"); !strings.Contains(got, words) { t.Errorf("the page does not say %q:\n%s", words, got) } } // waitFor waits for words to appear, through the page's own refresh. // A read that lands while a navigation replaces the page (a form's // redirect, the meta refresh) is tried again, not failed: the page it // was reading is gone. func waitFor(t *testing.T, rig *harness.Rig, words string) { t.Helper() for deadline := time.Now().Add(30 * time.Second); time.Now(). Before(deadline); time.Sleep(time.Second) { ctx, cancel := context.WithTimeout(rig.Context(), 5*time.Second) got, err := chromedp.Run(ctx, chromedp.Text("main")) cancel() if err == nil && strings.Contains(got, words) { return } } t.Fatalf("the page never said %q", words) } // shot saves the page to MAPA_IMPORT_SHOTS, when it is set. func shot(t *testing.T, rig *harness.Rig, live bool, name string) { dir := os.Getenv("MAPA_IMPORT_SHOTS") if dir == "" { return } if live { name += "-scripts-on" } picture := read(t, rig, chromedp.FullScreenshot(90)) if err := os.WriteFile(filepath.Join(dir, name+".png"), picture, 0o600); err != nil { t.Fatal(err) } }