cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

main
012 / e2e / config_test.go
config_test.go 4.2K
1package e2e23import (4 "os"5 "os/exec"6 "path/filepath"7 "strings"8 "testing"9)1011// Problems in the config file show on the context line; 012 starts12// anyway, with defaults for what's wrong.13func TestConfigWarnings(t *testing.T) {14 t.Parallel()15 s := startWith(t, options{config: "theme = Dracula\nbogus = 1\nchart-images = maybe\n"})16 s.waitFor(`Config: config:2: unknown key "bogus" (and 1 more; run 012 config)`)17 if !strings.Contains(s.html(), "background:#1e1f29") {18 t.Error("the valid theme line was not applied")19 }20}2122// openTheme opens File > Settings > Theme.23func openTheme(s *session) {24 s.keys("<alt+f>", "<up>", "<up>", "<right>", "<down>", "<down>", "<down>")25 s.waitFor("Pick a color theme")26 s.keys("<enter>")27 s.waitFor("─ Theme ─")28}2930// The theme picker previews the highlighted theme, Esc restores the one31// there was, and Enter keeps and saves it, so the next start has it.32func TestThemePicker(t *testing.T) {33 t.Parallel()34 cfg := t.TempDir()35 s := startWith(t, options{configDir: cfg})36 openTheme(s)37 s.keys("Dracula")38 s.eventually("Dracula previewed", func() bool { return strings.Contains(s.html(), "background:#1e1f29") })39 s.keys("<esc>")40 s.eventually("the terminal theme restored", func() bool { return !strings.Contains(s.html(), "#1e1f29") })41 s.waitFor("READY")4243 openTheme(s)44 s.keys("Dracula", "<enter>")45 s.waitFor("Theme Dracula, saved in")46 data, err := os.ReadFile(filepath.Join(cfg, "012", "config"))47 if err != nil || string(data) != "theme = Dracula\n" {48 t.Errorf("config file %q %v", data, err)49 }50 s.keys("<ctrl+q>")51 s.waitExit()5253 r := startWith(t, options{configDir: cfg})54 r.eventually("Dracula at start", func() bool { return strings.Contains(r.html(), "background:#1e1f29") })55}5657// A .env with the key next to the sheet is never read: JEV stays off,58// and the context line says how to store the key instead.59func TestDotEnvNotRead(t *testing.T) {60 t.Parallel()61 srv, requests := fakeTypeSafe(t)62 dir := t.TempDir()63 os.WriteFile(filepath.Join(dir, ".env"), []byte("TYPESAFE_API_KEY=test-key\nTYPESAFE_BASE_URL="+srv.URL+"\n"), 0o600)64 s := startWith(t, options{dir: dir})65 s.waitFor("012 no longer reads TYPESAFE_API_KEY from .env files")66 s.keys(`=JEV.TEST("x", "Q")`, "<enter>", "<up>")67 s.waitFor("JEV functions need an API key")68 if n := requests.Load(); n != 0 {69 t.Errorf("%d requests with a key from .env", n)70 }71}7273// 012 config set-key stores the key (here in the tests' stand-in for the74// credential store); the next start finds it there and JEV works.75func TestSetKeyThenJEV(t *testing.T) {76 t.Parallel()77 srv, requests := fakeTypeSafe(t)78 cfg := t.TempDir()79 cmd := exec.Command(binPath, "config", "set-key")80 cmd.Env = append(os.Environ(), "XDG_CONFIG_HOME="+cfg, "TYPESAFE_API_KEY=")81 cmd.Stdin = strings.NewReader("test-key\n")82 out, err := cmd.CombinedOutput()83 if err != nil || !strings.Contains(string(out), "Saved the JEV API key in the test credential store") || strings.Contains(string(out), "test-key") {84 t.Fatalf("set-key: %v\n%s", err, out)85 }86 s := startWith(t, options{configDir: cfg, env: []string{"TYPESAFE_BASE_URL=" + srv.URL}})87 s.keys(`=JEV.TEST("The box was crushed", "Is this a complaint?")`, "<enter>")88 s.waitForLine(gridRow1, " 1 TRUE")89 if requests.Load() != 1 {90 t.Errorf("%d requests", requests.Load())91 }92}9394// File > Settings > JEV API key takes the key masked, stores it and turns95// JEV on at once, after one test call with it.96func TestAPIKeyPrompt(t *testing.T) {97 t.Parallel()98 srv, _ := fakeTypeSafe(t)99 cfg := t.TempDir()100 s := startWith(t, options{configDir: cfg, env: []string{"TYPESAFE_BASE_URL=" + srv.URL}})101 s.keys(`=JEV.TEST("The box was crushed", "Is this a complaint?")`, "<enter>")102 s.keys("<alt+f>", "<up>", "<up>", "<right>", "<down>", "<down>", "<down>", "<down>")103 s.waitFor("Store the TypeSafe API key")104 s.keys("<enter>", "test-key")105 s.waitFor("TypeSafe API key: ••••••••")106 if strings.Contains(s.screen(), "test-key") {107 t.Fatal("the key is on screen")108 }109 s.keys("<enter>")110 s.waitFor("Key saved and checked; it's in the test credential store")111 s.waitForLine(gridRow1, " 1 TRUE")112 data, err := os.ReadFile(filepath.Join(cfg, "012", "test-credential-store"))113 if err != nil || string(data) != "test-key" {114 t.Errorf("stored %q %v", data, err)115 }116}