cat / 012
clone

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

main
012 / internal / serve / share_test.go
share_test.go 1.5K
1package serve23import (4 "context"5 "io"6 "path/filepath"7 "strings"8 "testing"9 "time"10)1112// Two sessions opening one file share its workbook: what one types the13// other shows at once, the first to quit leaves it to the other without14// asking, and stopping the server keeps the unsaved work once.15func TestSessionsShareAFile(t *testing.T) {16 key := newKey(t)17 srv, addr, dir := testServer(t, nil, key)18 saveSheet(t, filepath.Join(dir, "book.012"), "saved")19 c, err := dial(addr, key, srv.HostKey())20 if err != nil {21 t.Fatal(err)22 }23 defer c.Close()24 var terms []*term25 for range 3 {26 tm, err := openExec(t, c, "book.012")27 if err != nil {28 t.Fatal(err)29 }30 tm.waitFor(t, "saved")31 terms = append(terms, tm)32 }33 a, b, cy := terms[0], terms[1], terms[2]34 io.WriteString(a.stdin, "\x1b[Bfrom a\r")35 b.waitFor(t, "from a", "modified")36 cy.waitFor(t, "from a")3738 io.WriteString(b.stdin, "\x11") // Ctrl+Q: a and cy still have it39 b.waitClosed(t)40 a.waitFor(t, "from a")4142 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)43 defer cancel()44 if err := srv.Shutdown(ctx); err != nil {45 t.Fatal(err)46 }47 a.waitClosed(t)48 cy.waitClosed(t)49 files, _ := filepath.Glob(filepath.Join(dir, ".012-recovery", "*"))50 if len(files) != 1 {51 t.Fatalf("recovery files %v; want one for the shared workbook", files)52 }53 kept := 054 for _, tm := range []*term{a, cy} {55 if strings.Contains(tm.output(), "unsaved changes kept in") {56 kept++57 }58 }59 if kept != 1 {60 t.Errorf("%d sessions said they kept the changes", kept)61 }62}